Skip to content

Commit 7a1cb63

Browse files
authored
Merge pull request #868 from hieblmi/fix-rc-review-comments
StaticAddr: fix rc review comments
2 parents 9342b0f + acea303 commit 7a1cb63

File tree

3 files changed

+108
-93
lines changed

3 files changed

+108
-93
lines changed

cmd/loop/staticaddr.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,10 @@ func depositsToOutpoints(deposits []*looprpc.Deposit) []string {
641641

642642
func displayNewAddressWarning() error {
643643
fmt.Printf("\nWARNING: Be aware that loosing your l402.token file in " +
644-
".loop under your home directory \nwill take your ability to " +
644+
".loop under your home directory will take your ability to " +
645645
"spend funds sent to the static address via loop-ins or " +
646-
"\nwithdrawals. You will have to wait until the deposit " +
647-
"expires and your loop client \nsweeps the funds back to your " +
646+
"withdrawals. You will have to wait until the deposit " +
647+
"expires and your loop client sweeps the funds back to your " +
648648
"lnd wallet. The deposit expiry could be months in the " +
649649
"future.\n")
650650

loopd/daemon.go

+68-66
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,80 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
536536
}()
537537

538538
var (
539-
reservationManager *reservation.Manager
540-
instantOutManager *instantout.Manager
541-
542539
staticAddressManager *address.Manager
543540
depositManager *deposit.Manager
544541
withdrawalManager *withdraw.Manager
545542
staticLoopInManager *loopin.Manager
546543
)
547544

545+
// Static address manager setup.
546+
staticAddressStore := address.NewSqlStore(baseDb)
547+
addrCfg := &address.ManagerConfig{
548+
AddressClient: staticAddressClient,
549+
FetchL402: swapClient.Server.FetchL402,
550+
Store: staticAddressStore,
551+
WalletKit: d.lnd.WalletKit,
552+
ChainParams: d.lnd.ChainParams,
553+
ChainNotifier: d.lnd.ChainNotifier,
554+
}
555+
staticAddressManager = address.NewManager(addrCfg)
556+
557+
// Static address deposit manager setup.
558+
depositStore := deposit.NewSqlStore(baseDb)
559+
depoCfg := &deposit.ManagerConfig{
560+
AddressClient: staticAddressClient,
561+
AddressManager: staticAddressManager,
562+
SwapClient: swapClient,
563+
Store: depositStore,
564+
WalletKit: d.lnd.WalletKit,
565+
ChainParams: d.lnd.ChainParams,
566+
ChainNotifier: d.lnd.ChainNotifier,
567+
Signer: d.lnd.Signer,
568+
}
569+
depositManager = deposit.NewManager(depoCfg)
570+
571+
// Static address deposit withdrawal manager setup.
572+
withdrawalCfg := &withdraw.ManagerConfig{
573+
StaticAddressServerClient: staticAddressClient,
574+
AddressManager: staticAddressManager,
575+
DepositManager: depositManager,
576+
WalletKit: d.lnd.WalletKit,
577+
ChainParams: d.lnd.ChainParams,
578+
ChainNotifier: d.lnd.ChainNotifier,
579+
Signer: d.lnd.Signer,
580+
}
581+
withdrawalManager = withdraw.NewManager(withdrawalCfg)
582+
583+
// Static address loop-in manager setup.
584+
staticAddressLoopInStore := loopin.NewSqlStore(
585+
loopdb.NewTypedStore[loopin.Querier](baseDb),
586+
clock.NewDefaultClock(), d.lnd.ChainParams,
587+
)
588+
589+
staticLoopInManager = loopin.NewManager(&loopin.Config{
590+
Server: staticAddressClient,
591+
QuoteGetter: swapClient.Server,
592+
LndClient: d.lnd.Client,
593+
InvoicesClient: d.lnd.Invoices,
594+
NodePubkey: d.lnd.NodePubkey,
595+
AddressManager: staticAddressManager,
596+
DepositManager: depositManager,
597+
Store: staticAddressLoopInStore,
598+
WalletKit: d.lnd.WalletKit,
599+
ChainNotifier: d.lnd.ChainNotifier,
600+
NotificationManager: notificationManager,
601+
ChainParams: d.lnd.ChainParams,
602+
Signer: d.lnd.Signer,
603+
ValidateLoopInContract: loop.ValidateLoopInContract,
604+
MaxStaticAddrHtlcFeePercentage: d.cfg.MaxStaticAddrHtlcFeePercentage,
605+
MaxStaticAddrHtlcBackupFeePercentage: d.cfg.MaxStaticAddrHtlcBackupFeePercentage,
606+
})
607+
608+
var (
609+
reservationManager *reservation.Manager
610+
instantOutManager *instantout.Manager
611+
)
612+
548613
// Create the reservation and instantout managers.
549614
if d.cfg.EnableExperimental {
550615
reservationStore := reservation.NewSQLStore(
@@ -583,69 +648,6 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
583648
instantOutManager = instantout.NewInstantOutManager(
584649
instantOutConfig,
585650
)
586-
587-
// Static address manager setup.
588-
staticAddressStore := address.NewSqlStore(baseDb)
589-
addrCfg := &address.ManagerConfig{
590-
AddressClient: staticAddressClient,
591-
FetchL402: swapClient.Server.FetchL402,
592-
Store: staticAddressStore,
593-
WalletKit: d.lnd.WalletKit,
594-
ChainParams: d.lnd.ChainParams,
595-
ChainNotifier: d.lnd.ChainNotifier,
596-
}
597-
staticAddressManager = address.NewManager(addrCfg)
598-
599-
// Static address deposit manager setup.
600-
depositStore := deposit.NewSqlStore(baseDb)
601-
depoCfg := &deposit.ManagerConfig{
602-
AddressClient: staticAddressClient,
603-
AddressManager: staticAddressManager,
604-
SwapClient: swapClient,
605-
Store: depositStore,
606-
WalletKit: d.lnd.WalletKit,
607-
ChainParams: d.lnd.ChainParams,
608-
ChainNotifier: d.lnd.ChainNotifier,
609-
Signer: d.lnd.Signer,
610-
}
611-
depositManager = deposit.NewManager(depoCfg)
612-
613-
// Static address deposit withdrawal manager setup.
614-
withdrawalCfg := &withdraw.ManagerConfig{
615-
StaticAddressServerClient: staticAddressClient,
616-
AddressManager: staticAddressManager,
617-
DepositManager: depositManager,
618-
WalletKit: d.lnd.WalletKit,
619-
ChainParams: d.lnd.ChainParams,
620-
ChainNotifier: d.lnd.ChainNotifier,
621-
Signer: d.lnd.Signer,
622-
}
623-
withdrawalManager = withdraw.NewManager(withdrawalCfg)
624-
625-
// Static address loop-in manager setup.
626-
staticAddressLoopInStore := loopin.NewSqlStore(
627-
loopdb.NewTypedStore[loopin.Querier](baseDb),
628-
clock.NewDefaultClock(), d.lnd.ChainParams,
629-
)
630-
631-
staticLoopInManager = loopin.NewManager(&loopin.Config{
632-
Server: staticAddressClient,
633-
QuoteGetter: swapClient.Server,
634-
LndClient: d.lnd.Client,
635-
InvoicesClient: d.lnd.Invoices,
636-
NodePubkey: d.lnd.NodePubkey,
637-
AddressManager: staticAddressManager,
638-
DepositManager: depositManager,
639-
Store: staticAddressLoopInStore,
640-
WalletKit: d.lnd.WalletKit,
641-
ChainNotifier: d.lnd.ChainNotifier,
642-
NotificationManager: notificationManager,
643-
ChainParams: d.lnd.ChainParams,
644-
Signer: d.lnd.Signer,
645-
ValidateLoopInContract: loop.ValidateLoopInContract,
646-
MaxStaticAddrHtlcFeePercentage: d.cfg.MaxStaticAddrHtlcFeePercentage,
647-
MaxStaticAddrHtlcBackupFeePercentage: d.cfg.MaxStaticAddrHtlcBackupFeePercentage,
648-
})
649651
}
650652

651653
// Now finally fully initialize the swap client RPC server instance.

staticaddr/withdraw/manager.go

+37-24
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,39 @@ func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
380380
).FeePerKWeight()
381381
}
382382

