-
Notifications
You must be signed in to change notification settings - Fork 131
multi: add static address loop in monitor #1165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GustavoStingelin
wants to merge
7
commits into
lightninglabs:master
Choose a base branch
from
GustavoStingelin:feat/monitor-static-address
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,476
−161
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
365af4c
looprpc: expose static loop-in monitor state
GustavoStingelin 8720b72
staticaddr/loopin: preserve loop-in last update time
GustavoStingelin f29460a
loopd: map static address loop-ins to swap status
GustavoStingelin 81be5ad
loopd: include static loop-ins in monitor snapshots
GustavoStingelin 0007002
staticaddr/loopin: publish status updates after persistence
GustavoStingelin 09dce72
cmd/loop: display static loop-in monitor stages
GustavoStingelin 73bc60a
cmd/loop: add static loop-in monitor replay
GustavoStingelin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/lightninglabs/loop/looprpc" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMonitorSwapStateKeepsRegularLoopInFailureReason(t *testing.T) { | ||
| swap := &looprpc.SwapStatus{ | ||
| Type: looprpc.SwapType_LOOP_IN, | ||
| State: looprpc.SwapState_FAILED, | ||
| FailureReason: looprpc. | ||
| FailureReason_FAILURE_REASON_TIMEOUT, | ||
| } | ||
|
|
||
| got := monitorSwapState(swap) | ||
| require.Equal(t, "FAILED (FAILURE_REASON_TIMEOUT)", got) | ||
| } | ||
|
|
||
| func TestMonitorSwapStateLabelsStaticLoopInStages(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| staticState looprpc.StaticAddressLoopInSwapState | ||
| want string | ||
| }{ | ||
| { | ||
| name: "init htlc", | ||
| staticState: looprpc.StaticAddressLoopInSwapState_INIT_HTLC, | ||
| want: "INIT_HTLC", | ||
| }, | ||
| { | ||
| name: "sign htlc", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_SIGN_HTLC_TX, | ||
| want: "SIGN_HTLC_TX", | ||
| }, | ||
| { | ||
| name: "monitor invoice and htlc", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_MONITOR_INVOICE_HTLC_TX, | ||
| want: "MONITOR_INVOICE_HTLC_TX", | ||
| }, | ||
| { | ||
| name: "unlock deposits", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_UNLOCK_DEPOSITS, | ||
| want: "UNLOCK_DEPOSITS", | ||
| }, | ||
| { | ||
| name: "payment received", | ||
| staticState: looprpc.StaticAddressLoopInSwapState_PAYMENT_RECEIVED, | ||
| want: "PAYMENT_RECEIVED", | ||
| }, | ||
| { | ||
| name: "timeout sweep", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_SWEEP_STATIC_ADDRESS_HTLC_TIMEOUT, | ||
| want: "SWEEP_STATIC_ADDRESS_HTLC_TIMEOUT", | ||
| }, | ||
| { | ||
| name: "monitor timeout sweep", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_MONITOR_HTLC_TIMEOUT_SWEEP, | ||
| want: "MONITOR_HTLC_TIMEOUT_SWEEP", | ||
| }, | ||
| { | ||
| name: "timeout swept", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_HTLC_STATIC_ADDRESS_TIMEOUT_SWEPT, | ||
| want: "HTLC_STATIC_ADDRESS_TIMEOUT_SWEPT", | ||
| }, | ||
| { | ||
| name: "success", | ||
| staticState: looprpc.StaticAddressLoopInSwapState_SUCCEEDED, | ||
| want: "SUCCEEDED", | ||
| }, | ||
| { | ||
| name: "succeeded transitioning failed", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_SUCCEEDED_TRANSITIONING_FAILED, | ||
| want: "SUCCEEDED_TRANSITIONING_FAILED", | ||
| }, | ||
| { | ||
| name: "failed", | ||
| staticState: looprpc. | ||
| StaticAddressLoopInSwapState_FAILED_STATIC_ADDRESS_SWAP, | ||
| want: "FAILED_STATIC_ADDRESS_SWAP", | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| swap := &looprpc.SwapStatus{ | ||
| Type: looprpc.SwapType_STATIC_LOOP_IN, | ||
| StaticLoopInStateOptional: &looprpc.SwapStatus_StaticLoopInState{ | ||
| StaticLoopInState: test.staticState, | ||
| }, | ||
| } | ||
|
|
||
| got := monitorSwapState(swap) | ||
| require.Equal(t, test.want, got) | ||
| }) | ||
| } | ||
|
|
||
| initSwap := &looprpc.SwapStatus{ | ||
| Type: looprpc.SwapType_STATIC_LOOP_IN, | ||
| StaticLoopInStateOptional: &looprpc.SwapStatus_StaticLoopInState{ | ||
| StaticLoopInState: looprpc. | ||
| StaticAddressLoopInSwapState_INIT_HTLC, | ||
| }, | ||
| } | ||
| signSwap := &looprpc.SwapStatus{ | ||
| Type: looprpc.SwapType_STATIC_LOOP_IN, | ||
| StaticLoopInStateOptional: &looprpc.SwapStatus_StaticLoopInState{ | ||
| StaticLoopInState: looprpc. | ||
| StaticAddressLoopInSwapState_SIGN_HTLC_TX, | ||
| }, | ||
| } | ||
| require.NotEqual(t, monitorSwapState(initSwap), monitorSwapState(signSwap)) | ||
| } | ||
|
|
||
| func TestLogSwapDisplaysStaticLoopInTypeAndStage(t *testing.T) { | ||
| swap := &looprpc.SwapStatus{ | ||
| LastUpdateTime: time.Unix(1, 0).UnixNano(), | ||
| Type: looprpc.SwapType_STATIC_LOOP_IN, | ||
| StaticLoopInStateOptional: &looprpc.SwapStatus_StaticLoopInState{ | ||
| StaticLoopInState: looprpc. | ||
| StaticAddressLoopInSwapState_SIGN_HTLC_TX, | ||
| }, | ||
| Amt: 50_000, | ||
| HtlcAddressP2Tr: "tb1p5cyxnuxmeuwuvkwfem96llyxf8duyshm56t8k8", | ||
| } | ||
|
|
||
| output := captureStdout(t, func() { | ||
| logSwap(swap) | ||
| }) | ||
|
|
||
| require.Contains( | ||
| t, output, "STATIC_LOOP_IN SIGN_HTLC_TX 0.00050000 BTC", | ||
| ) | ||
| require.Contains( | ||
| t, output, "P2TR: tb1p5cyxnuxmeuwuvkwfem96llyxf8duyshm56t8k8", | ||
| ) | ||
| require.NotContains(t, output, "STATIC_LOOP_IN INIT_HTLC") | ||
| } | ||
|
|
||
| func captureStdout(t *testing.T, fn func()) string { | ||
| t.Helper() | ||
|
|
||
| originalStdout := os.Stdout | ||
| reader, writer, err := os.Pipe() | ||
| require.NoError(t, err) | ||
| os.Stdout = writer | ||
|
|
||
| fn() | ||
|
|
||
| require.NoError(t, writer.Close()) | ||
| os.Stdout = originalStdout | ||
|
|
||
| var buf bytes.Buffer | ||
| _, err = io.Copy(&buf, reader) | ||
| require.NoError(t, err) | ||
| require.NoError(t, reader.Close()) | ||
|
|
||
| return strings.TrimSpace(buf.String()) | ||
| } |
111 changes: 111 additions & 0 deletions
111
cmd/loop/testdata/sessions/basic-swaps/04_loop-monitor-static-loop-in.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| { | ||
| "metadata": { | ||
| "args": [ | ||
| "loop", | ||
| "monitor", | ||
| "--network", | ||
| "regtest" | ||
| ], | ||
| "env": {}, | ||
| "version": "0.31.7-beta commit=static-monitor-replay commit_hash=staticmonitorreplay", | ||
| "run_error": "recv: rpc error: code = Canceled desc = context canceled", | ||
| "duration": 1000000000, | ||
| "clock_start_unix": 1769414443 | ||
| }, | ||
| "events": [ | ||
| { | ||
| "time_ms": 1, | ||
| "kind": "grpc", | ||
| "data": { | ||
| "method": "/looprpc.SwapClient/Monitor", | ||
| "event": "send", | ||
| "message_type": "looprpc.MonitorRequest", | ||
| "payload": {} | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 1, | ||
| "kind": "stdout", | ||
| "data": { | ||
| "lines": [ | ||
| "Note: offchain cost may report as 0 after loopd restart during swap\n" | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 2, | ||
| "kind": "grpc", | ||
| "data": { | ||
| "method": "/looprpc.SwapClient/Monitor", | ||
| "event": "recv", | ||
| "message_type": "looprpc.SwapStatus", | ||
| "payload": { | ||
| "amt": "50000", | ||
| "id": "3f4ec343ba4087eec3cdf501b4e19733d9685cc151db360bb67d2c924677ce71", | ||
| "id_bytes": "P07DQ7pAh+7DzfUBtOGXM9loXMFR2zYLtn0skkZ3znE=", | ||
| "type": "STATIC_LOOP_IN", | ||
| "state": "INITIATED", | ||
| "static_loop_in_state": "SIGN_HTLC_TX", | ||
| "failure_reason": "FAILURE_REASON_NONE", | ||
| "initiation_time": "1769414443188873986", | ||
| "last_update_time": "1769414443188873986", | ||
| "htlc_address": "bcrt1ppuw9wtl65avd026t9nkxux8whz3gyhkjh6n9cryp8a5xz9jacf9qlgxsch", | ||
| "htlc_address_p2wsh": "", | ||
| "htlc_address_p2tr": "bcrt1ppuw9wtl65avd026t9nkxux8whz3gyhkjh6n9cryp8a5xz9jacf9qlgxsch", | ||
| "cost_server": "999", | ||
| "cost_onchain": "888", | ||
| "cost_offchain": "777", | ||
| "last_hop": "", | ||
| "outgoing_chan_set": [], | ||
| "label": "", | ||
| "asset_info": null | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 2, | ||
| "kind": "stdout", | ||
| "data": { | ||
| "lines": [ | ||
| "2026-01-26T03:00:43-05:00 STATIC_LOOP_IN SIGN_HTLC_TX 0.00050000 BTC - P2TR: bcrt1ppuw9wtl65avd026t9nkxux8whz3gyhkjh6n9cryp8a5xz9jacf9qlgxsch\n" | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 1000, | ||
| "kind": "signal", | ||
| "data": { | ||
| "signal": "interrupt" | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 1000, | ||
| "kind": "grpc", | ||
| "data": { | ||
| "method": "/looprpc.SwapClient/Monitor", | ||
| "event": "error", | ||
| "error": "rpc error: code = Canceled desc = context canceled", | ||
| "status": { | ||
| "code": 1, | ||
| "message": "context canceled" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 1000, | ||
| "kind": "stderr", | ||
| "data": { | ||
| "lines": [ | ||
| "[loop] recv: rpc error: code = Canceled desc = context canceled\n" | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "time_ms": 1000, | ||
| "kind": "exit", | ||
| "data": { | ||
| "run_error": "recv: rpc error: code = Canceled desc = context canceled" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hieblmi It is the thing that I'm mostly concerned about