Skip to content

Commit f907354

Browse files
committed
Refactor LookupID* APIs in StateManager and StateTree
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)
1 parent d23ea76 commit f907354

File tree

12 files changed

+43
-26
lines changed

12 files changed

+43
-26
lines changed

chain/consensus/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func checkBlockMessages(ctx context.Context, sm *stmgr.StateManager, cs *store.C
220220
// the sender exists and is an account actor, and the nonces make sense
221221
var sender address.Address
222222
if nv >= network.Version13 {
223-
sender, err = st.LookupID(m.From)
223+
sender, err = st.LookupIDAddress(m.From)
224224
if err != nil {
225225
return xerrors.Errorf("failed to lookup sender %s: %w", m.From, err)
226226
}

chain/state/statetree.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func NewStateTree(cst cbor.IpldStore, ver types.StateTreeVersion) (*StateTree, e
230230
Store: cst,
231231
snaps: newStateSnaps(),
232232
}
233-
s.lookupIDFun = s.lookupIDinternal
233+
s.lookupIDFun = s.lookupInternalIDAddress
234234
return s, nil
235235
}
236236

@@ -302,13 +302,13 @@ func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
302302
Store: cst,
303303
snaps: newStateSnaps(),
304304
}
305-
s.lookupIDFun = s.lookupIDinternal
305+
s.lookupIDFun = s.lookupInternalIDAddress
306306

307307
return s, nil
308308
}
309309

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

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

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

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

414-
iaddr, err := st.LookupID(addr)
414+
iaddr, err := st.LookupIDAddress(addr)
415415
if err != nil {
416416
if xerrors.Is(err, types.ErrActorNotFound) {
417417
return xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)

chain/stmgr/actors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ func (sm *StateManager) MarketBalance(ctx context.Context, addr address.Address,
542542
return api.MarketBalance{}, err
543543
}
544544

545-
addr, err = sm.LookupID(ctx, addr, ts)
545+
addr, err = sm.LookupIDAddress(ctx, addr, ts)
546546
if err != nil {
547547
return api.MarketBalance{}, err
548548
}

chain/stmgr/rpc/rpcstatemanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (s *RPCStateManager) LoadActorTsk(ctx context.Context, addr address.Address
4444
return s.gapi.StateGetActor(ctx, addr, tsk)
4545
}
4646

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

chain/stmgr/searchwait.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (sm *StateManager) searchBackForMsg(ctx context.Context, from *types.TipSet
243243
return nil, nil, cid.Undef, xerrors.Errorf("failed to load initital tipset")
244244
}
245245

246-
mFromId, err := sm.LookupID(ctx, m.VMMessage().From, from)
246+
mFromId, err := sm.LookupIDAddress(ctx, m.VMMessage().From, from)
247247
if err != nil {
248248
return nil, nil, cid.Undef, xerrors.Errorf("looking up From id address: %w", err)
249249
}

chain/stmgr/stmgr.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type StateManagerAPI interface {
4949
Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error)
5050
GetPaychState(ctx context.Context, addr address.Address, ts *types.TipSet) (*types.Actor, paych.State, error)
5151
LoadActorTsk(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error)
52-
LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
52+
LookupIDAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
5353
ResolveToDeterministicAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
5454
}
5555

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

403-
func (sm *StateManager) LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
403+
func (sm *StateManager) LookupIDAddress(_ context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
404+
// Check for the fast route first to avoid unnecessary CBOR store instantiation and state tree load.
405+
if addr.Protocol() == address.ID {
406+
return addr, nil
407+
}
408+
404409
cst := cbor.NewCborStore(sm.cs.StateBlockstore())
405410
state, err := state.LoadStateTree(cst, sm.parentState(ts))
406411
if err != nil {
407412
return address.Undef, xerrors.Errorf("load state tree: %w", err)
408413
}
409-
return state.LookupID(addr)
414+
return state.LookupIDAddress(addr)
415+
}
416+
417+
func (sm *StateManager) LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (abi.ActorID, error) {
418+
idAddr, err := sm.LookupIDAddress(ctx, addr, ts)
419+
if err != nil {
420+
return 0, xerrors.Errorf("state manager lookup id: %w", err)
421+
}
422+
id, err := address.IDFromAddress(idAddr)
423+
if err != nil {
424+
return 0, xerrors.Errorf("resolve actor id: id from addr: %w", err)
425+
}
426+
return abi.ActorID(id), nil
410427
}
411428

