From 306d17749c3f04816435026071830d4932b3390d Mon Sep 17 00:00:00 2001 From: Stephen Guo Date: Wed, 26 Apr 2023 16:21:47 +0800 Subject: [PATCH] light: use atomic type (#27169) * light: use atomic type * light: use a suitable name for the stopped switch in LightChain --- light/lightchain.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/light/lightchain.go b/light/lightchain.go index 24999f52b..e2d236fb8 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -70,9 +70,9 @@ type LightChain struct { wg sync.WaitGroup // Atomic boolean switches: - running int32 // whether LightChain is running or stopped - procInterrupt int32 // interrupts chain insert - disableCheckFreq int32 // disables header verification + stopped atomic.Bool // whether LightChain is stopped or running + procInterrupt atomic.Bool // interrupts chain insert + disableCheckFreq atomic.Bool // disables header verification } // NewLightChain returns a fully initialised light chain using information @@ -114,7 +114,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus. } func (lc *LightChain) getProcInterrupt() bool { - return atomic.LoadInt32(&lc.procInterrupt) == 1 + return lc.procInterrupt.Load() } // Odr returns the ODR backend of the chain @@ -302,7 +302,7 @@ func (lc *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*typ // Stop stops the blockchain service. If any imports are currently in progress // it will abort them using the procInterrupt. func (lc *LightChain) Stop() { - if !atomic.CompareAndSwapInt32(&lc.running, 0, 1) { + if !lc.stopped.CompareAndSwap(false, true) { return } close(lc.quit) @@ -315,7 +315,7 @@ func (lc *LightChain) Stop() { // errInsertionInterrupted as soon as possible. Insertion is permanently disabled after // calling this method. func (lc *LightChain) StopInsert() { - atomic.StoreInt32(&lc.procInterrupt, 1) + lc.procInterrupt.Store(true) } // 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 if len(chain) == 0 { return 0, nil } - if atomic.LoadInt32(&lc.disableCheckFreq) == 1 { + if lc.disableCheckFreq.Load() { checkFreq = 0 } start := time.Now() @@ -541,10 +541,10 @@ func (lc *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) // DisableCheckFreq disables header validation. This is used for ultralight mode. func (lc *LightChain) DisableCheckFreq() { - atomic.StoreInt32(&lc.disableCheckFreq, 1) + lc.disableCheckFreq.Store(true) } // EnableCheckFreq enables header validation. func (lc *LightChain) EnableCheckFreq() { - atomic.StoreInt32(&lc.disableCheckFreq, 0) + lc.disableCheckFreq.Store(false) }