move version to actors, from builtin

Otherwise, we're going to end up with an import cycle between the adt and this
version.
This commit is contained in:
Steven Allen 2020-09-23 17:01:40 -07:00
parent edb31e606a
commit 87351fa35c
10 changed files with 70 additions and 67 deletions

View File

@ -7,7 +7,8 @@ import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors"
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
adt1 "github.com/filecoin-project/specs-actors/v2/actors/util/adt"
@ -23,21 +24,21 @@ type Map interface {
ForEach(v cbor.Unmarshaler, fn func(key string) error) error
}
func AsMap(store Store, root cid.Cid, version builtin.Version) (Map, error) {
func AsMap(store Store, root cid.Cid, version actors.Version) (Map, error) {
switch version {
case builtin.Version0:
case actors.Version0:
return adt0.AsMap(store, root)
case builtin.Version1:
case actors.Version1:
return adt1.AsMap(store, root)
}
return nil, xerrors.Errorf("unknown network version: %d", version)
}
func NewMap(store Store, version builtin.Version) (Map, error) {
func NewMap(store Store, version actors.Version) (Map, error) {
switch version {
case builtin.Version0:
case actors.Version0:
return adt0.MakeEmptyMap(store), nil
case builtin.Version1:
case actors.Version1:
return adt1.MakeEmptyMap(store), nil
}
return nil, xerrors.Errorf("unknown network version: %d", version)
@ -55,20 +56,20 @@ type Array interface {
}
func AsArray(store Store, root cid.Cid, version network.Version) (Array, error) {
switch builtin.VersionForNetwork(version) {
case builtin.Version0:
switch actors.VersionForNetwork(version) {
case actors.Version0:
return adt0.AsArray(store, root)
case builtin.Version1:
case actors.Version1:
return adt1.AsArray(store, root)
}
return nil, xerrors.Errorf("unknown network version: %d", version)
}
func NewArray(store Store, version builtin.Version) (Array, error) {
func NewArray(store Store, version actors.Version) (Array, error) {
switch version {
case builtin.Version0:
case actors.Version0:
return adt0.MakeEmptyArray(store), nil
case builtin.Version1:
case actors.Version1:
return adt1.MakeEmptyArray(store), nil
}
return nil, xerrors.Errorf("unknown network version: %d", version)

View File

@ -1,35 +1,14 @@
package builtin
import (
"fmt"
"github.com/filecoin-project/go-state-types/abi"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
proof0 "github.com/filecoin-project/specs-actors/actors/runtime/proof"
smoothing0 "github.com/filecoin-project/specs-actors/actors/util/smoothing"
smoothing1 "github.com/filecoin-project/specs-actors/v2/actors/util/smoothing"
"github.com/filecoin-project/go-state-types/network"
)
type Version int
const (
Version0 = iota
Version1
)
// Converts a network version into a specs-actors version.
func VersionForNetwork(version network.Version) Version {
switch version {
case network.Version0, network.Version1, network.Version2:
return Version0
default:
panic(fmt.Sprintf("unsupported network version %d", version))
}
}
// TODO: Why does actors have 2 different versions of this?
type SectorInfo = proof0.SectorInfo
type PoStProof = proof0.PoStProof

View File

@ -4,13 +4,13 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
func getDataCap(store adt.Store, ver builtin.Version, root cid.Cid, addr address.Address) (bool, abi.StoragePower, error) {
func getDataCap(store adt.Store, ver actors.Version, root cid.Cid, addr address.Address) (bool, abi.StoragePower, error) {
if addr.Protocol() != address.ID {
return false, big.Zero(), xerrors.Errorf("can only look up ID addresses")
}
@ -30,7 +30,7 @@ func getDataCap(store adt.Store, ver builtin.Version, root cid.Cid, addr address
return true, dcap, nil
}
func forEachCap(store adt.Store, ver builtin.Version, root cid.Cid, cb func(addr address.Address, dcap abi.StoragePower) error) error {
func forEachCap(store adt.Store, ver actors.Version, root cid.Cid, cb func(addr address.Address, dcap abi.StoragePower) error) error {
vh, err := adt.AsMap(store, root, ver)
if err != nil {
return xerrors.Errorf("loading verified clients: %w", err)

View File

@ -4,8 +4,8 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
verifreg0 "github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
)
@ -18,17 +18,17 @@ type state0 struct {
}
func (s *state0) VerifiedClientDataCap(addr address.Address) (bool, abi.StoragePower, error) {
return getDataCap(s.store, builtin.Version0, s.State.VerifiedClients, addr)
return getDataCap(s.store, actors.Version0, s.State.VerifiedClients, addr)
}
func (s *state0) VerifierDataCap(addr address.Address) (bool, abi.StoragePower, error) {
return getDataCap(s.store, builtin.Version0, s.State.Verifiers, addr)
return getDataCap(s.store, actors.Version0, s.State.Verifiers, addr)
}
func (s *state0) ForEachVerifier(cb func(addr address.Address, dcap abi.StoragePower) error) error {
return forEachCap(s.store, builtin.Version0, s.State.Verifiers, cb)
return forEachCap(s.store, actors.Version0, s.State.Verifiers, cb)
}
func (s *state0) ForEachClient(cb func(addr address.Address, dcap abi.StoragePower) error) error {
return forEachCap(s.store, builtin.Version0, s.State.VerifiedClients, cb)
return forEachCap(s.store, actors.Version0, s.State.VerifiedClients, cb)
}

View File

@ -4,8 +4,8 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
verifreg1 "github.com/filecoin-project/specs-actors/v2/actors/builtin/verifreg"
)
@ -18,17 +18,17 @@ type state1 struct {
}
func (s *state1) VerifiedClientDataCap(addr address.Address) (bool, abi.StoragePower, error) {
return getDataCap(s.store, builtin.Version1, s.State.VerifiedClients, addr)
return getDataCap(s.store, actors.Version1, s.State.VerifiedClients, addr)
}
func (s *state1) VerifierDataCap(addr address.Address) (bool, abi.StoragePower, error) {
return getDataCap(s.store, builtin.Version1, s.State.Verifiers, addr)
return getDataCap(s.store, actors.Version1, s.State.Verifiers, addr)
}
func (s *state1) ForEachVerifier(cb func(addr address.Address, dcap abi.StoragePower) error) error {
return forEachCap(s.store, builtin.Version1, s.State.Verifiers, cb)
return forEachCap(s.store, actors.Version1, s.State.Verifiers, cb)
}
func (s *state1) ForEachClient(cb func(addr address.Address, dcap abi.StoragePower) error) error {
return forEachCap(s.store, builtin.Version1, s.State.VerifiedClients, cb)
return forEachCap(s.store, actors.Version1, s.State.VerifiedClients, cb)
}

24
chain/actors/version.go Normal file
View File

@ -0,0 +1,24 @@
package actors
import (
"fmt"
"github.com/filecoin-project/go-state-types/network"
)
type Version int
const (
Version0 = iota
Version1
)
// Converts a network version into an actors adt version.
func VersionForNetwork(version network.Version) Version {
switch version {
case network.Version0, network.Version1, network.Version2:
return Version0
default:
panic(fmt.Sprintf("unsupported network version %d", version))
}
}

View File

@ -24,7 +24,7 @@ import (
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
@ -115,7 +115,7 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge
return nil, nil, xerrors.Errorf("putting empty object: %w", err)
}
state, err := state.NewStateTree(cst, builtin.Version0)
state, err := state.NewStateTree(cst, actors.Version0)
if err != nil {
return nil, nil, xerrors.Errorf("making new state tree: %w", err)
}

View File

@ -13,7 +13,7 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors"
init_ "github.com/filecoin-project/lotus/chain/actors/builtin/init"
cbg "github.com/whyrusleeping/cbor-gen"
@ -26,7 +26,7 @@ var log = logging.Logger("statetree")
// StateTree stores actors state by their ID.
type StateTree struct {
root adt.Map
version builtin.Version // TODO
version actors.Version // TODO
info cid.Cid
Store cbor.IpldStore
@ -120,10 +120,10 @@ func (ss *stateSnaps) deleteActor(addr address.Address) {
ss.layers[len(ss.layers)-1].actors[addr] = streeOp{Delete: true}
}
func NewStateTree(cst cbor.IpldStore, version builtin.Version) (*StateTree, error) {
func NewStateTree(cst cbor.IpldStore, version actors.Version) (*StateTree, error) {
var info cid.Cid
switch version {
case builtin.Version0:
case actors.Version0:
// info is undefined
default:
return nil, xerrors.Errorf("unsupported state tree version: %d", version)
@ -148,18 +148,18 @@ func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
if err := cst.Get(context.TODO(), c, &root); err != nil {
// We failed to decode as the new version, must be an old version.
root.Actors = c
root.Version = builtin.Version0
root.Version = actors.Version0
}
// If that fails, load as an old-style state-tree (direct hampt, version 0.
nd, err := adt.AsMap(adt.WrapStore(context.TODO(), cst), root.Actors, builtin.Version(root.Version))
nd, err := adt.AsMap(adt.WrapStore(context.TODO(), cst), root.Actors, actors.Version(root.Version))
if err != nil {
log.Errorf("loading hamt node %s failed: %s", c, err)
return nil, err
}
switch root.Version {
case builtin.Version0:
case actors.Version0:
// supported
default:
return nil, xerrors.Errorf("unsupported state tree version: %d", root.Version)
@ -168,7 +168,7 @@ func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
return &StateTree{
root: nd,
info: root.Info,
version: builtin.Version(root.Version),
version: actors.Version(root.Version),
Store: cst,
snaps: newStateSnaps(),
}, nil
@ -305,7 +305,7 @@ func (st *StateTree) Flush(ctx context.Context) (cid.Cid, error) {
return cid.Undef, xerrors.Errorf("failed to flush state-tree hamt: %w", err)
}
// If we're version 0, return a raw tree.
if st.version == builtin.Version0 {
if st.version == actors.Version0 {
return root, nil
}
// Otherwise, return a versioned tree.

View File

@ -12,13 +12,13 @@ import (
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/lotus/build"
builtin2 "github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types"
)
func BenchmarkStateTreeSet(b *testing.B) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, builtin2.VersionForNetwork(build.NewestNetworkVersion))
st, err := NewStateTree(cst, actors.VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
b.Fatal(err)
}
@ -45,7 +45,7 @@ func BenchmarkStateTreeSet(b *testing.B) {
func BenchmarkStateTreeSetFlush(b *testing.B) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, builtin2.VersionForNetwork(build.NewestNetworkVersion))
st, err := NewStateTree(cst, actors.VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
b.Fatal(err)
}
@ -75,7 +75,7 @@ func BenchmarkStateTreeSetFlush(b *testing.B) {
func BenchmarkStateTree10kGetActor(b *testing.B) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, builtin2.VersionForNetwork(build.NewestNetworkVersion))
st, err := NewStateTree(cst, actors.VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
b.Fatal(err)
}
@ -117,7 +117,7 @@ func BenchmarkStateTree10kGetActor(b *testing.B) {
func TestSetCache(t *testing.T) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, builtin2.VersionForNetwork(build.NewestNetworkVersion))
st, err := NewStateTree(cst, actors.VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
t.Fatal(err)
}
@ -154,7 +154,7 @@ func TestSetCache(t *testing.T) {
func TestSnapshots(t *testing.T) {
ctx := context.Background()
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, builtin2.VersionForNetwork(build.NewestNetworkVersion))
st, err := NewStateTree(cst, actors.VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
t.Fatal(err)
}
@ -237,7 +237,7 @@ func assertNotHas(t *testing.T, st *StateTree, addr address.Address) {
func TestStateTreeConsistency(t *testing.T) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, builtin2.VersionForNetwork(build.NewestNetworkVersion))
st, err := NewStateTree(cst, actors.VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
t.Fatal(err)
}

View File

@ -24,7 +24,6 @@ import (
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
@ -291,7 +290,7 @@ func (sm *StateManager) ApplyBlocks(ctx context.Context, parentEpoch abi.ChainEp
}
// XXX: Is the height correct? Or should it be epoch-1?
rectarr, err := adt.NewArray(sm.cs.Store(ctx), builtin.VersionForNetwork(sm.GetNtwkVersion(ctx, epoch)))
rectarr, err := adt.NewArray(sm.cs.Store(ctx), actors.VersionForNetwork(sm.GetNtwkVersion(ctx, epoch)))
if err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("failed to create receipts amt: %w", err)
}