383+
// We'll now check the selected fee rate leaves a withdrawal output that
384+
// is above the dust limit. If not we cancel the withdrawal instead of
385+
// requesting a signature from the server.
386+
addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
387+
ctx,
388+
)
389+
if err != nil {
390+
return nil, fmt.Errorf("couldn't get confirmation height for "+
391+
"deposit, %w", err)
392+
}
393+
394+
// Calculate the fee value in satoshis.
383395
outpoints := toOutpoints(deposits)
396+
weight, err := withdrawalFee(len(outpoints), withdrawalAddress)
397+
if err != nil {
398+
return nil, err
399+
}
400+
feeValue := withdrawalSweepFeeRate.FeeForWeight(weight)
401+
402+
var (
403+
prevOuts = m.toPrevOuts(deposits, addressParams.PkScript)
404+
totalValue = withdrawalValue(prevOuts)
405+
outputValue = int64(totalValue) - int64(feeValue)
406+
// P2TRSize calculates a dust limit based on a 40 byte maximum
407+
// size witness output.
408+
dustLimit = lnwallet.DustLimitForSize(input.P2TRSize)
409+
)
410+
411+
if outputValue < int64(dustLimit) {
412+
return nil, fmt.Errorf("withdrawal output value %d sats "+
413+
"below dust limit %d sats", outputValue, dustLimit)
414+
}
415+
384416
resp, err := m.cfg.StaticAddressServerClient.ServerWithdrawDeposits(
385417
ctx, &staticaddressrpc.ServerWithdrawRequest{
386418
Outpoints: toPrevoutInfo(outpoints),
@@ -393,19 +425,9 @@ func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
393425
return nil, err
394426
}
395427

396-
addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
397-
ctx,
398-
)
399-
if err != nil {
400-
return nil, fmt.Errorf("couldn't get confirmation height for "+
401-
"deposit, %w", err)
402-
}
403-
404-
prevOuts := m.toPrevOuts(deposits, addressParams.PkScript)
405-
totalValue := withdrawalValue(prevOuts)
428+
withdrawalOutputValue := int64(totalValue - feeValue)
406429
withdrawalTx, err := m.createWithdrawalTx(
407-
outpoints, totalValue, withdrawalAddress,
408-
withdrawalSweepFeeRate,
430+
outpoints, withdrawalOutputValue, withdrawalAddress,
409431
)
410432
if err != nil {
411433
return nil, err
@@ -613,9 +635,8 @@ func byteSliceTo66ByteSlice(b []byte) ([musig2.PubNonceSize]byte, error) {
613635
}
614636

615637
func (m *Manager) createWithdrawalTx(outpoints []wire.OutPoint,
616-
withdrawlAmount btcutil.Amount, clientSweepAddress btcutil.Address,
617-
feeRate chainfee.SatPerKWeight) (*wire.MsgTx,
618-
error) {
638+
withdrawlOutputValue int64, clientSweepAddress btcutil.Address) (
639+
*wire.MsgTx, error) {
619640

620641
// First Create the tx.
621642
msgTx := wire.NewMsgTx(2)
@@ -628,22 +649,14 @@ func (m *Manager) createWithdrawalTx(outpoints []wire.OutPoint,
628649
})
629650
}
630651

631-
// Estimate the fee.
632-
weight, err := withdrawalFee(len(outpoints), clientSweepAddress)
633-
if err != nil {
634-
return nil, err
635-
}
636-
637652
pkscript, err := txscript.PayToAddrScript(clientSweepAddress)
638653
if err != nil {
639654
return nil, err
640655
}
641656

642-
fee := feeRate.FeeForWeight(weight)
643-
644657
// Create the sweep output
645658
sweepOutput := &wire.TxOut{
646-
Value: int64(withdrawlAmount) - int64(fee),
659+
Value: withdrawlOutputValue,
647660
PkScript: pkscript,
648661
}
649662

0 commit comments

Comments
 (0)