Skip to content

Commit

Permalink
fix: remove actor code mapping (#1087)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
birdychang and frrist authored Nov 17, 2022
1 parent a2eb052 commit d23f21c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 58 deletions.
4 changes: 2 additions & 2 deletions lens/lily/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,13 @@ func (m *LilyNodeAPI) GetMessageExecutionsForTipSet(ctx context.Context, next *t

out := make([]*lens.MessageExecution, len(executions))
for idx, execution := range executions {
toCode, found := getActorCode(execution.Msg.To)
toCode, found := getActorCode(ctx, execution.Msg.To)
// if the message failed to execute due to lack of gas then the TO actor may never have been created.
if !found {
log.Warnw("failed to find TO actor", "height", next.Height().String(), "message", execution.Msg.Cid().String(), "actor", execution.Msg.To.String())
}
// if the message sender cannot be found this is an unexpected error
fromCode, found := getActorCode(execution.Msg.From)
fromCode, found := getActorCode(ctx, execution.Msg.From)
if !found {
return nil, fmt.Errorf("failed to find from actor %s height %d message %s", execution.Msg.From, execution.TipSet.Height(), execution.Msg.Cid())
}
Expand Down
65 changes: 17 additions & 48 deletions lens/util/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/specs-actors/actors/util/adt"

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

Expand Down Expand Up @@ -170,68 +169,38 @@ func ActorNameAndFamilyFromCode(c cid.Cid) (name string, family string, err erro
return
}

func MakeGetActorCodeFunc(ctx context.Context, store adt.Store, next, current *types.TipSet) (func(a address.Address) (cid.Cid, bool), error) {
ctx, span := otel.Tracer("").Start(ctx, "MakeGetActorCodeFunc")
func MakeGetActorCodeFunc(ctx context.Context, store adt.Store, child, parent *types.TipSet) (func(ctx context.Context, a address.Address) (cid.Cid, bool), error) {
_, span := otel.Tracer("").Start(ctx, "MakeGetActorCodeFunc")
defer span.End()
nextStateTree, err := state.LoadStateTree(store, next.ParentState())
if err != nil {
return nil, fmt.Errorf("load state tree: %w", err)
}

// Build a lookup of actor codes that exist after all messages in the current epoch have been executed
actorCodes := map[address.Address]cid.Cid{}
if err := nextStateTree.ForEach(func(a address.Address, act *types.Actor) error {
actorCodes[a] = act.Code
return nil
}); err != nil {
return nil, fmt.Errorf("iterate actors: %w", err)
}

nextInitActor, err := nextStateTree.GetActor(builtininit.Address)
childStateTree, err := state.LoadStateTree(store, child.ParentState())
if err != nil {
return nil, fmt.Errorf("getting init actor: %w", err)
return nil, fmt.Errorf("loading child state: %w", err)
}

nextInitActorState, err := builtininit.Load(store, nextInitActor)
parentStateTree, err := state.LoadStateTree(store, parent.ParentState())
if err != nil {
return nil, fmt.Errorf("loading init actor state: %w", err)
return nil, fmt.Errorf("loading parent state: %w", err)
}

return func(a address.Address) (cid.Cid, bool) {
// TODO accept a context, don't take the function context.
return func(ctx context.Context, a address.Address) (cid.Cid, bool) {
_, innerSpan := otel.Tracer("").Start(ctx, "GetActorCode")
defer innerSpan.End()
// Shortcut lookup before resolving
c, ok := actorCodes[a]
if ok {
return c, true
}

ra, found, err := nextInitActorState.ResolveAddress(a)
if err != nil || !found {
log.Warnw("failed to resolve actor address", "address", a.String())
return cid.Undef, false
act, err := childStateTree.GetActor(a)
if err == nil {
return act.Code, true
}

c, ok = actorCodes[ra]
if ok {
return c, true
}

// Fall back to looking in current state tree. This actor may have been deleted.
currentStateTree, err := state.LoadStateTree(store, current.ParentState())
if err != nil {
log.Warnf("failed to load state tree: %v", err)
return cid.Undef, false
}

act, err := currentStateTree.GetActor(a)
if err != nil {
log.Warnw("failed to find actor in state tree", "address", a.String(), "error", err.Error())
return cid.Undef, false
// look in parent state, the address may have been deleted in the transition from parent -> child state.
log.Infof("failed to find actor %s in child init actor state (err: %s), falling back to parent", a, err)
act, err = parentStateTree.GetActor(a)
if err == nil {
return act.Code, true
}

return act.Code, true
log.Errorf("failed to find actor %s in parent state: %s", a, err)
return cid.Undef, false
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions tasks/messageexecutions/vm/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
return nil
})

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

toCode, found := getActorCode(child.Message.To)
toCode, found := getActorCode(ctx, child.Message.To)
if !found && child.Receipt.ExitCode == 0 {
// No destination actor code. Normally Lotus will create an account actor for unknown addresses but if the
// message fails then Lotus will not allow the actor to be created, and we are left with an address of an
Expand Down
4 changes: 2 additions & 2 deletions tasks/messages/gasoutput/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut

grp, grpCtx := errgroup.WithContext(ctx)

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

toActorCode, found := getActorCodeFn(m.VMMessage().To)
toActorCode, found := getActorCodeFn(ctx, m.VMMessage().To)
if !found {
toActorCode = cid.Undef
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/messages/parsedmessage/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (t *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut

grp, _ := errgroup.WithContext(ctx)

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

toActorCode, found := getActorCodeFn(m.VMMessage().To)
toActorCode, found := getActorCodeFn(ctx, m.VMMessage().To)
if !found && r.ExitCode == 0 {
// No destination actor code. Normally Lotus will create an account actor for unknown addresses but if the
// message fails then Lotus will not allow the actor to be created and we are left with an address of an
Expand Down
4 changes: 2 additions & 2 deletions tasks/msapprovals/msapprovals.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut

grp, _ := errgroup.WithContext(ctx)

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

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

0 comments on commit d23f21c

Please sign in to comment.