Skip to content

Commit d4678f9

Browse files
authored
Merge pull request #665 from hieblmi/finalize-insuff-funds-loop-in
Finalize swap if wallet holds insufficient confirmed funds
2 parents 9c1b745 + caa6463 commit d4678f9

File tree

7 files changed

+162
-119
lines changed

7 files changed

+162
-119
lines changed

client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ var (
4343
// is too soon for us.
4444
ErrExpiryTooFar = errors.New("swap expiry too far")
4545

46+
// ErrInsufficientBalance indicates insufficient confirmed balance to
47+
// publish a swap.
48+
ErrInsufficientBalance = errors.New("insufficient confirmed balance")
49+
4650
// serverRPCTimeout is the maximum time a gRPC request to the server
4751
// should be allowed to take.
4852
serverRPCTimeout = 30 * time.Second

loopd/swapclient_server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
291291
case loopdb.StateFailAbandoned:
292292
failureReason = clientrpc.FailureReason_FAILURE_REASON_ABANDONED
293293

294+
case loopdb.StateFailInsufficientConfirmedBalance:
295+
failureReason = clientrpc.FailureReason_FAILURE_REASON_INSUFFICIENT_CONFIRMED_BALANCE
296+
294297
default:
295298
return nil, fmt.Errorf("unknown swap state: %v", loopSwap.State)
296299
}

loopdb/swapstate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const (
6868
// StateFailAbandoned indicates that a swap has been abandoned. Its
6969
// execution has been canceled. It won't further be processed.
7070
StateFailAbandoned SwapState = 11
71+
72+
// StateFailInsufficientConfirmedBalance indicates that the swap wasn't
73+
// published due to insufficient confirmed balance.
74+
StateFailInsufficientConfirmedBalance SwapState = 12
7175
)
7276

7377
// SwapStateType defines the types of swap states that exist. Every swap state
@@ -153,6 +157,9 @@ func (s SwapState) String() string {
153157
case StateFailAbandoned:
154158
return "FailAbandoned"
155159

160+
case StateFailInsufficientConfirmedBalance:
161+
return "InsufficientConfirmedBalance"
162+
156163
default:
157164
return "Unknown"
158165
}

loopin.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,13 @@ func (s *loopInSwap) execute(mainCtx context.Context,
530530
// error occurs.
531531
err = s.executeSwap(mainCtx)
532532

533+
// If there are insufficient confirmed funds to publish the swap, we
534+
// finalize its state so a new swap will be published if funds become
535+
// available.
536+
if errors.Is(err, ErrInsufficientBalance) {
537+
return err
538+
}
539+
533540
// Stop the execution if the swap has been abandoned.
534541
if err != nil && s.state == loopdb.StateFailAbandoned {
535542
return err
@@ -781,7 +788,14 @@ func (s *loopInSwap) publishOnChainHtlc(ctx context.Context) (bool, error) {
781788
}}, feeRate, labels.LoopInHtlcLabel(swap.ShortHash(&s.hash)),
782789
)
783790
if err != nil {
784-
return false, fmt.Errorf("send outputs: %v", err)
791+
s.log.Errorf("send outputs: %v", err)
792+
s.setState(loopdb.StateFailInsufficientConfirmedBalance)
793+
794+
// If we cannot send out this update, there is nothing we can
795+
// do.
796+
_ = s.persistAndAnnounceState(ctx)
797+
798+
return false, ErrInsufficientBalance
785799
}
786800

787801
txHash := tx.TxHash()

looprpc/client.pb.go

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

looprpc/client.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,12 @@ enum FailureReason {
535535
the client manually abandoned the swap.
536536
*/
537537
FAILURE_REASON_ABANDONED = 7;
538+
539+
/*
540+
FAILURE_REASON_INSUFFICIENT_CONFIRMED_BALANCE indicates that a swap
541+
wasn't published due to insufficient confirmed balance.
542+
*/
543+
FAILURE_REASON_INSUFFICIENT_CONFIRMED_BALANCE = 8;
538544
}
539545

