Skip to content

Commit 0ec28eb

Browse files
s7v7nislandsholimans1na
authored andcommitted
eth/tracers: use atomic type (ethereum#27031)
Use the new atomic types in package eth/tracers --------- Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
1 parent 0dc4c25 commit 0ec28eb

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

eth/tracers/api_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,8 @@ func TestTraceChain(t *testing.T) {
835835
signer := types.HomesteadSigner{}
836836

837837
var (
838-
ref uint32 // total refs has made
839-
rel uint32 // total rels has made
838+
ref atomic.Uint32 // total refs has made
839+
rel atomic.Uint32 // total rels has made
840840
nonce uint64
841841
)
842842
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
@@ -849,8 +849,8 @@ func TestTraceChain(t *testing.T) {
849849
nonce += 1
850850
}
851851
})
852-
backend.refHook = func() { atomic.AddUint32(&ref, 1) }
853-
backend.relHook = func() { atomic.AddUint32(&rel, 1) }
852+
backend.refHook = func() { ref.Add(1) }
853+
backend.relHook = func() { rel.Add(1) }
854854
api := NewAPI(backend)
855855

856856
single := `{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}`
@@ -863,7 +863,8 @@ func TestTraceChain(t *testing.T) {
863863
{10, 20, nil}, // the middle chain range, blocks [11, 20]
864864
}
865865
for _, c := range cases {
866-
ref, rel = 0, 0 // clean up the counters
866+
ref.Store(0)
867+
rel.Store(0)
867868

868869
from, _ := api.blockByNumber(context.Background(), rpc.BlockNumber(c.start))
869870
to, _ := api.blockByNumber(context.Background(), rpc.BlockNumber(c.end))
@@ -888,8 +889,9 @@ func TestTraceChain(t *testing.T) {
888889
if next != c.end+1 {
889890
t.Error("Missing tracing block")
890891
}
891-
if ref != rel {
892-
t.Errorf("Ref and deref actions are not equal, ref %d rel %d", ref, rel)
892+
893+
if nref, nrel := ref.Load(), rel.Load(); nref != nrel {
894+
t.Errorf("Ref and deref actions are not equal, ref %d rel %d", nref, nrel)
893895
}
894896
}
895897
}

eth/tracers/logger/logger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ type StructLogger struct {
116116
gasLimit uint64
117117
usedGas uint64
118118

119-
interrupt uint32 // Atomic flag to signal execution interruption
120-
reason error // Textual reason for the interruption
119+
interrupt atomic.Bool // Atomic flag to signal execution interruption
120+
reason error // Textual reason for the interruption
121121
}
122122

123123
// NewStructLogger returns a new logger
@@ -149,7 +149,7 @@ func (l *StructLogger) CaptureStart(env *vm.EVM, from common.Address, to common.
149149
// CaptureState also tracks SLOAD/SSTORE ops to track storage change.
150150
func (l *StructLogger) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
151151
// If tracing was interrupted, set the error and stop
152-
if atomic.LoadUint32(&l.interrupt) > 0 {
152+
if l.interrupt.Load() {
153153
return
154154
}
155155
// check if already accumulated the specified number of logs
@@ -258,7 +258,7 @@ func (l *StructLogger) GetResult() (json.RawMessage, error) {
258258
// Stop terminates execution of the tracer at the first opportune moment.
259259
func (l *StructLogger) Stop(err error) {
260260
l.reason = err
261-
atomic.StoreUint32(&l.interrupt, 1)
261+
l.interrupt.Store(true)
262262
}
263263

264264
func (l *StructLogger) CaptureTxStart(gasLimit uint64) {

eth/tracers/native/4byte.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func init() {
4848
type fourByteTracer struct {
4949
noopTracer
5050
ids map[string]int // ids aggregates the 4byte ids found
51-
interrupt uint32 // Atomic flag to signal execution interruption
51+
interrupt atomic.Bool // Atomic flag to signal execution interruption
5252
reason error // Textual reason for the interruption
5353
activePrecompiles []common.Address // Updated on CaptureStart based on given rules
5454
}
@@ -93,7 +93,7 @@ func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to commo
9393
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
9494
func (t *fourByteTracer) CaptureEnter(op vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
9595
// Skip if tracing was interrupted
96-
if atomic.LoadUint32(&t.interrupt) > 0 {
96+
if t.interrupt.Load() {
9797
return
9898
}
9999
if len(input) < 4 {
@@ -124,7 +124,7 @@ func (t *fourByteTracer) GetResult() (json.RawMessage, error) {
124124
// Stop terminates execution of the tracer at the first opportune moment.
125125
func (t *fourByteTracer) Stop(err error) {
126126
t.reason = err
127-
atomic.StoreUint32(&t.interrupt, 1)
127+
t.interrupt.Store(true)
128128
}
129129

130130
func bytesToHex(s []byte) string {

eth/tracers/native/call.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ type callTracer struct {
102102
callstack []callFrame
103103
config callTracerConfig
104104
gasLimit uint64
105-
interrupt uint32 // Atomic flag to signal execution interruption
106-
reason error // Textual reason for the interruption
105+
interrupt atomic.Bool // Atomic flag to signal execution interruption
106+
reason error // Textual reason for the interruption
107107
}
108108

109109
type callTracerConfig struct {
@@ -161,7 +161,7 @@ func (t *callTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, sco
161161
return
162162
}
163163
// Skip if tracing was interrupted
164-
if atomic.LoadUint32(&t.interrupt) > 0 {
164+
if t.interrupt.Load() {
165165
return
166166
}
167167
switch op {
@@ -197,7 +197,7 @@ func (t *callTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.
197197
return
198198
}
199199
// Skip if tracing was interrupted
200-
if atomic.LoadUint32(&t.interrupt) > 0 {
200+
if t.interrupt.Load() {
201201
return
202202
}
203203

@@ -262,7 +262,7 @@ func (t *callTracer) GetResult() (json.RawMessage, error) {
262262
// Stop terminates execution of the tracer at the first opportune moment.
263263
func (t *callTracer) Stop(err error) {
264264
t.reason = err
265-
atomic.StoreUint32(&t.interrupt, 1)
265+
t.interrupt.Store(true)
266266
}
267267

268268
// clearFailedLogs clears the logs of a callframe and all its children

eth/tracers/native/prestate.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ type prestateTracer struct {
6262
to common.Address
6363
gasLimit uint64 // Amount of gas bought for the whole tx
6464
config prestateTracerConfig
65-
interrupt uint32 // Atomic flag to signal execution interruption
66-
reason error // Textual reason for the interruption
65+
interrupt atomic.Bool // Atomic flag to signal execution interruption
66+
reason error // Textual reason for the interruption
6767
created map[common.Address]bool
6868
deleted map[common.Address]bool
6969
}
@@ -136,6 +136,10 @@ func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64,
136136
if err != nil {
137137
return
138138
}
139+
// Skip if tracing was interrupted
140+
if t.interrupt.Load() {
141+
return
142+
}
139143
stack := scope.Stack
140144
stackData := stack.Data()
141145
stackLen := len(stackData)
@@ -259,7 +263,7 @@ func (t *prestateTracer) GetResult() (json.RawMessage, error) {
259263
// Stop terminates execution of the tracer at the first opportune moment.
260264
func (t *prestateTracer) Stop(err error) {
261265
t.reason = err
262-
atomic.StoreUint32(&t.interrupt, 1)
266+
t.interrupt.Store(true)
263267
}
264268

265269
// lookupAccount fetches details of an account and adds it to the prestate

0 commit comments

Comments
 (0)