Skip to content

Commit fac9595

Browse files
Trace txs that fail before execution as failed txs on release/v6.7 (PLT-1604) (#4866)
This is the v6.7 counterpart of #4865. `debug_traceBlockByNumber` and `debug_traceTransaction` return an error for a transaction that Sei included in a block but that failed before execution ([PLT-1604](https://linear.app/seilabs/issue/PLT-1604/return-prestatetracer-results-for-txs-that-fail-fee-precheck)). This is the provider report on pacific-1 block `0xe79434e`, and it reproduces on v6.7.4. `prestateTracer`, `4byteTracer`, `muxTracer` and the default struct logger fail with the precheck error. A block trace drops the sender's nonce bump, so later transactions of the same sender fail. The call tracers report zero gas for a failure the chain charged the gas limit for. The fork fix is [sei-protocol/go-ethereum#108](sei-protocol/go-ethereum#108), tagged as `v1.15.7-sei-20-hf`. That is the change from [#107](sei-protocol/go-ethereum#107) cherry-picked onto `v1.15.7-sei-20`, so v6.7 takes only this tracer fix and not the rest of `v1.15.7-sei-21`. `traceTx` now ends such a transaction as the failed transaction the chain included. Its top-level call fails with the error, the nonce is bumped through the hooked state, and the receipt carries the gas the chain charged. Every tracer then returns its result. The call tracers keep their synthetic frame with the charged gas, and `debug_traceCall` still reports the error. As on v6.8, `profiledTraceTx` and the parallel state advancement in `profiledTraceBlockParallel` call the same fork helpers. `block_trace_profiled.go` is identical on both branches, so its change is the same. Only trace output changes, and historical traces of affected transactions change from an error to the correct result. `traceDBFormatVersion` goes to 2, so a node discards the traces it baked before this change the first time it opens the trace cache with this build, instead of serving the old error. `TestTraceTxFailingBeforeExecution` fails against `v1.15.7-sei-20` with the production error and passes with the fix, and `./evmrpc/...` passes. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b2e33a2 commit fac9595

6 files changed

Lines changed: 169 additions & 73 deletions

File tree

‎evmrpc/block_trace_profiled.go‎

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
gethcommon "github.com/ethereum/go-ethereum/common"
14-
"github.com/ethereum/go-ethereum/common/hexutil"
1514
"github.com/ethereum/go-ethereum/core"
1615
gethstate "github.com/ethereum/go-ethereum/core/state"
1716
gethtracing "github.com/ethereum/go-ethereum/core/tracing"
@@ -236,7 +235,10 @@ func (api *DebugAPI) profiledTraceBlockParallel(
236235
return err
237236
}
238237
if _, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit)); err != nil {
239-
return err
238+
txctx := &tracers.Context{BlockHash: blockHash, BlockNumber: block.Number(), TxIndex: i, TxHash: tx.Hash()}
239+
if _, ended := tracers.EndFailedTx(nil, statedb, tx, msg, txctx, err); !ended {
240+
return err
241+
}
240242
}
241243
statedb.Finalise(evm.ChainConfig().IsEIP158(block.Number()))
242244
return nil
@@ -376,7 +378,7 @@ func (api *DebugAPI) profiledTraceTx(
376378
tracingStateDB := gethstate.NewHookedState(statedb, tracer.Hooks)
377379
tracerMtx = &sync.Mutex{}
378380
txContext := core.NewEVMTxContext(message)
379-
evm := vm.NewEVM(vmctx, tracingStateDB, api.backend.ChainConfigAtHeight(vmctx.BlockNumber.Int64()), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true}, api.backend.GetCustomPrecompiles(vmctx.BlockNumber.Int64()))
381+
evm := vm.NewEVM(vmctx, tracingStateDB, api.backend.ChainConfigAtHeight(vmctx.BlockNumber.Int64()), vm.Config{Tracer: tracers.WithoutFailedTxEnd(tracer.Hooks), NoBaseFee: true}, api.backend.GetCustomPrecompiles(vmctx.BlockNumber.Int64()))
380382
if precompiles != nil {
381383
evm.SetPrecompiles(precompiles)
382384
}
@@ -419,7 +421,13 @@ func (api *DebugAPI) profiledTraceTx(
419421
phaseDurations.ExecutionNanos = time.Since(executionStart).Nanoseconds()
420422
}
421423
if err != nil {
422-
return profiledErrorTrace(err, tx, message, txctx, vmctx, config)
424+
gasUsed, ended := tracers.EndFailedTx(tracer.Hooks, tracingStateDB, tx, message, txctx, err)
425+
if !ended {
426+
return profiledErrorTrace(err, tx, message, txctx, vmctx, config)
427+
}
428+
if frame, frameErr := tracers.ErrorFrame(err, tx, message, txctx, config, gasUsed); frame != nil || frameErr != nil {
429+
return frame, frameErr
430+
}
423431
}
424432
resultStart := time.Now()
425433
tracerMtx.Lock()
@@ -435,69 +443,8 @@ func (api *DebugAPI) profiledTraceTx(
435443
}
436444

