Skip to content

Commit

Permalink
feat: api: improve the correctness of Eth's trace_block (#11609)
Browse files Browse the repository at this point in the history
* Improve the correctness of Eth's trace_block

- Improve encoding/decoding of parameters and return values:
  - Encode "native" parameters and return values with Solidity ABI.
  - Correctly decode parameters to "create" calls.
  - Use the correct (ish) output for "create" calls.
  - Handle all forms of "create".
- Make robust with respect to reverts:
  - Use the actor ID/address from the trace instead of looking it up in
    the state-tree (may not exist in the state-tree due to a revert).
  - Gracefully handle failed actor/contract creation.
- Improve performance:
  - We avoid looking anything up in the state-tree when translating the
    trace, which should significantly improve performance.
- Improve code readability:
  - Remove all "backtracking" logic.
  - Use an "environment" struct to store temporary state instead of
    attaching it to the trace.
- Fix random bugs:
  - Fix an allocation bug in the "address" logic (need to set the
    capacity before modifying the slice).
  - Improved error checking/handling.
- Use correct types for `trace_block` action/results (create, call, etc.).
  - And use the correct types for Result/Action structs instead of reusing the same "Call" action every time.
- Improve error messages.
  • Loading branch information
Stebalien committed Feb 22, 2024
1 parent 2e5de1b commit 1f579f2
Show file tree
Hide file tree
Showing 12 changed files with 729 additions and 277 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ Replace the `CodeCid` field in the message trace (added in 1.23.4) with an `Invo

This means the trace now contains an accurate "snapshot" of the actor at the time of the call, information that may not be present in the final state-tree (e.g., due to reverts). This will hopefully improve the performance and accuracy of indexing services.

### Ethereum Tracing API (`trace_block` and `trace_replayBlockTransactions`)

For those with the Ethereum JSON-RPC API enabled, the experimental Ethereum Tracing API has been improved significantly and should be considered "functional". However, it's still new and should be tested extensively before relying on it. This API translates FVM traces to Ethereum-style traces, implementing the OpenEthereum `trace_block` and `trace_replayBlockTransactions` APIs.

This release fixes numerous bugs with this API and now ABI-encodes non-EVM inputs/outputs as if they were explicit EVM calls to [`handle_filecoin_method`][handlefilecoinmethod] for better block explorer compatibility.

However, there are some _significant_ limitations:

1. The Geth APIs are not implemented, only the OpenEthereum (Erigon, etc.) APIs.
2. Block rewards are not (yet) included in the trace.
3. Selfdestruct operations are not included in the trace.
4. EVM smart contract "create" events always specify `0xfe` as the "code" for newly created EVM smart contracts.

Additionally, Filecoin is not Ethereum no matter how much we try to provide API/tooling compatibility. This API attempts to translate Filecoin semantics into Ethereum semantics as accurately as possible, but it's hardly the best source of data unless you _need_ Filecoin to look like an Ethereum compatible chain. If you're trying to build a new integration with Filecoin, please use the native `StateCompute` method instead.

[handlefilecoinmethod]: https://fips.filecoin.io/FIPS/fip-0054.html#handlefilecoinmethod-general-handler-for-method-numbers--1024

# v1.25.2 / 2024-01-11

This is an optional but **highly recommended feature release** of Lotus, as it includes fixes for synchronizations issues that users have experienced. The feature release also introduces `Lotus-Provider` in its alpha testing phase, as well as the ability to call external PC2-binaries during the sealing process.
Expand Down
19 changes: 18 additions & 1 deletion api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,26 @@ type FullNode interface {
Web3ClientVersion(ctx context.Context) (string, error) //perm:read

// TraceAPI related methods

// Returns an OpenEthereum-compatible trace of the given block (implementing `trace_block`),
// translating Filecoin semantics into Ethereum semantics and tracing both EVM and FVM calls.
//
// Features:
//
// - FVM actor create events, calls, etc. show up as if they were EVM smart contract events.
// - Native FVM call inputs are ABI-encoded (Solidity ABI) as if they were calls to a
// `handle_filecoin_method(uint64 method, uint64 codec, bytes params)` function
// (where `codec` is the IPLD codec of `params`).
// - Native FVM call outputs (return values) are ABI-encoded as `(uint32 exit_code, uint64
// codec, bytes output)` where `codec` is the IPLD codec of `output`.
//
// Returns traces created at given block
// Limitations (for now):
//
// 1. Block rewards are not included in the trace.
// 2. SELFDESTRUCT operations are not included in the trace.
// 3. EVM smart contract "create" events always specify `0xfe` as the "code" for newly created EVM smart contracts.
EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtypes.EthTraceBlock, error) //perm:read

