Skip to content

Commit 5b684e2

Browse files
authored
Merge pull request #193 from carlaKC/40-clientpreimagepush
loopout: push preimage to server to speed up off chain claim
2 parents 9571371 + 65c8476 commit 5b684e2

15 files changed

+635
-112
lines changed

client.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,7 @@ func (s *Client) Run(ctx context.Context,
315315
func (s *Client) resumeSwaps(ctx context.Context,
316316
loopOutSwaps []*loopdb.LoopOut, loopInSwaps []*loopdb.LoopIn) {
317317

318-
swapCfg := &swapConfig{
319-
lnd: s.lndServices,
320-
store: s.Store,
321-
}
318+
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
322319

323320
for _, pend := range loopOutSwaps {
324321
if pend.State().State.Type() != loopdb.StateTypePending {
@@ -369,11 +366,7 @@ func (s *Client) LoopOut(globalCtx context.Context,
369366

370367
// Create a new swap object for this swap.
371368
initiationHeight := s.executor.height()
372-
swapCfg := &swapConfig{
373-
lnd: s.lndServices,
374-
store: s.Store,
375-
server: s.Server,
376-
}
369+
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
377370
swap, err := newLoopOutSwap(
378371
globalCtx, swapCfg, initiationHeight, request,
379372
)
@@ -486,13 +479,9 @@ func (s *Client) LoopIn(globalCtx context.Context,
486479

487480
// Create a new swap object for this swap.
488481
initiationHeight := s.executor.height()
489-
swapCfg := swapConfig{
490-
lnd: s.lndServices,
491-
store: s.Store,
492-
server: s.Server,
493-
}
482+
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
494483
swap, err := newLoopInSwap(
495-
globalCtx, &swapCfg, initiationHeight, request,
484+
globalCtx, swapCfg, initiationHeight, request,
496485
)
497486
if err != nil {
498487
return nil, err

client_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"github.com/lightninglabs/loop/lndclient"
1313
"github.com/lightninglabs/loop/loopdb"
1414
"github.com/lightninglabs/loop/test"
15+
"github.com/lightningnetwork/lnd/lnrpc"
1516
"github.com/lightningnetwork/lnd/lntypes"
17+
"github.com/stretchr/testify/require"
1618
)
1719

1820
var (
@@ -261,6 +263,10 @@ func testSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
261263

262264
ctx.AssertRegisterSpendNtfn(confIntent.PkScript)
263265

266+
// Assert that a call to track payment was sent, and respond with status
267+
// in flight so that our swap will push its preimage to the server.
268+
ctx.trackPayment(lnrpc.Payment_IN_FLIGHT)
269+
264270
// Publish tick.
265271
ctx.expiryChan <- testTime
266272

@@ -287,6 +293,13 @@ func testSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
287293
ctx.T.Fatalf("incorrect preimage")
288294
}
289295

296+
// Since we successfully published our sweep, we expect the preimage to
297+
// have been pushed to our mock server.
298+
preimage, err := lntypes.MakePreimage(clientPreImage)
299+
require.NoError(ctx.T, err)
300+
301+
ctx.assertPreimagePush(preimage)
302+
290303
// Simulate server pulling payment.
291304
signalSwapPaymentResult(nil)
292305

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191212020323-b444784ce75d
1414
github.com/lightningnetwork/lnd v0.10.0-beta.rc6.0.20200603030653-09bb9db78246
1515
github.com/lightningnetwork/lnd/queue v1.0.4
16+
github.com/stretchr/testify v1.4.0
1617
github.com/urfave/cli v1.20.0
1718
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
1819
golang.org/x/text v0.3.2 // indirect

go.sum

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2-
git.schwanenlied.me/yawning/bsaes.git v0.0.0-20180720073208-c0276d75487e h1:F2x1bq7RaNCIuqYpswggh1+c1JmwdnkHNC9wy1KDip0=
32
git.schwanenlied.me/yawning/bsaes.git v0.0.0-20180720073208-c0276d75487e/go.mod h1:BWqTsj8PgcPriQJGl7el20J/7TuT1d/hSyFDXMEpoEo=
43
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
54
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
6-
github.com/NebulousLabs/fastrand v0.0.0-20181203155948-6fb6489aac4e h1:n+DcnTNkQnHlwpsrHoQtkrJIO7CBx029fw6oR4vIob4=
75
github.com/NebulousLabs/fastrand v0.0.0-20181203155948-6fb6489aac4e/go.mod h1:Bdzq+51GR4/0DIhaICZEOm+OHvXGwwB2trKZ8B4Y6eQ=
8-
github.com/NebulousLabs/go-upnp v0.0.0-20180202185039-29b680b06c82 h1:MG93+PZYs9PyEsj/n5/haQu2gK0h4tUtSy9ejtMwWa0=
96
github.com/NebulousLabs/go-upnp v0.0.0-20180202185039-29b680b06c82/go.mod h1:GbuBk21JqF+driLX3XtJYNZjGa45YDoa9IqCTzNSfEc=
107
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
11-
github.com/Yawning/aez v0.0.0-20180114000226-4dad034d9db2 h1:2be4ykKKov3M1yISM2E8gnGXZ/N2SsPawfnGiXxaYEU=
128
github.com/Yawning/aez v0.0.0-20180114000226-4dad034d9db2/go.mod h1:9pIqrY6SXNL8vjRQE5Hd/OL5GyK/9MrGUWs87z/eFfk=
139
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
1410
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
@@ -23,14 +19,12 @@ github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
2319
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
2420
github.com/btcsuite/btcd v0.0.0-20190629003639-c26ffa870fd8/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
2521
github.com/btcsuite/btcd v0.0.0-20190824003749-130ea5bddde3/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
26-
github.com/btcsuite/btcd v0.20.1-beta h1:Ik4hyJqN8Jfyv3S4AGBOmyouMsYE3EdYODkMbQjwPGw=
2722
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
2823
github.com/btcsuite/btcd v0.20.1-beta.0.20200513120220-b470eee47728/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
2924
github.com/btcsuite/btcd v0.20.1-beta.0.20200515232429-9f0179fd2c46 h1:QyTpiR5nQe94vza2qkvf7Ns8XX2Rjh/vdIhO3RzGj4o=
3025
github.com/btcsuite/btcd v0.20.1-beta.0.20200515232429-9f0179fd2c46/go.mod h1:Yktc19YNjh/Iz2//CX0vfRTS4IJKM/RKO5YZ9Fn+Pgo=
3126
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo=
3227
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
33-
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng=
3428
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
3529
github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2uts=
3630
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
@@ -48,7 +42,6 @@ github.com/btcsuite/btcwallet/walletdb v1.0.0/go.mod h1:bZTy9RyYZh9fLnSua+/CD48T
4842
github.com/btcsuite/btcwallet/walletdb v1.2.0/go.mod h1:9cwc1Yyg4uvd4ZdfdoMnALji+V9gfWSMfxEdLdR5Vwc=
4943
github.com/btcsuite/btcwallet/walletdb v1.3.1 h1:lW1Ac3F1jJY4K11P+YQtRNcP5jFk27ASfrV7C6mvRU0=
5044
github.com/btcsuite/btcwallet/walletdb v1.3.1/go.mod h1:9cwc1Yyg4uvd4ZdfdoMnALji+V9gfWSMfxEdLdR5Vwc=
51-
github.com/btcsuite/btcwallet/wtxmgr v1.0.0 h1:aIHgViEmZmZfe0tQQqF1xyd2qBqFWxX5vZXkkbjtbeA=
5245
github.com/btcsuite/btcwallet/wtxmgr v1.0.0/go.mod h1:vc4gBprll6BP0UJ+AIGDaySoc7MdAmZf8kelfNb8CFY=
5346
github.com/btcsuite/btcwallet/wtxmgr v1.1.1-0.20200515224913-e0e62245ecbe h1:yQbJVYfsKbdqDQNLxd4hhiLSiMkIygefW5mSHMsdKpc=
5447
github.com/btcsuite/btcwallet/wtxmgr v1.1.1-0.20200515224913-e0e62245ecbe/go.mod h1:OwC0W0HhUszbWdvJvH6xvgabKSJ0lXl11YbmmqF9YXQ=
@@ -135,9 +128,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.12.2 h1:D0EVSTwQoQOyfY35QNSuPJA4jpZRtk
135128
github.com/grpc-ecosystem/grpc-gateway v1.12.2/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
136129
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
137130
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
138-
github.com/jackpal/gateway v1.0.5 h1:qzXWUJfuMdlLMtt0a3Dgt+xkWQiA5itDEITVJtuSwMc=
139131
github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA=
140-
github.com/jackpal/go-nat-pmp v0.0.0-20170405195558-28a68d0c24ad h1:heFfj7z0pGsNCekUlsFhO2jstxO4b5iQ665LjwM5mDc=
141132
github.com/jackpal/go-nat-pmp v0.0.0-20170405195558-28a68d0c24ad/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
142133
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
143134
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
@@ -178,7 +169,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
178169
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
179170
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI/f/O0Avg7t8sqkPo78HFzjmeYFl6DPnc=
180171
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk=
181-
github.com/lightninglabs/neutrino v0.11.0 h1:lPpYFCtsfJX2W5zI4pWycPmbbBdr7zU+BafYdLoD6k0=
182172
github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg=
183173
github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200 h1:j4iZ1XlUAPQmW6oSzMcJGILYsRHNs+4O3Gk+2Ms5Dww=
184174
github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200/go.mod h1:MlZmoKa7CJP3eR1s5yB7Rm5aSyadpKkxqAwLQmog7N0=
@@ -191,7 +181,6 @@ github.com/lightningnetwork/lnd v0.10.0-beta.rc6.0.20200603030653-09bb9db78246/g
191181
github.com/lightningnetwork/lnd/cert v1.0.2/go.mod h1:fmtemlSMf5t4hsQmcprSoOykypAPp+9c+0d0iqTScMo=
192182
github.com/lightningnetwork/lnd/clock v1.0.0 h1:U9uknV7DwBsNUzc+x11tx1bPvoXiEYcJ9wrIemRibKE=
193183
github.com/lightningnetwork/lnd/clock v1.0.0/go.mod h1:KnQudQ6w0IAMZi1SgvecLZQZ43ra2vpDNj7H/aasemg=
194-
github.com/lightningnetwork/lnd/queue v1.0.1 h1:jzJKcTy3Nj5lQrooJ3aaw9Lau3I0IwvQR5sqtjdv2R0=
195184
github.com/lightningnetwork/lnd/queue v1.0.1/go.mod h1:vaQwexir73flPW43Mrm7JOgJHmcEFBWWSl9HlyASoms=
196185
github.com/lightningnetwork/lnd/queue v1.0.4 h1:8Dq3vxAFSACPy+pKN88oPFhuCpCoAAChPBwa4BJxH4k=
197186
github.com/lightningnetwork/lnd/queue v1.0.4/go.mod h1:YTkTVZCxz8tAYreH27EO3s8572ODumWrNdYW2E/YKxg=
@@ -256,7 +245,6 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
256245
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
257246
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
258247
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
259-
github.com/tv42/zbase32 v0.0.0-20160707012821-501572607d02 h1:tcJ6OjwOMvExLlzrAVZute09ocAGa7KqOON60++Gz4E=
260248
github.com/tv42/zbase32 v0.0.0-20160707012821-501572607d02/go.mod h1:tHlrkM198S068ZqfrO6S8HsoJq2bF3ETfTL+kt4tInY=
261249
github.com/urfave/cli v1.18.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
262250
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
@@ -280,7 +268,6 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
280268
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
281269
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
282270
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
283-
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d h1:2+ZP7EfsZV7Vvmx3TIqSlSzATMkTAKqM14YGFPoSKjI=
284271
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
285272
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
286273
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@@ -320,7 +307,6 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
320307
golang.org/x/sys v0.0.0-20190209173611-3b5209105503/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
321308
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
322309
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
323-
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd h1:DBH9mDw0zluJT/R+nGuV3jWFWLFaHyYZWD4tOT+cjn0=
324310
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
325311
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
326312
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -373,7 +359,6 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
373359
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
374360
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
375361
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
376-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
377362
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
378363
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
379364
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

lndclient/router_client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/rand"
66
"encoding/hex"
77
"fmt"
8+
"io"
89
"time"
910

1011
"github.com/btcsuite/btcutil"
@@ -225,6 +226,8 @@ func (r *routerClient) TrackPayment(ctx context.Context,
225226

226227
// trackPayment takes an update stream from either a SendPayment or a
227228
// TrackPayment rpc call and converts it into distinct update and error streams.
229+
// Once the payment reaches a final state, the status and error channels will
230+
// be closed to signal that we are finished sending into them.
228231
func (r *routerClient) trackPayment(ctx context.Context,
229232
stream routerrpc.Router_TrackPaymentV2Client) (chan PaymentStatus,
230233
chan error, error) {
@@ -235,6 +238,17 @@ func (r *routerClient) trackPayment(ctx context.Context,
235238
for {
236239
payment, err := stream.Recv()
237240
if err != nil {
241+
// If we get an EOF error, the payment has
242+
// reached a final state and the server is
243+
// finished sending us updates. We close both
244+
// channels to signal that we are done sending
245+
// values on them and return.
246+
if err == io.EOF {
247+
close(statusChan)
248+
close(errorChan)
249+
return
250+
}
251+
238252
switch status.Convert(err).Code() {
239253

240254
// NotFound is only expected as a response to

loopin_test.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ func TestLoopInSuccess(t *testing.T) {
3232

3333
height := int32(600)
3434

35-
cfg := &swapConfig{
36-
lnd: &ctx.lnd.LndServices,
37-
store: ctx.store,
38-
server: ctx.server,
39-
}
35+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
4036

4137
swap, err := newLoopInSwap(
4238
context.Background(), cfg,
@@ -151,11 +147,7 @@ func testLoopInTimeout(t *testing.T,
151147

152148
height := int32(600)
153149

154-
cfg := &swapConfig{
155-
lnd: &ctx.lnd.LndServices,
156-
store: ctx.store,
157-
server: ctx.server,
158-
}
150+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
159151

160152
req := testLoopInRequest
161153
if externalValue != 0 {
@@ -282,12 +274,7 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
282274
defer test.Guard(t)()
283275

284276
ctx := newLoopInTestContext(t)
285-
286-
cfg := &swapConfig{
287-
lnd: &ctx.lnd.LndServices,
288-
store: ctx.store,
289-
server: ctx.server,
290-
}
277+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
291278

292279
senderKey := [33]byte{4}
293280
receiverKey := [33]byte{5}

loopout.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,20 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmed(globalCtx context.Context,
706706
return nil, fmt.Errorf("register spend ntfn: %v", err)
707707
}
708708

709+
// Track our payment status so that we can detect whether our off chain
710+
// htlc is settled. We track this information to determine whether it is
711+
// necessary to continue trying to push our preimage to the server.
712+
trackChan, trackErrChan, err := s.lnd.Router.TrackPayment(
713+
ctx, s.hash,
714+
)
715+
if err != nil {
716+
return nil, fmt.Errorf("track payment: %v", err)
717+
}
718+
719+
// paymentComplete tracks whether our payment is complete, and is used
720+
// to decide whether we need to push our preimage to the server.
721+
var paymentComplete bool
722+
709723
timerChan := s.timerFactory(republishDelay)
710724
for {
711725
select {
@@ -719,6 +733,45 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmed(globalCtx context.Context,
719733
case err := <-spendErr:
720734
return nil, err
721735

736+
// Receive status updates for our payment so that we can detect
737+
// whether we've successfully pushed our preimage.
738+
case status, ok := <-trackChan:
739+
// If our channel has been closed, indicating that the
740+
// server is finished providing updates because the
741+
// payment has reached a terminal state, we replace
742+
// the closed channel with nil so that we will no longer
743+
// listen on it.
744+
if !ok {
745+
trackChan = nil
746+
continue
747+
}
748+
749+
if status.State == lnrpc.Payment_SUCCEEDED {
750+
s.log.Infof("Off chain payment succeeded")
751+
752+
paymentComplete = true
753+
}
754+
755+
// If we receive a track payment error that indicates that the
756+
// server stream is complete, we ignore it because we want to
757+
// continue this loop beyond the completion of the payment.
758+
case err, ok := <-trackErrChan:
759+
// If our channel has been closed, indicating that the
760+
// server is finished providing updates because the
761+
// payment has reached a terminal state, we replace
762+
// the closed channel with nil so that we will no longer
763+
// listen on it.
764+
if !ok {
765+
trackErrChan = nil
766+
continue
767+
}
768+
769+
// Otherwise, if we receive a non-nil error, we return
770+
// it.
771+
if err != nil {
772+
return nil, err
773+
}
774+
722775
// New block arrived, update height and restart the republish
723776
// timer.
724777
case notification := <-s.blockEpochChan:
@@ -733,13 +786,39 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmed(globalCtx context.Context,
733786
return nil, err
734787
}
735788

789+
// If our off chain payment is not yet complete, we
790+
// try to push our preimage to the server.
791+
if !paymentComplete {
792+
s.pushPreimage(ctx)
793+
}
794+
736795
// Context canceled.
737796
case <-globalCtx.Done():
738797
return nil, globalCtx.Err()
739798
}
740799
}
741800
}
742801

802+
// pushPreimage pushes our preimage to the server if we have already revealed
803+
// our preimage on chain with a sweep attempt.
804+
func (s *loopOutSwap) pushPreimage(ctx context.Context) {
805+
// If we have not yet revealed our preimage through a sweep, we do not
806+
// push the preimage because we may choose to never sweep if fees are
807+
// too high.
808+
if s.state != loopdb.StatePreimageRevealed {
809+
return
810+
}
811+
812+
s.log.Infof("Pushing preimage to server")
813+
814+
// Push the preimage to the server, just log server errors since we rely
815+
// on our payment state rather than the server response to judge the
816+
// outcome of our preimage push.
817+
if err := s.server.PushLoopOutPreimage(ctx, s.Preimage); err != nil {
818+
s.log.Warnf("Could not push preimage: %v", err)
819+
}
820+
}
821+
743822
// sweep tries to sweep the given htlc to a destination address. It takes into
744823
// account the max miner fee and marks the preimage as revealed when it
745824
// published the tx.

0 commit comments

Comments
 (0)