437445
func profiledErrorTrace(err error, tx *gethtypes.Transaction, message *core.Message, txctx *tracers.Context, vmctx vm.BlockContext, config *tracers.TraceConfig) (value interface{}, returnErr error) {
438-
if config != nil && config.Tracer != nil {
439-
switch *config.Tracer {
440-
case "callTracer":
441-
errTrace := map[string]interface{}{
442-
"from": message.From.Hex(),
443-
"gas": hexutil.Uint64(message.GasLimit),
444-
"gasUsed": "0x0",
445-
"input": "0x",
446-
"error": err.Error(),
447-
"type": "CALL",
448-
}
449-
if message.Value != nil {
450-
errTrace["value"] = hexutil.Big(*message.Value)
451-
}
452-
if message.To != nil {
453-
errTrace["to"] = message.To.Hex()
454-
} else {
455-
errTrace["type"] = "CREATE"
456-
}
457-
if message.Data != nil {
458-
errTrace["input"] = hexutil.Encode(message.Data)
459-
}
460-
bz, marshalErr := json.Marshal(errTrace)
461-
if marshalErr != nil {
462-
return nil, fmt.Errorf("tracing failed: %w", marshalErr)
463-
}
464-
return json.RawMessage(bz), nil
465-
case "flatCallTracer":
466-
action := map[string]interface{}{
467-
"callType": "call",
468-
"from": message.From.Hex(),
469-
"gas": hexutil.Uint64(message.GasLimit),
470-
"input": "0x",
471-
}
472-
if message.Value != nil {
473-
action["value"] = hexutil.Big(*message.Value)
474-
}
475-
if message.To != nil {
476-
action["to"] = message.To.Hex()
477-
}
478-
if message.Data != nil {
479-
action["input"] = hexutil.Encode(message.Data)
480-
}
481-
errTrace := map[string]interface{}{
482-
"action": action,
483-
"blockHash": txctx.BlockHash,
484-
"blockNumber": txctx.BlockNumber,
485-
"result": map[string]interface{}{
486-
"gasUsed": "0x0",
487-
"output": "0x",
488-
},
489-
"subtraces": 0,
490-
"traceAddress": []string{},
491-
"transactionHash": tx.Hash(),
492-
"transactionPosition": txctx.TxIndex,
493-
"error": err.Error(),
494-
}
495-
bz, marshalErr := json.Marshal([]map[string]interface{}{errTrace})
496-
if marshalErr != nil {
497-
return nil, fmt.Errorf("tracing failed: %w", marshalErr)
498-
}
499-
return json.RawMessage(bz), nil
500-
}
446+
if frame, frameErr := tracers.ErrorFrame(err, tx, message, txctx, config, 0); frame != nil || frameErr != nil {
447+
return frame, frameErr
501448
}
502449
if strings.Contains(err.Error(), core.ErrInsufficientFunds.Error()) {
503450
return json.RawMessage(`{}`), nil
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package tests
2+
3+
import (
4+
"bytes"
5+
"math/big"
6+
"strings"
7+
"testing"
8+
9+
"github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/common/hexutil"
11+
ethtypes "github.com/ethereum/go-ethereum/core/types"
12+
evmrpcconfig "github.com/sei-protocol/sei-chain/evmrpc/config"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
// failedTxTracers are the tracer configurations a block transaction that fails before
17+
// execution is traced with.
18+
var failedTxTracers = map[string]map[string]interface{}{
19+
"structLogger": {},
20+
"callTracer": {"tracer": "callTracer"},
21+
"flatCallTracer": {"tracer": "flatCallTracer"},
22+
"prestateTracer": {"tracer": "prestateTracer"},
23+
"prestateTracer diffMode": {"tracer": "prestateTracer", "tracerConfig": map[string]interface{}{"diffMode": true}},
24+
"4byteTracer": {"tracer": "4byteTracer"},
25+
"noopTracer": {"tracer": "noopTracer"},
26+
"muxTracer": {"tracer": "muxTracer", "tracerConfig": map[string]interface{}{
27+
"callTracer": map[string]interface{}{},
28+
"prestateTracer": map[string]interface{}{"diffMode": true},
29+
"4byteTracer": map[string]interface{}{},
30+
}},
31+
}
32+
33+
// TestTraceTxFailingBeforeExecution traces a block whose first transaction fails before
34+
// execution, followed by a transaction of the same sender. The chain keeps the failed
35+
// transaction with its sender's nonce bumped, so every tracer has to report it as a
36+
// failed transaction and trace the next one on top of the bumped nonce.
37+
func TestTraceTxFailingBeforeExecution(t *testing.T) {
38+
sender := getAddrWithMnemonic(mnemonic1)
39+
recipient := common.HexToAddress("0x00000000000000000000000000000000000a0001")
40+
for _, tc := range []struct {
41+
name string
42+
failed *ethtypes.DynamicFeeTx
43+
txErr string
44+
gasUsed uint64
45+
}{
46+
{
47+
// Fails in the ante handler, which charges nothing.
48+
name: "fee cap below base fee",
49+
failed: &ethtypes.DynamicFeeTx{Nonce: 0, GasFeeCap: big.NewInt(999999999), Gas: 21000, To: &recipient, Value: big.NewInt(2000), ChainID: chainId},
50+
txErr: "max fee per gas less than block base fee",
51+
},
52+
{
53+
// Fails after the ante handler charged the gas limit: 100 non-zero bytes of
54+
// calldata cost 22600 gas of intrinsic gas, but 25000 gas of EIP-7623 floor.
55+
name: "gas limit below floor data gas",
56+
failed: &ethtypes.DynamicFeeTx{Nonce: 0, GasFeeCap: big.NewInt(1000000000), Gas: 22600, To: &recipient, Value: big.NewInt(2000), Data: bytes.Repeat([]byte{1}, 100), ChainID: chainId},
57+
txErr: "insufficient gas for floor data gas cost: have 22600, want 25000",
58+
gasUsed: 22600,
59+
},
60+
} {
61+
t.Run(tc.name, func(t *testing.T) {
62+
next := &ethtypes.DynamicFeeTx{Nonce: 1, GasFeeCap: big.NewInt(1000000000), Gas: 21000, To: &recipient, Value: big.NewInt(2000), ChainID: chainId}
63+
failedTx := signTxWithMnemonic(tc.failed, mnemonic1)
64+
nextTx := signTxWithMnemonic(next, mnemonic1)
65+
blocks := [][][]byte{{encodeEvmTx(tc.failed, failedTx), encodeEvmTx(next, nextTx)}}
66+
SetupTestServer(t, blocks, mnemonicInitializer(mnemonic1)).Run(func(port int) {
67+
receipt := sendRequestWithNamespace("eth", port, "getTransactionReceipt", failedTx.Hash().Hex())["result"].(map[string]interface{})
68+
require.Equal(t, "0x0", receipt["status"])
69+
require.Equal(t, hexutil.Uint64(tc.gasUsed).String(), receipt["gasUsed"])
70+
nextReceipt := sendRequestWithNamespace("eth", port, "getTransactionReceipt", nextTx.Hash().Hex())["result"].(map[string]interface{})
71+
require.Equal(t, "0x1", nextReceipt["status"])
72+
require.Equal(t, "0x2", sendRequestWithNamespace("eth", port, "getTransactionCount", sender.Hex(), "0x2")["result"])
73+
74+
for name, config := range failedTxTracers {
75+
requireBlockTraceMatchesTxTraces(t, port, name, config, failedTx, nextTx)
76+
}
77+
78+
trace := func(config map[string]interface{}) map[string]interface{} {
79+
return sendRequestWithNamespace("debug", port, "traceTransaction", failedTx.Hash().Hex(), config)["result"].(map[string]interface{})
80+
}
81+
structLog := trace(failedTxTracers["structLogger"])
82+
require.Equal(t, true, structLog["failed"])
83+
require.Equal(t, float64(tc.gasUsed), structLog["gas"])
84+
call := trace(failedTxTracers["callTracer"])
85+
require.Equal(t, hexutil.Uint64(tc.gasUsed).String(), call["gasUsed"])
86+
require.Contains(t, call["error"], tc.txErr)
87+
88+
// The diff matches what the chain did to the sender: the nonce is bumped,
89+
// and the balance drops by the gas the receipt charges.
90+
diff := trace(failedTxTracers["prestateTracer diffMode"])
91+
senderKey := strings.ToLower(sender.Hex())
92+
pre := diff["pre"].(map[string]interface{})[senderKey].(map[string]interface{})
93+
post := diff["post"].(map[string]interface{})[senderKey].(map[string]interface{})
94+
require.Nil(t, pre["nonce"])
95+
require.Equal(t, float64(1), post["nonce"])
96+
price, err := hexutil.DecodeBig(receipt["effectiveGasPrice"].(string))
97+
require.NoError(t, err)
98+
charged := new(big.Int).Mul(new(big.Int).SetUint64(tc.gasUsed), price)
99+
if charged.Sign() == 0 {
100+
require.Nil(t, post["balance"])
101+
} else {
102+
preBalance, err := hexutil.DecodeBig(pre["balance"].(string))
103+
require.NoError(t, err)
104+
postBalance, err := hexutil.DecodeBig(post["balance"].(string))
105+
require.NoError(t, err)
106+
require.Equal(t, charged, new(big.Int).Sub(preBalance, postBalance))
107+
}
108+
})
109+
110+
parallelized := func(cfg *evmrpcconfig.Config) { cfg.EnableParallelizedBlockTrace = true }
111+
SetupTestServerWithConfig(t, parallelized, blocks, mnemonicInitializer(mnemonic1)).Run(func(port int) {
112+
requireBlockTraceMatchesTxTraces(t, port, "parallelized structLogger", failedTxTracers["structLogger"], failedTx, nextTx)
113+
})
114+
})
115+
}
116+
}
117+
118+
// requireBlockTraceMatchesTxTraces requires the trace of block 2, which holds txs, to
119+
// trace every transaction without error and as tracing it alone does.
120+
func requireBlockTraceMatchesTxTraces(t *testing.T, port int, name string, config map[string]interface{}, txs ...*ethtypes.Transaction) {
121+
t.Helper()
122+
res := sendRequestWithNamespace("debug", port, "traceBlockByNumber", "0x2", config)
123+
require.Nil(t, res["error"], name)
124+
results := res["result"].([]interface{})
125+
require.Len(t, results, len(txs), name)
126+
for i, tx := range txs {
127+
blockResult := results[i].(map[string]interface{})
128+
require.Equal(t, tx.Hash().Hex(), blockResult["txHash"], name)
129+
require.Nil(t, blockResult["error"], "%s: transaction %d", name, i)
130+
single := sendRequestWithNamespace("debug", port, "traceTransaction", tx.Hash().Hex(), config)
131+
require.Nil(t, single["error"], "%s: transaction %d", name, i)
132+
require.Equal(t, single["result"], blockResult["result"], "%s: transaction %d", name, i)
133+
}
134+
}

‎evmrpc/tests/utils.go‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ func SetupTestServer(
104104
t *testing.T,
105105
blocks [][][]byte,
106106
initializer ...func(sdk.Context, *app.App),
107+
) TestServer {
108+
return SetupTestServerWithConfig(t, nil, blocks, initializer...)
109+
}
110+
111+
// SetupTestServerWithConfig is SetupTestServer with configure applied to the EVM RPC
112+
// config of the server.
113+
func SetupTestServerWithConfig(
114+
t *testing.T,
115+
configure func(*evmrpcconfig.Config),
116+
blocks [][][]byte,
117+
initializer ...func(sdk.Context, *app.App),
107118
) TestServer {
108119
a, res := initializeApp(t, "sei-test", initializer...)
109120
mockClient := &MockClient{blocks: append([][][]byte{{}}, blocks...)}
@@ -129,7 +140,7 @@ func SetupTestServer(
129140
_, _ = a.Commit(context.Background())
130141
mockClient.recordBlockResult(res.TxResults, res.ConsensusParamUpdates, res.Events)
131142
}
132-
return setupTestServer(a, a.RPCContextProvider, mockClient)
143+
return setupTestServer(a, a.RPCContextProvider, mockClient, configure)
133144
}
134145

135146
func SetupMockPacificTestServer(t *testing.T, initializer func(*app.App, *MockClient) sdk.Context) TestServer {
@@ -138,19 +149,23 @@ func SetupMockPacificTestServer(t *testing.T, initializer func(*app.App, *MockCl
138149
// seed mock client with genesis block results so latest height queries work
139150
mockClient.recordBlockResult(res.TxResults, res.ConsensusParamUpdates, res.Events)
140151
ctx := initializer(a, mockClient)
141-
return setupTestServer(a, func(int64) sdk.Context { return ctx }, mockClient)
152+
return setupTestServer(a, func(int64) sdk.Context { return ctx }, mockClient, nil)
142153
}
143154

144155
func setupTestServer(
145156
a *app.App,
146157
ctxProvider func(int64) sdk.Context,
147158
mockClient *MockClient,
159+
configure func(*evmrpcconfig.Config),
148160
) TestServer {
149161
port := int(portProvider.Add(1))
150162
cfg := evmrpcconfig.DefaultConfig
151163
cfg.HTTPEnabled = true
152164
cfg.HTTPPort = port
153165
cfg.EnabledLegacySeiApis = evmrpc.SeiLegacyAllGatedMethodNames()
166+
if configure != nil {
167+
configure(&cfg)
168+
}
154169
s, err := evmrpc.NewEVMHTTPServer(
155170
cfg,
156171
mockClient,

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ replace (
279279
github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
280280
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.23.2
281281
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
282-
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.15.7-sei-20
282+
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.15.7-sei-20-hf
283283
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
284284
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
285285
github.com/keybase/go-keychain => github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,8 +1807,8 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
18071807
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
18081808
github.com/segmentio/kafka-go v0.4.50 h1:mcyC3tT5WeyWzrFbd6O374t+hmcu1NKt2Pu1L3QaXmc=
18091809
github.com/segmentio/kafka-go v0.4.50/go.mod h1:Y1gn60kzLEEaW28YshXyk2+VCUKbJ3Qr6DrnT3i4+9E=
1810-
github.com/sei-protocol/go-ethereum v1.15.7-sei-20 h1:aAYapq4DrxqXMCrfQE/8i6CY6kzePTF1eWgyLgIQ07w=
1811-
github.com/sei-protocol/go-ethereum v1.15.7-sei-20/go.mod h1:+S9k+jFzlyVTNcYGvqFhzN/SFhI6vA+aOY4T5tLSPL0=
1810+
github.com/sei-protocol/go-ethereum v1.15.7-sei-20-hf h1:xo4GiRCRfdUeSS+9JdvwjSsbjH3fYGLDNcEPx4pwMaA=
1811+
github.com/sei-protocol/go-ethereum v1.15.7-sei-20-hf/go.mod h1:+S9k+jFzlyVTNcYGvqFhzN/SFhI6vA+aOY4T5tLSPL0=
18121812
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
18131813
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
18141814
github.com/sei-protocol/sei-config v0.0.21 h1:0VXOz91v9YeM2dmpKi1XaIFy+p3EamDAJtnj02XLKIw=

‎x/evm/keeper/trace_db.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
// rows; NewTraceDB discards rows stored under any other version, and a DB without
3737
// a stamp reads as version 0. Increment it in any change that alters the trace a
3838
// replay of an already committed block produces.
39-
const traceDBFormatVersion uint64 = 1
39+
const traceDBFormatVersion uint64 = 2
4040

4141
// NewTraceDB opens the trace DB at <homeDir>/data/trace_db. When the DB is stamped
4242
// with a format version other than traceDBFormatVersion, it deletes every row,

0 commit comments

Comments
 (0)