Skip to content

Commit 90dff73

Browse files
committed
graph: updated builder to use atomic ints
Instead of relying on devs to remember that they must only be accessed atomically.
1 parent fe34d62 commit 90dff73

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

graph/builder.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ type Builder struct {
116116
started atomic.Bool
117117
stopped atomic.Bool
118118

119-
ntfnClientCounter uint64 // To be used atomically.
120-
bestHeight uint32 // To be used atomically.
119+
ntfnClientCounter atomic.Uint64
120+
bestHeight atomic.Uint32
121121

122122
cfg *Config
123123

@@ -278,7 +278,7 @@ func (b *Builder) Start() error {
278278
if err != nil {
279279
return err
280280
}
281-
b.bestHeight = uint32(bestHeight)
281+
b.bestHeight.Store(uint32(bestHeight))
282282

283283
// Before we begin normal operation of the router, we first need
284284
// to synchronize the channel graph to the latest state of the
@@ -340,7 +340,7 @@ func (b *Builder) syncGraphWithChain() error {
340340
if err != nil {
341341
return err
342342
}
343-
b.bestHeight = uint32(bestHeight)
343+
b.bestHeight.Store(uint32(bestHeight))
344344

345345
pruneHash, pruneHeight, err := b.cfg.Graph.PruneTip()
346346
if err != nil {
@@ -806,7 +806,7 @@ func (b *Builder) networkHandler() {
806806
// Since this block is stale, we update our best height
807807
// to the previous block.
808808
blockHeight := chainUpdate.Height
809-
atomic.StoreUint32(&b.bestHeight, blockHeight-1)
809+
b.bestHeight.Store(blockHeight - 1)
810810

811811
// Update the channel graph to reflect that this block
812812
// was disconnected.
@@ -834,7 +834,7 @@ func (b *Builder) networkHandler() {
834834
// directly to the end of our main chain. If not, then
835835
// we've somehow missed some blocks. Here we'll catch
836836
// up the chain with the latest blocks.
837-
currentHeight := atomic.LoadUint32(&b.bestHeight)
837+
currentHeight := b.bestHeight.Load()
838838
switch {
839839
case chainUpdate.Height == currentHeight+1:
840840
err := b.updateGraphWithClosedChannels(
@@ -991,7 +991,7 @@ func (b *Builder) updateGraphWithClosedChannels(
991991
// of the chain tip.
992992
blockHeight := chainUpdate.Height
993993

994-
atomic.StoreUint32(&b.bestHeight, blockHeight)
994+
b.bestHeight.Store(blockHeight)
995995
log.Infof("Pruning channel graph using block %v (height=%v)",
996996
chainUpdate.Hash, blockHeight)
997997

@@ -1342,7 +1342,7 @@ func (b *Builder) processUpdate(msg interface{},
13421342
},
13431343
}
13441344
err = b.cfg.ChainView.UpdateFilter(
1345-
filterUpdate, atomic.LoadUint32(&b.bestHeight),
1345+
filterUpdate, b.bestHeight.Load(),
13461346
)
13471347
if err != nil {
13481348
return errors.Errorf("unable to update chain "+
@@ -1658,7 +1658,7 @@ func (b *Builder) CurrentBlockHeight() (uint32, error) {
16581658
// is synced to. This can differ from the above chain height if the goroutine
16591659
// responsible for processing the blocks isn't yet up to speed.
16601660
func (b *Builder) SyncedHeight() uint32 {
1661-
return atomic.LoadUint32(&b.bestHeight)
1661+
return b.bestHeight.Load()
16621662
}
16631663

16641664
// GetChannelByID return the channel by the channel id.

graph/builder_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net"
1212
"os"
1313
"strings"
14-
"sync/atomic"
1514
"testing"
1615
"time"
1716

@@ -1392,12 +1391,10 @@ func TestBlockDifferenceFix(t *testing.T) {
13921391

13931392
err := wait.NoError(func() error {
13941393
// Then router height should be updated to the latest block.
1395-
if atomic.LoadUint32(&ctx.builder.bestHeight) !=
1396-
newBlockHeight {
1397-
1394+
if ctx.builder.bestHeight.Load() != newBlockHeight {
13981395
return fmt.Errorf("height should have been updated "+
13991396
"to %v, instead got %v", newBlockHeight,
1400-
ctx.builder.bestHeight)
1397+
ctx.builder.bestHeight.Load())
14011398
}
14021399

14031400
return nil

graph/notifications.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"image/color"
66
"net"
77
"sync"
8-
"sync/atomic"
98

109
"github.com/btcsuite/btcd/btcec/v2"
1110
"github.com/btcsuite/btcd/btcutil"
@@ -65,7 +64,7 @@ func (b *Builder) SubscribeTopology() (*TopologyClient, error) {
6564

6665
// We'll first atomically obtain the next ID for this client from the
6766
// incrementing client ID counter.
68-
clientID := atomic.AddUint64(&b.ntfnClientCounter, 1)
67+
clientID := b.ntfnClientCounter.Add(1)
6968

7069
log.Debugf("New graph topology client subscription, client %v",
7170
clientID)

0 commit comments

Comments
 (0)