412429
func (sm *StateManager) LookupRobustAddress(ctx context.Context, idAddr address.Address, ts *types.TipSet) (address.Address, error) {

chain/store/messages.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (cs *ChainStore) BlockMsgsForTipset(ctx context.Context, ts *types.TipSet)
119119
var sender address.Address
120120
if ts.Height() >= build.UpgradeHyperdriveHeight {
121121
if useIds {
122-
sender, err = st.LookupID(m.From)
122+
sender, err = st.LookupIDAddress(m.From)
123123
if err != nil {
124124
return false, xerrors.Errorf("failed to resolve sender: %w", err)
125125
}
@@ -131,14 +131,14 @@ func (cs *ChainStore) BlockMsgsForTipset(ctx context.Context, ts *types.TipSet)
131131
// uh-oh, we actually have an ID-sender!
132132
useIds = true
133133
for robust, nonce := range applied {
134-
resolved, err := st.LookupID(robust)
134+
resolved, err := st.LookupIDAddress(robust)
135135
if err != nil {
136136
return false, xerrors.Errorf("failed to resolve sender: %w", err)
137137
}
138138
applied[resolved] = nonce
139139
}
140140

141-
sender, err = st.LookupID(m.From)
141+
sender, err = st.LookupIDAddress(m.From)
142142
if err != nil {
143143
return false, xerrors.Errorf("failed to resolve sender: %w", err)
144144
}

chain/vm/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount {
111111
}
112112

113113
func (rt *Runtime) ResolveAddress(addr address.Address) (ret address.Address, ok bool) {
114-
r, err := rt.state.LookupID(addr)
114+
r, err := rt.state.LookupIDAddress(addr)
115115
if err != nil {
116116
if xerrors.Is(err, types.ErrActorNotFound) {
117117
return address.Undef, false

chain/vm/vm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ func (vm *LegacyVM) transfer(from, to address.Address, amt types.BigInt, network
902902
return aerrors.Newf(exitcode.SysErrForbidden, "attempted to transfer negative value: %s", amt)
903903
}
904904

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

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

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

943-
toID, err = vm.cstate.LookupID(to)
943+
toID, err = vm.cstate.LookupIDAddress(to)
944944
if err != nil {
945945
return aerrors.Fatalf("transfer failed when resolving receiver address: %s", err)
946946
}

itests/migration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func TestMigrationNV18(t *testing.T) {
584584
// check the EthZeroAddress
585585
ethZeroAddr, err := (ethtypes.EthAddress{}).ToFilecoinAddress()
586586
require.NoError(t, err)
587-
ethZeroAddrID, err := newStateTree.LookupID(ethZeroAddr)
587+
ethZeroAddrID, err := newStateTree.LookupIDAddress(ethZeroAddr)
588588
require.NoError(t, err)
589589
ethZeroActor, err := newStateTree.GetActor(ethZeroAddrID)
590590
require.NoError(t, err)

node/impl/full/eth_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func lookupEthAddress(addr address.Address, st *state.StateTree) (ethtypes.EthAd
392392
}
393393

394394
// Otherwise, resolve the ID addr.
395-
idAddr, err := st.LookupID(addr)
395+
idAddr, err := st.LookupIDAddress(addr)
396396
if err != nil {
397397
return ethtypes.EthAddress{}, err
398398
}

node/impl/full/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func (m *StateModule) StateLookupID(ctx context.Context, addr address.Address, t
486486
return address.Undef, xerrors.Errorf("loading tipset %s: %w", tsk, err)
487487
}
488488

489-
ret, err := m.StateManager.LookupID(ctx, addr, ts)
489+
ret, err := m.StateManager.LookupIDAddress(ctx, addr, ts)
490490
if err != nil && xerrors.Is(err, types.ErrActorNotFound) {
491491
return address.Undef, &api.ErrActorNotFound{}
492492
}

0 commit comments

Comments
 (0)