From 8747c6083e6ecc1c60620c8dcf67bb07a304d8d4 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 18 Sep 2020 14:20:53 -0700 Subject: [PATCH] abstract over account actor --- chain/actors/builtin/account/account.go | 31 +++++++++++++++++++++++++ chain/actors/builtin/account/v0.go | 16 +++++++++++++ chain/vm/vm.go | 13 ++++------- 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 chain/actors/builtin/account/account.go create mode 100644 chain/actors/builtin/account/v0.go diff --git a/chain/actors/builtin/account/account.go b/chain/actors/builtin/account/account.go new file mode 100644 index 000000000..d4cbe4177 --- /dev/null +++ b/chain/actors/builtin/account/account.go @@ -0,0 +1,31 @@ +package account + +import ( + "golang.org/x/xerrors" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-state-types/cbor" + v0builtin "github.com/filecoin-project/specs-actors/actors/builtin" + + "github.com/filecoin-project/lotus/chain/actors/adt" + "github.com/filecoin-project/lotus/chain/types" +) + +func Load(store adt.Store, act *types.Actor) (State, error) { + switch act.Code { + case v0builtin.AccountActorCodeID: + out := v0State{store: store} + err := store.Get(store.Context(), act.Head, &out) + if err != nil { + return nil, err + } + return &out, nil + } + return nil, xerrors.Errorf("unknown actor code %s", act.Code) +} + +type State interface { + cbor.Marshaler + + PubkeyAddress() (address.Address, error) +} diff --git a/chain/actors/builtin/account/v0.go b/chain/actors/builtin/account/v0.go new file mode 100644 index 000000000..6001dd7bf --- /dev/null +++ b/chain/actors/builtin/account/v0.go @@ -0,0 +1,16 @@ +package account + +import ( + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/lotus/chain/actors/adt" + "github.com/filecoin-project/specs-actors/actors/builtin/account" +) + +type v0State struct { + account.State + store adt.Store +} + +func (s *v0State) PubkeyAddress() (address.Address, error) { + return s.Address, nil +} diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 542dfee69..25c937ca3 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -23,10 +23,11 @@ import ( "github.com/filecoin-project/go-state-types/exitcode" "github.com/filecoin-project/go-state-types/network" "github.com/filecoin-project/specs-actors/actors/builtin" - "github.com/filecoin-project/specs-actors/actors/builtin/account" "github.com/filecoin-project/lotus/build" + "github.com/filecoin-project/lotus/chain/actors/adt" "github.com/filecoin-project/lotus/chain/actors/aerrors" + "github.com/filecoin-project/lotus/chain/actors/builtin/account" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/lib/blockstore" @@ -49,16 +50,12 @@ func ResolveToKeyAddr(state types.StateTree, cst cbor.IpldStore, addr address.Ad return address.Undef, xerrors.Errorf("failed to find actor: %s", addr) } - if act.Code != builtin.AccountActorCodeID { - return address.Undef, xerrors.Errorf("address %s was not for an account actor", addr) - } - - var aast account.State - if err := cst.Get(context.TODO(), act.Head, &aast); err != nil { + aast, err := account.Load(adt.WrapStore(context.TODO(), cst), act) + if err != nil { return address.Undef, xerrors.Errorf("failed to get account actor state for %s: %w", addr, err) } - return aast.Address, nil + return aast.PubkeyAddress() } var _ cbor.IpldBlockstore = (*gasChargingBlocks)(nil)