Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 306d177

Browse files
authored
light: use atomic type (ethereum#27169)
* light: use atomic type * light: use a suitable name for the stopped switch in LightChain
1 parent 25f9977 commit 306d177

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

light/lightchain.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ type LightChain struct {
7070
wg sync.WaitGroup
7171

7272
// Atomic boolean switches:
73-
running int32 // whether LightChain is running or stopped
74-
procInterrupt int32 // interrupts chain insert
75-
disableCheckFreq int32 // disables header verification
73+
stopped atomic.Bool // whether LightChain is stopped or running
74+
procInterrupt atomic.Bool // interrupts chain insert
75+
disableCheckFreq atomic.Bool // disables header verification
7676
}
7777

7878
// NewLightChain returns a fully initialised light chain using information
@@ -114,7 +114,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
114114
}
115115

116116
func (lc *LightChain) getProcInterrupt() bool {
117-
return atomic.LoadInt32(&lc.procInterrupt) == 1
117+
return lc.procInterrupt.Load()
118118
}
119119

120120
// Odr returns the ODR backend of the chain
@@ -302,7 +302,7 @@ func (lc *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*typ
302302
// Stop stops the blockchain service. If any imports are currently in progress
303303
// it will abort them using the procInterrupt.
304304
func (lc *LightChain) Stop() {
305-
if !atomic.CompareAndSwapInt32(&lc.running, 0, 1) {
305+
if !lc.stopped.CompareAndSwap(false, true) {
306306
return
307307
}
308308
close(lc.quit)
@@ -315,7 +315,7 @@ func (lc *LightChain) Stop() {
315315
// errInsertionInterrupted as soon as possible. Insertion is permanently disabled after
316316
// calling this method.
317317
func (lc *LightChain) StopInsert() {
318-
atomic.StoreInt32(&lc.procInterrupt, 1)
318+
lc.procInterrupt.Store(true)
319319
}
320320

321321
// Rollback is designed to remove a chain of links from the database that aren't
@@ -393,7 +393,7 @@ func (lc *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
393393
if len(chain) == 0 {
394394
return 0, nil
395395
}
396-
if atomic.LoadInt32(&lc.disableCheckFreq) == 1 {
396+
if lc.disableCheckFreq.Load() {
397397
checkFreq = 0
398398
}
399399
start := time.Now()
@@ -541,10 +541,10 @@ func (lc *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)
541541

542542
// DisableCheckFreq disables header validation. This is used for ultralight mode.
543543
func (lc *LightChain) DisableCheckFreq() {
544-
atomic.StoreInt32(&lc.disableCheckFreq, 1)
544+
lc.disableCheckFreq.Store(true)
545545
}
546546

547547
// EnableCheckFreq enables header validation.
548548
func (lc *LightChain) EnableCheckFreq() {
549-
atomic.StoreInt32(&lc.disableCheckFreq, 0)
549+
lc.disableCheckFreq.Store(false)
550550
}

0 commit comments

Comments
 (0)