Skip to content

Commit 124eeea

Browse files
authored
Merge pull request #378 from carlaKC/383-cancelation
loopout: Add swap cancelation for swaps that cannot route off-chain
2 parents 0a8e037 + 6350d87 commit 124eeea

14 files changed

+1085
-119
lines changed

client.go

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
142142
sweeper: sweeper,
143143
createExpiryTimer: config.CreateExpiryTimer,
144144
loopOutMaxParts: cfg.LoopOutMaxParts,
145+
cancelSwap: swapServerClient.CancelLoopOutSwap,
145146
})
146147

147148
client := &Client{

client_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func TestFailOffchain(t *testing.T) {
100100
signalPrepaymentResult(
101101
errors.New(lndclient.PaymentResultUnknownPaymentHash),
102102
)
103+
<-ctx.serverMock.cancelSwap
103104
ctx.assertStatus(loopdb.StateFailOffchainPayments)
104105

105106
ctx.assertStoreFinished(loopdb.StateFailOffchainPayments)

executor.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type executorConfig struct {
2525
createExpiryTimer func(expiry time.Duration) <-chan time.Time
2626

2727
loopOutMaxParts uint32
28+
29+
cancelSwap func(ctx context.Context, details *outCancelDetails) error
2830
}
2931

3032
// executor is responsible for executing swaps.
@@ -138,13 +140,17 @@ func (s *executor) run(mainCtx context.Context,
138140
go func() {
139141
defer s.wg.Done()
140142

141-
newSwap.execute(mainCtx, &executeConfig{
143+
err := newSwap.execute(mainCtx, &executeConfig{
142144
statusChan: statusChan,
143145
sweeper: s.sweeper,
144146
blockEpochChan: queue.ChanOut(),
145147
timerFactory: s.executorConfig.createExpiryTimer,
146148
loopOutMaxParts: s.executorConfig.loopOutMaxParts,
149+
cancelSwap: s.executorConfig.cancelSwap,
147150
}, height)
151+
if err != nil && err != context.Canceled {
152+
log.Errorf("Execute error: %v", err)
153+
}
148154

149155
select {
150156
case swapDoneChan <- swapID:

loopdb/protocol_version.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ const (
3939
// invoice so that the server can perform a multi-path probe.
4040
ProtocolVersionMultiLoopIn ProtocolVersion = 6
4141

42+
// ProtocolVersionLoopOutCancel indicates that the client supports
43+
// canceling loop out swaps.
44+
ProtocolVersionLoopOutCancel = 7
45+
4246
// ProtocolVersionUnrecorded is set for swaps were created before we
4347
// started saving protocol version with swaps.
4448
ProtocolVersionUnrecorded ProtocolVersion = math.MaxUint32
4549

4650
// CurrentRPCProtocolVersion defines the version of the RPC protocol
4751
// that is currently supported by the loop client.
48-
CurrentRPCProtocolVersion = looprpc.ProtocolVersion_MULTI_LOOP_IN
52+
CurrentRPCProtocolVersion = looprpc.ProtocolVersion_LOOP_OUT_CANCEL
4953

5054
// CurrentInternalProtocolVersion defines the RPC current protocol in
5155
// the internal representation.
@@ -81,6 +85,9 @@ func (p ProtocolVersion) String() string {
8185
case ProtocolVersionHtlcV2:
8286
return "HTLC V2"
8387

88+
case ProtocolVersionLoopOutCancel:
89+
return "Loop Out Cancel"
90+
8491
default:
8592
return "Unknown"
8693
}

loopdb/protocol_version_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestProtocolVersionSanity(t *testing.T) {
2121
ProtocolVersionUserExpiryLoopOut,
2222
ProtocolVersionHtlcV2,
2323
ProtocolVersionMultiLoopIn,
24+
ProtocolVersionLoopOutCancel,
2425
}
2526

2627
rpcVersions := [...]looprpc.ProtocolVersion{
@@ -31,6 +32,7 @@ func TestProtocolVersionSanity(t *testing.T) {
3132
looprpc.ProtocolVersion_USER_EXPIRY_LOOP_OUT,
3233
looprpc.ProtocolVersion_HTLC_V2,
3334
looprpc.ProtocolVersion_MULTI_LOOP_IN,
35+
looprpc.ProtocolVersion_LOOP_OUT_CANCEL,
3436
}
3537

3638
require.Equal(t, len(versions), len(rpcVersions))

loopin_testcontext_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func newLoopInTestContext(t *testing.T) *loopInTestContext {
3939
sweeper: &sweeper,
4040
blockEpochChan: blockEpochChan,
4141
timerFactory: timerFactory,
42+
cancelSwap: server.CancelLoopOutSwap,
4243
}
4344

4445
return &loopInTestContext{

0 commit comments

Comments
 (0)