Merge remote-tracking branch 'origin/master' into feat/lp-seal

This commit is contained in:
Łukasz Magiera 2024-02-22 12:17:09 +01:00
commit 848c132e43
18 changed files with 837 additions and 621 deletions

View File

@ -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.

View File

@ -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.
//
// 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.
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

View File

@ -3,3 +3,6 @@
/dns4/bootstrap-2.calibration.fildev.network/tcp/1347/p2p/12D3KooWNRxTHUn8bf7jz1KEUPMc2dMgGfa4f8ZJTsquVSn3vHCG
/dns4/bootstrap-3.calibration.fildev.network/tcp/1347/p2p/12D3KooWFWUqE9jgXvcKHWieYs9nhyp6NF4ftwLGAHm4sCv73jjK
/dns4/calibration.node.glif.io/tcp/1237/p2p/12D3KooWQPYouEAsUQKzvFUA9sQ8tz4rfpqtTzh2eL6USd9bwg7x
/dns4/bootstrap-calibnet-0.chainsafe-fil.io/tcp/34000/p2p/12D3KooWABQ5gTDHPWyvhJM7jPhtNwNJruzTEo32Lo4gcS5ABAMm
/dns4/bootstrap-calibnet-1.chainsafe-fil.io/tcp/34000/p2p/12D3KooWS3ZRhMYL67b4bD5XQ6fcpTyVQXnDe8H89LvwrDqaSbiT
/dns4/bootstrap-calibnet-2.chainsafe-fil.io/tcp/34000/p2p/12D3KooWEiBN8jBX8EBoM3M47pVRLRWV812gDRUJhMxgyVkUoR48

View File

@ -10,3 +10,6 @@
/dns4/node.glif.io/tcp/1235/p2p/12D3KooWBF8cpp65hp2u9LK5mh19x67ftAam84z9LsfaquTDSBpt
/dns4/bootstarp-0.1475.io/tcp/61256/p2p/12D3KooWRzCVDwHUkgdK7eRgnoXbjDAELhxPErjHzbRLguSV1aRt
/dns4/bootstrap-venus.mainnet.filincubator.com/tcp/8888/p2p/QmQu8C6deXwKvJP2D8B6QGyhngc3ZiDnFzEHBDx8yeBXST
/dns4/bootstrap-mainnet-0.chainsafe-fil.io/tcp/34000/p2p/12D3KooWKKkCZbcigsWTEu1cgNetNbZJqeNtysRtFpq7DTqw3eqH
/dns4/bootstrap-mainnet-1.chainsafe-fil.io/tcp/34000/p2p/12D3KooWGnkd9GQKo3apkShQDaq1d6cKJJmsVe6KiQkacUk1T8oZ
/dns4/bootstrap-mainnet-2.chainsafe-fil.io/tcp/34000/p2p/12D3KooWHQRSDFv4FvAjtU32shQ7znz7oRbLBryXzZ9NMK2feyyH

View File

@ -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": [
{
@ -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",
@ -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": {
@ -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": {
@ -8519,6 +8455,9 @@
"transactionPosition": {
"title": "number",
"type": "number"
},
"type": {
"type": "string"
}
},
"type": [
@ -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",
@ -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": {
@ -8717,6 +8592,9 @@
"type": "number"
},
"type": "array"
},
"type": {
"type": "string"
}
},
"type": "object"

BIN
build/openrpc/full.json.gz Normal file

Binary file not shown.

View File

@ -4439,23 +4439,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",
@ -4467,52 +4458,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": {
@ -4529,22 +4476,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": {
@ -4572,6 +4508,9 @@
"transactionPosition": {
"title": "number",
"type": "number"
},
"type": {
"type": "string"
}
},
"type": [
@ -4650,23 +4589,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",
@ -4693,70 +4623,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": {
@ -4770,6 +4645,9 @@
"type": "number"
},
"type": "array"
},
"type": {
"type": "string"
}
},
"type": "object"

Binary file not shown.

View File

