2022-05-12 13:42:00 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-17 18:14:49 +00:00
|
|
|
"fmt"
|
2022-05-12 13:42:00 +00:00
|
|
|
"os"
|
|
|
|
|
2022-05-26 19:14:29 +00:00
|
|
|
"github.com/ipld/go-car"
|
|
|
|
|
2022-05-12 13:42:00 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-05-17 17:38:32 +00:00
|
|
|
func FetchAndLoadBundleFromRelease(ctx context.Context, basePath string, bs blockstore.Blockstore, av actors.Version, rel, netw string) (cid.Cid, error) {
|
2022-05-12 13:42:00 +00:00
|
|
|
fetcher, err := NewBundleFetcher(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error creating fetcher for builtin-actors version %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
2022-05-17 17:38:32 +00:00
|
|
|
path, err := fetcher.FetchFromRelease(int(av), rel, netw)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error fetching bundle for builtin-actors version %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoadBundle(ctx, bs, path, av)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FetchAndLoadBundleFromURL(ctx context.Context, basePath string, bs blockstore.Blockstore, av actors.Version, rel, netw, url, cksum 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.FetchFromURL(int(av), rel, netw, url, cksum)
|
2022-05-12 13:42:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error fetching bundle for builtin-actors version %d: %w", av, err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:09:09 +00:00
|
|
|
return LoadBundle(ctx, bs, path, av)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadBundle(ctx context.Context, bs blockstore.Blockstore, path string, av actors.Version) (cid.Cid, error) {
|
2022-05-12 13:42:00 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2022-05-17 22:33:15 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("error opening bundle for builtin-actors version %d: %w", av, err)
|
2022-05-12 13:42:00 +00:00
|
|
|
}
|
|
|
|
defer f.Close() //nolint
|
|
|
|
|
2022-06-02 02:22:52 +00:00
|
|
|
hdr, err := car.LoadCar(ctx, bs, f)
|
2022-05-26 19:14:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error loading builtin actors v%d bundle: %w", av, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check that this only has one root?
|
|
|
|
manifestCid := hdr.Roots[0]
|
|
|
|
|
|
|
|
if val, ok := build.ActorsCIDs[av]; ok {
|
|
|
|
if val != manifestCid {
|
|
|
|
return cid.Undef, xerrors.Errorf("actors V%d manifest CID %s did not match CID given in params file: %s", av, manifestCid, val)
|
|
|
|
}
|
2022-05-12 13:42:00 +00:00
|
|
|
}
|
2022-05-26 19:14:29 +00:00
|
|
|
actors.AddManifest(av, manifestCid)
|
2022-05-12 13:42:00 +00:00
|
|
|
|
|
|
|
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
|
2022-05-16 18:09:09 +00:00
|
|
|
func FetchAndLoadBundles(ctx context.Context, bs blockstore.Blockstore, bar map[actors.Version]build.Bundle) error {
|
2022-05-24 16:33:57 +00:00
|
|
|
netw := build.NetworkBundle
|
2022-05-12 13:42:00 +00:00
|
|
|
|
|
|
|
path := os.Getenv("LOTUS_PATH")
|
|
|
|
if path == "" {
|
|
|
|
var err error
|
|
|
|
path, err = homedir.Expand("~/.lotus")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:09:09 +00:00
|
|
|
for av, bd := range bar {
|
2022-05-17 18:47:01 +00:00
|
|
|
envvar := fmt.Sprintf("LOTUS_BUILTIN_ACTORS_V%d_BUNDLE", av)
|
2022-05-17 17:38:32 +00:00
|
|
|
switch {
|
2022-05-17 18:14:49 +00:00
|
|
|
case os.Getenv(envvar) != "":
|
2022-05-17 17:38:32 +00:00
|
|
|
// this is a local bundle, specified by an env var to load from the filesystem
|
2022-05-17 18:14:49 +00:00
|
|
|
path := os.Getenv(envvar)
|
2022-05-17 17:38:32 +00:00
|
|
|
|
|
|
|
if _, err := LoadBundle(ctx, bs, path, av); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-17 18:45:17 +00:00
|
|
|
case bd.Path[netw] != "":
|
|
|
|
if _, err := LoadBundle(ctx, bs, bd.Path[netw], av); err != nil {
|
2022-05-16 18:09:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-17 17:38:32 +00:00
|
|
|
|
2022-05-17 18:45:17 +00:00
|
|
|
case bd.URL[netw].URL != "":
|
|
|
|
if _, err := FetchAndLoadBundleFromURL(ctx, path, bs, av, bd.Release, netw, bd.URL[netw].URL, bd.URL[netw].Checksum); err != nil {
|
2022-05-17 17:38:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case bd.Release != "":
|
|
|
|
if _, err := FetchAndLoadBundleFromRelease(ctx, path, bs, av, bd.Release, netw); err != nil {
|
2022-05-16 18:09:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-12 13:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cborStore := cbor.NewCborStore(bs)
|
|
|
|
if err := actors.LoadManifests(ctx, cborStore); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|