Skip to content

Commit b1c19e8

Browse files
committed
staticaddr: add initiation height of address
1 parent 0d4fbae commit b1c19e8

File tree

10 files changed

+87
-36
lines changed

10 files changed

+87
-36
lines changed

loopd/daemon.go

+1
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
592592
Store: staticAddressStore,
593593
WalletKit: d.lnd.WalletKit,
594594
ChainParams: d.lnd.ChainParams,
595+
ChainNotifier: d.lnd.ChainNotifier,
595596
}
596597
staticAddressManager = address.NewManager(addrCfg)
597598

loopdb/sqlc/migrations/000009_static_address.up.sql

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ CREATE TABLE IF NOT EXISTS static_addresses (
3434
-- Note that this version is not upgraded if the client upgrades or
3535
-- downgrades their protocol version for static address outputs already in
3636
-- use.
37-
protocol_version INTEGER NOT NULL
37+
protocol_version INTEGER NOT NULL,
38+
39+
-- initiation_height is the block height at which the static address was
40+
-- created.
41+
initiation_height INT NOT NULL
3842
);

loopdb/sqlc/models.go

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_addresses.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ INSERT INTO static_addresses (
1313
client_key_family,
1414
client_key_index,
1515
pkscript,
16-
protocol_version
16+
protocol_version,
17+
initiation_height
1718
) VALUES (
1819
$1,
1920
$2,
2021
$3,
2122
$4,
2223
$5,
2324
$6,
24-
$7
25+
$7,
26+
$8
2527
);

loopdb/sqlc/static_addresses.sql.go

+17-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/address/interface.go

+3
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ type Parameters struct {
5454

5555
// ProtocolVersion is the protocol version of the static address.
5656
ProtocolVersion version.AddressProtocolVersion
57+
58+
// InitiationHeight is the height at which the address was initiated.
59+
InitiationHeight int32
5760
}

staticaddr/address/manager.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@ type ManagerConfig struct {
4040
// ChainParams is the chain configuration(mainnet, testnet...) this
4141
// manager uses.
4242
ChainParams *chaincfg.Params
43+
44+
// ChainNotifier is the chain notifier that is used to listen for new
45+
// blocks.
46+
ChainNotifier lndclient.ChainNotifierClient
4347
}
4448

4549
// Manager manages the address state machines.
4650
type Manager struct {
4751
cfg *ManagerConfig
4852

4953
sync.Mutex
54+
55+
currentHeight int32
5056
}
5157

5258
// NewManager creates a new address manager.
@@ -58,8 +64,26 @@ func NewManager(cfg *ManagerConfig) *Manager {
5864

5965
// Run runs the address manager.
6066
func (m *Manager) Run(ctx context.Context) error {
61-
<-ctx.Done()
62-
return nil
67+
newBlockChan, newBlockErrChan, err :=
68+
m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx)
69+
70+
if err != nil {
71+
return err
72+
}
73+
74+
for {
75+
select {
76+
case currentHeight := <-newBlockChan:
77+
m.currentHeight = currentHeight
78+
79+
case err = <-newBlockErrChan:
80+
return err
81+
82+
case <-ctx.Done():
83+
// Signal subroutines that the manager is exiting.
84+
return ctx.Err()
85+
}
86+
}
6387
}
6488

6589
// NewAddress starts a new address creation flow.
@@ -86,6 +110,11 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
86110
}
87111
m.Unlock()
88112

113+
// Ensure that we have that we have a sane current block height.
114+
if m.currentHeight == 0 {
115+
return nil, fmt.Errorf("current block height is unknown")
116+
}
117+
89118
// We are fetching a new L402 token from the server. There is one static
90119
// address per L402 token allowed.
91120
err = m.cfg.FetchL402(ctx)
@@ -147,6 +176,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
147176
ProtocolVersion: version.AddressProtocolVersion(
148177
protocolVersion,
149178
),
179+
InitiationHeight: m.currentHeight,
150180
}
151181
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams)
152182
if err != nil {

staticaddr/address/manager_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestManager(t *testing.T) {
9797
// Start the manager.
9898
go func() {
9999
err := testContext.manager.Run(ctxb)
100-
require.NoError(t, err)
100+
require.ErrorIs(t, err, context.Canceled)
101101
}()
102102

103103
// Create the expected static address.
@@ -179,6 +179,7 @@ func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext {
179179
WalletKit: mockLnd.WalletKit,
180180
ChainParams: mockLnd.ChainParams,
181181
AddressClient: mockStaticAddressClient,
182+
ChainNotifier: mockLnd.ChainNotifier,
182183
FetchL402: func(context.Context) error { return nil },
183184
}
184185

staticaddr/address/sql_store.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
2828
addrParams *Parameters) error {
2929

3030
createArgs := sqlc.CreateStaticAddressParams{
31-
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
32-
ServerPubkey: addrParams.ServerPubkey.SerializeCompressed(),
33-
Expiry: int32(addrParams.Expiry),
34-
ClientKeyFamily: int32(addrParams.KeyLocator.Family),
35-
ClientKeyIndex: int32(addrParams.KeyLocator.Index),
36-
Pkscript: addrParams.PkScript,
37-
ProtocolVersion: int32(addrParams.ProtocolVersion),
31+
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
32+
ServerPubkey: addrParams.ServerPubkey.SerializeCompressed(),
33+
Expiry: int32(addrParams.Expiry),
34+
ClientKeyFamily: int32(addrParams.KeyLocator.Family),
35+
ClientKeyIndex: int32(addrParams.KeyLocator.Index),
36+
Pkscript: addrParams.PkScript,
37+
ProtocolVersion: int32(addrParams.ProtocolVersion),
38+
InitiationHeight: addrParams.InitiationHeight,
3839
}
3940

4041
return s.baseDB.Queries.CreateStaticAddress(ctx, createArgs)
@@ -106,5 +107,6 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
106107
ProtocolVersion: version.AddressProtocolVersion(
107108
row.ProtocolVersion,
108109
),
110+
InitiationHeight: row.InitiationHeight,
109111
}, nil
110112
}

staticaddr/deposit/manager.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,11 @@ func (m *Manager) getBlockHeight(ctx context.Context,
336336
"deposit, %w", err)
337337
}
338338

339-
notifChan, errChan, err := m.cfg.ChainNotifier.RegisterConfirmationsNtfn( //nolint:lll
340-
ctx, &utxo.OutPoint.Hash, addressParams.PkScript, MinConfs,
341-
int32(m.initiationHeight),
342-
)
339+
notifChan, errChan, err :=
340+
m.cfg.ChainNotifier.RegisterConfirmationsNtfn(
341+
ctx, &utxo.OutPoint.Hash, addressParams.PkScript,
342+
MinConfs, addressParams.InitiationHeight,
343+
)
343344
if err != nil {
344345
return 0, err
345346
}

0 commit comments

Comments
 (0)