Skip to content

Commit 712ca01

Browse files
gzliudans7v7nislandsholimans1na
authored
eth/tracers: use atomic type ethereum#27031 (#1326)
Use the new atomic types in package eth/tracers --------- Co-authored-by: s7v7nislands <s7v7nislands@gmail.com> Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
1 parent 9fd13e5 commit 712ca01

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
@@ -687,8 +687,8 @@ func TestTraceChain(t *testing.T) {
687687
signer := types.HomesteadSigner{}
688688

689689
var (
690-
ref uint32 // total refs has made
691-
rel uint32 // total rels has made
690+
ref atomic.Uint32 // total refs has made
691+
rel atomic.Uint32 // total rels has made
692692
nonce uint64
693693
)
694694
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
@@ -701,8 +701,8 @@ func TestTraceChain(t *testing.T) {
701701
nonce += 1
702702
}
703703
})
704-
backend.refHook = func() { atomic.AddUint32(&ref, 1) }
705-
backend.relHook = func() { atomic.AddUint32(&rel, 1) }
704+
backend.refHook = func() { ref.Add(1) }
705+
backend.relHook = func() { rel.Add(1) }
706706
api := NewAPI(backend)
707707

708708
single := `{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}`
@@ -715,7 +715,8 @@ func TestTraceChain(t *testing.T) {
715715
{10, 20, nil}, // the middle chain range, blocks [11, 20]
716716
}
717717
for _, c := range cases {
718-
ref, rel = 0, 0 // clean up the counters
718+
ref.Store(0)
719+
rel.Store(0)
719720

720721
from, _ := api.blockByNumber(context.Background(), rpc.BlockNumber(c.start))
721722
to, _ := api.blockByNumber(context.Background(), rpc.BlockNumber(c.end))
@@ -740,8 +741,9 @@ func TestTraceChain(t *testing.T) {
740741
if next != c.end+1 {
741742
t.Error("Missing tracing block")
742743
}
743-
if ref != rel {
744-
t.Errorf("Ref and deref actions are not equal, ref %d rel %d", ref, rel)
744+
745+
if nref, nrel := ref.Load(), rel.Load(); nref != nrel {
746+
t.Errorf("Ref and deref actions are not equal, ref %d rel %d", nref, nrel)
745747
}
746748
}
747749
}

eth/tracers/logger/logger.go

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

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

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

265265
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)