Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Jun 9, 2022
1 parent 339692c commit 87c7fd4
Show file tree
Hide file tree
Showing 11 changed files with 7,797 additions and 59 deletions.
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/miner.json.gz
Binary file not shown.
Binary file modified build/openrpc/worker.json.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions chain/actors/builtin/system/actor.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions chain/actors/builtin/system/state.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions chain/actors/builtin/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions chain/actors/builtin/system/v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
95 changes: 46 additions & 49 deletions chain/consensus/filcns/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"runtime"
"time"

adt8 "github.com/filecoin-project/specs-actors/v8/actors/util/adt"

"github.com/docker/go-units"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
Expand All @@ -17,7 +19,6 @@ import (
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/go-state-types/rt"

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"
Expand All @@ -32,8 +33,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"
Expand Down Expand Up @@ -1433,12 +1432,12 @@ 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)
Expand All @@ -1449,29 +1448,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)
}
Expand All @@ -1485,6 +1479,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{}
Expand All @@ -1496,6 +1494,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)

Expand All @@ -1507,42 +1512,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)
Expand All @@ -1552,26 +1557,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()
Expand Down
5 changes: 4 additions & 1 deletion chain/gen/genesis/f00_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 87c7fd4

Please sign in to comment.