Skip to content

Commit 4c292f1

Browse files
stariushieblmi
authored andcommitted
staticaddr: fix race conditions in current height
Make the current height an atomic variable in staticaddr/address/manager.go and in staticaddr/withdraw/manager.go. Removed the initiation height from staticaddr/deposit/manager.go (not needed).
1 parent e0e66a9 commit 4c292f1

File tree

5 files changed

+12
-26
lines changed

5 files changed

+12
-26
lines changed

loopd/daemon.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -861,16 +861,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
861861
go func() {
862862
defer d.wg.Done()
863863

864-
// Lnd's GetInfo call supplies us with the current block
865-
// height.
866-
info, err := d.lnd.Client.GetInfo(d.mainCtx)
867-
if err != nil {
868-
d.internalErrChan <- err
869-
return
870-
}
871-
872864
infof("Starting static address deposit manager...")
873-
err = depositManager.Run(d.mainCtx, info.BlockHeight)
865+
err = depositManager.Run(d.mainCtx)
874866
if err != nil && !errors.Is(context.Canceled, err) {
875867
d.internalErrChan <- err
876868
}

staticaddr/address/manager.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"sync"
8+
"sync/atomic"
89

910
"github.com/btcsuite/btcd/btcec/v2"
1011
"github.com/btcsuite/btcd/btcec/v2/schnorr"
@@ -52,7 +53,7 @@ type Manager struct {
5253

5354
sync.Mutex
5455

55-
currentHeight int32
56+
currentHeight atomic.Int32
5657
}
5758

5859
// NewManager creates a new address manager.
@@ -74,7 +75,7 @@ func (m *Manager) Run(ctx context.Context) error {
7475
for {
7576
select {
7677
case currentHeight := <-newBlockChan:
77-
m.currentHeight = currentHeight
78+
m.currentHeight.Store(currentHeight)
7879

7980
case err = <-newBlockErrChan:
8081
return err
@@ -111,7 +112,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
111112
m.Unlock()
112113

113114
// Ensure that we have that we have a sane current block height.
114-
if m.currentHeight == 0 {
115+
if m.currentHeight.Load() == 0 {
115116
return nil, fmt.Errorf("current block height is unknown")
116117
}
117118

@@ -176,7 +177,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
176177
ProtocolVersion: version.AddressProtocolVersion(
177178
protocolVersion,
178179
),
179-
InitiationHeight: m.currentHeight,
180+
InitiationHeight: m.currentHeight.Load(),
180181
}
181182
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams)
182183
if err != nil {

staticaddr/deposit/manager.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ type Manager struct {
8484
// activeDeposits contains all the active static address outputs.
8585
activeDeposits map[wire.OutPoint]*FSM
8686

87-
// initiationHeight stores the currently best known block height.
88-
initiationHeight uint32
89-
9087
// deposits contains all the deposits that have ever been made to the
9188
// static address. This field is used to store and recover deposits. It
9289
// also serves as basis for reconciliation of newly detected deposits by
@@ -111,9 +108,7 @@ func NewManager(cfg *ManagerConfig) *Manager {
111108
}
112109

113110
// Run runs the address manager.
114-
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
115-
m.initiationHeight = currentHeight
116-
111+
func (m *Manager) Run(ctx context.Context) error {
117112
newBlockChan, newBlockErrChan, err := m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx) //nolint:lll
118113
if err != nil {
119114
return err

staticaddr/deposit/manager_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ func TestManager(t *testing.T) {
219219

220220
// Start the deposit manager.
221221
go func() {
222-
err := testContext.manager.Run(
223-
ctx, uint32(testContext.mockLnd.Height),
224-
)
225-
require.NoError(t, err)
222+
require.NoError(t, testContext.manager.Run(ctx))
226223
}()
227224

228225
// Ensure that the manager has been initialized.

staticaddr/withdraw/manager.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"reflect"
88
"strings"
9+
"sync/atomic"
910

1011
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1112
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
@@ -106,7 +107,7 @@ type Manager struct {
106107
errChan chan error
107108

108109
// initiationHeight stores the currently best known block height.
109-
initiationHeight uint32
110+
initiationHeight atomic.Uint32
110111

111112
// finalizedWithdrawalTx are the finalized withdrawal transactions that
112113
// are published to the network and re-published on block arrivals.
@@ -127,7 +128,7 @@ func NewManager(cfg *ManagerConfig) *Manager {
127128

128129
// Run runs the deposit withdrawal manager.
129130
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
130-
m.initiationHeight = currentHeight
131+
m.initiationHeight.Store(currentHeight)
131132

132133
newBlockChan, newBlockErrChan, err :=
133134
m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx)
@@ -479,7 +480,7 @@ func (m *Manager) handleWithdrawal(ctx context.Context,
479480

480481
confChan, errChan, err := m.cfg.ChainNotifier.RegisterConfirmationsNtfn(
481482
ctx, &txHash, withdrawalPkScript, MinConfs,
482-
int32(m.initiationHeight),
483+
int32(m.initiationHeight.Load()),
483484
)
484485
if err != nil {
485486
return err

0 commit comments

Comments
 (0)