-
Notifications
You must be signed in to change notification settings - Fork 405
Add transaction sync crate #1870
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
Merged
TheBlueMatt
merged 3 commits into
lightningdevkit:main
from
tnull:2022-11-add-transaction-sync-crate
Feb 10, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,33 @@ | ||
[package] | ||
name = "lightning-transaction-sync" | ||
version = "0.0.113" | ||
authors = ["Elias Rohrer"] | ||
license = "MIT OR Apache-2.0" | ||
repository = "http://github.com/lightningdevkit/rust-lightning" | ||
description = """ | ||
Utilities for syncing LDK via the transaction-based `Confirm` interface. | ||
""" | ||
edition = "2018" | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[features] | ||
default = [] | ||
esplora-async = ["async-interface", "esplora-client/async", "futures"] | ||
esplora-blocking = ["esplora-client/blocking"] | ||
async-interface = [] | ||
|
||
[dependencies] | ||
lightning = { version = "0.0.113", path = "../lightning" } | ||
bitcoin = "0.29.0" | ||
bdk-macros = "0.6" | ||
futures = { version = "0.3", optional = true } | ||
esplora-client = { version = "0.3.0", default-features = false, optional = true } | ||
|
||
[dev-dependencies] | ||
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] } | ||
electrum-client = "0.12.0" | ||
once_cell = "1.16.0" | ||
tokio = { version = "1.14.0", features = ["full"] } |
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,75 @@ | ||
use lightning::chain::WatchedOutput; | ||
use bitcoin::{Txid, BlockHash, Transaction, BlockHeader, OutPoint}; | ||
|
||
use std::collections::{HashSet, HashMap}; | ||
|
||
|
||
// Represents the current state. | ||
pub(crate) struct SyncState { | ||
// Transactions that were previously processed, but must not be forgotten | ||
// yet since they still need to be monitored for confirmation on-chain. | ||
pub watched_transactions: HashSet<Txid>, | ||
// Outputs that were previously processed, but must not be forgotten yet as | ||
// as we still need to monitor any spends on-chain. | ||
pub watched_outputs: HashMap<OutPoint, WatchedOutput>, | ||
// The tip hash observed during our last sync. | ||
pub last_sync_hash: Option<BlockHash>, | ||
// Indicates whether we need to resync, e.g., after encountering an error. | ||
pub pending_sync: bool, | ||
} | ||
|
||
impl SyncState { | ||
pub fn new() -> Self { | ||
Self { | ||
watched_transactions: HashSet::new(), | ||
watched_outputs: HashMap::new(), | ||
last_sync_hash: None, | ||
pending_sync: false, | ||
} | ||
} | ||
} | ||
|
||
|
||
// A queue that is to be filled by `Filter` and drained during the next syncing round. | ||
pub(crate) struct FilterQueue { | ||
// Transactions that were registered via the `Filter` interface and have to be processed. | ||
pub transactions: HashSet<Txid>, | ||
// Outputs that were registered via the `Filter` interface and have to be processed. | ||
pub outputs: HashMap<OutPoint, WatchedOutput>, | ||
} | ||
|
||
impl FilterQueue { | ||
pub fn new() -> Self { | ||
Self { | ||
transactions: HashSet::new(), | ||
outputs: HashMap::new(), | ||
} | ||
} | ||
|
||
// Processes the transaction and output queues and adds them to the given [`SyncState`]. | ||
// | ||
// Returns `true` if new items had been registered. | ||
pub fn process_queues(&mut self, sync_state: &mut SyncState) -> bool { | ||
let mut pending_registrations = false; | ||
|
||
if !self.transactions.is_empty() { | ||
pending_registrations = true; | ||
|
||
sync_state.watched_transactions.extend(self.transactions.drain()); | ||
} | ||
|
||
if !self.outputs.is_empty() { | ||
pending_registrations = true; | ||
|
||
sync_state.watched_outputs.extend(self.outputs.drain()); | ||
} | ||
pending_registrations | ||
} | ||
} | ||
|
||
pub(crate) struct ConfirmedTx { | ||
pub tx: Transaction, | ||
pub block_header: BlockHeader, | ||
pub block_height: u32, | ||
pub pos: usize, | ||
} |
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,63 @@ | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
/// An error that possibly needs to be handled by the user. | ||
pub enum TxSyncError { | ||
/// A transaction sync failed and needs to be retried eventually. | ||
Failed, | ||
jkczyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl std::error::Error for TxSyncError {} | ||
|
||
impl fmt::Display for TxSyncError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
Self::Failed => write!(f, "Failed to conduct transaction sync."), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] | ||
pub(crate) enum InternalError { | ||
/// A transaction sync failed and needs to be retried eventually. | ||
Failed, | ||
/// An inconsisteny was encounterd during transaction sync. | ||
Inconsistency, | ||
} | ||
|
||
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] | ||
impl fmt::Display for InternalError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
Self::Failed => write!(f, "Failed to conduct transaction sync."), | ||
Self::Inconsistency => { | ||
write!(f, "Encountered an inconsisteny during transaction sync.") | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] | ||
impl std::error::Error for InternalError {} | ||
|
||
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] | ||
impl From<esplora_client::Error> for TxSyncError { | ||
fn from(_e: esplora_client::Error) -> Self { | ||
Self::Failed | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] | ||
impl From<esplora_client::Error> for InternalError { | ||
fn from(_e: esplora_client::Error) -> Self { | ||
Self::Failed | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] | ||
impl From<InternalError> for TxSyncError { | ||
fn from(_e: InternalError) -> Self { | ||
Self::Failed | ||
} | ||
} |
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.
Now disabled building on windows. While building itself should be fine, testing doesn't work as the
ElecrtrsD
/BitcoinD
crates don't support Windows. Let me now if I should split building/testing in two different variables to enable more finer grained control. So far, I'm thinking checking that it builds on Windows is not worth the additional noise in the CI script.