Skip to content

Commit

Permalink
Refactor LookupID* APIs in StateManager and StateTree
Browse files Browse the repository at this point in the history
The naming of `LookupID` can cause confusion when resolving actor IDs vs
 ID addresses. To avoid this:

* Refactor `StateTree` `LookupID` to `LookupIDAddress`, because it
returns ID address.
* Refactor `StateManager` `LookupID` to
`LookupIDAddress` because it also returns ID address via a chain call to
`StateTree`.
* Introduce a new API `StateManager` dedicated to resolving address to
actor ID, called `LookupID` which returns `abi.ActorID`.

For context, see:
 * #11723 (comment)
  • Loading branch information
masih committed Apr 24, 2024
1 parent d23ea76 commit f907354
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion chain/consensus/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func checkBlockMessages(ctx context.Context, sm *stmgr.StateManager, cs *store.C
// the sender exists and is an account actor, and the nonces make sense
var sender address.Address
if nv >= network.Version13 {
sender, err = st.LookupID(m.From)
sender, err = st.LookupIDAddress(m.From)
if err != nil {
return xerrors.Errorf("failed to lookup sender %s: %w", m.From, err)
}
Expand Down
16 changes: 8 additions & 8 deletions chain/state/statetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func NewStateTree(cst cbor.IpldStore, ver types.StateTreeVersion) (*StateTree, e
Store: cst,
snaps: newStateSnaps(),
}
s.lookupIDFun = s.lookupIDinternal
s.lookupIDFun = s.lookupInternalIDAddress
return s, nil
}

Expand Down Expand Up @@ -302,13 +302,13 @@ func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
Store: cst,
snaps: newStateSnaps(),
}
s.lookupIDFun = s.lookupIDinternal
s.lookupIDFun = s.lookupInternalIDAddress

return s, nil
}

func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
iaddr, err := st.LookupID(addr)
iaddr, err := st.LookupIDAddress(addr)
if err != nil {
return xerrors.Errorf("ID lookup failed: %w", err)
}
Expand All @@ -318,7 +318,7 @@ func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
return nil
}

