Skip to content

Commit

Permalink
light: use atomic type (#27169)
Browse files Browse the repository at this point in the history
* light: use atomic type

* light: use a suitable name for the stopped switch in LightChain
  • Loading branch information
stephenfire committed Apr 26, 2023
1 parent 25f9977 commit 306d177
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions light/lightchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

0 comments on commit 306d177

Please sign in to comment.