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).
This commit is contained in:
Boris Nagaev 2025-03-10 19:36:54 -03:00 committed by Slyghtning
parent e0e66a9b38
commit 4c292f179f
No known key found for this signature in database
GPG key ID: F82D456EA023C9BF
5 changed files with 12 additions and 26 deletions

View file

@ -861,16 +861,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
go func() {
defer d.wg.Done()
// Lnd's GetInfo call supplies us with the current block
// height.
info, err := d.lnd.Client.GetInfo(d.mainCtx)
if err != nil {
d.internalErrChan <- err
return
}
infof("Starting static address deposit manager...")
err = depositManager.Run(d.mainCtx, info.BlockHeight)
err = depositManager.Run(d.mainCtx)
if err != nil && !errors.Is(context.Canceled, err) {
d.internalErrChan <- err
}

View file

@ -5,6 +5,7 @@ import (
"context"
"fmt"
"sync"
"sync/atomic"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
@ -52,7 +53,7 @@ type Manager struct {
sync.Mutex
currentHeight int32
currentHeight atomic.Int32
}
// NewManager creates a new address manager.
@ -74,7 +75,7 @@ func (m *Manager) Run(ctx context.Context) error {
for {
select {
case currentHeight := <-newBlockChan:
m.currentHeight = currentHeight
m.currentHeight.Store(currentHeight)
case err = <-newBlockErrChan:
return err
@ -111,7 +112,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
m.Unlock()
// Ensure that we have that we have a sane current block height.
if m.currentHeight == 0 {
if m.currentHeight.Load() == 0 {
return nil, fmt.Errorf("current block height is unknown")
}
@ -176,7 +177,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
ProtocolVersion: version.AddressProtocolVersion(
protocolVersion,
),
InitiationHeight: m.currentHeight,
InitiationHeight: m.currentHeight.Load(),
}
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams)
if err != nil {

View file

@ -84,9 +84,6 @@ type Manager struct {
// activeDeposits contains all the active static address outputs.
activeDeposits map[wire.OutPoint]*FSM
// initiationHeight stores the currently best known block height.
initiationHeight uint32
// deposits contains all the deposits that have ever been made to the
// static address. This field is used to store and recover deposits. It
// also serves as basis for reconciliation of newly detected deposits by
@ -111,9 +108,7 @@ func NewManager(cfg *ManagerConfig) *Manager {
}
// Run runs the address manager.
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
m.initiationHeight = currentHeight
func (m *Manager) Run(ctx context.Context) error {
newBlockChan, newBlockErrChan, err := m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx) //nolint:lll
if err != nil {
return err

View file

@ -219,10 +219,7 @@ func TestManager(t *testing.T) {
// Start the deposit manager.
go func() {
err := testContext.manager.Run(
ctx, uint32(testContext.mockLnd.Height),
)
require.NoError(t, err)
require.NoError(t, testContext.manager.Run(ctx))
}()
// Ensure that the manager has been initialized.

View file

@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strings"
"sync/atomic"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
@ -106,7 +107,7 @@ type Manager struct {
errChan chan error
// initiationHeight stores the currently best known block height.
initiationHeight uint32
initiationHeight atomic.Uint32
// finalizedWithdrawalTx are the finalized withdrawal transactions that
// are published to the network and re-published on block arrivals.
@ -127,7 +128,7 @@ func NewManager(cfg *ManagerConfig) *Manager {
// Run runs the deposit withdrawal manager.
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
m.initiationHeight = currentHeight
m.initiationHeight.Store(currentHeight)
newBlockChan, newBlockErrChan, err :=
m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx)
@ -479,7 +480,7 @@ func (m *Manager) handleWithdrawal(ctx context.Context,
confChan, errChan, err := m.cfg.ChainNotifier.RegisterConfirmationsNtfn(
ctx, &txHash, withdrawalPkScript, MinConfs,
int32(m.initiationHeight),
int32(m.initiationHeight.Load()),
)
if err != nil {
return err