Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:: backport #11609 to the feat/nv22 branch #11644

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ OPTIONS:
```


### 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 @@ -875,9 +875,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 @@ -3082,9 +3082,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 @@ -3100,23 +3114,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 @@ -3149,23 +3154,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