Skip to content

Commit e2c54bd

Browse files
authored
Merge pull request #259 from carlaKC/252-loopstates
looprpc: surface all failure reasons for failed swaps
2 parents d046a68 + a9849bb commit e2c54bd

File tree

5 files changed

+310
-112
lines changed

5 files changed

+310
-112
lines changed

cmd/loop/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,22 @@ func parseAmt(text string) (btcutil.Amount, error) {
277277
}
278278

279279
func logSwap(swap *looprpc.SwapStatus) {
280+
// If our swap failed, we add our failure reason to the state.
281+
swapState := fmt.Sprintf("%v", swap.State)
282+
if swap.State == looprpc.SwapState_FAILED {
283+
swapState = fmt.Sprintf("%v (%v)", swapState, swap.FailureReason)
284+
}
285+
280286
if swap.Type == looprpc.SwapType_LOOP_OUT {
281287
fmt.Printf("%v %v %v %v - %v",
282288
time.Unix(0, swap.LastUpdateTime).Format(time.RFC3339),
283-
swap.Type, swap.State, btcutil.Amount(swap.Amt),
289+
swap.Type, swapState, btcutil.Amount(swap.Amt),
284290
swap.HtlcAddressP2Wsh,
285291
)
286292
} else {
287293
fmt.Printf("%v %v %v %v -",
288294
time.Unix(0, swap.LastUpdateTime).Format(time.RFC3339),
289-
swap.Type, swap.State, btcutil.Amount(swap.Amt))
295+
swap.Type, swapState, btcutil.Amount(swap.Amt))
290296
if swap.HtlcAddressP2Wsh != "" {
291297
fmt.Printf(" P2WSH: %v", swap.HtlcAddressP2Wsh)
292298
}

loopd/swapclient_server.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,57 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
121121
func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
122122
*looprpc.SwapStatus, error) {
123123

124-
var state looprpc.SwapState
124+
var (
125+
state looprpc.SwapState
126+
failureReason = looprpc.FailureReason_FAILURE_REASON_NONE
127+
)
128+
129+
// Set our state var for non-failure states. If we get a failure, we
130+
// will update our failure reason. To remain backwards compatible with
131+
// previous versions where we squashed all failure reasons to a single
132+
// failure state, we set a failure reason for all our different failure
133+
// states, and set our failed state for all of them.
125134
switch loopSwap.State {
126135
case loopdb.StateInitiated:
127136
state = looprpc.SwapState_INITIATED
137+
128138
case loopdb.StatePreimageRevealed:
129139
state = looprpc.SwapState_PREIMAGE_REVEALED
140+
130141
case loopdb.StateHtlcPublished:
131142
state = looprpc.SwapState_HTLC_PUBLISHED
143+
132144
case loopdb.StateInvoiceSettled:
133145
state = looprpc.SwapState_INVOICE_SETTLED
146+
134147
case loopdb.StateSuccess:
135148
state = looprpc.SwapState_SUCCESS
149+
150+
case loopdb.StateFailOffchainPayments:
151+
failureReason = looprpc.FailureReason_FAILURE_REASON_OFFCHAIN
152+
153+
case loopdb.StateFailTimeout:
154+
failureReason = looprpc.FailureReason_FAILURE_REASON_TIMEOUT
155+
156+
case loopdb.StateFailSweepTimeout:
157+
failureReason = looprpc.FailureReason_FAILURE_REASON_SWEEP_TIMEOUT
158+
159+
case loopdb.StateFailInsufficientValue:
160+
failureReason = looprpc.FailureReason_FAILURE_REASON_INSUFFICIENT_VALUE
161+
162+
case loopdb.StateFailTemporary:
163+
failureReason = looprpc.FailureReason_FAILURE_REASON_TEMPORARY
164+
165+
case loopdb.StateFailIncorrectHtlcAmt:
166+
failureReason = looprpc.FailureReason_FAILURE_REASON_INCORRECT_AMOUNT
167+
136168
default:
137-
// Return less granular status over rpc.
169+
return nil, fmt.Errorf("unknown swap state: %v", loopSwap.State)
170+
}
171+
172+
// If we have a failure reason, we have a failure state, so should use
173+
// our catchall failed state.
174+
if failureReason != looprpc.FailureReason_FAILURE_REASON_NONE {
138175
state = looprpc.SwapState_FAILED
139176
}
140177

@@ -167,6 +204,7 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
167204
Id: loopSwap.SwapHash.String(),
168205
IdBytes: loopSwap.SwapHash[:],
169206
State: state,
207+
FailureReason: failureReason,
170208
InitiationTime: loopSwap.InitiationTime.UnixNano(),
171209
LastUpdateTime: loopSwap.LastUpdate.UnixNano(),
172210
HtlcAddress: htlcAddress,

0 commit comments

Comments
 (0)