Skip to content
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

One chain source to rule them all #564

Merged
merged 2 commits into from
Dec 5, 2024
Merged

Conversation

dangeross
Copy link
Collaborator

@dangeross dangeross commented Nov 13, 2024

This PR:

  • tracks new blocks and trigger swap checks/rescans for that block
  • recovers and stores onchain data for every monitored swap
  • partially syncs to pickup direct Liquid or MRH txs in the mempool
  • triggers PaymentWaitingConfirmation event for receive payments via MRH tx when in the mempool
  • prevents duplicate SDK events by comparing swap before/after update

TODO (in a followup PR):

  • Reuse fetch of chain data from the swap update ws loop
  • Trigger any claims/refunds based on fetched chain data
Every new liquid block:
- Fetch onchain data for all swaps that are not in final state (not in succeed, refunded) we can skip expired more than X blocks behind like we do today for efficiency.
- For refundable send lightning swaps refund if needed.
- For claimable receive lightning swaps, claim if needed. (zero conf claims can still be handled from the web socket loop).
Every new bitcoin block:
- fetch onchain data forFetch onchain data for All onchain swaps (both send/receive) -  we can skip expired more than X blocks behind like we do today for efficiency.
- For refundable send onchain swaps refund if needed.
- For claimable receive onchain swaps claim if needed.
Notes:
- In order to get a new block event we can query electrum every 10 seconds for the current tip (shouldn’t be too expensive).
- For both liquid and bitcoin we start by fetching the onchain data aggregated for all swaps we need, persisting it and then acting on refundables or claimables.
I think this should cover the following cases (let’s verify):
- Non functioning swapper web socket statuses
- Records coming from realtime sync (created and controlled on another device).
- Failure to claim/refund from the swapper websocket status loop.

@dangeross dangeross force-pushed the savage-single-chain-source branch 2 times, most recently from c329c16 to 0b838eb Compare November 15, 2024 18:45
}

impl LiquidSdk {
pub(crate) async fn get_monitored_swaps_list(&self, height: u32) -> Result<SwapsList> {
let send_swaps = self.persister.list_pending_send_swaps()?;
let receive_swaps = self.persister.list_ongoing_receive_swaps()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we list here also received swaps that were expired?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but the expiry will be the same as the invoice expiry, so the invoice shouldn't be valid. At this point also the MRH address can be reused from the address pool, so shouldn't be associated to the swap

@dangeross dangeross force-pushed the savage-single-chain-source branch from 807bc5b to ef748f0 Compare November 19, 2024 08:26
@dangeross dangeross marked this pull request as ready for review November 19, 2024 09:44
@dangeross dangeross force-pushed the savage-single-chain-source branch 2 times, most recently from a073084 to fad9a69 Compare November 20, 2024 21:17
let mrh_tx_id = receive_data.mrh_tx_id.clone().map(|h| h.txid.to_string());
_ = self
.receive_swap_handler
.update_swap_info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this send trigger a payment event? Previously the event was triggered only on change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they are repeated anyway in some cases. I seem to remember seeing multiple PaymentPending or PaymentWaitingConfirmation events. Probably because its valid to update Pending to Pending even if the details are the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean also on the update_swap_info occurences bellow we don't seem to be checking if it is a really state transition so it looks like on every sync we will get payment events.
Previousely the code was checking for every associated claim if it really transitioned to from mempool to confirmed (completed) or just intruduced as undonfirmed (pending) before emitting events.

Copy link
Member

@roeierez roeierez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

lib/core/src/sdk.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Show resolved Hide resolved
lib/core/src/chain_swap.rs Show resolved Hide resolved
lib/core/src/chain_swap.rs Outdated Show resolved Hide resolved
lib/core/src/model.rs Show resolved Hide resolved
lib/core/src/receive_swap.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Outdated Show resolved Hide resolved
lib/core/src/restore.rs Outdated Show resolved Hide resolved
@dangeross dangeross force-pushed the savage-single-chain-source branch 2 times, most recently from ad3f936 to 07bb41e Compare November 27, 2024 16:45
lib/core/src/restore.rs Outdated Show resolved Hide resolved
@dangeross dangeross force-pushed the savage-single-chain-source branch 2 times, most recently from c77576f to 9279ab9 Compare December 5, 2024 09:17
@dangeross dangeross changed the base branch from main to yse-rt-address December 5, 2024 09:17
@dangeross dangeross force-pushed the savage-single-chain-source branch from a89090e to 54609d3 Compare December 5, 2024 09:54
@dangeross dangeross force-pushed the savage-single-chain-source branch from 54609d3 to 9279ab9 Compare December 5, 2024 11:44
@dangeross dangeross changed the base branch from yse-rt-address to rt-sync December 5, 2024 11:45
@dangeross dangeross force-pushed the savage-single-chain-source branch from 9279ab9 to 044f956 Compare December 5, 2024 11:46
@dangeross dangeross force-pushed the savage-single-chain-source branch from 044f956 to 8e04fe1 Compare December 5, 2024 12:14
@dangeross dangeross merged commit 5f1fda0 into rt-sync Dec 5, 2024
8 of 9 checks passed
@dangeross dangeross deleted the savage-single-chain-source branch January 3, 2025 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants