Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
travisperson committed Mar 9, 2023
1 parent 814c146 commit b4e589a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
6 changes: 3 additions & 3 deletions chain/stmgr/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func (sm *StateManager) HandleStateForks(ctx context.Context, root cid.Cid, heig
retCid := root
u := sm.stateMigrations[height]
if u != nil && u.upgrade != nil {
migCid, ok, err := u.resultCache.Result(ctx, root)
migCid, ok, err := u.migrationResultCache.Get(ctx, root)
if err == nil && ok {
log.Warnw("CACHED migration", "height", height, "from", root, "to", migCid)
log.Infow("CACHED migration", "height", height, "from", root, "to", migCid)
return migCid, nil
} else if err != nil {
log.Errorw("failed to lookup previous migration result", "err", err)
Expand Down Expand Up @@ -206,7 +206,7 @@ func (sm *StateManager) HandleStateForks(ctx context.Context, root cid.Cid, heig
)

// Only set if migration ran, we do not want a root => root mapping
if err := u.resultCache.Store(ctx, root, retCid); err != nil {
if err := u.migrationResultCache.Store(ctx, root, retCid); err != nil {
log.Errorw("failed to store migration result", "err", err)
}
}
Expand Down
41 changes: 19 additions & 22 deletions chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ipfs/go-cid"
dstore "github.com/ipfs/go-datastore"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -53,32 +54,29 @@ type versionSpec struct {
}

type migration struct {
upgrade MigrationFunc
preMigrations []PreMigration
cache *nv16.MemMigrationCache
resultCache *resultCache
upgrade MigrationFunc
preMigrations []PreMigration
cache *nv16.MemMigrationCache
migrationResultCache *migrationResultCache
}

type resultCache struct {
type migrationResultCache struct {
ds dstore.Batching
keyPrefix string
}

func (m *resultCache) Result(ctx context.Context, root cid.Cid) (cid.Cid, bool, error) {
kStr := fmt.Sprintf("%s-%s", m.keyPrefix, root)
k := dstore.NewKey(kStr)

found, err := m.ds.Has(ctx, k)
if err != nil {
return cid.Undef, false, xerrors.Errorf("error looking up migration result: %w", err)
}
func (m *migrationResultCache) keyForMigration(root cid.Cid) dstore.Key {
kStr := fmt.Sprintf("%s/%s", m.keyPrefix, root)
return dstore.NewKey(kStr)
}

if !found {
return cid.Undef, false, nil
}
func (m *migrationResultCache) Get(ctx context.Context, root cid.Cid) (cid.Cid, bool, error) {
k := m.keyForMigration(root)

bs, err := m.ds.Get(ctx, k)
if err != nil {
if ipld.IsNotFound(err) {
return cid.Undef, false, nil
} else if err != nil {
return cid.Undef, false, xerrors.Errorf("error loading migration result: %w", err)
}

Expand All @@ -90,9 +88,8 @@ func (m *resultCache) Result(ctx context.Context, root cid.Cid) (cid.Cid, bool,
return c, true, nil
}

func (m *resultCache) Store(ctx context.Context, root cid.Cid, resultCid cid.Cid) error {
kStr := fmt.Sprintf("%s-%s", m.keyPrefix, root)
k := dstore.NewKey(kStr)
func (m *migrationResultCache) Store(ctx context.Context, root cid.Cid, resultCid cid.Cid) error {
k := m.keyForMigration(root)
if err := m.ds.Put(ctx, k, resultCid.Bytes()); err != nil {
return err
}
Expand Down Expand Up @@ -166,8 +163,8 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
upgrade: upgrade.Migration,
preMigrations: upgrade.PreMigrations,
cache: nv16.NewMemMigrationCache(),
resultCache: &resultCache{
keyPrefix: fmt.Sprintf("nv%d-%d", upgrade.Network, upgrade.Height),
migrationResultCache: &migrationResultCache{
keyPrefix: fmt.Sprintf("/migration-cache/nv%d", upgrade.Network),
ds: metadataDs,
},
}
Expand Down

0 comments on commit b4e589a

Please sign in to comment.