|
1 | 1 | package test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "math" |
| 6 | + "net" |
| 7 | + "path/filepath" |
5 | 8 | "testing" |
| 9 | + "time" |
6 | 10 |
|
| 11 | + "github.com/elementsproject/glightning/jrpc2" |
7 | 12 | "github.com/elementsproject/peerswap/clightning" |
8 | 13 | "github.com/elementsproject/peerswap/premium" |
9 | 14 | "github.com/elementsproject/peerswap/swap" |
10 | 15 | "github.com/elementsproject/peerswap/testframework" |
11 | 16 | ) |
12 | 17 |
|
| 18 | +type clnSocketProvider interface { |
| 19 | + GetDataDir() string |
| 20 | +} |
| 21 | + |
| 22 | +func notifyCLN(t *testing.T, node clnSocketProvider, method jrpc2.Method) { |
| 23 | + t.Helper() |
| 24 | + |
| 25 | + require := requireNew(t) |
| 26 | + |
| 27 | + socketPath := filepath.Join(node.GetDataDir(), "lightning-rpc") |
| 28 | + conn, err := net.DialTimeout("unix", socketPath, 1*time.Second) |
| 29 | + require.NoError(err) |
| 30 | + defer conn.Close() |
| 31 | + |
| 32 | + req := &jrpc2.Request{ |
| 33 | + // No id means this is treated as a JSON-RPC notification, and no |
| 34 | + // response is expected. This avoids leaving a long-running pending RPC |
| 35 | + // request in glightning when the node is intentionally stopped mid-test. |
| 36 | + Id: nil, |
| 37 | + Method: method, |
| 38 | + } |
| 39 | + payload, err := json.Marshal(req) |
| 40 | + require.NoError(err) |
| 41 | + |
| 42 | + _, err = conn.Write(append(payload, '\n', '\n')) |
| 43 | + require.NoError(err) |
| 44 | +} |
| 45 | + |
13 | 46 | // Test_RestoreFromPassedCSV checks the following scenario: A swap is initiated |
14 | 47 | // and the opening tx is broadcasted. The maker node then goes offline before |
15 | 48 | // the opening tx is confirmed such that the taker node can not pay the invoice. |
@@ -59,18 +92,12 @@ func Test_RestoreFromPassedCSV(t *testing.T) { |
59 | 92 | asset := "btc" |
60 | 93 |
|
61 | 94 | // Do swap. |
62 | | - go func() { |
63 | | - // We need to run this in a go routine as the Request call is blocking and sometimes does not return. |
64 | | - var response map[string]interface{} |
65 | | - if err := lightningds[0].Rpc.Request(&clightning.SwapOut{ |
66 | | - SatAmt: params.swapAmt, |
67 | | - ShortChannelId: params.scid, |
68 | | - Asset: asset, |
69 | | - PremiumLimitRatePPM: params.premiumLimitRatePPM, |
70 | | - }, &response); err != nil { |
71 | | - t.Errorf("SwapOut request failed: %v", err) |
72 | | - } |
73 | | - }() |
| 95 | + notifyCLN(t, lightningds[0], &clightning.SwapOut{ |
| 96 | + SatAmt: params.swapAmt, |
| 97 | + ShortChannelId: params.scid, |
| 98 | + Asset: asset, |
| 99 | + PremiumLimitRatePPM: params.premiumLimitRatePPM, |
| 100 | + }) |
74 | 101 |
|
75 | 102 | var premiumAmt uint64 |
76 | 103 | if params.swapType == swap.SWAPTYPE_OUT { |
@@ -198,18 +225,12 @@ func Test_Recover_PassedSwap_BTC(t *testing.T) { |
198 | 225 | asset := "btc" |
199 | 226 |
|
200 | 227 | // Do swap. |
201 | | - go func() { |
202 | | - // We need to run this in a go routine as the Request call is blocking and sometimes does not return. |
203 | | - var response map[string]interface{} |
204 | | - if err := lightningds[0].Rpc.Request(&clightning.SwapOut{ |
205 | | - SatAmt: params.swapAmt, |
206 | | - ShortChannelId: params.scid, |
207 | | - Asset: asset, |
208 | | - PremiumLimitRatePPM: params.premiumLimitRatePPM, |
209 | | - }, &response); err != nil { |
210 | | - t.Errorf("SwapOut request failed: %v", err) |
211 | | - } |
212 | | - }() |
| 228 | + notifyCLN(t, lightningds[0], &clightning.SwapOut{ |
| 229 | + SatAmt: params.swapAmt, |
| 230 | + ShortChannelId: params.scid, |
| 231 | + Asset: asset, |
| 232 | + PremiumLimitRatePPM: params.premiumLimitRatePPM, |
| 233 | + }) |
213 | 234 |
|
214 | 235 | var premiumAmt uint64 |
215 | 236 | if params.swapType == swap.SWAPTYPE_OUT { |
@@ -310,18 +331,12 @@ func Test_Recover_PassedSwap_LBTC(t *testing.T) { |
310 | 331 | asset := "lbtc" |
311 | 332 |
|
312 | 333 | // Do swap. |
313 | | - go func() { |
314 | | - // We need to run this in a go routine as the Request call is blocking and sometimes does not return. |
315 | | - var response map[string]interface{} |
316 | | - if err := lightningds[0].Rpc.Request(&clightning.SwapOut{ |
317 | | - SatAmt: params.swapAmt, |
318 | | - ShortChannelId: params.scid, |
319 | | - Asset: asset, |
320 | | - PremiumLimitRatePPM: params.premiumLimitRatePPM, |
321 | | - }, &response); err != nil { |
322 | | - t.Errorf("SwapOut request failed: %v", err) |
323 | | - } |
324 | | - }() |
| 334 | + notifyCLN(t, lightningds[0], &clightning.SwapOut{ |
| 335 | + SatAmt: params.swapAmt, |
| 336 | + ShortChannelId: params.scid, |
| 337 | + Asset: asset, |
| 338 | + PremiumLimitRatePPM: params.premiumLimitRatePPM, |
| 339 | + }) |
325 | 340 |
|
326 | 341 | var premiumAmt uint64 |
327 | 342 | if params.swapType == swap.SWAPTYPE_OUT { |
|
0 commit comments