Skip to content

Commit

Permalink
an attempt at cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Jun 28, 2022
1 parent 3ad2fc5 commit a52d584
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 161 deletions.
16 changes: 16 additions & 0 deletions chain/actors/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ func ReadManifest(ctx context.Context, store cbor.IpldStore, mfCid cid.Cid) (map
return metadata, nil
}

// Given a Manifest CID (NOT the ManifestData CID), load the underlying ManifestData
func LoadManifestData(ctx context.Context, mfCid cid.Cid, adtStore adt.Store) (*manifest.ManifestData, error) {
var mf manifest.Manifest
var mfData manifest.ManifestData

if err := adtStore.Get(ctx, mfCid, &mf); err != nil {
return nil, xerrors.Errorf("error reading manifest: %w", err)
}

if err := adtStore.Get(ctx, mf.Data, &mfData); err != nil {
return nil, xerrors.Errorf("error fetching data: %w", err)
}

return &mfData, nil
}

// GetActorCodeID looks up a builtin actor's code CID by actor version and canonical actor name.
func GetActorCodeID(av Version, name string) (cid.Cid, bool) {
manifestMx.RLock()
Expand Down
19 changes: 6 additions & 13 deletions chain/consensus/filcns/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -1477,22 +1477,19 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
return cid.Undef, xerrors.Errorf("failed to load state tree: %w", err)
}

oldManifest, err := stmgr.GetManifest(ctx, st)
oldManifestData, err := stmgr.GetManifestData(ctx, st)
if err != nil {
return cid.Undef, xerrors.Errorf("error loading old actor manifest: %w", err)
}
oldManifestData := manifest.ManifestData{}
if err := store.Get(ctx, oldManifest.Data, &oldManifestData); err != nil {
return cid.Undef, xerrors.Errorf("error loading old manifest data: %w", err)
}

