From 7be42d9935e940854e5d710fb0b3378b6d7eca5b Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 12 May 2022 16:42:00 +0300 Subject: [PATCH] refactor to pass the network bundle name through ldflags from build --- build/bundle.go | 3 ++ chain/actors/manifest.go | 70 ----------------------------- cli/init.go | 4 +- cmd/lotus-miner/init.go | 3 +- node/bundle/manifest.go | 82 ++++++++++++++++++++++++++++++++++ node/modules/builtin_actors.go | 17 ++++--- 6 files changed, 97 insertions(+), 82 deletions(-) create mode 100644 build/bundle.go create mode 100644 node/bundle/manifest.go diff --git a/build/bundle.go b/build/bundle.go new file mode 100644 index 000000000..67c4906df --- /dev/null +++ b/build/bundle.go @@ -0,0 +1,3 @@ +package build + +var NetworkBundle string diff --git a/chain/actors/manifest.go b/chain/actors/manifest.go index 966167fe0..661f156a9 100644 --- a/chain/actors/manifest.go +++ b/chain/actors/manifest.go @@ -3,8 +3,6 @@ package actors import ( "bytes" "context" - "io" - "os" "strings" "sync" @@ -16,10 +14,7 @@ import ( "github.com/filecoin-project/lotus/blockstore" "github.com/filecoin-project/lotus/chain/actors/adt" - "github.com/filecoin-project/lotus/node/bundle" "github.com/filecoin-project/specs-actors/v8/actors/builtin/manifest" - - "github.com/mitchellh/go-homedir" ) var manifestCids map[Version]cid.Cid = map[Version]cid.Cid{ @@ -122,40 +117,6 @@ func CanonicalName(name string) string { return name } -func FetchAndLoadBundle(ctx context.Context, basePath string, bs blockstore.Blockstore, av Version, rel, netw string) (cid.Cid, error) { - fetcher, err := bundle.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(int(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) - } - defer f.Close() //nolint - - 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 -} - func LoadBundle(ctx context.Context, bs blockstore.Blockstore, av Version, data []byte) error { blobr := bytes.NewReader(data) @@ -169,34 +130,3 @@ func LoadBundle(ctx context.Context, bs blockstore.Blockstore, av Version, data return nil } - -// 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 - } - - path := os.Getenv("LOTUS_PATH") - if path == "" { - var err error - path, err = homedir.Expand("~/.lotus") - if err != nil { - return err - } - } - - for av, rel := range bar { - if _, err := FetchAndLoadBundle(ctx, path, bs, av, rel, netw); err != nil { - return err - } - } - - cborStore := cbor.NewCborStore(bs) - if err := LoadManifests(ctx, cborStore); err != nil { - return err - } - - return nil -} diff --git a/cli/init.go b/cli/init.go index 199fb86e2..e5dc45fea 100644 --- a/cli/init.go +++ b/cli/init.go @@ -5,7 +5,7 @@ import ( "github.com/filecoin-project/lotus/blockstore" "github.com/filecoin-project/lotus/build" - "github.com/filecoin-project/lotus/chain/actors" + "github.com/filecoin-project/lotus/node/bundle" ) func init() { @@ -13,7 +13,7 @@ func init() { // go through CI bs := blockstore.NewMemory() - if err := actors.FetchAndLoadBundles(context.Background(), bs, build.BuiltinActorReleases); err != nil { + if err := bundle.FetchAndLoadBundles(context.Background(), bs, build.BuiltinActorReleases); err != nil { panic(err) } } diff --git a/cmd/lotus-miner/init.go b/cmd/lotus-miner/init.go index 144fb63ce..db3a4d978 100644 --- a/cmd/lotus-miner/init.go +++ b/cmd/lotus-miner/init.go @@ -58,6 +58,7 @@ import ( "github.com/filecoin-project/lotus/journal" "github.com/filecoin-project/lotus/journal/fsjournal" storageminer "github.com/filecoin-project/lotus/miner" + "github.com/filecoin-project/lotus/node/bundle" "github.com/filecoin-project/lotus/node/modules" "github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/repo" @@ -219,7 +220,7 @@ var initCmd = &cli.Command{ // load bundles bs := blockstore.NewMemory() - if err := actors.FetchAndLoadBundles(ctx, bs, build.BuiltinActorReleases); err != nil { + if err := bundle.FetchAndLoadBundles(ctx, bs, build.BuiltinActorReleases); err != nil { return err } diff --git a/node/bundle/manifest.go b/node/bundle/manifest.go new file mode 100644 index 000000000..45c632bdb --- /dev/null +++ b/node/bundle/manifest.go @@ -0,0 +1,82 @@ +package bundle + +import ( + "context" + "io" + "os" + + "golang.org/x/xerrors" + + "github.com/filecoin-project/lotus/blockstore" + "github.com/filecoin-project/lotus/build" + "github.com/filecoin-project/lotus/chain/actors" + + cid "github.com/ipfs/go-cid" + cbor "github.com/ipfs/go-ipld-cbor" + + "github.com/mitchellh/go-homedir" +) + +func FetchAndLoadBundle(ctx context.Context, basePath string, bs blockstore.Blockstore, av actors.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(int(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) + } + defer f.Close() //nolint + + 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 := actors.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 := actors.GetManifest(av) + if !ok { + return cid.Undef, xerrors.Errorf("missing manifest CID for builtin-actors vrsion %d", av) + } + + return mfCid, nil +} + +// utility for blanket loading outside DI +func FetchAndLoadBundles(ctx context.Context, bs blockstore.Blockstore, bar map[actors.Version]string) error { + netw := build.NetworkBundle + if netw == "" { + netw = "mainnet" + } + + path := os.Getenv("LOTUS_PATH") + if path == "" { + var err error + path, err = homedir.Expand("~/.lotus") + if err != nil { + return err + } + } + + for av, rel := range bar { + if _, err := FetchAndLoadBundle(ctx, path, bs, av, rel, netw); err != nil { + return err + } + } + + cborStore := cbor.NewCborStore(bs) + if err := actors.LoadManifests(ctx, cborStore); err != nil { + return err + } + + return nil +} diff --git a/node/modules/builtin_actors.go b/node/modules/builtin_actors.go index 1fd683850..36b8efa05 100644 --- a/node/modules/builtin_actors.go +++ b/node/modules/builtin_actors.go @@ -2,7 +2,6 @@ package modules import ( "fmt" - "os" "sync" "go.uber.org/fx" @@ -10,6 +9,7 @@ import ( "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/actors" + "github.com/filecoin-project/lotus/node/bundle" "github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/node/repo" @@ -22,12 +22,11 @@ import ( func LoadBuiltinActors(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo, bs dtypes.UniversalBlockstore, ds dtypes.MetadataDS) (result dtypes.BuiltinActorsLoaded, err error) { ctx := helpers.LifecycleCtx(mctx, lc) - // TODO how to properly get the network name? - // putting it as a dep in inputs causes a stack overflow in DI from circular dependency - // sigh... - netw := "mainnet" - if v := os.Getenv("LOTUS_FIL_NETWORK"); v != "" { - netw = v + // 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 + netw := build.NetworkBundle + if netw == "" { + netw = "mainnet" } for av, rel := range build.BuiltinActorReleases { @@ -58,7 +57,7 @@ func LoadBuiltinActors(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRe } // ok, we don't have it -- fetch it and add it to the blockstore - mfCid, err := actors.FetchAndLoadBundle(ctx, r.Path(), bs, av, rel, netw) + mfCid, err := bundle.FetchAndLoadBundle(ctx, r.Path(), bs, av, rel, netw) if err != nil { return result, err } @@ -95,7 +94,7 @@ func LoadBuiltinActorsTesting(lc fx.Lifecycle, mctx helpers.MetricsCtx, bs dtype for av, rel := range build.BuiltinActorReleases { const basePath = "/tmp/lotus-testing" - if _, err := actors.FetchAndLoadBundle(ctx, basePath, bs, av, rel, netw); err != nil { + if _, err := bundle.FetchAndLoadBundle(ctx, basePath, bs, av, rel, netw); err != nil { return result, xerrors.Errorf("error loading bundle for builtin-actors vresion %d: %w", av, err) } }