Skip to content

Commit

Permalink
working on #11984
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Seiler authored and Michael Seiler committed Jun 10, 2024
1 parent f00b50f commit d50e4e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
25 changes: 10 additions & 15 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp

allTraces := make([]*ethtypes.EthTraceBlock, 0, len(trace))
msgIdx := 0
deployedCode := make(map[abi.ActorID]*cid.Cid)
deployedCode := make(map[abi.ActorID]*ethtypes.EthBytes)

for _, ir := range trace {
// ignore messages from system actor
Expand All @@ -890,12 +890,12 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp
return nil, xerrors.Errorf("when processing message %s: %w", ir.MsgCid, err)
}

var deployed map[abi.ActorID]*cid.Cid
var deployed map[abi.ActorID]*ethtypes.EthBytes
if ir.MsgRct.ExitCode.IsSuccess() {
deployed = deployedCode
}

err = buildTraces(env, []int{}, &ir.ExecutionTrace, deployed)
err = buildTraces(ctx, a, env, []int{}, &ir.ExecutionTrace, deployed)
if err != nil {
return nil, xerrors.Errorf("failed building traces for msg %s: %w", ir.MsgCid, err)
}
Expand Down Expand Up @@ -924,8 +924,9 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp
return allTraces, nil
}

func (a *EthModule) updateContractAddresses(ctx context.Context, ts *types.TipSet, allTraces []*ethtypes.EthTrace, deployedCode map[abi.ActorID]*cid.Cid) error {
func (a *EthModule) updateContractAddresses(ctx context.Context, ts *types.TipSet, allTraces []*ethtypes.EthTrace, deployedCode map[abi.ActorID]*ethtypes.EthBytes) error {
// Update Contract Addresses if contract still exists

for _, trace := range allTraces {
switch r := trace.Result.(type) {
case *ethtypes.EthCreateTraceResult:
Expand All @@ -951,17 +952,11 @@ func (a *EthModule) updateContractAddresses(ctx context.Context, ts *types.TipSe
continue
}

codeCid, ok := deployedCode[id]
code, ok := deployedCode[id]
if !ok {
continue
}

blk, err := a.Chain.StateBlockstore().Get(ctx, *codeCid)
if blk == nil || err != nil {
continue
}

r.Code = blk.RawData()
r.Code = *code // TODO is this a slow copy in go?
}
}
return nil
Expand All @@ -987,7 +982,7 @@ func (a *EthModule) EthTraceReplayBlockTransactions(ctx context.Context, blkNum
return nil, xerrors.Errorf("failed load computed state-tree: %w", err)
}

deployedCode := make(map[abi.ActorID]*cid.Cid)
deployedCode := make(map[abi.ActorID]*ethtypes.EthBytes)

allTraces := make([]*ethtypes.EthTraceReplayBlockTransaction, 0, len(trace))
for _, ir := range trace {
Expand All @@ -1009,12 +1004,12 @@ func (a *EthModule) EthTraceReplayBlockTransactions(ctx context.Context, blkNum
return nil, xerrors.Errorf("when processing message %s: %w", ir.MsgCid, err)
}

var deployed map[abi.ActorID]*cid.Cid
var deployed map[abi.ActorID]*ethtypes.EthBytes
if ir.MsgRct.ExitCode.IsSuccess() {
deployed = deployedCode
}

err = buildTraces(env, []int{}, &ir.ExecutionTrace, deployed)
err = buildTraces(ctx, a, env, []int{}, &ir.ExecutionTrace, deployed)
if err != nil {
return nil, xerrors.Errorf("failed building traces for msg %s: %w", ir.MsgCid, err)
}
Expand Down
28 changes: 20 additions & 8 deletions node/impl/full/eth_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package full

import (
"bytes"
"context"
"fmt"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multicodec"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -164,8 +164,8 @@ func traceErrMsg(et *types.ExecutionTrace) string {
}

// buildTraces recursively builds the traces for a given ExecutionTrace by walking the subcalls
func buildTraces(env *environment, addr []int, et *types.ExecutionTrace, deployedCode map[abi.ActorID]*cid.Cid) error {
trace, recurseInto, err := buildTrace(env, addr, et, deployedCode)
func buildTraces(ctx context.Context, a *EthModule, env *environment, addr []int, et *types.ExecutionTrace, deployedCode map[abi.ActorID]*ethtypes.EthBytes) error {
trace, recurseInto, err := buildTrace(ctx, a, env, addr, et, deployedCode)
if err != nil {
return xerrors.Errorf("at trace %v: %w", addr, err)
}
Expand All @@ -189,7 +189,13 @@ func buildTraces(env *environment, addr []int, et *types.ExecutionTrace, deploye
// end up repeatedly mutating previous paths.
addr = addr[:len(addr):len(addr)]
for i := range recurseInto.Subcalls {
err := buildTraces(subEnv, append(addr, subEnv.subtraceCount), &recurseInto.Subcalls[i], deployedCode)

var deployedSubcall map[abi.ActorID]*ethtypes.EthBytes
if recurseInto.Subcalls[i].MsgRct.ExitCode == 0 {
deployedSubcall = deployedCode
}

err := buildTraces(ctx, a, subEnv, append(addr, subEnv.subtraceCount), &recurseInto.Subcalls[i], deployedSubcall)
if err != nil {
return err
}
Expand All @@ -203,7 +209,7 @@ func buildTraces(env *environment, addr []int, et *types.ExecutionTrace, deploye
// buildTrace processes the passed execution trace and updates the environment, if necessary.
//
// On success, it returns a trace to add (or nil to skip) and the trace recurse into (or nil to skip).
func buildTrace(env *environment, addr []int, et *types.ExecutionTrace, deployedCode map[abi.ActorID]*cid.Cid) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
func buildTrace(ctx context.Context, a *EthModule, env *environment, addr []int, et *types.ExecutionTrace, deployedCode map[abi.ActorID]*ethtypes.EthBytes) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
// This function first assumes that the call is a "native" call, then handles all the "not
// native" cases. If we get any unexpected results in any of these special cases, we just
// keep the "native" interpretation and move on.
Expand Down Expand Up @@ -257,7 +263,7 @@ func buildTrace(env *environment, addr []int, et *types.ExecutionTrace, deployed
case builtin.EthereumAddressManagerActorAddr:
switch et.Msg.Method {
case builtin.MethodsEAM.Create, builtin.MethodsEAM.Create2, builtin.MethodsEAM.CreateExternal:
return traceEthCreate(env, addr, et, deployedCode)
return traceEthCreate(ctx, a, env, addr, et, deployedCode)
}
}

Expand Down Expand Up @@ -454,7 +460,7 @@ func decodeCreateViaEAM(et *types.ExecutionTrace) (initcode []byte, addr *ethtyp

// Build an EthTrace for an EVM "create" operation. This should only be called with an
// ExecutionTrace for a Create, Create2, or CreateExternal method invocation on the EAM.
func traceEthCreate(env *environment, addr []int, et *types.ExecutionTrace, deployedCode map[abi.ActorID]*cid.Cid) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
func traceEthCreate(ctx context.Context, a *EthModule, env *environment, addr []int, et *types.ExecutionTrace, deployedCode map[abi.ActorID]*ethtypes.EthBytes) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
// Same as the Init actor case above, see the comment there.
if et.Msg.ReadOnly {
return nil, nil, nil
Expand Down Expand Up @@ -502,7 +508,13 @@ func traceEthCreate(env *environment, addr []int, et *types.ExecutionTrace, depl
if deployedCode != nil {
var getBytecodeReturn evm12.GetBytecodeReturn
if err := getBytecodeReturn.UnmarshalCBOR(bytes.NewReader(et.MsgRct.Return)); err == nil && getBytecodeReturn.Cid != nil {
deployedCode[abi.ActorID(id)] = getBytecodeReturn.Cid

blk, err := a.Chain.StateBlockstore().Get(ctx, *getBytecodeReturn.Cid)
if blk == nil || err != nil {
return nil, nil, xerrors.Errorf("cid not found: %w", err)
}
codeRawData := ethtypes.EthBytes(blk.RawData())
deployedCode[abi.ActorID(id)] = &codeRawData
}
}

Expand Down

0 comments on commit d50e4e1

Please sign in to comment.