use actors state-tree abstraction

Well, not fully, but this is enough to ensure we use the correct bitwidth and such.
This commit is contained in:
Steven Allen 2021-01-18 16:28:18 -08:00
parent 9d2c430138
commit 1a790f5ee3

View File

@ -21,11 +21,9 @@ import (
"github.com/filecoin-project/lotus/chain/actors/adt" "github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt" states0 "github.com/filecoin-project/specs-actors/actors/states"
adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt" states2 "github.com/filecoin-project/specs-actors/v2/actors/states"
adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" states3 "github.com/filecoin-project/specs-actors/v3/actors/states"
builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"
) )
var log = logging.Logger("statetree") var log = logging.Logger("statetree")
@ -165,15 +163,27 @@ func NewStateTree(cst cbor.IpldStore, ver types.StateTreeVersion) (*StateTree, e
return nil, xerrors.Errorf("unsupported state tree version: %d", ver) return nil, xerrors.Errorf("unsupported state tree version: %d", ver)
} }
var hamt adt.Map
store := adt.WrapStore(context.TODO(), cst) store := adt.WrapStore(context.TODO(), cst)
var hamt adt.Map
switch ver { switch ver {
case types.StateTreeVersion0: case types.StateTreeVersion0:
hamt = adt0.MakeEmptyMap(store) tree, err := states0.NewTree(store)
if err != nil {
return nil, xerrors.Errorf("failed to create state tree: %w", err)
}
hamt = tree.Map
case types.StateTreeVersion1: case types.StateTreeVersion1:
hamt = adt2.MakeEmptyMap(store) tree, err := states2.NewTree(store)
if err != nil {
return nil, xerrors.Errorf("failed to create state tree: %w", err)
}
hamt = tree.Map
case types.StateTreeVersion2: case types.StateTreeVersion2:
hamt = adt3.MakeEmptyMap(store, builtin3.DefaultHamtBitwidth) tree, err := states3.NewTree(store)
if err != nil {
return nil, xerrors.Errorf("failed to create state tree: %w", err)
}
hamt = tree.Map
default: default:
return nil, xerrors.Errorf("unsupported state tree version: %d", ver) return nil, xerrors.Errorf("unsupported state tree version: %d", ver)
} }
@ -200,27 +210,30 @@ func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
store := adt.WrapStore(context.TODO(), cst) store := adt.WrapStore(context.TODO(), cst)
var ( var hamt adt.Map
hamt adt.Map
err error
)
switch root.Version { switch root.Version {
case types.StateTreeVersion0: case types.StateTreeVersion0:
hamt, err = adt0.AsMap(store, root.Actors) tree, err := states0.LoadTree(store, root.Actors)
if err != nil {
return nil, xerrors.Errorf("failed to load state tree: %w", err)
}
hamt = tree.Map
case types.StateTreeVersion1: case types.StateTreeVersion1:
hamt, err = adt2.AsMap(store, root.Actors) tree, err := states2.LoadTree(store, root.Actors)
if err != nil {
return nil, xerrors.Errorf("failed to load state tree: %w", err)
}
hamt = tree.Map
case types.StateTreeVersion2: case types.StateTreeVersion2:
hamt, err = adt3.AsMap(store, root.Actors, builtin3.DefaultHamtBitwidth) tree, err := states3.LoadTree(store, root.Actors)
if err != nil {
return nil, xerrors.Errorf("failed to load state tree: %w", err)
}
hamt = tree.Map
default: default:
return nil, xerrors.Errorf("unsupported state tree version: %d", root.Version) return nil, xerrors.Errorf("unsupported state tree version: %d", root.Version)
} }
if err != nil {
log.Errorf("loading hamt node %s failed: %s", c, err)
return nil, err
}
s := &StateTree{ s := &StateTree{
root: hamt, root: hamt,
info: root.Info, info: root.Info,