// Replays all transactions in a block returning the requested traces for each transaction
EthTraceReplayBlockTransactions(ctx context.Context, blkNum string, traceTypes []string) ([]*ethtypes.EthTraceReplayBlockTransaction, error) //perm:read

Expand Down
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/gateway.json.gz
Binary file not shown.
14 changes: 14 additions & 0 deletions chain/actors/builtin/evm/actor.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ import (
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/types"

"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/go-state-types/manifest"

builtin{{.latestVersion}} "github.com/filecoin-project/go-state-types/builtin"
)

var Methods = builtin{{.latestVersion}}.MethodsEVM

// See https://github.com/filecoin-project/builtin-actors/blob/6e781444cee5965278c46ef4ffe1fb1970f18d7d/actors/evm/src/lib.rs#L35-L42
const (
ErrReverted exitcode.ExitCode = iota + 33 // EVM exit codes start at 33
ErrInvalidInstruction
ErrUndefinedInstruction
ErrStackUnderflow
ErrStackOverflow
ErrIllegalMemoryAccess
ErrBadJumpdest
ErrSelfdestructFailed
)

func Load(store adt.Store, act *types.Actor) (State, error) {
if name, av, ok := actors.GetActorMetaByCode(act.Code); ok {
if name != manifest.EvmKey {
Expand Down
13 changes: 13 additions & 0 deletions chain/actors/builtin/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
actorstypes "github.com/filecoin-project/go-state-types/actors"
builtin13 "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/go-state-types/manifest"

"github.com/filecoin-project/lotus/chain/actors"
Expand All @@ -16,6 +17,18 @@ import (

var Methods = builtin13.MethodsEVM

// See https://github.com/filecoin-project/builtin-actors/blob/6e781444cee5965278c46ef4ffe1fb1970f18d7d/actors/evm/src/lib.rs#L35-L42
const (
ErrReverted exitcode.ExitCode = iota + 33 // EVM exit codes start at 33
ErrInvalidInstruction
ErrUndefinedInstruction
ErrStackUnderflow
ErrStackOverflow
ErrIllegalMemoryAccess
ErrBadJumpdest
ErrSelfdestructFailed
)

func Load(store adt.Store, act *types.Actor) (State, error) {
if name, av, ok := actors.GetActorMetaByCode(act.Code); ok {
if name != manifest.EvmKey {
Expand Down
58 changes: 30 additions & 28 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,21 @@ func IsEthAddress(addr address.Address) bool {
return namespace == builtintypes.EthereumAddressManagerActorID && len(payload) == 20 && !bytes.HasPrefix(payload, maskedIDPrefix[:])
}

func EthAddressFromActorID(id abi.ActorID) EthAddress {
var ethaddr EthAddress
ethaddr[0] = 0xff
binary.BigEndian.PutUint64(ethaddr[12:], uint64(id))
return ethaddr
}

func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
switch addr.Protocol() {
case address.ID:
id, err := address.IDFromAddress(addr)
if err != nil {
return EthAddress{}, err
}
var ethaddr EthAddress
ethaddr[0] = 0xff
binary.BigEndian.PutUint64(ethaddr[12:], id)
return ethaddr, nil
return EthAddressFromActorID(abi.ActorID(id)), nil
case address.Delegated:
payload := addr.Payload()
namespace, n, err := varint.FromUvarint(payload)
Expand Down Expand Up @@ -971,22 +975,12 @@ func (e *EthBlockNumberOrHash) UnmarshalJSON(b []byte) error {
}

type EthTrace struct {
Action EthTraceAction `json:"action"`
Result EthTraceResult `json:"result"`
Subtraces int `json:"subtraces"`
TraceAddress []int `json:"traceAddress"`
Type string `json:"Type"`

Parent *EthTrace `json:"-"`

// if a subtrace makes a call to GetBytecode, we store a pointer to that subtrace here
// which we then lookup when checking for delegatecall (InvokeContractDelegate)
LastByteCode *EthTrace `json:"-"`
}

func (t *EthTrace) SetCallType(callType string) {
t.Action.CallType = callType
t.Type = callType
Type string `json:"type"`
Error string `json:"error,omitempty"`
Subtraces int `json:"subtraces"`
TraceAddress []int `json:"traceAddress"`
Action any `json:"action"`
Result any `json:"result"`
}

type EthTraceBlock struct {
Expand All @@ -1005,21 +999,29 @@ type EthTraceReplayBlockTransaction struct {
VmTrace *string `json:"vmTrace"`
}

type EthTraceAction struct {
type EthCallTraceAction struct {
CallType string `json:"callType"`
From EthAddress `json:"from"`
To EthAddress `json:"to"`
Gas EthUint64 `json:"gas"`
Input EthBytes `json:"input"`
Value EthBigInt `json:"value"`

FilecoinMethod abi.MethodNum `json:"-"`
FilecoinCodeCid cid.Cid `json:"-"`
FilecoinFrom address.Address `json:"-"`
FilecoinTo address.Address `json:"-"`
Input EthBytes `json:"input"`
}

type EthTraceResult struct {
type EthCallTraceResult struct {
GasUsed EthUint64 `json:"gasUsed"`
Output EthBytes `json:"output"`
}

type EthCreateTraceAction struct {
From EthAddress `json:"from"`
Gas EthUint64 `json:"gas"`
Value EthBigInt `json:"value"`
Init EthBytes `json:"init"`
}

type EthCreateTraceResult struct {
Address *EthAddress `json:"address,omitempty"`
GasUsed EthUint64 `json:"gasUsed"`
Code EthBytes `json:"code"`
}
52 changes: 24 additions & 28 deletions documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -3080,9 +3080,23 @@ Inputs: `null`
Response: `false`

### EthTraceBlock
TraceAPI related methods
Returns an OpenEthereum-compatible trace of the given block (implementing `trace_block`),
translating Filecoin semantics into Ethereum semantics and tracing both EVM and FVM calls.

Returns traces created at given block
Features:

- FVM actor create events, calls, etc. show up as if they were EVM smart contract events.
- Native FVM call inputs are ABI-encoded (Solidity ABI) as if they were calls to a
`handle_filecoin_method(uint64 method, uint64 codec, bytes params)` function
(where `codec` is the IPLD codec of `params`).
- Native FVM call outputs (return values) are ABI-encoded as `(uint32 exit_code, uint64
codec, bytes output)` where `codec` is the IPLD codec of `output`.

Limitations (for now):

1. Block rewards are not included in the trace.
2. SELFDESTRUCT operations are not included in the trace.
3. EVM smart contract "create" events always specify `0xfe` as the "code" for newly created EVM smart contracts.


Perms: read
Expand All @@ -3098,23 +3112,14 @@ Response:
```json
[
{
"action": {
"callType": "string value",
"from": "0x5cbeecf99d3fdb3f25e309cc264f240bb0664031",
"to": "0x5cbeecf99d3fdb3f25e309cc264f240bb0664031",
"gas": "0x5",
"input": "0x07",
"value": "0x0"
},
"result": {
"gasUsed": "0x5",
"output": "0x07"
},
"type": "string value",
"error": "string value",
"subtraces": 123,
"traceAddress": [
123
],
"Type": "string value",
"action": {},
"result": {},
"blockHash": "0x37690cfec6c1bf4c3b9288c7a5d783e98731e90b0a4c177c2a374c7a9427355e",
"blockNumber": 9,
"transactionHash": "0x37690cfec6c1bf4c3b9288c7a5d783e98731e90b0a4c177c2a374c7a9427355e",
Expand Down Expand Up @@ -3147,23 +3152,14 @@ Response:
"stateDiff": "string value",
"trace": [
{
"action": {
"callType": "string value",
"from": "0x5cbeecf99d3fdb3f25e309cc264f240bb0664031",
"to": "0x5cbeecf99d3fdb3f25e309cc264f240bb0664031",
"gas": "0x5",
"input": "0x07",
"value": "0x0"
},
"result": {
"gasUsed": "0x5",
"output": "0x07"
},
"type": "string value",
"error": "string value",
"subtraces": 123,
"traceAddress": [
123
],
"Type": "string value"
"action": {},
"result": {}
}
],
"transactionHash": "0x37690cfec6c1bf4c3b9288c7a5d783e98731e90b0a4c177c2a374c7a9427355e",
Expand Down
61 changes: 31 additions & 30 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin"
builtintypes "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v10/evm"
"github.com/filecoin-project/go-state-types/exitcode"
Expand Down Expand Up @@ -883,28 +882,28 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp
return nil, xerrors.Errorf("failed to get transaction hash by cid: %w", err)
}
if txHash == nil {
log.Warnf("cannot find transaction hash for cid %s", ir.MsgCid)
continue
return nil, xerrors.Errorf("cannot find transaction hash for cid %s", ir.MsgCid)
}

traces := []*ethtypes.EthTrace{}
err = buildTraces(&traces, nil, []int{}, ir.ExecutionTrace, int64(ts.Height()), st)
env, err := baseEnvironment(st, ir.Msg.From)
if err != nil {
return nil, xerrors.Errorf("failed building traces: %w", err)
return nil, xerrors.Errorf("when processing message %s: %w", ir.MsgCid, err)
}

traceBlocks := make([]*ethtypes.EthTraceBlock, 0, len(traces))
for _, trace := range traces {
traceBlocks = append(traceBlocks, &ethtypes.EthTraceBlock{
err = buildTraces(env, []int{}, &ir.ExecutionTrace)
if err != nil {
return nil, xerrors.Errorf("failed building traces for msg %s: %w", ir.MsgCid, err)
}

for _, trace := range env.traces {
allTraces = append(allTraces, &ethtypes.EthTraceBlock{
EthTrace: trace,
BlockHash: blkHash,
BlockNumber: int64(ts.Height()),
TransactionHash: *txHash,
TransactionPosition: msgIdx,
})
}

allTraces = append(allTraces, traceBlocks...)
}

return allTraces, nil
Expand Down Expand Up @@ -942,34 +941,36 @@ func (a *EthModule) EthTraceReplayBlockTransactions(ctx context.Context, blkNum
return nil, xerrors.Errorf("failed to get transaction hash by cid: %w", err)
}
if txHash == nil {
log.Warnf("cannot find transaction hash for cid %s", ir.MsgCid)
continue
return nil, xerrors.Errorf("cannot find transaction hash for cid %s", ir.MsgCid)
}

var output ethtypes.EthBytes
invokeCreateOnEAM := ir.Msg.To == builtin.EthereumAddressManagerActorAddr && (ir.Msg.Method == builtin.MethodsEAM.Create || ir.Msg.Method == builtin.MethodsEAM.Create2)
if ir.Msg.Method == builtin.MethodsEVM.InvokeContract || invokeCreateOnEAM {
output, err = decodePayload(ir.ExecutionTrace.MsgRct.Return, ir.ExecutionTrace.MsgRct.ReturnCodec)
if err != nil {
return nil, xerrors.Errorf("failed to decode payload: %w", err)
env, err := baseEnvironment(st, ir.Msg.From)
if err != nil {
return nil, xerrors.Errorf("when processing message %s: %w", ir.MsgCid, err)
}

err = buildTraces(env, []int{}, &ir.ExecutionTrace)
if err != nil {
return nil, xerrors.Errorf("failed building traces for msg %s: %w", ir.MsgCid, err)
}

var output []byte
if len(env.traces) > 0 {
switch r := env.traces[0].Result.(type) {
case *ethtypes.EthCallTraceResult:
output = r.Output
case *ethtypes.EthCreateTraceResult:
output = r.Code
}
} else {
output = encodeFilecoinReturnAsABI(ir.ExecutionTrace.MsgRct.ExitCode, ir.ExecutionTrace.MsgRct.ReturnCodec, ir.ExecutionTrace.MsgRct.Return)
}

t := ethtypes.EthTraceReplayBlockTransaction{
allTraces = append(allTraces, &ethtypes.EthTraceReplayBlockTransaction{
Output: output,
TransactionHash: *txHash,
Trace: env.traces,
StateDiff: nil,
VmTrace: nil,
}

err = buildTraces(&t.Trace, nil, []int{}, ir.ExecutionTrace, int64(ts.Height()), st)
if err != nil {
return nil, xerrors.Errorf("failed building traces: %w", err)
}

allTraces = append(allTraces, &t)
})
}

return allTraces, nil
Expand Down
Loading

0 comments on commit 1f579f2

Please sign in to comment.