Skip to content

Commit 917e3db

Browse files
author
vitaliyb
committed
Fix fake tx tracing
1 parent 8a1ba76 commit 917e3db

File tree

5 files changed

+93
-14
lines changed

5 files changed

+93
-14
lines changed

packages/evm/evmutil/tx.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package evmutil
2+
3+
import (
4+
"slices"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/ethereum/go-ethereum/core/types"
8+
)
9+
10+
func IsFakeTransaction(tx *types.Transaction) bool {
11+
sender, err := GetSender(tx)
12+
13+
// the error will fire when the transaction is invalid. This is most of the time a fake evm tx we use for internal calls, therefore it's fine to assume both.
14+
if slices.Equal(sender.Bytes(), common.Address{}.Bytes()) || err != nil {
15+
return true
16+
}
17+
18+
return false
19+
}

packages/evm/jsonrpc/evmchain.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ func (e *EVMChain) traceTransaction(
699699
BlockNumber: new(big.Int).SetUint64(blockNumber),
700700
TxIndex: int(txIndex),
701701
TxHash: tx.Hash(),
702-
}, config.TracerConfig, false)
702+
}, config.TracerConfig, false, nil)
703703
if err != nil {
704704
return nil, err
705705
}
@@ -736,18 +736,22 @@ func (e *EVMChain) debugTraceBlock(config *tracers.TraceConfig, block *types.Blo
736736

737737
blockNumber := uint64(iscBlock.BlockIndex())
738738

739+
blockTxs := block.Transactions()
740+
fakeTxs := make([]*types.Transaction, 0, len(blockTxs))
741+
for _, tx := range blockTxs {
742+
if e.isFakeTransaction(tx) {
743+
fakeTxs = append(fakeTxs, tx)
744+
}
745+
}
746+
739747
tracer, err := newTracer(tracerType, &tracers.Context{
740748
BlockHash: block.Hash(),
741749
BlockNumber: new(big.Int).SetUint64(blockNumber),
742-
}, config.TracerConfig, true)
750+
}, config.TracerConfig, true, types.Transactions(fakeTxs))
743751
if err != nil {
744752
return nil, err
745753
}
746754

