2022-04-04 14:48:13 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
2022-04-19 14:33:08 +00:00
|
|
|
"fmt"
|
2022-05-17 17:38:32 +00:00
|
|
|
"os"
|
2022-05-11 19:30:07 +00:00
|
|
|
"sync"
|
2022-04-19 14:33:08 +00:00
|
|
|
|
2022-04-04 14:48:13 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2022-05-12 13:42:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/bundle"
|
2022-04-04 14:48:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2022-05-11 19:30:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2022-04-04 14:48:13 +00:00
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
dstore "github.com/ipfs/go-datastore"
|
2022-04-04 14:48:13 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
)
|
|
|
|
|
2022-05-12 06:47:27 +00:00
|
|
|
func LoadBuiltinActors(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo, bs dtypes.UniversalBlockstore, ds dtypes.MetadataDS) (result dtypes.BuiltinActorsLoaded, err error) {
|
2022-04-04 14:48:13 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2022-05-12 13:42:00 +00:00
|
|
|
// We can't put it as a dep in inputs causes a stack overflow in DI from circular dependency
|
|
|
|
// So we pass it through ldflags instead
|
2022-05-16 19:02:37 +00:00
|
|
|
netw := build.GetNetworkBundle()
|
2022-04-04 14:48:13 +00:00
|
|
|
|
2022-05-16 18:09:09 +00:00
|
|
|
for av, bd := range build.BuiltinActorReleases {
|
2022-05-12 19:03:18 +00:00
|
|
|
// first check to see if we know this release
|
2022-05-16 18:09:09 +00:00
|
|
|
key := dstore.NewKey(fmt.Sprintf("/builtin-actors/v%d/%s", av, bd.Release))
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
data, err := ds.Get(ctx, key)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2022-05-12 19:03:18 +00:00
|
|
|
// ok, we do, this should be the manifest cid
|
2022-05-11 19:30:07 +00:00
|
|
|
mfCid, err := cid.Cast(data)
|
|
|
|
if err != nil {
|
|
|
|
return result, xerrors.Errorf("error parsing cid for %s: %w", key, err)
|
|
|
|
}
|
|
|
|
|
2022-05-12 19:03:18 +00:00
|
|
|
// check the blockstore for existence of the manifest
|
2022-05-11 19:30:07 +00:00
|
|
|
has, err := bs.Has(ctx, mfCid)
|
|
|
|
if err != nil {
|
|
|
|
return result, xerrors.Errorf("error checking blockstore for manifest cid %s: %w", mfCid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
2022-05-12 19:03:18 +00:00
|
|
|
// it's there, no need to reload the bundle to the blockstore; just add it to the manifest list.
|
2022-05-11 19:30:07 +00:00
|
|
|
actors.AddManifest(av, mfCid)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-12 19:03:18 +00:00
|
|
|
// we have a release key but don't have the manifest in the blockstore; maybe the user
|
|
|
|
// nuked his blockstore to restart from a snapshot. So fallthrough to refetch (if necessary)
|
|
|
|
// and reload the bundle.
|
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
case dstore.ErrNotFound:
|
2022-05-12 19:03:18 +00:00
|
|
|
// we don't have a release key, we need to load the bundle
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return result, xerrors.Errorf("error loading %s from datastore: %w", key, err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:38:09 +00:00
|
|
|
// we haven't recorded it in the datastore, so we need to load it
|
2022-05-17 18:47:01 +00:00
|
|
|
envvar := fmt.Sprintf("LOTUS_BUILTIN_ACTORS_V%d_BUNDLE", av)
|
2022-05-16 18:09:09 +00:00
|
|
|
var mfCid cid.Cid
|
2022-05-16 18:44:31 +00:00
|
|
|
switch {
|
2022-05-17 18:14:49 +00:00
|
|
|
case os.Getenv(envvar) != "":
|
|
|
|
path := os.Getenv(envvar)
|
2022-05-17 17:38:32 +00:00
|
|
|
|
|
|
|
mfCid, err = bundle.LoadBundle(ctx, bs, path, av)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-05-17 18:45:17 +00:00
|
|
|
case bd.Path[netw] != "":
|
2022-05-16 18:09:09 +00:00
|
|
|
// this is a local bundle, load it directly from the filessystem
|
2022-05-17 18:45:17 +00:00
|
|
|
mfCid, err = bundle.LoadBundle(ctx, bs, bd.Path[netw], av)
|
2022-05-16 18:09:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2022-05-16 18:44:31 +00:00
|
|
|
|
2022-05-17 18:45:17 +00:00
|
|
|
case bd.URL[netw].URL != "":
|
2022-05-17 17:38:32 +00:00
|
|
|
// fetch it from the specified URL
|
2022-05-17 18:45:17 +00:00
|
|
|
mfCid, err = bundle.FetchAndLoadBundleFromURL(ctx, r.Path(), bs, av, bd.Release, netw, bd.URL[netw].URL, bd.URL[netw].Checksum)
|
2022-05-17 17:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:44:31 +00:00
|
|
|
case bd.Release != "":
|
2022-05-16 18:09:09 +00:00
|
|
|
// fetch it and add it to the blockstore
|
2022-05-17 17:38:32 +00:00
|
|
|
mfCid, err = bundle.FetchAndLoadBundleFromRelease(ctx, r.Path(), bs, av, bd.Release, netw)
|
2022-05-16 18:09:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2022-05-16 18:44:31 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return result, xerrors.Errorf("no release or path specified for version %d bundle", av)
|
2022-04-12 17:38:25 +00:00
|
|
|
}
|
2022-05-11 19:30:07 +00:00
|
|
|
|
2022-05-16 18:44:31 +00:00
|
|
|
if bd.Development || bd.Release == "" {
|
2022-05-16 12:11:10 +00:00
|
|
|
// don't store the release key so that we always load development bundles
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-12 19:03:18 +00:00
|
|
|
// add the release key with the manifest to avoid reloading it in next restart.
|
2022-05-11 19:30:07 +00:00
|
|
|
if err := ds.Put(ctx, key, mfCid.Bytes()); err != nil {
|
|
|
|
return result, xerrors.Errorf("error storing manifest CID for builtin-actors vrsion %d to the datastore: %w", av, err)
|
|
|
|
}
|
2022-04-12 17:38:25 +00:00
|
|
|
}
|
2022-04-04 14:48:13 +00:00
|
|
|
|
2022-05-12 19:03:18 +00:00
|
|
|
// we've loaded all the bundles, now load the manifests to get actor code CIDs.
|
2022-04-04 14:48:13 +00:00
|
|
|
cborStore := cbor.NewCborStore(bs)
|
|
|
|
if err := actors.LoadManifests(ctx, cborStore); err != nil {
|
2022-04-04 17:06:49 +00:00
|
|
|
return result, xerrors.Errorf("error loading actor manifests: %w", err)
|
2022-04-04 14:48:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 17:06:49 +00:00
|
|
|
return result, nil
|
2022-04-04 14:48:13 +00:00
|
|
|
}
|
2022-04-19 14:33:08 +00:00
|
|
|
|
|
|
|
// for itests
|
2022-05-11 19:30:07 +00:00
|
|
|
var testingBundleMx sync.Mutex
|
|
|
|
|
2022-04-19 14:33:08 +00:00
|
|
|
func LoadBuiltinActorsTesting(lc fx.Lifecycle, mctx helpers.MetricsCtx, bs dtypes.UniversalBlockstore) (result dtypes.BuiltinActorsLoaded, err error) {
|
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
var netw string
|
2022-04-22 08:34:18 +00:00
|
|
|
if build.InsecurePoStValidation {
|
2022-05-11 19:30:07 +00:00
|
|
|
netw = "testing-fake-proofs"
|
2022-04-22 08:34:18 +00:00
|
|
|
} else {
|
2022-05-11 19:30:07 +00:00
|
|
|
netw = "testing"
|
2022-04-22 08:34:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 19:30:07 +00:00
|
|
|
testingBundleMx.Lock()
|
|
|
|
defer testingBundleMx.Unlock()
|
2022-04-19 14:33:08 +00:00
|
|
|
|
2022-05-17 18:45:17 +00:00
|
|
|
const basePath = "/tmp/lotus-testing"
|
2022-05-16 18:09:09 +00:00
|
|
|
for av, bd := range build.BuiltinActorReleases {
|
2022-05-16 18:44:31 +00:00
|
|
|
switch {
|
2022-05-17 18:45:17 +00:00
|
|
|
case bd.Path[netw] != "":
|
|
|
|
if _, err := bundle.LoadBundle(ctx, bs, bd.Path[netw], av); err != nil {
|
2022-05-16 18:09:09 +00:00
|
|
|
return result, xerrors.Errorf("error loading testing bundle for builtin-actors version %d/%s: %w", av, netw, err)
|
|
|
|
}
|
2022-05-16 18:44:31 +00:00
|
|
|
|
2022-05-17 18:45:17 +00:00
|
|
|
case bd.URL[netw].URL != "":
|
|
|
|
// fetch it from the specified URL
|
|
|
|
if _, err := bundle.FetchAndLoadBundleFromURL(ctx, basePath, bs, av, bd.Release, netw, bd.URL[netw].URL, bd.URL[netw].Checksum); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:44:31 +00:00
|
|
|
case bd.Release != "":
|
2022-05-17 17:38:32 +00:00
|
|
|
if _, err := bundle.FetchAndLoadBundleFromRelease(ctx, basePath, bs, av, bd.Release, netw); err != nil {
|
2022-05-16 18:09:09 +00:00
|
|
|
return result, xerrors.Errorf("error loading testing bundle for builtin-actors version %d/%s: %w", av, netw, err)
|
|
|
|
}
|
2022-05-16 18:44:31 +00:00
|
|
|
|
|
|
|
default:
|
2022-05-17 17:38:32 +00:00
|
|
|
return result, xerrors.Errorf("no path or release specified for version %d testing bundle", av)
|
2022-04-19 14:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cborStore := cbor.NewCborStore(bs)
|
|
|
|
if err := actors.LoadManifests(ctx, cborStore); err != nil {
|
|
|
|
return result, xerrors.Errorf("error loading actor manifests: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|