Skip to content

Commit d8953be

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

File tree

9 files changed

+83
-38
lines changed

9 files changed

+83
-38
lines changed

loopd/daemon.go

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_addresses.sql

Lines changed: 4 additions & 2 deletions
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

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/address/interface.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var (
1818
type Store interface {
1919
// CreateStaticAddress inserts a new static address with its parameters
2020
// into the store.
21-
CreateStaticAddress(ctx context.Context, addrParams *Parameters) error
21+
CreateStaticAddress(ctx context.Context, addrParams *Parameters,
22+
currentHeight int32) error
2223

2324
// GetStaticAddress fetches static address parameters for a given
2425
// address ID.
@@ -54,4 +55,7 @@ type Parameters struct {
5455

5556
// ProtocolVersion is the protocol version of the static address.
5657
ProtocolVersion version.AddressProtocolVersion
58+
59+
// InitiationHeight is the height at which the address was initiated.
60+
InitiationHeight int32
5761
}

staticaddr/address/manager.go

Lines changed: 27 additions & 3 deletions
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.
@@ -148,7 +172,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
148172
protocolVersion,
149173
),
150174
}
151-
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams)
175+
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams, m.currentHeight)
152176
if err != nil {
153177
return nil, err
154178
}

staticaddr/address/sql_store.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
2525

2626
// CreateStaticAddress creates a static address record in the database.
2727
func (s *SqlStore) CreateStaticAddress(ctx context.Context,
28-
addrParams *Parameters) error {
28+
addrParams *Parameters, currentHeight int32) 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: currentHeight,
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

Lines changed: 5 additions & 4 deletions
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)