747-
// if e.isFakeTransaction(tx) {
748-
// return tracer.TraceFakeTx(tx)
749-
// }
750-
751755
err = e.backend.EVMTrace(
752756
iscBlock.PreviousAliasOutput,
753757
iscBlock.Timestamp,

packages/evm/jsonrpc/tracer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ type Tracer struct {
1313
TraceFakeTx func(tx *types.Transaction) (json.RawMessage, error)
1414
}
1515

16-
type tracerFactory func(traceCtx *tracers.Context, cfg json.RawMessage, traceBlock bool) (*Tracer, error)
16+
type tracerFactory func(traceCtx *tracers.Context, cfg json.RawMessage, traceBlock bool, initValue any) (*Tracer, error)
1717

1818
var allTracers = map[string]tracerFactory{}
1919

2020
func registerTracer(tracerType string, fn tracerFactory) {
2121
allTracers[tracerType] = fn
2222
}
2323

24-
func newTracer(tracerType string, ctx *tracers.Context, cfg json.RawMessage, traceBlock bool) (*Tracer, error) {
24+
func newTracer(tracerType string, ctx *tracers.Context, cfg json.RawMessage, traceBlock bool, initValue any) (*Tracer, error) {
2525
fn := allTracers[tracerType]
2626
if fn == nil {
2727
return nil, fmt.Errorf("unsupported tracer type: %s", tracerType)
2828
}
29-
return fn(ctx, cfg, traceBlock)
29+
return fn(ctx, cfg, traceBlock, initValue)
3030
}

packages/evm/jsonrpc/tracer_call.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package jsonrpc
66
import (
77
"encoding/json"
88
"errors"
9+
"fmt"
910
"math/big"
1011
"strings"
1112
"sync/atomic"
@@ -127,6 +128,7 @@ type callTracer struct {
127128
interrupt atomic.Bool // Atomic flag to signal execution interruption
128129
reason error // Textual reason for the interruption
129130
traceBlock bool
131+
fakeTxs []*types.Transaction
130132
}
131133

132134
type callTracerConfig struct {
@@ -136,8 +138,21 @@ type callTracerConfig struct {
136138

137139
// newCallTracer returns a native go tracer which tracks
138140
// call frames of a tx, and implements vm.EVMLogger.
139-
func newCallTracer(ctx *tracers.Context, cfg json.RawMessage, traceBlock bool) (*Tracer, error) {
140-
t, err := newCallTracerObject(ctx, cfg, traceBlock)
141+
func newCallTracer(ctx *tracers.Context, cfg json.RawMessage, traceBlock bool, initValue any) (*Tracer, error) {
142+
var fakeTxs types.Transactions
143+
144+
if initValue == nil && traceBlock {
145+
return nil, fmt.Errorf("initValue with block transactions is required for block tracing")
146+
}
147+
148+
if initValue != nil {
149+
var ok bool
150+
fakeTxs, ok = initValue.(types.Transactions)
151+
if !ok {
152+
return nil, fmt.Errorf("invalid init value type for calltracer: %T", initValue)
153+
}
154+
}
155+
t, err := newCallTracerObject(ctx, cfg, traceBlock, fakeTxs)
141156
if err != nil {
142157
return nil, err
143158
}
@@ -157,7 +172,7 @@ func newCallTracer(ctx *tracers.Context, cfg json.RawMessage, traceBlock bool) (
157172
}, nil
158173
}
159174

160-
func newCallTracerObject(_ *tracers.Context, cfg json.RawMessage, traceBlock bool) (*callTracer, error) {
175+
func newCallTracerObject(_ *tracers.Context, cfg json.RawMessage, traceBlock bool, fakeTxs []*types.Transaction) (*callTracer, error) {
161176
var config callTracerConfig
162177
if cfg != nil {
163178
if err := json.Unmarshal(cfg, &config); err != nil {
@@ -166,7 +181,7 @@ func newCallTracerObject(_ *tracers.Context, cfg json.RawMessage, traceBlock boo
166181
}
167182
// First callframe contains tx context info
168183
// and is populated on start and end.
169-
return &callTracer{txToStack: make(map[common.Hash][]CallFrame), currentTx: common.Hash{}, config: config, traceBlock: traceBlock}, nil
184+
return &callTracer{txToStack: make(map[common.Hash][]CallFrame), currentTx: common.Hash{}, config: config, traceBlock: traceBlock, fakeTxs: fakeTxs}, nil
170185
}
171186

172187
// OnEnter is called when EVM enters a new scope (via call, create or selfdestruct).
@@ -290,6 +305,14 @@ func (t *callTracer) GetResult() (json.RawMessage, error) {
290305
})
291306
}
292307

308+
for _, tx := range t.fakeTxs {
309+
csJSON, err := t.TraceFakeTx(tx)
310+
if err != nil {
311+
return nil, err
312+
}
313+
results = append(results, TxTraceResult{TxHash: tx.Hash(), Result: csJSON})
314+
}
315+
293316
res, err := json.Marshal(results)
294317
if err != nil {
295318
return nil, err

packages/evm/jsonrpc/tracer_prestate.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package jsonrpc
66
import (
77
"bytes"
88
"encoding/json"
9+
"fmt"
910
"sync/atomic"
1011

1112
"github.com/ethereum/go-ethereum/common"
@@ -57,13 +58,27 @@ type prestateTracer struct {
5758
interrupt atomic.Bool // Atomic flag to signal execution interruption
5859
reason error // Textual reason for the interruption
5960
traceBlock bool
61+
fakeTxs types.Transactions
6062
}
6163

6264
type prestateTracerConfig struct {
6365
DiffMode bool `json:"diffMode"` // If true, this tracer will return state modifications
6466
}
6567

66-
func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage, traceBlock bool) (*Tracer, error) {
68+
func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage, traceBlock bool, initValue any) (*Tracer, error) {
69+
var fakeTxs types.Transactions
70+
71+
if initValue == nil && traceBlock {
72+
return nil, fmt.Errorf("initValue with block transactions is required for block tracing")
73+
}
74+
75+
if initValue != nil {
76+
var ok bool
77+
fakeTxs, ok = initValue.(types.Transactions)
78+
if !ok {
79+
return nil, fmt.Errorf("invalid init value type for prestateTracer: %T", initValue)
80+
}
81+
}
6782
var config prestateTracerConfig
6883
if err := json.Unmarshal(cfg, &config); err != nil {
6984
return nil, err
@@ -72,6 +87,7 @@ func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage, traceBlock boo
7287
config: config,
7388
traceBlock: traceBlock,
7489
states: make(map[common.Hash]*PrestateTxValue),
90+
fakeTxs: fakeTxs,
7591
}
7692
return &Tracer{
7793
Tracer: &tracers.Tracer{
@@ -192,6 +208,14 @@ func (t *prestateTracer) GetResult() (json.RawMessage, error) {
192208
return nil, err
193209
}
194210
result = append(result, TxTraceResult{TxHash: txHash, Result: diffResult})
211+
212+
for _, tx := range t.fakeTxs {
213+
csJSON, err := t.TraceFakeTx(tx)
214+
if err != nil {
215+
return nil, err
216+
}
217+
result = append(result, TxTraceResult{TxHash: tx.Hash(), Result: csJSON})
218+
}
195219
}
196220
res, err = json.Marshal(result)
197221
} else {
@@ -203,6 +227,15 @@ func (t *prestateTracer) GetResult() (json.RawMessage, error) {
203227
}
204228
result = append(result, TxTraceResult{TxHash: txHash, Result: preState})
205229
}
230+
231+
for _, tx := range t.fakeTxs {
232+
csJSON, err := t.TraceFakeTx(tx)
233+
if err != nil {
234+
return nil, err
235+
}
236+
result = append(result, TxTraceResult{TxHash: tx.Hash(), Result: csJSON})
237+
}
238+
206239
res, err = json.Marshal(result)
207240
}
208241
} else {

0 commit comments

Comments
 (0)