Skip to content

Commit d23f21c

Browse files
birdychangfrrist
andauthored
fix: remove actor code mapping (#1087)
* fix: remove actor code mapping * Remov unused vars. * refactor: polish GetActorCodeFunc (#1089) * refactor: polish GetActorCodeFunc - remove address resolution, its handled by lotus internally via state.GetActor Co-authored-by: Frrist <[email protected]>
1 parent a2eb052 commit d23f21c

File tree

6 files changed

+27
-58
lines changed

6 files changed

+27
-58
lines changed

lens/lily/impl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,13 @@ func (m *LilyNodeAPI) GetMessageExecutionsForTipSet(ctx context.Context, next *t
514514

515515
out := make([]*lens.MessageExecution, len(executions))
516516
for idx, execution := range executions {
517-
toCode, found := getActorCode(execution.Msg.To)
517+
toCode, found := getActorCode(ctx, execution.Msg.To)
518518
// if the message failed to execute due to lack of gas then the TO actor may never have been created.
519519
if !found {
520520
log.Warnw("failed to find TO actor", "height", next.Height().String(), "message", execution.Msg.Cid().String(), "actor", execution.Msg.To.String())
521521
}
522522
// if the message sender cannot be found this is an unexpected error
523-
fromCode, found := getActorCode(execution.Msg.From)
523+
fromCode, found := getActorCode(ctx, execution.Msg.From)
524524
if !found {
525525
return nil, fmt.Errorf("failed to find from actor %s height %d message %s", execution.Msg.From, execution.TipSet.Height(), execution.Msg.Cid())
526526
}

lens/util/repo.go

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/filecoin-project/lotus/chain/vm"
2626
"github.com/filecoin-project/specs-actors/actors/util/adt"
2727

28-
builtininit "github.com/filecoin-project/lily/chain/actors/builtin/init"
2928
"github.com/filecoin-project/lily/lens"
3029
)
3130

@@ -170,68 +169,38 @@ func ActorNameAndFamilyFromCode(c cid.Cid) (name string, family string, err erro
170169
return
171170
}
172171

173-
func MakeGetActorCodeFunc(ctx context.Context, store adt.Store, next, current *types.TipSet) (func(a address.Address) (cid.Cid, bool), error) {
174-
ctx, span := otel.Tracer("").Start(ctx, "MakeGetActorCodeFunc")
172+
func MakeGetActorCodeFunc(ctx context.Context, store adt.Store, child, parent *types.TipSet) (func(ctx context.Context, a address.Address) (cid.Cid, bool), error) {
173+
_, span := otel.Tracer("").Start(ctx, "MakeGetActorCodeFunc")
175174
defer span.End()
176-
nextStateTree, err := state.LoadStateTree(store, next.ParentState())
177-
if err != nil {
178-
return nil, fmt.Errorf("load state tree: %w", err)
179-
}
180-
181-
// Build a lookup of actor codes that exist after all messages in the current epoch have been executed
182-
actorCodes := map[address.Address]cid.Cid{}
183-
if err := nextStateTree.ForEach(func(a address.Address, act *types.Actor) error {
184-
actorCodes[a] = act.Code
185-
return nil
186-
}); err != nil {
187-
return nil, fmt.Errorf("iterate actors: %w", err)
188-
}
189175

190-
nextInitActor, err := nextStateTree.GetActor(builtininit.Address)
176+
childStateTree, err := state.LoadStateTree(store, child.ParentState())
191177
if err != nil {
192-
return nil, fmt.Errorf("getting init actor: %w", err)
178+
return nil, fmt.Errorf("loading child state: %w", err)
193179
}
194180

195-
nextInitActorState, err := builtininit.Load(store, nextInitActor)
181+
parentStateTree, err := state.LoadStateTree(store, parent.ParentState())
196182
if err != nil {
197-
return nil, fmt.Errorf("loading init actor state: %w", err)
183+
return nil, fmt.Errorf("loading parent state: %w", err)
198184
}
199185

200-
return func(a address.Address) (cid.Cid, bool) {
201-
// TODO accept a context, don't take the function context.
186+
return func(ctx context.Context, a address.Address) (cid.Cid, bool) {
202187
_, innerSpan := otel.Tracer("").Start(ctx, "GetActorCode")
203188
defer innerSpan.End()
204-
// Shortcut lookup before resolving
205-
c, ok := actorCodes[a]
206-
if ok {
207-
return c, true
208-
}
209189

210-
ra, found, err := nextInitActorState.ResolveAddress(a)
211-
if err != nil || !found {
212-
log.Warnw("failed to resolve actor address", "address", a.String())
213-
return cid.Undef, false
190+
act, err := childStateTree.GetActor(a)
191+
if err == nil {
192+
return act.Code, true
214193
}
215194

216-
c, ok = actorCodes[ra]
217-
if ok {
218-
return c, true
219-
}
220-
221-
// Fall back to looking in current state tree. This actor may have been deleted.
222-
currentStateTree, err := state.LoadStateTree(store, current.ParentState())
223-
if err != nil {
224-
log.Warnf("failed to load state tree: %v", err)
225-
return cid.Undef, false
226-
}
227-
228-
act, err := currentStateTree.GetActor(a)
229-
if err != nil {
230-
log.Warnw("failed to find actor in state tree", "address", a.String(), "error", err.Error())
231-
return cid.Undef, false
195+
// look in parent state, the address may have been deleted in the transition from parent -> child state.
196+
log.Infof("failed to find actor %s in child init actor state (err: %s), falling back to parent", a, err)
197+
act, err = parentStateTree.GetActor(a)
198+
if err == nil {
199+
return act.Code, true
232200
}
233201

234-
return act.Code, true
202+
log.Errorf("failed to find actor %s in parent state: %s", a, err)
203+
return cid.Undef, false
235204
}, nil
236205
}
237206

tasks/messageexecutions/vm/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
5858
return nil
5959
})
6060

61-
var getActorCode func(a address.Address) (cid.Cid, bool)
61+
var getActorCode func(ctx context.Context, a address.Address) (cid.Cid, bool)
6262
grp.Go(func() error {
6363
var err error
6464
getActorCode, err = util.MakeGetActorCodeFunc(ctx, t.node.Store(), current, executed)
@@ -101,7 +101,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
101101
// Cid() computes a CID, so only call it once
102102
childCid := child.Message.Cid()
103103

104-
toCode, found := getActorCode(child.Message.To)
104+
toCode, found := getActorCode(ctx, child.Message.To)
105105
if !found && child.Receipt.ExitCode == 0 {
106106
// No destination actor code. Normally Lotus will create an account actor for unknown addresses but if the
107107
// message fails then Lotus will not allow the actor to be created, and we are left with an address of an

tasks/messages/gasoutput/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
5252

5353
grp, grpCtx := errgroup.WithContext(ctx)
5454

55-
var getActorCodeFn func(address address.Address) (cid.Cid, bool)
55+
var getActorCodeFn func(ctx context.Context, address address.Address) (cid.Cid, bool)
5656
grp.Go(func() error {
5757
var err error
5858
getActorCodeFn, err = util.MakeGetActorCodeFunc(grpCtx, t.node.Store(), current, executed)
@@ -113,7 +113,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
113113
}
114114
exeMsgSeen[m.Cid()] = true
115115

116-
toActorCode, found := getActorCodeFn(m.VMMessage().To)
116+
toActorCode, found := getActorCodeFn(ctx, m.VMMessage().To)
117117
if !found {
118118
toActorCode = cid.Undef
119119
}

tasks/messages/parsedmessage/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
5454

5555
grp, _ := errgroup.WithContext(ctx)
5656

57-
var getActorCodeFn func(address address.Address) (cid.Cid, bool)
57+
var getActorCodeFn func(ctx context.Context, address address.Address) (cid.Cid, bool)
5858
grp.Go(func() error {
5959
var err error
6060
getActorCodeFn, err = util.MakeGetActorCodeFunc(ctx, t.node.Store(), current, executed)
@@ -105,7 +105,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
105105
}
106106
exeMsgSeen[m.Cid()] = true
107107

108-
toActorCode, found := getActorCodeFn(m.VMMessage().To)
108+
toActorCode, found := getActorCodeFn(ctx, m.VMMessage().To)
109109
if !found && r.ExitCode == 0 {
110110
// No destination actor code. Normally Lotus will create an account actor for unknown addresses but if the
111111
// message fails then Lotus will not allow the actor to be created and we are left with an address of an

tasks/msapprovals/msapprovals.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
5959

6060
grp, _ := errgroup.WithContext(ctx)
6161

62-
var getActorCodeFn func(address address.Address) (cid.Cid, bool)
62+
var getActorCodeFn func(ctx context.Context, address address.Address) (cid.Cid, bool)
6363
grp.Go(func() error {
6464
var err error
6565
getActorCodeFn, err = util.MakeGetActorCodeFunc(ctx, p.node.Store(), current, executed)
@@ -108,7 +108,7 @@ func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
108108
}
109109

110110
// Only interested in messages to multisig actors
111-
msgToCode, found := getActorCodeFn(msg.VMMessage().To)
111+
msgToCode, found := getActorCodeFn(ctx, msg.VMMessage().To)
112112
if !found {
113113
return nil, nil, fmt.Errorf("failed to find to actor %s height %d message %s", msg.VMMessage().To, current.Height(), msg.Cid())
114114
}

0 commit comments

Comments
 (0)