Skip to content

Commit d0963ba

Browse files
committed
loopd: handle unconfirmed deposits in RPC responses
Extract depositBlocksUntilExpiry helper that returns the full CSV value for unconfirmed deposits (ConfirmationHeight <= 0) since their CSV has not started. Use it in ListStaticAddressSwaps and populateBlocksUntilExpiry. In GetStaticAddressSummary, derive the unconfirmed value from deposits in the Deposited state with no confirmation height instead of issuing a separate wallet ListUnspent call.
1 parent 1ce09ef commit d0963ba

2 files changed

Lines changed: 64 additions & 19 deletions

File tree

loopd/swapclient_server.go

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,22 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
17781778
}, err
17791779
}
17801780

1781+
// confirmedDeposits filters the given deposits and returns only those that have
1782+
// a positive confirmation height, i.e. deposits that have been confirmed
1783+
// on-chain.
1784+
func confirmedDeposits(deposits []*deposit.Deposit) []*deposit.Deposit {
1785+
confirmed := make([]*deposit.Deposit, 0, len(deposits))
1786+
for _, d := range deposits {
1787+
if d.ConfirmationHeight <= 0 {
1788+
continue
1789+
}
1790+
1791+
confirmed = append(confirmed, d)
1792+
}
1793+
1794+
return confirmed
1795+
}
1796+
17811797
// ListStaticAddressDeposits returns a list of all sufficiently confirmed
17821798
// deposits behind the static address and displays properties like value,
17831799
// state or blocks til expiry.
@@ -1951,9 +1967,10 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
19511967
protoDeposits = make([]*looprpc.Deposit, 0, len(ds))
19521968
for _, d := range ds {
19531969
state := toClientDepositState(d.GetState())
1954-
blocksUntilExpiry := d.ConfirmationHeight +
1955-
int64(addrParams.Expiry) -
1956-
int64(lndInfo.BlockHeight)
1970+
blocksUntilExpiry := depositBlocksUntilExpiry(
1971+
d.ConfirmationHeight, addrParams.Expiry,
1972+
int64(lndInfo.BlockHeight),
1973+
)
19571974

19581975
pd := &looprpc.Deposit{
19591976
Id: d.ID[:],
@@ -2015,23 +2032,16 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
20152032
htlcTimeoutSwept int64
20162033
)
20172034

2018-
// Value unconfirmed.
2019-
utxos, err := s.staticAddressManager.ListUnspent(
2020-
ctx, 0, deposit.MinConfs-1,
2021-
)
2022-
if err != nil {
2023-
return nil, err
2024-
}
2025-
for _, u := range utxos {
2026-
valueUnconfirmed += int64(u.Value)
2027-
}
2028-
2029-
// Confirmed total values by category.
2035+
// Total values by category.
20302036
for _, d := range allDeposits {
20312037
value := int64(d.Value)
20322038
switch d.GetState() {
20332039
case deposit.Deposited:
2034-
valueDeposited += value
2040+
if d.ConfirmationHeight <= 0 {
2041+
valueUnconfirmed += value
2042+
} else {
2043+
valueDeposited += value
2044+
}
20352045

20362046
case deposit.Expired:
20372047
valueExpired += value
@@ -2174,13 +2184,27 @@ func (s *swapClientServer) populateBlocksUntilExpiry(ctx context.Context,
21742184
return err
21752185
}
21762186
for i := range len(deposits) {
2177-
deposits[i].BlocksUntilExpiry =
2178-
deposits[i].ConfirmationHeight +
2179-
int64(params.Expiry) - bestBlockHeight
2187+
deposits[i].BlocksUntilExpiry = depositBlocksUntilExpiry(
2188+
deposits[i].ConfirmationHeight, params.Expiry,
2189+
bestBlockHeight,
2190+
)
21802191
}
21812192
return nil
21822193
}
21832194

2195+
// depositBlocksUntilExpiry returns the remaining blocks until a deposit
2196+
// expires. Unconfirmed deposits return the full CSV value because the timeout
2197+
// has not started yet.
2198+
func depositBlocksUntilExpiry(confirmationHeight int64, expiry uint32,
2199+
bestBlockHeight int64) int64 {
2200+
2201+
if confirmationHeight <= 0 {
2202+
return int64(expiry)
2203+
}
2204+
2205+
return confirmationHeight + int64(expiry) - bestBlockHeight
2206+
}
2207+
21842208
// StaticOpenChannel initiates an open channel request using static address
21852209
// deposits.
21862210
func (s *swapClientServer) StaticOpenChannel(ctx context.Context,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package loopd
2+
3+
import "testing"
4+
5+
// TestDepositBlocksUntilExpiry checks blocks-until-expiry handling for
6+
// confirmed and unconfirmed deposits.
7+
func TestDepositBlocksUntilExpiry(t *testing.T) {
8+
t.Run("unconfirmed", func(t *testing.T) {
9+
if blocks := depositBlocksUntilExpiry(0, 144, 500); blocks != 144 {
10+
t.Fatalf("expected 144 blocks for unconfirmed deposit, got %d",
11+
blocks)
12+
}
13+
})
14+
15+
t.Run("confirmed", func(t *testing.T) {
16+
if blocks := depositBlocksUntilExpiry(450, 144, 500); blocks != 94 {
17+
t.Fatalf("expected 94 blocks until expiry, got %d",
18+
blocks)
19+
}
20+
})
21+
}

0 commit comments

Comments
 (0)