review fixes

This commit is contained in:
Geoff Stuart 2022-06-08 19:46:51 -04:00
parent 339692cde5
commit 3c4792d3e9
14 changed files with 7896 additions and 84 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -49,11 +49,11 @@ func Load(store adt.Store, act *types.Actor) (State, error) {
return nil, xerrors.Errorf("unknown actor code %s", act.Code)
}
func MakeState(store adt.Store, av actors.Version) (State, error) {
func MakeState(store adt.Store, av actors.Version, builtinActors cid.Cid) (State, error) {
switch av {
{{range .versions}}
case actors.Version{{.}}:
return make{{.}}(store)
return make{{.}}(store{{if (ge . 8)}}, builtinActors{{end}})
{{end}}
}
return nil, xerrors.Errorf("unknown actor version %d", av)

View File

@ -23,9 +23,11 @@ func load{{.v}}(store adt.Store, root cid.Cid) (State, error) {
return &out, nil
}
func make{{.v}}(store adt.Store) (State, error) {
func make{{.v}}(store adt.Store{{if (ge .v 8)}}, builtinActors cid.Cid{{end}}) (State, error) {
out := state{{.v}}{store: store}
out.State = system{{.v}}.State{}
out.State = system{{.v}}.State{
{{if (ge .v 8)}}BuiltinActors: builtinActors,{{end}}
}
return &out, nil
}

View File

@ -71,7 +71,7 @@ func Load(store adt.Store, act *types.Actor) (State, error) {
return nil, xerrors.Errorf("unknown actor code %s", act.Code)
}
func MakeState(store adt.Store, av actors.Version) (State, error) {
func MakeState(store adt.Store, av actors.Version, builtinActors cid.Cid) (State, error) {
switch av {
case actors.Version0:
@ -96,7 +96,7 @@ func MakeState(store adt.Store, av actors.Version) (State, error) {
return make7(store)
case actors.Version8:
return make8(store)
return make8(store, builtinActors)
}
return nil, xerrors.Errorf("unknown actor version %d", av)

View File

@ -19,9 +19,11 @@ func load8(store adt.Store, root cid.Cid) (State, error) {
return &out, nil
}
func make8(store adt.Store) (State, error) {
func make8(store adt.Store, builtinActors cid.Cid) (State, error) {
out := state8{store: store}
out.State = system8.State{}
out.State = system8.State{
BuiltinActors: builtinActors,
}
return &out, nil
}

View File

@ -16,8 +16,8 @@ import (
"github.com/filecoin-project/go-state-types/manifest"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/go-state-types/rt"
gstStore "github.com/filecoin-project/go-state-types/store"
system8 "github.com/filecoin-project/go-state-types/builtin/v8/system"
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
multisig0 "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
@ -32,8 +32,6 @@ import (
"github.com/filecoin-project/specs-actors/v6/actors/migration/nv14"
"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"
"github.com/filecoin-project/specs-actors/v8/actors/migration/nv16"
states8 "github.com/filecoin-project/specs-actors/v8/actors/states"
adt8 "github.com/filecoin-project/specs-actors/v8/actors/util/adt"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/build"
@ -1433,15 +1431,15 @@ func upgradeActorsV8Common(
return newRoot, nil
}
func UpgradeActorsCode(ctx context.Context, sm *stmgr.StateManager, newActorsManifestCid cid.Cid, root cid.Cid) (cid.Cid, error) {
func UpgradeActorsCode(ctx context.Context, sm *stmgr.StateManager, newActorsManifestCid cid.Cid, root cid.Cid, av actors.Version, oldStateTreeVersion types.StateTreeVersion, newStateTreeVersion types.StateTreeVersion) (cid.Cid, error) { // nolint
bstore := sm.ChainStore().StateBlockstore()
return LiteMigration(ctx, bstore, newActorsManifestCid, root)
return LiteMigration(ctx, bstore, newActorsManifestCid, root, av, oldStateTreeVersion, newStateTreeVersion)
}
func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsManifestCid cid.Cid, root cid.Cid) (cid.Cid, error) {
func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsManifestCid cid.Cid, root cid.Cid, av actors.Version, oldStateTreeVersion types.StateTreeVersion, newStateTreeVersion types.StateTreeVersion) (cid.Cid, error) {
buf := blockstore.NewTieredBstore(bstore, blockstore.NewMemorySync())
store := store.ActorStore(ctx, buf)
adtStore := adt8.WrapStore(ctx, store)
adtStore := gstStore.WrapStore(ctx, store)
// Load the state root.
var stateRoot types.StateRoot
@ -1449,29 +1447,24 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
return cid.Undef, xerrors.Errorf("failed to decode state root: %w", err)
}
if stateRoot.Version != types.StateTreeVersion4 {
if stateRoot.Version != oldStateTreeVersion {
return cid.Undef, xerrors.Errorf(
"expected state root version 4 for actors code upgrade, got %d",
stateRoot.Version,
)
}
// Get old actors
actorsIn, err := states8.LoadTree(adtStore, stateRoot.Actors)
st, err := state.LoadStateTree(store, root)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to load state tree: %w", err)
}
systemActor, ok, err := actorsIn.GetActor(system.Address)
if !ok {
// Get old actors
systemActor, err := st.GetActor(system.Address)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get system actor: %w", err)
}
systemActorType := types.Actor{
Code: systemActor.Code,
Head: systemActor.Head,
Nonce: systemActor.CallSeqNum,
Balance: systemActor.Balance,
}
systemActorState, err := system.Load(store, &systemActorType)
systemActorState, err := system.Load(store, systemActor)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to load system actor state: %w", err)
}
@ -1485,6 +1478,10 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
if err := oldManifest.Load(ctx, adtStore); 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{}
@ -1496,6 +1493,13 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
return cid.Undef, xerrors.Errorf("error loading new manifest data: %w", err)
}
if len(newManifestData.Entries) != len(actors.GetBuiltinActorsKeys()) {
return cid.Undef, xerrors.Errorf("incomplete new manifest with %d code CIDs", len(newManifestData.Entries))
}
if len(oldManifestData.Entries) != len(actors.GetBuiltinActorsKeys()) {
return cid.Undef, xerrors.Errorf("incomplete old manifest with %d code CIDs", len(oldManifestData.Entries))
}
// Maps prior version code CIDs to migration functions.
migrations := make(map[cid.Cid]cid.Cid)
@ -1507,42 +1511,42 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
migrations[oldCodeCid] = entry.Code
}
if len(migrations) != 11 {
return cid.Undef, xerrors.Errorf("incomplete manifest with %d code CIDs", len(migrations))
}
startTime := time.Now()
// Load output state tree
actorsOut, err := states8.NewTree(adtStore)
actorsOut, err := state.NewStateTree(adtStore, newStateTreeVersion)
if err != nil {
return cid.Undef, err
}
// Insert migrated records in output state tree.
err = actorsIn.ForEach(func(addr address.Address, actorIn *states8.Actor) error {
var newActor states8.Actor
err = st.ForEach(func(addr address.Address, actorIn *types.Actor) error {
newCid, ok := migrations[actorIn.Code]
if !ok {
return xerrors.Errorf("new code cid for system actor not found in migrations for actor %s", addr)
return xerrors.Errorf("new code cid not found in migrations for actor %s", addr)
}
var newActor types.Actor
if addr == system.Address {
newSystemState := system8.State{BuiltinActors: newManifest.Data}
systemStateHead, err := store.Put(ctx, &newSystemState)
newSystemState, err := system.MakeState(store, av, newManifest.Data)
if err != nil {
return xerrors.Errorf("could not make system actor state: %w", err)
}
systemStateHead, err := store.Put(ctx, newSystemState)
if err != nil {
return xerrors.Errorf("could not set system actor state head: %w", err)
}
newActor = states8.Actor{
Code: newCid,
Head: systemStateHead,
CallSeqNum: actorIn.CallSeqNum,
Balance: actorIn.Balance,
newActor = types.Actor{
Code: newCid,
Head: systemStateHead,
Nonce: actorIn.Nonce,
Balance: actorIn.Balance,
}
} else {
newActor = states8.Actor{
Code: newCid,
Head: actorIn.Head,
CallSeqNum: actorIn.CallSeqNum,
Balance: actorIn.Balance,
newActor = types.Actor{
Code: newCid,
Head: actorIn.Head,
Nonce: actorIn.Nonce,
Balance: actorIn.Balance,
}
}
err = actorsOut.SetActor(addr, &newActor)
@ -1552,26 +1556,18 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("failed update actor states: %w", err)
}
elapsed := time.Since(startTime)
log.Infof("All done after %v. Flushing state tree root.", elapsed)
newHamtRoot, err := actorsOut.Flush()
newRoot, err := actorsOut.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to flush new actors: %w", err)
}
// Persist the result.
newRoot, err := store.Put(ctx, &types.StateRoot{
Version: types.StateTreeVersion4,
Actors: newHamtRoot,
Info: stateRoot.Info,
})
if err != nil {
return cid.Undef, xerrors.Errorf("failed to persist new state root: %w", err)
}
// Persist the new tree.
{
from := buf
to := buf.Read()

View File

@ -3,6 +3,8 @@ package genesis
import (
"context"
"github.com/ipfs/go-cid"
systemtypes "github.com/filecoin-project/go-state-types/builtin/v8/system"
"github.com/filecoin-project/go-state-types/manifest"
@ -25,7 +27,8 @@ import (
func SetupSystemActor(ctx context.Context, bs bstore.Blockstore, av actors.Version) (*types.Actor, error) {
cst := cbor.NewCborStore(bs)
st, err := system.MakeState(adt.WrapStore(ctx, cst), av)
// TODO pass in built-in actors cid for V8 and later
st, err := system.MakeState(adt.WrapStore(ctx, cst), av, cid.Undef)
if err != nil {
return nil, err
}

View File

@ -3,20 +3,21 @@ package stmgr
import (
"bytes"
"context"
"fmt"
"os"
"github.com/filecoin-project/lotus/chain/rand"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/manifest"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/go-state-types/store"
cid "github.com/ipfs/go-cid"
"golang.org/x/xerrors"
miner_types "github.com/filecoin-project/go-state-types/builtin/v8/miner"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin"
@ -24,7 +25,10 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/system"
"github.com/filecoin-project/lotus/chain/beacon"
"github.com/filecoin-project/lotus/chain/rand"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
@ -552,4 +556,60 @@ func (sm *StateManager) MarketBalance(ctx context.Context, addr address.Address,
return out, nil
}
func (sm *StateManager) GetManifest(ctx context.Context, root cid.Cid) (*manifest.Manifest, error) {
st, err := sm.StateTree(root)
if err != nil {
return nil, fmt.Errorf("failed to get state tree: %w", err)
}
return GetManifestFromStateTree(ctx, st)
}
func GetManifestFromStateTree(ctx context.Context, st *state.StateTree) (*manifest.Manifest, error) {
wrapStore := store.WrapStore(ctx, st.Store)
systemActor, err := st.GetActor(system.Address)
if err != nil {
return nil, fmt.Errorf("failed to get system actor: %w", err)
}
systemActorState, err := system.Load(wrapStore, systemActor)
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
}
func (sm *StateManager) GetManifestData(ctx context.Context, root cid.Cid) ([]manifest.ManifestEntry, error) {
st, err := sm.StateTree(root)
if err != nil {
return nil, fmt.Errorf("failed to get state tree: %w", err)
}
return GetManifestDataFromStateTree(ctx, st)
}
func GetManifestDataFromStateTree(ctx context.Context, st *state.StateTree) ([]manifest.ManifestEntry, error) {
mf, err := GetManifestFromStateTree(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)
}
return manifestData.Entries, nil
}
var _ StateManagerAPI = (*StateManager)(nil)

File diff suppressed because it is too large Load Diff

2
go.mod
View File

@ -41,7 +41,7 @@ require (
github.com/filecoin-project/go-legs v0.3.10
github.com/filecoin-project/go-padreader v0.0.1
github.com/filecoin-project/go-paramfetch v0.0.4
github.com/filecoin-project/go-state-types v0.1.7
github.com/filecoin-project/go-state-types v0.1.9
github.com/filecoin-project/go-statemachine v1.0.2
github.com/filecoin-project/go-statestore v0.2.0
github.com/filecoin-project/go-storedcounter v0.1.0

4
go.sum
View File

@ -372,8 +372,8 @@ github.com/filecoin-project/go-state-types v0.1.3/go.mod h1:ezYnPf0bNkTsDibL/psS
github.com/filecoin-project/go-state-types v0.1.4-0.20220511200558-7a486892661a/go.mod h1:xCA/WfKlC2zcn3fUmDv4IrzznwS98X5XW/irUP3Lhxg=
github.com/filecoin-project/go-state-types v0.1.4/go.mod h1:xCA/WfKlC2zcn3fUmDv4IrzznwS98X5XW/irUP3Lhxg=
github.com/filecoin-project/go-state-types v0.1.6/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.7 h1:r/ZzyUA+CqY8IXyHsLtliqRgPFaON+aC2MmWKm1nl98=
github.com/filecoin-project/go-state-types v0.1.7/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.9 h1:7ffQu+arDAiW5dphWTl8WdgWTo9Kt3BsjGcNYr99crI=
github.com/filecoin-project/go-state-types v0.1.9/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-statemachine v0.0.0-20200925024713-05bd7c71fbfe/go.mod h1:FGwQgZAt2Gh5mjlwJUlVB62JeYdo+if0xWxSEfBD9ig=
github.com/filecoin-project/go-statemachine v1.0.2-0.20220322104818-27f8fbb86dfd/go.mod h1:jZdXXiHa61n4NmgWFG4w8tnqgvZVHYbJ3yW7+y8bF54=
github.com/filecoin-project/go-statemachine v1.0.2 h1:421SSWBk8GIoCoWYYTE/d+qCWccgmRH0uXotXRDjUbc=

View File

@ -6,21 +6,21 @@ import (
"testing"
"time"
"github.com/filecoin-project/lotus/chain/actors/builtin/system"
"github.com/filecoin-project/lotus/chain/state"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/filecoin-project/lotus/blockstore"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/manifest"
"github.com/filecoin-project/go-state-types/network"
gstStore "github.com/filecoin-project/go-state-types/store"
"github.com/filecoin-project/specs-actors/v8/actors/util/adt"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/itests/kit"
)
@ -38,32 +38,50 @@ func TestLiteMigration(t *testing.T) {
})
bs := blockstore.NewAPIBlockstore(client16)
ctxStore := adt.WrapBlockStore(ctx, bs)
ctxStore := gstStore.WrapBlockStore(ctx, bs)
ts, err := client16.ChainHead(ctx)
require.NoError(t, err)
stateRoot := ts.ParentState()
oldStateTree, err := state.LoadStateTree(ctxStore, stateRoot)
require.NoError(t, err)
oldManifest, err := stmgr.GetManifestFromStateTree(ctx, oldStateTree)
require.NoError(t, err)
newManifestCid := makeTestManifest(t, ctxStore)
newStateRoot, err := filcns.LiteMigration(ctx, bs, newManifestCid, stateRoot)
require.NoError(t, err)
stateTree, err := state.LoadStateTree(ctxStore, newStateRoot)
require.NoError(t, err)
systemActor, err := stateTree.GetActor(system.Address)
// 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)
manifestSystemCodeCid, ok := newManifest.Get("system")
require.True(t, ok)
newManifestData := manifest.ManifestData{}
err = ctxStore.Get(ctx, newManifest.Data, &newManifestData)
require.NoError(t, err)
require.Equal(t, systemActor.Code, manifestSystemCodeCid)
newStateRoot, err := filcns.LiteMigration(ctx, bs, newManifestCid, stateRoot, actors.Version8, types.StateTreeVersion4, types.StateTreeVersion4)
require.NoError(t, err)
newStateTree, err := state.LoadStateTree(ctxStore, newStateRoot)
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
}
err = newStateTree.ForEach(func(addr address.Address, newActorState *types.Actor) error {
oldActor, err := oldStateTree.GetActor(addr)
require.NoError(t, err)
newCodeCid, ok := migrations[oldActor.Code]
require.True(t, ok)
require.Equal(t, newCodeCid, newActorState.Code)
return nil
})
require.NoError(t, err)
}
func makeTestManifest(t *testing.T, ctxStore adt.Store) cid.Cid {