Skip to content

Commit

Permalink
staticaddr: add initiation height of address
Browse files Browse the repository at this point in the history
  • Loading branch information
hieblmi committed Dec 9, 2024
1 parent 0d4fbae commit 7b6e602
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 39 deletions.
1 change: 1 addition & 0 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
Store: staticAddressStore,
WalletKit: d.lnd.WalletKit,
ChainParams: d.lnd.ChainParams,
ChainNotifier: d.lnd.ChainNotifier,
}
staticAddressManager = address.NewManager(addrCfg)

Expand Down
6 changes: 5 additions & 1 deletion loopdb/sqlc/migrations/000009_static_address.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ CREATE TABLE IF NOT EXISTS static_addresses (
-- Note that this version is not upgraded if the client upgrades or
-- downgrades their protocol version for static address outputs already in
-- use.
protocol_version INTEGER NOT NULL
protocol_version INTEGER NOT NULL,

-- initiation_height is the block height at which the static address was
-- created.
initiation_height INT NOT NULL
);
17 changes: 9 additions & 8 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions loopdb/sqlc/queries/static_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ INSERT INTO static_addresses (
client_key_family,
client_key_index,
pkscript,
protocol_version
protocol_version,
initiation_height
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7
$7,
$8
);
28 changes: 17 additions & 11 deletions loopdb/sqlc/static_addresses.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion staticaddr/address/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var (
type Store interface {
// CreateStaticAddress inserts a new static address with its parameters
// into the store.
CreateStaticAddress(ctx context.Context, addrParams *Parameters) error
CreateStaticAddress(ctx context.Context, addrParams *Parameters,
currentHeight int32) error

// GetStaticAddress fetches static address parameters for a given
// address ID.
Expand Down Expand Up @@ -54,4 +55,7 @@ type Parameters struct {

// ProtocolVersion is the protocol version of the static address.
ProtocolVersion version.AddressProtocolVersion

// InitiationHeight is the height at which the address was initiated.
InitiationHeight int32
}
30 changes: 27 additions & 3 deletions staticaddr/address/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ type ManagerConfig struct {
// ChainParams is the chain configuration(mainnet, testnet...) this
// manager uses.
ChainParams *chaincfg.Params

// ChainNotifier is the chain notifier that is used to listen for new
// blocks.
ChainNotifier lndclient.ChainNotifierClient
}

// Manager manages the address state machines.
type Manager struct {
cfg *ManagerConfig

sync.Mutex

currentHeight int32
}

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

// Run runs the address manager.
func (m *Manager) Run(ctx context.Context) error {
<-ctx.Done()
return nil
newBlockChan, newBlockErrChan, err :=
m.cfg.ChainNotifier.RegisterBlockEpochNtfn(ctx)

if err != nil {
return err
}

for {
select {
case currentHeight := <-newBlockChan:
m.currentHeight = currentHeight

case err = <-newBlockErrChan:
return err

case <-ctx.Done():
// Signal subroutines that the manager is exiting.
return ctx.Err()
}
}
}

// NewAddress starts a new address creation flow.
Expand Down Expand Up @@ -148,7 +172,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
protocolVersion,
),
}
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams)
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams, m.currentHeight)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion staticaddr/address/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestManager(t *testing.T) {
// Start the manager.
go func() {
err := testContext.manager.Run(ctxb)
require.NoError(t, err)
require.ErrorContains(t, err, "context canceled")
}()

// Create the expected static address.
Expand Down Expand Up @@ -179,6 +179,7 @@ func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext {
WalletKit: mockLnd.WalletKit,
ChainParams: mockLnd.ChainParams,
AddressClient: mockStaticAddressClient,
ChainNotifier: mockLnd.ChainNotifier,
FetchL402: func(context.Context) error { return nil },
}

Expand Down
18 changes: 10 additions & 8 deletions staticaddr/address/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {

// CreateStaticAddress creates a static address record in the database.
func (s *SqlStore) CreateStaticAddress(ctx context.Context,
addrParams *Parameters) error {
addrParams *Parameters, currentHeight int32) error {

createArgs := sqlc.CreateStaticAddressParams{
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
ServerPubkey: addrParams.ServerPubkey.SerializeCompressed(),
Expiry: int32(addrParams.Expiry),
ClientKeyFamily: int32(addrParams.KeyLocator.Family),
ClientKeyIndex: int32(addrParams.KeyLocator.Index),
Pkscript: addrParams.PkScript,
ProtocolVersion: int32(addrParams.ProtocolVersion),
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
ServerPubkey: addrParams.ServerPubkey.SerializeCompressed(),
Expiry: int32(addrParams.Expiry),
ClientKeyFamily: int32(addrParams.KeyLocator.Family),
ClientKeyIndex: int32(addrParams.KeyLocator.Index),
Pkscript: addrParams.PkScript,
ProtocolVersion: int32(addrParams.ProtocolVersion),
InitiationHeight: currentHeight,
}

return s.baseDB.Queries.CreateStaticAddress(ctx, createArgs)
Expand Down Expand Up @@ -106,5 +107,6 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
ProtocolVersion: version.AddressProtocolVersion(
row.ProtocolVersion,
),
InitiationHeight: row.InitiationHeight,
}, nil
}
9 changes: 5 additions & 4 deletions staticaddr/deposit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ func (m *Manager) getBlockHeight(ctx context.Context,
"deposit, %w", err)
}

notifChan, errChan, err := m.cfg.ChainNotifier.RegisterConfirmationsNtfn( //nolint:lll
ctx, &utxo.OutPoint.Hash, addressParams.PkScript, MinConfs,
int32(m.initiationHeight),
)
notifChan, errChan, err :=
m.cfg.ChainNotifier.RegisterConfirmationsNtfn(
ctx, &utxo.OutPoint.Hash, addressParams.PkScript,
MinConfs, addressParams.InitiationHeight,
)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 7b6e602

Please sign in to comment.