|
| 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: ðtypes.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: ðtypes.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 := ðtypes.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 | +} |
0 commit comments