func (st *StateTree) lookupIDinternal(addr address.Address) (address.Address, error) {
func (st *StateTree) lookupInternalIDAddress(addr address.Address) (address.Address, error) {
act, err := st.GetActor(init_.Address)
if err != nil {
return address.Undef, xerrors.Errorf("getting init actor: %w", err)
Expand All @@ -339,8 +339,8 @@ func (st *StateTree) lookupIDinternal(addr address.Address) (address.Address, er
return a, err
}

// LookupID gets the ID address of this actor's `addr` stored in the `InitActor`.
func (st *StateTree) LookupID(addr address.Address) (address.Address, error) {
// LookupIDAddress gets the ID address of this actor's `addr` stored in the `InitActor`.
func (st *StateTree) LookupIDAddress(addr address.Address) (address.Address, error) {
if addr.Protocol() == address.ID {
return addr, nil
}
Expand All @@ -366,7 +366,7 @@ func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) {
}

// Transform `addr` to its ID format.
iaddr, err := st.LookupID(addr)
iaddr, err := st.LookupIDAddress(addr)
if err != nil {
if xerrors.Is(err, types.ErrActorNotFound) {
return nil, xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)
Expand Down Expand Up @@ -411,7 +411,7 @@ func (st *StateTree) DeleteActor(addr address.Address) error {
return xerrors.Errorf("DeleteActor called on undefined address")
}

iaddr, err := st.LookupID(addr)
iaddr, err := st.LookupIDAddress(addr)
if err != nil {
if xerrors.Is(err, types.ErrActorNotFound) {
return xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)
Expand Down
2 changes: 1 addition & 1 deletion chain/stmgr/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func (sm *StateManager) MarketBalance(ctx context.Context, addr address.Address,
return api.MarketBalance{}, err
}

addr, err = sm.LookupID(ctx, addr, ts)
addr, err = sm.LookupIDAddress(ctx, addr, ts)
if err != nil {
return api.MarketBalance{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion chain/stmgr/rpc/rpcstatemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *RPCStateManager) LoadActorTsk(ctx context.Context, addr address.Address
return s.gapi.StateGetActor(ctx, addr, tsk)
}

func (s *RPCStateManager) LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
func (s *RPCStateManager) LookupIDAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
return s.gapi.StateLookupID(ctx, addr, ts.Key())
}

Expand Down
2 changes: 1 addition & 1 deletion chain/stmgr/searchwait.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (sm *StateManager) searchBackForMsg(ctx context.Context, from *types.TipSet
return nil, nil, cid.Undef, xerrors.Errorf("failed to load initital tipset")
}

mFromId, err := sm.LookupID(ctx, m.VMMessage().From, from)
mFromId, err := sm.LookupIDAddress(ctx, m.VMMessage().From, from)
if err != nil {
return nil, nil, cid.Undef, xerrors.Errorf("looking up From id address: %w", err)
}
Expand Down
23 changes: 20 additions & 3 deletions chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type StateManagerAPI interface {
Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error)
GetPaychState(ctx context.Context, addr address.Address, ts *types.TipSet) (*types.Actor, paych.State, error)
LoadActorTsk(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error)
LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
LookupIDAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
ResolveToDeterministicAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
}

Expand Down Expand Up @@ -400,13 +400,30 @@ func (sm *StateManager) GetBlsPublicKey(ctx context.Context, addr address.Addres
return kaddr.Payload(), nil
}

func (sm *StateManager) LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
func (sm *StateManager) LookupIDAddress(_ context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
// Check for the fast route first to avoid unnecessary CBOR store instantiation and state tree load.
if addr.Protocol() == address.ID {
return addr, nil
}

cst := cbor.NewCborStore(sm.cs.StateBlockstore())
state, err := state.LoadStateTree(cst, sm.parentState(ts))
if err != nil {
return address.Undef, xerrors.Errorf("load state tree: %w", err)
}
return state.LookupID(addr)
return state.LookupIDAddress(addr)
}

func (sm *StateManager) LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (abi.ActorID, error) {
idAddr, err := sm.LookupIDAddress(ctx, addr, ts)
if err != nil {
return 0, xerrors.Errorf("state manager lookup id: %w", err)
}
id, err := address.IDFromAddress(idAddr)
if err != nil {
return 0, xerrors.Errorf("resolve actor id: id from addr: %w", err)
}
return abi.ActorID(id), nil
}

func (sm *StateManager) LookupRobustAddress(ctx context.Context, idAddr address.Address, ts *types.TipSet) (address.Address, error) {
Expand Down
6 changes: 3 additions & 3 deletions chain/store/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (cs *ChainStore) BlockMsgsForTipset(ctx context.Context, ts *types.TipSet)
var sender address.Address
if ts.Height() >= build.UpgradeHyperdriveHeight {
if useIds {
sender, err = st.LookupID(m.From)
sender, err = st.LookupIDAddress(m.From)
if err != nil {
return false, xerrors.Errorf("failed to resolve sender: %w", err)
}
Expand All @@ -131,14 +131,14 @@ func (cs *ChainStore) BlockMsgsForTipset(ctx context.Context, ts *types.TipSet)
// uh-oh, we actually have an ID-sender!
useIds = true
for robust, nonce := range applied {
resolved, err := st.LookupID(robust)
resolved, err := st.LookupIDAddress(robust)
if err != nil {
return false, xerrors.Errorf("failed to resolve sender: %w", err)
}
applied[resolved] = nonce
}

sender, err = st.LookupID(m.From)
sender, err = st.LookupIDAddress(m.From)
if err != nil {
return false, xerrors.Errorf("failed to resolve sender: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion chain/vm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount {
}

func (rt *Runtime) ResolveAddress(addr address.Address) (ret address.Address, ok bool) {
r, err := rt.state.LookupID(addr)
r, err := rt.state.LookupIDAddress(addr)
if err != nil {
if xerrors.Is(err, types.ErrActorNotFound) {
return address.Undef, false
Expand Down
8 changes: 4 additions & 4 deletions chain/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ func (vm *LegacyVM) transfer(from, to address.Address, amt types.BigInt, network
return aerrors.Newf(exitcode.SysErrForbidden, "attempted to transfer negative value: %s", amt)
}

fromID, err = vm.cstate.LookupID(from)
fromID, err = vm.cstate.LookupIDAddress(from)
if err != nil {
return aerrors.Fatalf("transfer failed when resolving sender address: %s", err)
}
Expand All @@ -921,7 +921,7 @@ func (vm *LegacyVM) transfer(from, to address.Address, amt types.BigInt, network
return nil
}

toID, err = vm.cstate.LookupID(to)
toID, err = vm.cstate.LookupIDAddress(to)
if err != nil {
return aerrors.Fatalf("transfer failed when resolving receiver address: %s", err)
}
Expand All @@ -935,12 +935,12 @@ func (vm *LegacyVM) transfer(from, to address.Address, amt types.BigInt, network
return nil
}

fromID, err = vm.cstate.LookupID(from)
fromID, err = vm.cstate.LookupIDAddress(from)
if err != nil {
return aerrors.Fatalf("transfer failed when resolving sender address: %s", err)
}

toID, err = vm.cstate.LookupID(to)
toID, err = vm.cstate.LookupIDAddress(to)
if err != nil {
return aerrors.Fatalf("transfer failed when resolving receiver address: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion itests/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func TestMigrationNV18(t *testing.T) {
// check the EthZeroAddress
ethZeroAddr, err := (ethtypes.EthAddress{}).ToFilecoinAddress()
require.NoError(t, err)
ethZeroAddrID, err := newStateTree.LookupID(ethZeroAddr)
ethZeroAddrID, err := newStateTree.LookupIDAddress(ethZeroAddr)
require.NoError(t, err)
ethZeroActor, err := newStateTree.GetActor(ethZeroAddrID)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion node/impl/full/eth_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func lookupEthAddress(addr address.Address, st *state.StateTree) (ethtypes.EthAd
}

// Otherwise, resolve the ID addr.
idAddr, err := st.LookupID(addr)
idAddr, err := st.LookupIDAddress(addr)
if err != nil {
return ethtypes.EthAddress{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion node/impl/full/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func (m *StateModule) StateLookupID(ctx context.Context, addr address.Address, t
return address.Undef, xerrors.Errorf("loading tipset %s: %w", tsk, err)
}

ret, err := m.StateManager.LookupID(ctx, addr, ts)
ret, err := m.StateManager.LookupIDAddress(ctx, addr, ts)
if err != nil && xerrors.Is(err, types.ErrActorNotFound) {
return address.Undef, &api.ErrActorNotFound{}
}
Expand Down

0 comments on commit f907354

Please sign in to comment.