@ -10,6 +10,8 @@ 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"
@ -17,6 +19,18 @@ import (
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 {

View File

@ -7,6 +7,7 @@ import (
actorstypes "github.com/filecoin-project/go-state-types/actors"
builtin12 "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"
@ -16,6 +17,18 @@ import (
var Methods = builtin12.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 {

View File

@ -178,18 +178,16 @@ func (sm *StateManager) HandleStateForks(ctx context.Context, root cid.Cid, heig
retCid := root
u := sm.stateMigrations[height]
if u != nil && u.upgrade != nil {
if height != build.UpgradeWatermelonFixHeight {
migCid, ok, err := u.migrationResultCache.Get(ctx, root)
if err == nil {
if ok {
log.Infow("CACHED migration", "height", height, "from", root, "to", migCid)
return migCid, nil
}
} else if !errors.Is(err, datastore.ErrNotFound) {
log.Errorw("failed to lookup previous migration result", "err", err)
} else {
log.Debug("no cached migration found, migrating from scratch")
migCid, ok, err := u.migrationResultCache.Get(ctx, root)
if err == nil {
if ok {
log.Infow("CACHED migration", "height", height, "from", root, "to", migCid)
return migCid, nil
}
} else if !errors.Is(err, datastore.ErrNotFound) {
log.Errorw("failed to lookup previous migration result", "err", err)
} else {
log.Debug("no cached migration found, migrating from scratch")
}
startTime := time.Now()
@ -197,7 +195,6 @@ func (sm *StateManager) HandleStateForks(ctx context.Context, root cid.Cid, heig
// Yes, we clone the cache, even for the final upgrade epoch. Why? Reverts. We may
// have to migrate multiple times.
tmpCache := u.cache.Clone()
var err error
retCid, err = u.upgrade(ctx, sm, tmpCache, cb, root, height, ts)
if err != nil {
log.Errorw("FAILED migration", "height", height, "from", root, "error", err)

View File

@ -349,6 +349,13 @@ 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:
@ -356,10 +363,7 @@ func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
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)
@ -983,22 +987,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 {
@ -1017,21 +1011,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"`
}

View File

@ -3076,9 +3076,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
@ -3094,23 +3108,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",
@ -3143,23 +3148,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",

View File

@ -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"
@ -875,19 +874,21 @@ 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()),
@ -895,8 +896,6 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp
TransactionPosition: msgIdx,
})
}
allTraces = append(allTraces, traceBlocks...)
}
return allTraces, nil
@ -934,34 +933,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

View File

@ -1,11 +1,14 @@
package full
import (
"bytes"
"encoding/hex"
"testing"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multicodec"
"github.com/stretchr/testify/require"
cbg "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/go-state-types/big"
@ -177,3 +180,40 @@ func TestABIEncoding(t *testing.T) {
require.Equal(t, expectedBytes, encodeAsABIHelper(22, 81, dataBytes))
}
func TestDecodePayload(t *testing.T) {
// "empty"
b, err := decodePayload(nil, 0)
require.NoError(t, err)
require.Empty(t, b)
// raw empty
_, err = decodePayload(nil, uint64(multicodec.Raw))
require.NoError(t, err)
require.Empty(t, b)
// raw non-empty
b, err = decodePayload([]byte{1}, uint64(multicodec.Raw))
require.NoError(t, err)
require.EqualValues(t, b, []byte{1})
// Invalid cbor bytes
_, err = decodePayload(nil, uint64(multicodec.DagCbor))
require.Error(t, err)
// valid cbor bytes
var w bytes.Buffer
require.NoError(t, cbg.WriteByteArray(&w, []byte{1}))
b, err = decodePayload(w.Bytes(), uint64(multicodec.DagCbor))
require.NoError(t, err)
require.EqualValues(t, b, []byte{1})
// regular cbor also works.
b, err = decodePayload(w.Bytes(), uint64(multicodec.Cbor))
require.NoError(t, err)
require.EqualValues(t, b, []byte{1})
// random codec should fail
_, err = decodePayload(w.Bytes(), 42)
require.Error(t, err)
}

View File

@ -2,15 +2,22 @@ package full
import (
"bytes"
"fmt"
"github.com/multiformats/go-multicodec"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v10/evm"
eam12 "github.com/filecoin-project/go-state-types/builtin/v12/eam"
evm12 "github.com/filecoin-project/go-state-types/builtin/v12/evm"
init12 "github.com/filecoin-project/go-state-types/builtin/v12/init"
"github.com/filecoin-project/go-state-types/exitcode"
builtinactors "github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/evm"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
@ -18,10 +25,6 @@ import (
// decodePayload is a utility function which decodes the payload using the given codec
func decodePayload(payload []byte, codec uint64) (ethtypes.EthBytes, error) {
if len(payload) == 0 {
return nil, nil
}
switch multicodec.Code(codec) {
case multicodec.Identity:
return nil, nil
@ -38,217 +41,565 @@ func decodePayload(payload []byte, codec uint64) (ethtypes.EthBytes, error) {
return nil, xerrors.Errorf("decodePayload: unsupported codec: %d", codec)
}
// buildTraces recursively builds the traces for a given ExecutionTrace by walking the subcalls
func buildTraces(traces *[]*ethtypes.EthTrace, parent *ethtypes.EthTrace, addr []int, et types.ExecutionTrace, height int64, st *state.StateTree) error {
// lookup the eth address from the from/to addresses. Note that this may fail but to support
// this we need to include the ActorID in the trace. For now, just log a warning and skip
// this trace.
//
// TODO: Add ActorID in trace, see https://github.com/filecoin-project/lotus/pull/11100#discussion_r1302442288
from, err := lookupEthAddress(et.Msg.From, st)
if err != nil {
log.Warnf("buildTraces: failed to lookup from address %s: %v", et.Msg.From, err)
return nil
}
to, err := lookupEthAddress(et.Msg.To, st)
if err != nil {
log.Warnf("buildTraces: failed to lookup to address %s: %w", et.Msg.To, err)
return nil
func decodeParams[P any, T interface {
*P
cbg.CBORUnmarshaler
}](msg *types.MessageTrace) (T, error) {
var params T = new(P)
switch msg.ParamsCodec {
case uint64(multicodec.DagCbor), uint64(multicodec.Cbor):
default:
return nil, xerrors.Errorf("Method called with unexpected codec %d", msg.ParamsCodec)
}
// Skip the trace if we never reached the point where we invoked this actor.
if err := params.UnmarshalCBOR(bytes.NewReader(msg.Params)); err != nil {
return nil, xerrors.Errorf("failed to decode params: %w", err)
}
return params, nil
}
func decodeReturn[R any, T interface {
*R
cbg.CBORUnmarshaler
}](ret *types.ReturnTrace) (T, error) {
var retval T = new(R)
switch ret.ReturnCodec {
case uint64(multicodec.DagCbor), uint64(multicodec.Cbor):
default:
return nil, xerrors.Errorf("Method returned an unexpected codec %d", ret.ReturnCodec)
}
if err := retval.UnmarshalCBOR(bytes.NewReader(ret.Return)); err != nil {
return nil, xerrors.Errorf("failed to decode return value: %w", err)
}
return retval, nil
}
func find[T any](values []T, cb func(t *T) *T) *T {
for i := range values {
if o := cb(&values[i]); o != nil {
return o
}
}
return nil
}
type environment struct {
caller ethtypes.EthAddress
isEVM bool
subtraceCount int
traces []*ethtypes.EthTrace
lastByteCode *ethtypes.EthAddress
}
func baseEnvironment(st *state.StateTree, from address.Address) (*environment, error) {
sender, err := lookupEthAddress(from, st)
if err != nil {
return nil, xerrors.Errorf("top-level message sender %s s could not be found: %w", from, err)
}
return &environment{caller: sender}, nil
}
func traceToAddress(act *types.ActorTrace) ethtypes.EthAddress {
if act.State.Address != nil {
if addr, err := ethtypes.EthAddressFromFilecoinAddress(*act.State.Address); err == nil {
return addr
}
}
return ethtypes.EthAddressFromActorID(act.Id)
}
// traceIsEVMOrEAM returns true if the trace is a call to an EVM or EAM actor.
func traceIsEVMOrEAM(et *types.ExecutionTrace) bool {
if et.InvokedActor == nil {
return false
}
return builtinactors.IsEvmActor(et.InvokedActor.State.Code) ||
et.InvokedActor.Id != abi.ActorID(builtin.EthereumAddressManagerActorID)
}
func traceErrMsg(et *types.ExecutionTrace) string {
code := et.MsgRct.ExitCode
if code.IsSuccess() {
return ""
}
// EVM tools often expect this literal string.
if code == exitcode.SysErrOutOfGas {
return "out of gas"
}
// indicate when we have a "system" error.
if code < exitcode.FirstActorErrorCode {
return fmt.Sprintf("vm error: %s", code)
}
// handle special exit codes from the EVM/EAM.
if traceIsEVMOrEAM(et) {
switch code {
case evm.ErrReverted:
return "Reverted" // capitalized for compatibility
case evm.ErrInvalidInstruction:
return "invalid instruction"
case evm.ErrUndefinedInstruction:
return "undefined instruction"
case evm.ErrStackUnderflow:
return "stack underflow"
case evm.ErrStackOverflow:
return "stack overflow"
case evm.ErrIllegalMemoryAccess:
return "illegal memory access"
case evm.ErrBadJumpdest:
return "invalid jump destination"
case evm.ErrSelfdestructFailed:
return "self destruct failed"
}
}
// everything else...
return fmt.Sprintf("actor error: %s", code.Error())
}
// buildTraces recursively builds the traces for a given ExecutionTrace by walking the subcalls
func buildTraces(env *environment, addr []int, et *types.ExecutionTrace) error {
trace, recurseInto, err := buildTrace(env, addr, et)
if err != nil {
return xerrors.Errorf("at trace %v: %w", addr, err)
}
if trace != nil {
env.traces = append(env.traces, trace)
env.subtraceCount++
}
// Skip if there's nothing more to do and/or `buildTrace` told us to skip this one.
if recurseInto == nil || recurseInto.InvokedActor == nil || len(recurseInto.Subcalls) == 0 {
return nil
}
trace := &ethtypes.EthTrace{
Action: ethtypes.EthTraceAction{
From: from,
To: to,
Gas: ethtypes.EthUint64(et.Msg.GasLimit),
Input: nil,
Value: ethtypes.EthBigInt(et.Msg.Value),
FilecoinFrom: et.Msg.From,
FilecoinTo: et.Msg.To,
FilecoinMethod: et.Msg.Method,
FilecoinCodeCid: et.InvokedActor.State.Code,
},
Result: ethtypes.EthTraceResult{
GasUsed: ethtypes.EthUint64(et.SumGas().TotalGas),
Output: nil,
},
Subtraces: 0, // will be updated by the children once they are added to the trace
TraceAddress: addr,
Parent: parent,
LastByteCode: nil,
subEnv := &environment{
caller: traceToAddress(recurseInto.InvokedActor),
isEVM: builtinactors.IsEvmActor(recurseInto.InvokedActor.State.Code),
traces: env.traces,
}
trace.SetCallType("call")
if et.Msg.Method == builtin.MethodsEVM.InvokeContract {
log.Debugf("COND1 found InvokeContract call at height: %d", height)
// TODO: ignore return errors since actors can send gibberish and we don't want
// to fail the whole trace in that case
trace.Action.Input, err = decodePayload(et.Msg.Params, et.Msg.ParamsCodec)
if err != nil {
return xerrors.Errorf("buildTraces: %w", err)
}
trace.Result.Output, err = decodePayload(et.MsgRct.Return, et.MsgRct.ReturnCodec)
if err != nil {
return xerrors.Errorf("buildTraces: %w", err)
}
} else if et.Msg.To == builtin.EthereumAddressManagerActorAddr &&
et.Msg.Method == builtin.MethodsEAM.CreateExternal {
log.Debugf("COND2 found CreateExternal call at height: %d", height)
trace.Action.Input, err = decodePayload(et.Msg.Params, et.Msg.ParamsCodec)
if err != nil {
return xerrors.Errorf("buildTraces: %w", err)
}
if et.MsgRct.ExitCode.IsSuccess() {
// ignore return value
trace.Result.Output = nil
} else {
// return value is the error message
trace.Result.Output, err = decodePayload(et.MsgRct.Return, et.MsgRct.ReturnCodec)
if err != nil {
return xerrors.Errorf("buildTraces: %w", err)
}
}
// treat this as a contract creation
trace.SetCallType("create")
} else {
// we are going to assume a native method, but we may change it in one of the edge cases below
// TODO: only do this if we know it's a native method (optimization)
trace.Action.Input = encodeFilecoinParamsAsABI(et.Msg.Method, et.Msg.ParamsCodec, et.Msg.Params)
trace.Result.Output = encodeFilecoinReturnAsABI(et.MsgRct.ExitCode, et.MsgRct.ReturnCodec, et.MsgRct.Return)
}
// TODO: is it OK to check this here or is this only specific to certain edge case (evm to evm)?
if et.Msg.ReadOnly {
trace.SetCallType("staticcall")
}
// there are several edge cases that require special handling when displaying the traces. Note that while iterating over
// the traces we update the trace backwards (through the parent pointer)
if parent != nil {
// Handle Native actor creation
//
// Actor A calls to the init actor on method 2 and The init actor creates the target actor B then calls it on method 1
if parent.Action.FilecoinTo == builtin.InitActorAddr &&
parent.Action.FilecoinMethod == builtin.MethodsInit.Exec &&
et.Msg.Method == builtin.MethodConstructor {
log.Debugf("COND3 Native actor creation! method:%d, code:%s, height:%d", et.Msg.Method, et.InvokedActor.State.Code.String(), height)
parent.SetCallType("create")
parent.Action.To = to
parent.Action.Input = []byte{0xFE}
parent.Result.Output = nil
// there should never be any subcalls when creating a native actor
//
// TODO: add support for native actors calling another when created
return nil
}
// Handle EVM contract creation
//
// To detect EVM contract creation we need to check for the following sequence of events:
//
// 1) EVM contract A calls the EAM (Ethereum Address Manager) on method 2 (create) or 3 (create2).
// 2) The EAM calls the init actor on method 3 (Exec4).
// 3) The init actor creates the target actor B then calls it on method 1.
if parent.Parent != nil {
calledCreateOnEAM := parent.Parent.Action.FilecoinTo == builtin.EthereumAddressManagerActorAddr &&
(parent.Parent.Action.FilecoinMethod == builtin.MethodsEAM.Create || parent.Parent.Action.FilecoinMethod == builtin.MethodsEAM.Create2)
eamCalledInitOnExec4 := parent.Action.FilecoinTo == builtin.InitActorAddr &&
parent.Action.FilecoinMethod == builtin.MethodsInit.Exec4
initCreatedActor := trace.Action.FilecoinMethod == builtin.MethodConstructor
// TODO: We need to handle failures in contract creations and support resurrections on an existing but dead EVM actor)
if calledCreateOnEAM && eamCalledInitOnExec4 && initCreatedActor {
log.Debugf("COND4 EVM contract creation method:%d, code:%s, height:%d", et.Msg.Method, et.InvokedActor.State.Code.String(), height)
if parent.Parent.Action.FilecoinMethod == builtin.MethodsEAM.Create {
parent.Parent.SetCallType("create")
} else {
parent.Parent.SetCallType("create2")
}
// update the parent.parent to make this
parent.Parent.Action.To = trace.Action.To
parent.Parent.Subtraces = 0
// delete the parent (the EAM) and skip the current trace (init)
*traces = (*traces)[:len(*traces)-1]
return nil
}
}
if builtinactors.IsEvmActor(parent.Action.FilecoinCodeCid) {
// Handle delegate calls
//
// 1) Look for trace from an EVM actor to itself on InvokeContractDelegate, method 6.
// 2) Check that the previous trace calls another actor on method 3 (GetByteCode) and they are at the same level (same parent)
// 3) Treat this as a delegate call to actor A.
if parent.LastByteCode != nil && trace.Action.From == trace.Action.To &&
trace.Action.FilecoinMethod == builtin.MethodsEVM.InvokeContractDelegate {
log.Debugf("COND7 found delegate call, height: %d", height)
prev := parent.LastByteCode
if prev.Action.From == trace.Action.From && prev.Action.FilecoinMethod == builtin.MethodsEVM.GetBytecode && prev.Parent == trace.Parent {
trace.SetCallType("delegatecall")
trace.Action.To = prev.Action.To
var dp evm.DelegateCallParams
err := dp.UnmarshalCBOR(bytes.NewReader(et.Msg.Params))
if err != nil {
return xerrors.Errorf("failed UnmarshalCBOR: %w", err)
}
trace.Action.Input = dp.Input
trace.Result.Output, err = decodePayload(et.MsgRct.Return, et.MsgRct.ReturnCodec)
if err != nil {
return xerrors.Errorf("failed decodePayload: %w", err)
}
}
} else {
// Handle EVM call special casing
//
// Any outbound call from an EVM actor on methods 1-1023 are side-effects from EVM instructions
// and should be dropped from the trace.
if et.Msg.Method > 0 &&
et.Msg.Method <= 1023 {
log.Debugf("Infof found outbound call from an EVM actor on method 1-1023 method:%d, code:%s, height:%d", et.Msg.Method, parent.Action.FilecoinCodeCid.String(), height)
if et.Msg.Method == builtin.MethodsEVM.GetBytecode {
// save the last bytecode trace to handle delegate calls
parent.LastByteCode = trace
}
return nil
}
}
}
}
// we are adding trace to the traces so update the parent subtraces count as it was originally set to zero
if parent != nil {
parent.Subtraces++
}
*traces = append(*traces, trace)
for i, call := range et.Subcalls {
err := buildTraces(traces, trace, append(addr, i), call, height, st)
// Set capacity to the length so each `append` below creates a new slice. Otherwise, we'll
// 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])
if err != nil {
return err
}
}
trace.Subtraces = subEnv.subtraceCount
env.traces = subEnv.traces
return nil
}
// 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) (*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.
//
// 1. If we're invoking a contract (even if the caller is a native account/actor), we
// attempt to decode the params/return value as a contract invocation.
// 2. If we're calling the EAM and/or init actor, we try to treat the call as a CREATE.
// 3. Finally, if the caller is an EVM smart contract and it's calling a "private" (1-1023)
// method, we know something special is going on. We look for calls related to
// DELEGATECALL and drop everything else (everything else includes calls triggered by,
// e.g., EXTCODEHASH).
// If we don't have sufficient funds, or we have a fatal error, or we have some
// other syscall error: skip the entire trace to mimic Ethereum (Ethereum records
// traces _after_ checking things like this).
//
// NOTE: The FFI currently folds all unknown syscall errors into "sys assertion
// failed" which is turned into SysErrFatal.
if len(addr) > 0 {
switch et.MsgRct.ExitCode {
case exitcode.SysErrInsufficientFunds, exitcode.SysErrFatal:
return nil, nil, nil
}
}
// We may fail before we can even invoke the actor. In that case, we have no 100% reliable
// way of getting its address (e.g., due to reverts) so we're just going to drop the entire
// trace. This is OK (ish) because the call never really "happened".
if et.InvokedActor == nil {
return nil, nil, nil
}
// Step 2: Decode as a contract invocation
//
// Normal EVM calls. We don't care if the caller/receiver are actually EVM actors, we only
// care if the call _looks_ like an EVM call. If we fail to decode it as an EVM call, we
// fallback on interpreting it as a native call.
if et.Msg.Method == builtin.MethodsEVM.InvokeContract {
return traceEVMCall(env, addr, et)
}
// Step 3: Decode as a contract deployment
switch et.Msg.To {
// NOTE: this will only catch _direct_ calls to the init actor. Calls through the EAM will
// be caught and _skipped_ below in the next case.
case builtin.InitActorAddr:
switch et.Msg.Method {
case builtin.MethodsInit.Exec, builtin.MethodsInit.Exec4:
return traceNativeCreate(env, addr, et)
}
case builtin.EthereumAddressManagerActorAddr:
switch et.Msg.Method {
case builtin.MethodsEAM.Create, builtin.MethodsEAM.Create2, builtin.MethodsEAM.CreateExternal:
return traceEthCreate(env, addr, et)
}
}
// Step 4: Handle DELEGATECALL
//
// EVM contracts cannot call methods in the range 1-1023, only the EVM itself can. So, if we
// see a call in this range, we know it's an implementation detail of the EVM and not an
// explicit call by the user.
//
// While the EVM calls several methods in this range (some we've already handled above with
// respect to the EAM), we only care about the ones relevant DELEGATECALL and can _ignore_
// all the others.
if env.isEVM && et.Msg.Method > 0 && et.Msg.Method < 1024 {
return traceEVMPrivate(env, addr, et)
}
return traceNativeCall(env, addr, et), et, nil
}
// Build an EthTrace for a "call" with the given input & output.
func traceCall(env *environment, addr []int, et *types.ExecutionTrace, input, output ethtypes.EthBytes) *ethtypes.EthTrace {
to := traceToAddress(et.InvokedActor)
callType := "call"
if et.Msg.ReadOnly {
callType = "staticcall"
}
return &ethtypes.EthTrace{
Type: "call",
Action: &ethtypes.EthCallTraceAction{
CallType: callType,
From: env.caller,
To: to,
Gas: ethtypes.EthUint64(et.Msg.GasLimit),
Value: ethtypes.EthBigInt(et.Msg.Value),
Input: input,
},
Result: &ethtypes.EthCallTraceResult{
GasUsed: ethtypes.EthUint64(et.SumGas().TotalGas),
Output: output,
},
TraceAddress: addr,
Error: traceErrMsg(et),
}
}
// Build an EthTrace for a "call", parsing the inputs & outputs as a "native" FVM call.
func traceNativeCall(env *environment, addr []int, et *types.ExecutionTrace) *ethtypes.EthTrace {
return traceCall(env, addr, et,
encodeFilecoinParamsAsABI(et.Msg.Method, et.Msg.ParamsCodec, et.Msg.Params),
encodeFilecoinReturnAsABI(et.MsgRct.ExitCode, et.MsgRct.ReturnCodec, et.MsgRct.Return),
)
}
// Build an EthTrace for a "call", parsing the inputs & outputs as an EVM call (falling back on
// treating it as a native call).
func traceEVMCall(env *environment, addr []int, et *types.ExecutionTrace) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
input, err := decodePayload(et.Msg.Params, et.Msg.ParamsCodec)
if err != nil {
log.Debugf("failed to decode contract invocation payload: %w", err)
return traceNativeCall(env, addr, et), et, nil
}
output, err := decodePayload(et.MsgRct.Return, et.MsgRct.ReturnCodec)
if err != nil {
log.Debugf("failed to decode contract invocation return: %w", err)
return traceNativeCall(env, addr, et), et, nil
}
return traceCall(env, addr, et, input, output), et, nil
}
// Build an EthTrace for a native "create" operation. This should only be called with an
// ExecutionTrace is an Exec or Exec4 method invocation on the Init actor.
func traceNativeCreate(env *environment, addr []int, et *types.ExecutionTrace) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
if et.Msg.ReadOnly {
// "create" isn't valid in a staticcall, so we just skip this trace
// (couldn't have created an actor anyways).
// This mimic's the EVM: it doesn't trace CREATE calls when in
// read-only mode.
return nil, nil, nil
}
subTrace := find(et.Subcalls, func(c *types.ExecutionTrace) *types.ExecutionTrace {
if c.Msg.Method == builtin.MethodConstructor {
return c
}
return nil
})
if subTrace == nil {
// If we succeed in calling Exec/Exec4 but don't even try to construct
// something, we have a bug in our tracing logic or a mismatch between our
// tracing logic and the actors.
if et.MsgRct.ExitCode.IsSuccess() {
return nil, nil, xerrors.Errorf("successful Exec/Exec4 call failed to call a constructor")
}
// Otherwise, this can happen if creation fails early (bad params,
// out of gas, contract already exists, etc.). The EVM wouldn't
// trace such cases, so we don't either.
//
// NOTE: It's actually impossible to run out of gas before calling
// initcode in the EVM (without running out of gas in the calling
// contract), but this is an equivalent edge-case to InvokedActor
// being nil, so we treat it the same way and skip the entire
// operation.
return nil, nil, nil
}
// Native actors that aren't the EAM can attempt to call Exec4, but such
// call should fail immediately without ever attempting to construct an
// actor. I'm catching this here because it likely means that there's a bug
// in our trace-conversion logic.
if et.Msg.Method == builtin.MethodsInit.Exec4 {
return nil, nil, xerrors.Errorf("direct call to Exec4 successfully called a constructor!")
}
var output ethtypes.EthBytes
var createdAddr *ethtypes.EthAddress
if et.MsgRct.ExitCode.IsSuccess() {
// We're supposed to put the "installed bytecode" here. But this
// isn't an EVM actor, so we just put some invalid bytecode (this is
// the answer you'd get if you called EXTCODECOPY on a native
// non-account actor, anyways).
output = []byte{0xFE}
// Extract the address of the created actor from the return value.
initReturn, err := decodeReturn[init12.ExecReturn](&et.MsgRct)
if err != nil {
return nil, nil, xerrors.Errorf("failed to decode init params after a successful Init.Exec call: %w", err)
}
actorId, err := address.IDFromAddress(initReturn.IDAddress)
if err != nil {
return nil, nil, xerrors.Errorf("failed to extract created actor ID from address: %w", err)
}
ethAddr := ethtypes.EthAddressFromActorID(abi.ActorID(actorId))
createdAddr = &ethAddr
}
return &ethtypes.EthTrace{
Type: "create",
Action: &ethtypes.EthCreateTraceAction{
From: env.caller,
Gas: ethtypes.EthUint64(et.Msg.GasLimit),
Value: ethtypes.EthBigInt(et.Msg.Value),
// If we get here, this isn't a native EVM create. Those always go through
// the EAM. So we have no "real" initcode and must use the sentinel value
// for "invalid" initcode.
Init: []byte{0xFE},
},
Result: &ethtypes.EthCreateTraceResult{
GasUsed: ethtypes.EthUint64(et.SumGas().TotalGas),
Address: createdAddr,
Code: output,
},
TraceAddress: addr,
Error: traceErrMsg(et),
}, subTrace, nil
}
// Assert that these are all identical so we can simplify the below code and decode once.
var _ *eam12.Return = (*eam12.Return)((*eam12.CreateReturn)(nil))
var _ *eam12.Return = (*eam12.Return)((*eam12.Create2Return)(nil))
var _ *eam12.Return = (*eam12.Return)((*eam12.CreateExternalReturn)(nil))
// Decode the parameters and return value of an EVM smart contract creation through the EAM. This
// should only be called with an ExecutionTrace for a Create, Create2, or CreateExternal method
// invocation on the EAM.
func decodeCreateViaEAM(et *types.ExecutionTrace) (initcode []byte, addr *ethtypes.EthAddress, err error) {
switch et.Msg.Method {
case builtin.MethodsEAM.Create:
params, err := decodeParams[eam12.CreateParams](&et.Msg)
if err != nil {
return nil, nil, err
}
initcode = params.Initcode
case builtin.MethodsEAM.Create2:
params, err := decodeParams[eam12.Create2Params](&et.Msg)
if err != nil {
return nil, nil, err
}
initcode = params.Initcode
case builtin.MethodsEAM.CreateExternal:
input, err := decodePayload(et.Msg.Params, et.Msg.ParamsCodec)
if err != nil {
return nil, nil, err
}
initcode = input
default:
return nil, nil, xerrors.Errorf("unexpected CREATE method %d", et.Msg.Method)
}
ret, err := decodeReturn[eam12.CreateReturn](&et.MsgRct)
if err != nil {
return nil, (*ethtypes.EthAddress)(&ret.EthAddress), err
}
return initcode, (*ethtypes.EthAddress)(&ret.EthAddress), nil
}
// 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) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
// Same as the Init actor case above, see the comment there.
if et.Msg.ReadOnly {
return nil, nil, nil
}
// Look for a call to either a constructor or the EVM's resurrect method.
subTrace := find(et.Subcalls, func(et *types.ExecutionTrace) *types.ExecutionTrace {
if et.Msg.To == builtinactors.InitActorAddr {
return find(et.Subcalls, func(et *types.ExecutionTrace) *types.ExecutionTrace {
if et.Msg.Method == builtinactors.MethodConstructor {
return et
}
return nil
})
}
if et.Msg.Method == builtin.MethodsEVM.Resurrect {
return et
}
return nil
})
// Same as the Init actor case above, see the comment there.
if subTrace == nil {
if et.MsgRct.ExitCode.IsSuccess() {
return nil, nil, xerrors.Errorf("successful Create/Create2 call failed to call a constructor")
}
return nil, nil, nil
}
// Decode inputs & determine create type.
initcode, createdAddr, err := decodeCreateViaEAM(et)
if err != nil {
return nil, nil, xerrors.Errorf("EAM called with invalid params or returned an invalid result, but it still tried to construct the contract: %w", err)
}
var output ethtypes.EthBytes
// Handle the output.
switch et.MsgRct.ExitCode {
case 0: // success
// We're _supposed_ to include the contracts bytecode here, but we
// can't do that reliably (e.g., if some part of the trace reverts).
// So we don't try and include a sentinel "impossible bytecode"
// value (the value specified by EIP-3541).
output = []byte{0xFE}
case 33: // Reverted, parse the revert message.
// If we managed to call the constructor, parse/return its revert message. If we
// fail, we just return no output.
output, _ = decodePayload(subTrace.MsgRct.Return, subTrace.MsgRct.ReturnCodec)
}
return &ethtypes.EthTrace{
Type: "create",
Action: &ethtypes.EthCreateTraceAction{
From: env.caller,
Gas: ethtypes.EthUint64(et.Msg.GasLimit),
Value: ethtypes.EthBigInt(et.Msg.Value),
Init: initcode,
},
Result: &ethtypes.EthCreateTraceResult{
GasUsed: ethtypes.EthUint64(et.SumGas().TotalGas),
Address: createdAddr,
Code: output,
},
TraceAddress: addr,
Error: traceErrMsg(et),
}, subTrace, nil
}
// Build an EthTrace for a "private" method invocation from the EVM. This should only be called with
// an ExecutionTrace from an EVM instance and on a method between 1 and 1023 inclusive.
func traceEVMPrivate(env *environment, addr []int, et *types.ExecutionTrace) (*ethtypes.EthTrace, *types.ExecutionTrace, error) {
// The EVM actor implements DELEGATECALL by:
//
// 1. Asking the callee for its bytecode by calling it on the GetBytecode method.
// 2. Recursively invoking the currently executing contract on the
// InvokeContractDelegate method.
//
// The code below "reconstructs" that delegate call by:
//
// 1. Remembering the last contract on which we called GetBytecode.
// 2. Treating the contract invoked in step 1 as the DELEGATECALL receiver.
//
// Note, however: GetBytecode will be called, e.g., if the user invokes the
// EXTCODECOPY instruction. It's not an error to see multiple GetBytecode calls
// before we see an InvokeContractDelegate.
switch et.Msg.Method {
case builtin.MethodsEVM.GetBytecode:
// NOTE: I'm not checking anything about the receiver here. The EVM won't
// DELEGATECALL any non-EVM actor, but there's no need to encode that fact
// here in case we decide to loosen this up in the future.
if et.MsgRct.ExitCode.IsSuccess() {
to := traceToAddress(et.InvokedActor)
env.lastByteCode = &to
} else {
env.lastByteCode = nil
}
return nil, nil, nil
case builtin.MethodsEVM.InvokeContractDelegate:
// NOTE: We return errors in all the failure cases below instead of trying
// to continue because the caller is an EVM actor. If something goes wrong
// here, there's a bug in our EVM implementation.
// Handle delegate calls
//
// 1) Look for trace from an EVM actor to itself on InvokeContractDelegate,
// method 6.
// 2) Check that the previous trace calls another actor on method 3
// (GetByteCode) and they are at the same level (same parent)
// 3) Treat this as a delegate call to actor A.
if env.lastByteCode == nil {
return nil, nil, xerrors.Errorf("unknown bytecode for delegate call")
}
if to := traceToAddress(et.InvokedActor); env.caller != to {
return nil, nil, xerrors.Errorf("delegate-call not from & to self: %s != %s", env.caller, to)
}
dp, err := decodeParams[evm12.DelegateCallParams](&et.Msg)
if err != nil {
return nil, nil, xerrors.Errorf("failed to decode delegate-call params: %w", err)
}
output, err := decodePayload(et.MsgRct.Return, et.MsgRct.ReturnCodec)
if err != nil {
return nil, nil, xerrors.Errorf("failed to decode delegate-call return: %w", err)
}
return &ethtypes.EthTrace{
Type: "call",
Action: &ethtypes.EthCallTraceAction{
CallType: "delegatecall",
From: env.caller,
To: *env.lastByteCode,
Gas: ethtypes.EthUint64(et.Msg.GasLimit),
Value: ethtypes.EthBigInt(et.Msg.Value),
Input: dp.Input,
},
Result: &ethtypes.EthCallTraceResult{
GasUsed: ethtypes.EthUint64(et.SumGas().TotalGas),
Output: output,
},
TraceAddress: addr,
Error: traceErrMsg(et),
}, et, nil
}
// We drop all other "private" calls from FEVM. We _forbid_ explicit calls between 0 and
// 1024 (exclusive), so any calls in this range must be implementation details.
return nil, nil, nil
}

View File

@ -382,34 +382,35 @@ func parseEthRevert(ret []byte) string {
// 3. Otherwise, we fall back to returning a masked ID Ethereum address. If the supplied address is an f0 address, we
// use that ID to form the masked ID address.
// 4. Otherwise, we fetch the actor's ID from the state tree and form the masked ID with it.
//
// If the actor doesn't exist in the state-tree but we have its ID, we use a masked ID address. It could have been deleted.
func lookupEthAddress(addr address.Address, st *state.StateTree) (ethtypes.EthAddress, error) {
// BLOCK A: We are trying to get an actual Ethereum address from an f410 address.
// Attempt to convert directly, if it's an f4 address.
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(addr)
if err == nil && !ethAddr.IsMaskedID() {
return ethAddr, nil
}
// Lookup on the target actor and try to get an f410 address.
if actor, err := st.GetActor(addr); err != nil {
return ethtypes.EthAddress{}, err
} else if actor.Address != nil {
if ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address); err == nil && !ethAddr.IsMaskedID() {
return ethAddr, nil
}
}
// BLOCK B: We gave up on getting an actual Ethereum address and are falling back to a Masked ID address.
// Check if we already have an ID addr, and use it if possible.
if err == nil && ethAddr.IsMaskedID() {
return ethAddr, nil
}
// Otherwise, resolve the ID addr.
idAddr, err := st.LookupID(addr)
if err != nil {
return ethtypes.EthAddress{}, err
}
// Lookup on the target actor and try to get an f410 address.
if actor, err := st.GetActor(idAddr); errors.Is(err, types.ErrActorNotFound) {
// Not found -> use a masked ID address
} else if err != nil {
// Any other error -> fail.
return ethtypes.EthAddress{}, err
} else if actor.Address == nil {
// No delegated address -> use masked ID address.
} else if ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address); err == nil && !ethAddr.IsMaskedID() {
// Conversable into an eth address, use it.
return ethAddr, nil
}
// Otherwise, use the masked address.
return ethtypes.EthAddressFromFilecoinAddress(idAddr)
}

View File

@ -1,17 +1,22 @@
#!/usr/bin/env bash
sed_replace='/import (/ {
:1
$!N
s/\n\n/\'$'\n''/
/)/!b1
}'
go_files() {
find . -type f -name \*.go -not -name \*_cbor_gen.go | grep -v './extern/filecoin-ffi' | grep -v './extern/test-vectors'
}
go_files | xargs -I '{}' sed -i.ifixbak -e '/import (/ {
:1
$!N
s/\n\n/\'$'\n''/
/)/!b1
}' '{}'
find . -type f -name \*.go.ifixbak | xargs rm
# Because -i works differently on macOS, we need to use a different sed command
if [[ "$OSTYPE" == "darwin"* ]]; then
go_files | xargs -I '{}' sed -i '' -e "$sed_replace" '{}'
else
go_files | xargs -I '{}' sed -i -e "$sed_replace" '{}'
fi
go_files | xargs -I '{}' goimports -w -local "github.com/filecoin-project" '{}'
go_files | xargs -I '{}' goimports -w -local "github.com/filecoin-project/lotus" '{}'