// load new manifest
newManifest := manifest.Manifest{}
if err := store.Get(ctx, newActorsManifestCid, &newManifest); err != nil {
return cid.Undef, xerrors.Errorf("error loading new manifest: %w", err)
}
newManifestData := manifest.ManifestData{}
if err := store.Get(ctx, newManifest.Data, &newManifestData); err != nil {

newManifestData, err := actors.LoadManifestData(ctx, newActorsManifestCid, store)
if err != nil {
return cid.Undef, xerrors.Errorf("error loading new manifest data: %w", err)
}

Expand All @@ -1506,12 +1503,8 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
// Maps prior version code CIDs to migration functions.
migrations := make(map[cid.Cid]cid.Cid)

for _, entry := range newManifestData.Entries {
oldCodeCid, ok := oldManifest.Get(entry.Name)
if !ok {
return cid.Undef, xerrors.Errorf("code cid for %s actor not found in old manifest", entry.Name)
}
migrations[oldCodeCid] = entry.Code
for i, entry := range newManifestData.Entries {
migrations[oldManifestData.Entries[i].Code] = entry.Code
}

startTime := time.Now()
Expand Down
31 changes: 7 additions & 24 deletions chain/stmgr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (sm *StateManager) ListAllActors(ctx context.Context, ts *types.TipSet) ([]
return out, nil
}

func GetManifest(ctx context.Context, st *state.StateTree) (*manifest.Manifest, error) {
func GetManifestData(ctx context.Context, st *state.StateTree) (*manifest.ManifestData, error) {
wrapStore := gstStore.WrapStore(ctx, st.Store)

systemActor, err := st.GetActor(system.Address)
Expand All @@ -226,30 +226,13 @@ func GetManifest(ctx context.Context, st *state.StateTree) (*manifest.Manifest,
if err != nil {
return nil, xerrors.Errorf("failed to load system actor state: %w", err)
}
actorsManifestCid := systemActorState.GetBuiltinActors()

mf := manifest.Manifest{
Version: 1,
Data: actorsManifestCid,
}
if err := mf.Load(ctx, wrapStore); err != nil {
return nil, xerrors.Errorf("failed to load actor manifest: %w", err)
}
manifestData := manifest.ManifestData{}
if err := st.Store.Get(ctx, mf.Data, &manifestData); err != nil {
return nil, xerrors.Errorf("failed to load manifest data: %w", err)
}
return &mf, nil
}
actorsManifestDataCid := systemActorState.GetBuiltinActors()

func GetManifestEntries(ctx context.Context, st *state.StateTree) ([]manifest.ManifestEntry, error) {
mf, err := GetManifest(ctx, st)
if err != nil {
return nil, xerrors.Errorf("failed to get manifest: %w", err)
}
manifestData := manifest.ManifestData{}
if err := st.Store.Get(ctx, mf.Data, &manifestData); err != nil {
return nil, xerrors.Errorf("filed to load manifest data: %w", err)
var mfData manifest.ManifestData
if err := wrapStore.Get(ctx, actorsManifestDataCid, &mfData); err != nil {
return nil, xerrors.Errorf("error fetching data: %w", err)
}
return manifestData.Entries, nil

return &mfData, nil
}
64 changes: 5 additions & 59 deletions chain/vm/fvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ import (
"sync"
"time"

builtin7 "github.com/filecoin-project/specs-actors/v7/actors/builtin"
cbg "github.com/whyrusleeping/cbor-gen"

"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"

ffi "github.com/filecoin-project/filecoin-ffi"
ffi_cgo "github.com/filecoin-project/filecoin-ffi/cgo"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/go-state-types/manifest"

"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/build"
Expand Down Expand Up @@ -324,6 +321,7 @@ func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {

baseBstore := opts.Bstore
overlayBstore := blockstore.NewMemorySync()
cborStore := cbor.NewCborStore(overlayBstore)
vmBstore := blockstore.NewTieredBstore(overlayBstore, baseBstore)

fvmopts := &ffi.FVMOpts{
Expand All @@ -344,14 +342,6 @@ func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
Debug: true,
}

loadBundle := func(path string) (cid.Cid, error) {
return bundle.LoadBundleData(ctx, path, overlayBstore)
}

loadManifest := func(mfCid cid.Cid) (manifest.ManifestData, error) {
return bundle.LoadManifestData(ctx, mfCid, overlayBstore)
}

putMapping := func(ar map[cid.Cid]cid.Cid) (cid.Cid, error) {
var mapping xMapping

Expand All @@ -364,7 +354,6 @@ func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
})

// Passing this as a pointer of structs has proven to be an enormous PiTA; hence this code.
cborStore := cbor.NewCborStore(overlayBstore)
mappingCid, err := cborStore.Put(context.TODO(), &mapping)
if err != nil {
return cid.Undef, err
Expand All @@ -379,57 +368,14 @@ func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
}

switch av {
case actors.Version7:
if bundle := os.Getenv("LOTUS_FVM_DEBUG_BUNDLE_V7"); bundle != "" {
mfCid, err := loadBundle(bundle)
if err != nil {
return nil, err
}

mf, err := loadManifest(mfCid)
if err != nil {
return nil, err
}

// create actor redirect mapping from the synthetic Cid to the debug code
actorRedirect := make(map[cid.Cid]cid.Cid)
fromMap := map[string]cid.Cid{
"init": builtin7.InitActorCodeID,
"cron": builtin7.CronActorCodeID,
"account": builtin7.AccountActorCodeID,
"storagepower": builtin7.StoragePowerActorCodeID,
"storageminer": builtin7.StorageMinerActorCodeID,
"paymentchannel": builtin7.PaymentChannelActorCodeID,
"multisig": builtin7.MultisigActorCodeID,
"reward": builtin7.RewardActorCodeID,
"verifiedregistry": builtin7.VerifiedRegistryActorCodeID,
}

for _, e := range mf.Entries {
from, ok := fromMap[e.Name]
if !ok {
return nil, xerrors.Errorf("error mapping %s for debug redirect: %w", e.Name, err)
}
actorRedirect[from] = e.Code
}

if len(actorRedirect) > 0 {
mappingCid, err := putMapping(actorRedirect)
if err != nil {
return nil, xerrors.Errorf("error writing redirect mapping: %w", err)
}
fvmopts.ActorRedirect = mappingCid
}
}

case actors.Version8:
if bundle := os.Getenv("LOTUS_FVM_DEBUG_BUNDLE_V8"); bundle != "" {
mfCid, err := loadBundle(bundle)
if bundlePath := os.Getenv("LOTUS_FVM_DEBUG_BUNDLE_V8"); bundlePath != "" {
mfCid, err := bundle.LoadBundleFromFile(ctx, overlayBstore, bundlePath)
if err != nil {
return nil, err
}

mf, err := loadManifest(mfCid)
mf, err := actors.LoadManifestData(ctx, mfCid, adt.WrapStore(ctx, cborStore))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion chain/vm/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"os"

"github.com/filecoin-project/go-state-types/network"
cid "github.com/ipfs/go-cid"

"github.com/filecoin-project/go-state-types/network"

"github.com/filecoin-project/lotus/chain/types"
)

Expand Down
16 changes: 4 additions & 12 deletions itests/lite_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,11 @@ func TestLiteMigration(t *testing.T) {
oldStateTree, err := state.LoadStateTree(ctxStore, stateRoot)
require.NoError(t, err)

oldManifest, err := stmgr.GetManifest(ctx, oldStateTree)
oldManifestData, err := stmgr.GetManifestData(ctx, oldStateTree)
require.NoError(t, err)
newManifestCid := makeTestManifest(t, ctxStore)
// Use the Cid we generated to get the new manifest instead of loading it from the state tree, because that would not test that we have the correct manifest in the state
var newManifest manifest.Manifest
err = ctxStore.Get(ctx, newManifestCid, &newManifest)
require.NoError(t, err)
err = newManifest.Load(ctx, ctxStore)
require.NoError(t, err)
newManifestData := manifest.ManifestData{}
err = ctxStore.Get(ctx, newManifest.Data, &newManifestData)
newManifestData, err := actors.LoadManifestData(ctx, newManifestCid, ctxStore)
require.NoError(t, err)

newStateRoot, err := filcns.LiteMigration(ctx, bs, newManifestCid, stateRoot, actors.Version8, types.StateTreeVersion4, types.StateTreeVersion4)
Expand All @@ -67,10 +61,8 @@ func TestLiteMigration(t *testing.T) {
require.NoError(t, err)

migrations := make(map[cid.Cid]cid.Cid)
for _, entry := range newManifestData.Entries {
oldCodeCid, ok := oldManifest.Get(entry.Name)
require.True(t, ok)
migrations[oldCodeCid] = entry.Code
for i, entry := range newManifestData.Entries {
migrations[oldManifestData.Entries[i].Code] = entry.Code
}

err = newStateTree.ForEach(func(addr address.Address, newActorState *types.Actor) error {
Expand Down
52 changes: 0 additions & 52 deletions node/bundle/util.go

This file was deleted.

0 comments on commit a52d584

Please sign in to comment.