2022-04-04 11:14:27 +00:00
|
|
|
package actors
|
|
|
|
|
|
|
|
import (
|
2022-04-12 17:38:25 +00:00
|
|
|
"bytes"
|
2022-04-04 11:14:27 +00:00
|
|
|
"context"
|
2022-05-11 19:30:07 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-04-08 00:03:07 +00:00
|
|
|
"strings"
|
2022-04-04 17:11:19 +00:00
|
|
|
"sync"
|
2022-04-04 11:14:27 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2022-04-12 17:38:25 +00:00
|
|
|
car "github.com/ipld/go-car"
|
2022-04-04 11:14:27 +00:00
|
|
|
|
2022-04-12 17:38:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2022-04-04 11:14:27 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
"github.com/filecoin-project/specs-actors/v8/actors/builtin/manifest"
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
2022-04-04 11:14:27 +00:00
|
|
|
)
|
|
|
|
|
2022-04-06 08:25:46 +00:00
|
|
|
var manifestCids map[Version]cid.Cid = map[Version]cid.Cid{
|
2022-04-04 14:48:13 +00:00
|
|
|
// TODO fill in manifest CIDs for v8 and upwards once these are fixed
|
2022-04-04 11:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var manifests map[Version]*manifest.Manifest
|
|
|
|
var actorMeta map[cid.Cid]actorEntry
|
|
|
|
|
2022-04-04 17:11:19 +00:00
|
|
|
var (
|
2022-04-21 10:53:30 +00:00
|
|
|
manifestMx sync.RWMutex
|
2022-04-04 17:11:19 +00:00
|
|
|
)
|
|
|
|
|
2022-04-04 11:14:27 +00:00
|
|
|
type actorEntry struct {
|
|
|
|
name string
|
|
|
|
version Version
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:25:46 +00:00
|
|
|
func AddManifest(av Version, manifestCid cid.Cid) {
|
|
|
|
manifestMx.Lock()
|
|
|
|
defer manifestMx.Unlock()
|
|
|
|
|
|
|
|
manifestCids[av] = manifestCid
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetManifest(av Version) (cid.Cid, bool) {
|
2022-04-21 10:53:30 +00:00
|
|
|
manifestMx.RLock()
|
|
|
|
defer manifestMx.RUnlock()
|
2022-04-06 08:25:46 +00:00
|
|
|
|
|
|
|
c, ok := manifestCids[av]
|
|
|
|
return c, ok
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:14:27 +00:00
|
|
|
func LoadManifests(ctx context.Context, store cbor.IpldStore) error {
|
2022-04-21 10:53:30 +00:00
|
|
|
manifestMx.Lock()
|
|
|
|
defer manifestMx.Unlock()
|
|
|
|
|
|
|
|
return loadManifests(ctx, store)
|
2022-04-04 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadManifests(ctx context.Context, store cbor.IpldStore) error {
|
2022-04-04 11:14:27 +00:00
|
|
|
adtStore := adt.WrapStore(ctx, store)
|
|
|
|
|
|
|
|
manifests = make(map[Version]*manifest.Manifest)
|
|
|
|
actorMeta = make(map[cid.Cid]actorEntry)
|
|
|
|
|
2022-04-06 08:25:46 +00:00
|
|
|
for av, mfCid := range manifestCids {
|
2022-04-04 11:14:27 +00:00
|
|
|
mf := &manifest.Manifest{}
|
|
|
|
if err := adtStore.Get(ctx, mfCid, mf); err != nil {
|
|
|
|
return xerrors.Errorf("error reading manifest for network version %d (cid: %s): %w", av, mfCid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mf.Load(ctx, adtStore); err != nil {
|
|
|
|
return xerrors.Errorf("error loading manifest for network version %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifests[av] = mf
|
|
|
|
|
|
|
|
for _, name := range []string{"system", "init", "cron", "account", "storagepower", "storageminer", "storagemarket", "paymentchannel", "multisig", "reward", "verifiedregistry"} {
|
|
|
|
c, ok := mf.Get(name)
|
|
|
|
if ok {
|
|
|
|
actorMeta[c] = actorEntry{name: name, version: av}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetActorCodeID(av Version, name string) (cid.Cid, bool) {
|
2022-04-21 10:53:30 +00:00
|
|
|
manifestMx.RLock()
|
|
|
|
defer manifestMx.RUnlock()
|
|
|
|
|
2022-04-04 11:14:27 +00:00
|
|
|
mf, ok := manifests[av]
|
|
|
|
if ok {
|
|
|
|
return mf.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid.Undef, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetActorMetaByCode(c cid.Cid) (string, Version, bool) {
|
2022-04-21 10:53:30 +00:00
|
|
|
manifestMx.RLock()
|
|
|
|
defer manifestMx.RUnlock()
|
|
|
|
|
2022-04-04 11:14:27 +00:00
|
|
|
entry, ok := actorMeta[c]
|
|
|
|
if !ok {
|
|
|
|
return "", -1, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.name, entry.version, true
|
|
|
|
}
|
2022-04-08 00:03:07 +00:00
|
|
|
|
|
|
|
func CanonicalName(name string) string {
|
|
|
|
idx := strings.LastIndex(name, "/")
|
|
|
|
if idx >= 0 {
|
|
|
|
return name[idx+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
2022-04-12 17:38:25 +00:00
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
func FetchAndLoadBundle(ctx context.Context, basePath string, bs blockstore.Blockstore, av Version, rel, netw string) (cid.Cid, error) {
|
|
|
|
fetcher, err := NewBundleFetcher(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error creating fetcher for builtin-actors version %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := fetcher.Fetch(av, rel, netw)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error fetching bundle for builtin-actors version %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error opening bundle for builtin-actors vresion %d: %w", av, err)
|
|
|
|
}
|
2022-05-11 19:48:01 +00:00
|
|
|
defer f.Close() //nolint
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
data, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error reading bundle for builtin-actors vresion %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LoadBundle(ctx, bs, av, data); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error loading bundle for builtin-actors vresion %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mfCid, ok := GetManifest(av)
|
|
|
|
if !ok {
|
|
|
|
return cid.Undef, xerrors.Errorf("missing manifest CID for builtin-actors vrsion %d", av)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mfCid, nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 17:38:25 +00:00
|
|
|
func LoadBundle(ctx context.Context, bs blockstore.Blockstore, av Version, data []byte) error {
|
|
|
|
blobr := bytes.NewReader(data)
|
|
|
|
|
|
|
|
hdr, err := car.LoadCar(ctx, bs, blobr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error loading builtin actors v%d bundle: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestCid := hdr.Roots[0]
|
|
|
|
AddManifest(av, manifestCid)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-15 17:15:28 +00:00
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
// utility for blanket loading outside DI
|
|
|
|
func FetchAndLoadBundles(ctx context.Context, bs blockstore.Blockstore, bar map[Version]string) error {
|
|
|
|
// TODO: how to get the network name properly?
|
|
|
|
netw := "mainnet"
|
|
|
|
if v := os.Getenv("LOTUS_FIL_NETWORK"); v != "" {
|
|
|
|
netw = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: how to get the repo properly?
|
|
|
|
path, err := homedir.Expand("~/.lotus")
|
|
|
|
if err != nil {
|
2022-04-15 17:15:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
if p := os.Getenv("LOTUS_PATH"); p != "" {
|
|
|
|
path = p
|
|
|
|
}
|
|
|
|
|
|
|
|
for av, rel := range bar {
|
|
|
|
if _, err := FetchAndLoadBundle(ctx, path, bs, av, rel, netw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:15:28 +00:00
|
|
|
cborStore := cbor.NewCborStore(bs)
|
2022-05-11 19:30:07 +00:00
|
|
|
if err := LoadManifests(ctx, cborStore); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-04-15 17:15:28 +00:00
|
|
|
}
|