Skip to content

Commit dc4a564

Browse files
stariushieblmi
authored andcommitted
multi: initialize block height in New, not in Run
If Run sets the current height field, it is technically a race between Run and other methods reading the field. Setting in New is a safer option. Removed a check that height is not 0 from static address manager.
1 parent 3cbfadd commit dc4a564

File tree

6 files changed

+46
-69
lines changed

6 files changed

+46
-69
lines changed

instantout/manager.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ type Manager struct {
4141
}
4242

4343
// NewInstantOutManager creates a new instantout manager.
44-
func NewInstantOutManager(cfg *Config) *Manager {
44+
func NewInstantOutManager(cfg *Config, height int32) *Manager {
4545
return &Manager{
4646
cfg: cfg,
4747
activeInstantOuts: make(map[lntypes.Hash]*FSM),
4848
blockEpochChan: make(chan int32),
49+
currentHeight: height,
4950
}
5051
}
5152

5253
// Run runs the instantout manager.
53-
func (m *Manager) Run(ctx context.Context, initChan chan struct{},
54-
height int32) error {
55-
54+
func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
5655
log.Debugf("Starting instantout manager")
5756
defer func() {
5857
log.Debugf("Stopping instantout manager")
@@ -62,7 +61,6 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{},
6261
defer cancel()
6362

6463
m.runCtx = runCtx
65-
m.currentHeight = height
6664

6765
err := m.recoverInstantOuts(runCtx)
6866
if err != nil {

loopd/daemon.go

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,14 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
446446
chainParams,
447447
)
448448

449+
// We need to know the current block height to properly initialize
450+
// managers.
451+
getInfo, err := d.lnd.Client.GetInfo(d.mainCtx)
452+
if err != nil {
453+
return fmt.Errorf("failed to get current block height: %w", err)
454+
}
455+
blockHeight := getInfo.BlockHeight
456+
449457
// If we're running an asset client, we'll log something here.
450458
if d.assetClient != nil {
451459
getInfo, err := d.assetClient.GetInfo(
@@ -580,7 +588,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
580588
ChainParams: d.lnd.ChainParams,
581589
ChainNotifier: d.lnd.ChainNotifier,
582590
}
583-
staticAddressManager = address.NewManager(addrCfg)
591+
staticAddressManager = address.NewManager(addrCfg, int32(blockHeight))
584592

585593
// Static address deposit manager setup.
586594
depositStore := deposit.NewSqlStore(baseDb)
@@ -606,7 +614,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
606614
ChainNotifier: d.lnd.ChainNotifier,
607615
Signer: d.lnd.Signer,
608616
}
609-
withdrawalManager = withdraw.NewManager(withdrawalCfg)
617+
withdrawalManager = withdraw.NewManager(withdrawalCfg, blockHeight)
610618

611619
// Static address loop-in manager setup.
612620
staticAddressLoopInStore := loopin.NewSqlStore(
@@ -631,7 +639,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
631639
ValidateLoopInContract: loop.ValidateLoopInContract,
632640
MaxStaticAddrHtlcFeePercentage: d.cfg.MaxStaticAddrHtlcFeePercentage,
633641
MaxStaticAddrHtlcBackupFeePercentage: d.cfg.MaxStaticAddrHtlcBackupFeePercentage,
634-
})
642+
}, blockHeight)
635643

636644
var (
637645
reservationManager *reservation.Manager
@@ -674,7 +682,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
674682
}
675683

676684
instantOutManager = instantout.NewInstantOutManager(
677-
instantOutConfig,
685+
instantOutConfig, int32(blockHeight),
678686
)
679687
}
680688