540546
message ListSwapsRequest {

looprpc/client.swagger.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,11 @@
555555
"FAILURE_REASON_INSUFFICIENT_VALUE",
556556
"FAILURE_REASON_TEMPORARY",
557557
"FAILURE_REASON_INCORRECT_AMOUNT",
558-
"FAILURE_REASON_ABANDONED"
558+
"FAILURE_REASON_ABANDONED",
559+
"FAILURE_REASON_INSUFFICIENT_CONFIRMED_BALANCE"
559560
],
560561
"default": "FAILURE_REASON_NONE",
561-
"description": " - FAILURE_REASON_NONE: FAILURE_REASON_NONE is set when the swap did not fail, it is either in\nprogress or succeeded.\n - FAILURE_REASON_OFFCHAIN: FAILURE_REASON_OFFCHAIN indicates that a loop out failed because it wasn't\npossible to find a route for one or both off chain payments that met the fee\nand timelock limits required.\n - FAILURE_REASON_TIMEOUT: FAILURE_REASON_TIMEOUT indicates that the swap failed because on chain htlc\ndid not confirm before its expiry, or it confirmed too late for us to reveal\nour preimage and claim.\n - FAILURE_REASON_SWEEP_TIMEOUT: FAILURE_REASON_SWEEP_TIMEOUT indicates that a loop out permanently failed\nbecause the on chain htlc wasn't swept before the server revoked the\nhtlc.\n - FAILURE_REASON_INSUFFICIENT_VALUE: FAILURE_REASON_INSUFFICIENT_VALUE indicates that a loop out has failed\nbecause the on chain htlc had a lower value than requested.\n - FAILURE_REASON_TEMPORARY: FAILURE_REASON_TEMPORARY indicates that a swap cannot continue due to an\ninternal error. Manual intervention such as a restart is required.\n - FAILURE_REASON_INCORRECT_AMOUNT: FAILURE_REASON_INCORRECT_AMOUNT indicates that a loop in permanently failed\nbecause the amount extended by an external loop in htlc is insufficient.\n - FAILURE_REASON_ABANDONED: FAILURE_REASON_ABANDONED indicates that a swap permanently failed because\nthe client manually abandoned the swap."
562+
"description": " - FAILURE_REASON_NONE: FAILURE_REASON_NONE is set when the swap did not fail, it is either in\nprogress or succeeded.\n - FAILURE_REASON_OFFCHAIN: FAILURE_REASON_OFFCHAIN indicates that a loop out failed because it wasn't\npossible to find a route for one or both off chain payments that met the fee\nand timelock limits required.\n - FAILURE_REASON_TIMEOUT: FAILURE_REASON_TIMEOUT indicates that the swap failed because on chain htlc\ndid not confirm before its expiry, or it confirmed too late for us to reveal\nour preimage and claim.\n - FAILURE_REASON_SWEEP_TIMEOUT: FAILURE_REASON_SWEEP_TIMEOUT indicates that a loop out permanently failed\nbecause the on chain htlc wasn't swept before the server revoked the\nhtlc.\n - FAILURE_REASON_INSUFFICIENT_VALUE: FAILURE_REASON_INSUFFICIENT_VALUE indicates that a loop out has failed\nbecause the on chain htlc had a lower value than requested.\n - FAILURE_REASON_TEMPORARY: FAILURE_REASON_TEMPORARY indicates that a swap cannot continue due to an\ninternal error. Manual intervention such as a restart is required.\n - FAILURE_REASON_INCORRECT_AMOUNT: FAILURE_REASON_INCORRECT_AMOUNT indicates that a loop in permanently failed\nbecause the amount extended by an external loop in htlc is insufficient.\n - FAILURE_REASON_ABANDONED: FAILURE_REASON_ABANDONED indicates that a swap permanently failed because\nthe client manually abandoned the swap.\n - FAILURE_REASON_INSUFFICIENT_CONFIRMED_BALANCE: FAILURE_REASON_INSUFFICIENT_CONFIRMED_BALANCE indicates that a swap\nwasn't published due to insufficient confirmed balance."
562563
},
563564
"looprpcGetInfoResponse": {
564565
"type": "object",

0 commit comments

Comments
 (0)