Skip to content

Commit 82da34b

Browse files
committed
staticaddr: attach address params to deposit
1 parent 8a3c496 commit 82da34b

File tree

7 files changed

+41
-41
lines changed

7 files changed

+41
-41
lines changed

loopd/swapclient_server_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,15 @@ func TestListUnspentDeposits(t *testing.T) {
993993
}, 0)
994994

995995
initChan := make(chan struct{})
996-
go addrMgr.Run(t.Context(), initChan)
996+
go func() {
997+
err := addrMgr.Run(ctx, initChan)
998+
require.NoError(t, err)
999+
}()
9971000

9981001
select {
9991002
case <-initChan:
1000-
case <-t.Context().Done():
1001-
t.Fatalf("failed to initialize address manager: %v",
1002-
t.Context().Err())
1003+
case <-ctx.Done():
1004+
t.Fatalf("failed to initialize address manager: %v", ctx.Err())
10031005
}
10041006

10051007
// Construct several UTXOs with different confirmation counts.

staticaddr/deposit/actions.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@ func (f *FSM) PublishDepositExpirySweepAction(ctx context.Context,
2626

2727
msgTx := wire.NewMsgTx(2)
2828

29-
params, err := f.cfg.AddressManager.GetStaticAddressParameters(ctx)
30-
if err != nil {
31-
return fsm.OnError
32-
}
33-
3429
// Add the deposit outpoint as input to the transaction.
3530
msgTx.AddTxIn(&wire.TxIn{
3631
PreviousOutPoint: f.deposit.OutPoint,
37-
Sequence: params.Expiry,
32+
Sequence: f.deposit.AddressParams.Expiry,
3833
SignatureScript: nil,
3934
})
4035

41-
// Estimate the fee rate of an expiry spend transaction.
36+
// Estimate the fee rate of an expiry spending transaction.
4237
feeRateEstimator, err := f.cfg.WalletKit.EstimateFeeRate(
4338
ctx, DefaultConfTarget,
4439
)
@@ -65,7 +60,7 @@ func (f *FSM) PublishDepositExpirySweepAction(ctx context.Context,
6560

6661
txOut := &wire.TxOut{
6762
Value: int64(f.deposit.Value),
68-
PkScript: params.PkScript,
63+
PkScript: f.deposit.AddressParams.PkScript,
6964
}
7065

7166
prevOut := []*wire.TxOut{txOut}

staticaddr/deposit/deposit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/btcsuite/btcd/chaincfg/chainhash"
1010
"github.com/btcsuite/btcd/wire"
1111
"github.com/lightninglabs/loop/fsm"
12+
"github.com/lightninglabs/loop/staticaddr/address"
1213
"github.com/lightningnetwork/lnd/lntypes"
1314
)
1415

@@ -62,6 +63,10 @@ type Deposit struct {
6263
// FinalizedWithdrawalTx is the coop-signed withdrawal transaction. It
6364
// is republished on new block arrivals and on client restarts.
6465
FinalizedWithdrawalTx *wire.MsgTx
66+
67+
// AddressParams are the parameters of the address that are backing this
68+
// deposit.
69+
AddressParams *address.Parameters
6570
}
6671

6772
// IsInFinalState returns true if the deposit is final.

staticaddr/deposit/fsm.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package deposit
33
import (
44
"context"
55
"errors"
6-
"fmt"
76

87
"github.com/btcsuite/btcd/txscript"
98
"github.com/btcsuite/btcd/wire"
109
"github.com/lightninglabs/lndclient"
1110
"github.com/lightninglabs/loop/fsm"
1211
"github.com/lightninglabs/loop/staticaddr/address"
13-
"github.com/lightninglabs/loop/staticaddr/script"
1412
"github.com/lightninglabs/loop/staticaddr/version"
1513
"github.com/lightningnetwork/lnd/input"
1614
"github.com/lightningnetwork/lnd/keychain"
@@ -137,15 +135,13 @@ type FSM struct {
137135

138136
params *address.Parameters
139137

140-
address *script.StaticAddress
141-
142138
blockNtfnChan chan uint32
143139

144140
// quitChan stops after the FSM stops consuming blockNtfnChan.
145141
quitChan chan struct{}
146142

147143
// finalizedDepositChan is used to signal that the deposit has been
148-
// finalized and the FSM can be removed from the manager's memory.
144+
// finalized, and the FSM can be removed from the manager's memory.
149145
finalizedDepositChan chan wire.OutPoint
150146
}
151147

@@ -155,29 +151,17 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
155151
finalizedDepositChan chan wire.OutPoint,
156152
recoverStateMachine bool) (*FSM, error) {
157153

158-
params, err := cfg.AddressManager.GetStaticAddressParameters(ctx)
159-
if err != nil {
160-
return nil, fmt.Errorf("unable to get static address "+
161-
"parameters: %w", err)
162-
}
163-
164-
address, err := cfg.AddressManager.GetStaticAddress(ctx)
165-
if err != nil {
166-
return nil, fmt.Errorf("unable to get static address: %w", err)
167-
}
168-
169154
depoFsm := &FSM{
170155
cfg: cfg,
171156
deposit: deposit,
172-
params: params,
173-
address: address,
157+
params: deposit.AddressParams,
174158
blockNtfnChan: make(chan uint32),
175159
quitChan: make(chan struct{}),
176160
finalizedDepositChan: finalizedDepositChan,
177161
}
178162

179163
depositStates := depoFsm.DepositStatesV0()
180-
switch params.ProtocolVersion {
164+
switch deposit.AddressParams.ProtocolVersion {
181165
case version.ProtocolVersion_V0:
182166

183167
default:

staticaddr/deposit/interface.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ type Store interface {
3434

3535
// AddressManager handles fetching of address parameters.
3636
type AddressManager interface {
37-
// GetStaticAddressParameters returns the static address parameters.
38-
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
39-
error)
37+
// GetParameters returns the static address parameters for the given
38+
// pkScript.
39+
GetParameters(pkScript []byte) *address.Parameters
4040

4141
// GetStaticAddress returns the deposit address for the given
4242
// client and server public keys.

staticaddr/deposit/manager.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
179179
for i, d := range deposits {
180180
m.deposits[d.OutPoint] = deposits[i]
181181

182-
// If the current deposit is final it wasn't active when we
182+
// If the current deposit is final, it wasn't active when we
183183
// shut down the client last. So we don't need to start a fsm
184184
// for it.
185185
if d.IsInFinalState() {
@@ -307,13 +307,21 @@ func (m *Manager) createNewDeposit(ctx context.Context,
307307
if err != nil {
308308
return nil, err
309309
}
310+
311+
params := m.cfg.AddressManager.GetParameters(utxo.PkScript)
312+
if params == nil {
313+
return nil, fmt.Errorf("couldn't find static address "+
314+
"parameters for deposit with pkscript %x", utxo.PkScript)
315+
}
316+
310317
deposit := &Deposit{
311318
ID: id,
312319
state: Deposited,
313320
OutPoint: utxo.OutPoint,
314321
Value: utxo.Value,
315322
ConfirmationHeight: int64(blockHeight),
316323
TimeOutSweepPkScript: timeoutSweepPkScript,
324+
AddressParams: params,
317325
}
318326

319327
err = m.cfg.Store.CreateDeposit(ctx, deposit)
@@ -332,12 +340,10 @@ func (m *Manager) createNewDeposit(ctx context.Context,
332340
func (m *Manager) getBlockHeight(ctx context.Context,
333341
utxo *lnwallet.Utxo) (uint32, error) {
334342

335-
addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
336-
ctx,
337-
)
338-
if err != nil {
339-
return 0, fmt.Errorf("couldn't get confirmation height for "+
340-
"deposit, %w", err)
343+
addressParams := m.cfg.AddressManager.GetParameters(utxo.PkScript)
344+
if addressParams == nil {
345+
return 0, fmt.Errorf("couldn't get confirmation height for " +
346+
"deposit")
341347
}
342348

343349
notifChan, errChan, err :=

staticaddr/deposit/manager_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ func (m *mockAddressManager) GetStaticAddressParameters(ctx context.Context) (
116116
args.Error(1)
117117
}
118118

119+
func (m *mockAddressManager) GetParameters(
120+
pkScript []byte) *address.Parameters {
121+
122+
args := m.Called(pkScript)
123+
124+
return args.Get(0).(*address.Parameters)
125+
}
126+
119127
func (m *mockAddressManager) GetStaticAddress(ctx context.Context) (
120128
*script.StaticAddress, error) {
121129

0 commit comments

Comments
 (0)