@@ -769,19 +777,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
769777
go func() {
770778
defer d.wg.Done()
771779

772-
// We need to know the current block height to properly
773-
// initialize the reservation manager.
774-
getInfo, err := d.lnd.Client.GetInfo(d.mainCtx)
775-
if err != nil {
776-
d.internalErrChan <- err
777-
return
778-
}
779-
780780
infof("Starting reservation manager")
781781
defer infof("Reservation manager stopped")
782782

783-
err = d.reservationManager.Run(
784-
d.mainCtx, int32(getInfo.BlockHeight), initChan,
783+
err := d.reservationManager.Run(
784+
d.mainCtx, int32(blockHeight), initChan,
785785
)
786786
if err != nil && !errors.Is(err, context.Canceled) {
787787
d.internalErrChan <- err
@@ -809,18 +809,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
809809
go func() {
810810
defer d.wg.Done()
811811

812-
getInfo, err := d.lnd.Client.GetInfo(d.mainCtx)
813-
if err != nil {
814-
d.internalErrChan <- err
815-
return
816-
}
817-
818812
infof("Starting instantout manager")
819813
defer infof("Instantout manager stopped")
820814

821-
err = d.instantOutManager.Run(
822-
d.mainCtx, initChan, int32(getInfo.BlockHeight),
823-
)
815+
err := d.instantOutManager.Run(d.mainCtx, initChan)
824816
if err != nil && !errors.Is(err, context.Canceled) {
825817
d.internalErrChan <- err
826818
}
@@ -847,7 +839,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
847839
defer d.wg.Done()
848840

849841
infof("Starting static address manager...")
850-
err = staticAddressManager.Run(d.mainCtx)
842+
err := staticAddressManager.Run(d.mainCtx)
851843
if err != nil && !errors.Is(context.Canceled, err) {
852844
d.internalErrChan <- err
853845
}
@@ -862,7 +854,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
862854
defer d.wg.Done()
863855

864856
infof("Starting static address deposit manager...")
865-
err = depositManager.Run(d.mainCtx)
857+
err := depositManager.Run(d.mainCtx)
866858
if err != nil && !errors.Is(context.Canceled, err) {
867859
d.internalErrChan <- err
868860
}
@@ -877,17 +869,9 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
877869
go func() {
878870
defer d.wg.Done()
879871

880-
// Lnd's GetInfo call supplies us with the current block
881-
// height.
882-
info, err := d.lnd.Client.GetInfo(d.mainCtx)
883-
if err != nil {
884-
d.internalErrChan <- err
885-
return
886-
}
887-
888872
infof("Starting static address deposit withdrawal " +
889873
"manager...")
890-
err = withdrawalManager.Run(d.mainCtx, info.BlockHeight)
874+
err := withdrawalManager.Run(d.mainCtx)
891875
if err != nil && !errors.Is(context.Canceled, err) {
892876
d.internalErrChan <- err
893877
}
@@ -903,19 +887,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
903887
go func() {
904888
defer d.wg.Done()
905889

906-
// Lnd's GetInfo call supplies us with the current block
907-
// height.
908-
info, err := d.lnd.Client.GetInfo(d.mainCtx)
909-
if err != nil {
910-
d.internalErrChan <- err
911-
912-
return
913-
}
914-
915890
infof("Starting static address loop-in manager...")
916-
err = staticLoopInManager.Run(
917-
d.mainCtx, info.BlockHeight,
918-
)
891+
err := staticLoopInManager.Run(d.mainCtx)
919892
if err != nil && !errors.Is(context.Canceled, err) {
920893
d.internalErrChan <- err
921894
}

staticaddr/address/manager.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ type Manager struct {
5757
}
5858

5959
// NewManager creates a new address manager.
60-
func NewManager(cfg *ManagerConfig) *Manager {
61-
return &Manager{
60+
func NewManager(cfg *ManagerConfig, currentHeight int32) *Manager {
61+
m := &Manager{
6262
cfg: cfg,
6363
}
64+
m.currentHeight.Store(currentHeight)
65+
66+
return m
6467
}
6568

6669
// Run runs the address manager.
@@ -111,11 +114,6 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
111114
}
112115
m.Unlock()
113116

114-
// Ensure that we have that we have a sane current block height.
115-
if m.currentHeight.Load() == 0 {
116-
return nil, fmt.Errorf("current block height is unknown")
117-
}
118-
119117
// We are fetching a new L402 token from the server. There is one static
120118
// address per L402 token allowed.
121119
err = m.cfg.FetchL402(ctx)

staticaddr/address/manager_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ type ManagerTestContext struct {
154154
// NewAddressManagerTestContext creates a new test context for the static
155155
// address manager.
156156
func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext {
157+
ctxb, cancel := context.WithCancel(context.Background())
158+
defer cancel()
159+
157160
mockLnd := test.NewMockLnd()
158161
lndContext := test.NewContext(t, mockLnd)
159162

@@ -183,7 +186,10 @@ func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext {
183186
FetchL402: func(context.Context) error { return nil },
184187
}
185188

186-
manager := NewManager(cfg)
189+
getInfo, err := mockLnd.Client.GetInfo(ctxb)
190+
require.NoError(t, err)
191+
192+
manager := NewManager(cfg, int32(getInfo.BlockHeight))
187193

188194
return &ManagerTestContext{
189195
manager: manager,

staticaddr/loopin/manager.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,22 @@ type Manager struct {
139139
}
140140

141141
// NewManager creates a new deposit withdrawal manager.
142-
func NewManager(cfg *Config) *Manager {
143-
return &Manager{
142+
func NewManager(cfg *Config, currentHeight uint32) *Manager {
143+
m := &Manager{
144144
cfg: cfg,
145145
initChan: make(chan struct{}),
146146
newLoopInChan: make(chan *newSwapRequest),
147147
exitChan: make(chan struct{}),
148148
errChan: make(chan error),
149149
activeLoopIns: make(map[lntypes.Hash]*FSM),
150150
}
151+
m.currentHeight.Store(currentHeight)
152+
153+
return m
151154
}
152155

153156
// Run runs the static address loop-in manager.
154-
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
155-
m.currentHeight.Store(currentHeight)
156-
157+
func (m *Manager) Run(ctx context.Context) error {
157158
registerBlockNtfn := m.cfg.ChainNotifier.RegisterBlockEpochNtfn
158159
newBlockChan, newBlockErrChan, err := registerBlockNtfn(ctx)
159160
if err != nil {

staticaddr/withdraw/manager.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,22 @@ type Manager struct {
115115
}
116116

117117
// NewManager creates a new deposit withdrawal manager.
118-
func NewManager(cfg *ManagerConfig) *Manager {
119-
return &Manager{
118+
func NewManager(cfg *ManagerConfig, currentHeight uint32) *Manager {
119+
m := &Manager{
120120
cfg: cfg,
121121
initChan: make(chan struct{}),
122122
finalizedWithdrawalTxns: make(map[chainhash.Hash]*wire.MsgTx),
123123
exitChan: make(chan struct{}),
124124
newWithdrawalRequestChan: make(chan newWithdrawalRequest),
125125
errChan: make(chan error),
126126
}
127+
m.initiationHeight.Store(currentHeight)
128+
129+
return m
127130
}
128131

129132
// Run runs the deposit withdrawal manager.
130-
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
131-
m.initiationHeight.Store(currentHeight)
132-
133+
func (m *Manager) Run(ctx context.Context) error {
133134
newBlockChan, newBlockErrChan, err :=
134135
m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx)
135136

0 commit comments

Comments
 (0)