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 authored Feb 21, 2024
1 parent 01cfe8f commit 8ba491b
Show file tree
Hide file tree
Showing 14 changed files with 778 additions and 570 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 @@ -868,9 +868,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
172 changes: 25 additions & 147 deletions build/openrpc/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -8359,7 +8359,7 @@
{
"name": "Filecoin.EthTraceBlock",
"description": "```go\nfunc (s *FullNodeStruct) EthTraceBlock(p0 context.Context, p1 string) ([]*ethtypes.EthTraceBlock, error) {\n\tif s.Internal.EthTraceBlock == nil {\n\t\treturn *new([]*ethtypes.EthTraceBlock), ErrNotSupported\n\t}\n\treturn s.Internal.EthTraceBlock(p0, p1)\n}\n```",
"summary": "TraceAPI related methods\n\nReturns traces created at given block\n",
"summary": "Returns an OpenEthereum-compatible trace of the given block (implementing `trace_block`),\ntranslating Filecoin semantics into Ethereum semantics and tracing both EVM and FVM calls.\n\nFeatures:\n\n- FVM actor create events, calls, etc. show up as if they were EVM smart contract events.\n- Native FVM call inputs are ABI-encoded (Solidity ABI) as if they were calls to a\n `handle_filecoin_method(uint64 method, uint64 codec, bytes params)` function\n (where `codec` is the IPLD codec of `params`).\n- Native FVM call outputs (return values) are ABI-encoded as `(uint32 exit_code, uint64\n codec, bytes output)` where `codec` is the IPLD codec of `output`.\n\nLimitations (for now):\n\n1. Block rewards are not included in the trace.\n2. SELFDESTRUCT operations are not included in the trace.\n3. EVM smart contract \"create\" events always specify `0xfe` as the \"code\" for newly created EVM smart contracts.\n",
"paramStructure": "by-position",
"params": [
{
Expand All @@ -8386,23 +8386,14 @@
"examples": [
[
{
"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 All @@ -8414,52 +8405,8 @@
{
"additionalProperties": false,
"properties": {
"Type": {
"type": "string"
},
"action": {
"additionalProperties": false,
"properties": {
"callType": {
"type": "string"
},
"from": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"maxItems": 20,
"minItems": 20,
"type": "array"
},
"gas": {
"title": "number",
"type": "number"
},
"input": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"type": "array"
},
"to": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"maxItems": 20,
"minItems": 20,
"type": "array"
},
"value": {
"additionalProperties": false,
"type": "object"
}
},
"additionalProperties": true,
"type": "object"
},
"blockHash": {
Expand All @@ -8476,22 +8423,11 @@
"title": "number",
"type": "number"
},
"error": {
"type": "string"
},
"result": {
"additionalProperties": false,
"properties": {
"gasUsed": {
"title": "number",
"type": "number"
},
"output": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"type": "array"
}
},
"additionalProperties": true,
"type": "object"
},
"subtraces": {
Expand Down Expand Up @@ -8519,6 +8455,9 @@
"transactionPosition": {
"title": "number",
"type": "number"
},
"type": {
"type": "string"
}
},
"type": [
Expand Down Expand Up @@ -8597,23 +8536,14 @@
"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 All @@ -8640,70 +8570,15 @@
"items": {
"additionalProperties": false,
"properties": {
"Type": {
"type": "string"
},
"action": {
"additionalProperties": false,
"properties": {
"callType": {
"type": "string"
},
"from": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"maxItems": 20,
"minItems": 20,
"type": "array"
},
"gas": {
"title": "number",
"type": "number"
},
"input": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"type": "array"
},
"to": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"maxItems": 20,
"minItems": 20,
"type": "array"
},
"value": {
"additionalProperties": false,
"type": "object"
}
},
"additionalProperties": true,
"type": "object"
},
"error": {
"type": "string"
},
"result": {
"additionalProperties": false,
"properties": {
"gasUsed": {
"title": "number",
"type": "number"
},
"output": {
"items": {
"description": "Number is a number",
"title": "number",
"type": "number"
},
"type": "array"
}
},
"additionalProperties": true,
"type": "object"
},
"subtraces": {
Expand All @@ -8717,6 +8592,9 @@
"type": "number"
},
"type": "array"
},
"type": {
"type": "string"
}
},
"type": "object"
Expand Down
Binary file added build/openrpc/full.json.gz
Binary file not shown.
Loading

0 comments on commit 8ba491b

Please sign in to comment.