Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticAddr: fix deposit detection #864

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 3 additions & 0 deletions staticaddr/address/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,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
}
34 changes: 32 additions & 2 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)
bhandras marked this conversation as resolved.
Show resolved Hide resolved

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 All @@ -86,6 +110,11 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
}
m.Unlock()

// Ensure that we have that we have a sane current block height.
if m.currentHeight == 0 {
return nil, fmt.Errorf("current block height is unknown")
}

// We are fetching a new L402 token from the server. There is one static
// address per L402 token allowed.
err = m.cfg.FetchL402(ctx)
Expand Down Expand Up @@ -147,6 +176,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
ProtocolVersion: version.AddressProtocolVersion(
protocolVersion,
),
InitiationHeight: m.currentHeight,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can currentHeight be uninitialized (0) at this point? IMHO we should either return an error here if currentHeight is 0 (if we are sure that this won't happen) or wait for its initialization (e.g. using a cond var).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I've added a check on entry of NewAddress.

}
err = m.cfg.Store.CreateStaticAddress(ctx, addrParams)
if err != nil {
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.ErrorIs(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
16 changes: 9 additions & 7 deletions staticaddr/address/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
addrParams *Parameters) 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: addrParams.InitiationHeight,
}

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
Loading