-
Notifications
You must be signed in to change notification settings - Fork 354
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
Introduce evicted-at
/last-evicted
timestamps
#1839
base: master
Are you sure you want to change the base?
Conversation
6dc26c4
to
0fcc097
Compare
540a83f
to
5107b3a
Compare
5718a3e
to
07903a8
Compare
evicted-at
/last-evicted
timestamps
07903a8
to
9d6c795
Compare
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.
cACK 9d6c795
This unlocks functionality on demand and I think the approach is OK. Tests are thorough enough.
This branch includes: - bitcoindevkit/bdk#1855 - bitcoindevkit/bdk#1839 - the patch for esplora-client
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.
Nit: Minor typos and grammar fixes.
This is a set of evicted txs from the mempool.
c11515d
to
6afb703
Compare
The evicted-at and last-evicted timestamp informs the `TxGraph` when the transaction was last deemed as missing (evicted) from the mempool. The canonicalization algorithm is changed to disregard transactions with a last-evicted timestamp greater or equal to it's last-seen timestamp. The exception is when we have a canonical descendant due to rules of transitivity. Update rusqlite_impl to persist `last_evicted`. Also update docs: * Remove duplicate paragraphs about `ChangeSet`s. * Add "Canonicalization" section which expands on methods that require canonicalization and the associated data types used in the canonicalization algorithm.
The spk history returned from Electrum should have these txs present. Any missing tx will be considered evicted from the mempool.
* `TxGraph::try_list_expected_spk_txids` * `TxGraph::list_expected_spk_txids` * `IndexedTxGraph::try_list_expected_spk_txids` * `IndexedTxGraph::list_expected_spk_txids` Co-authored-by: valued mammal <valuedmammal@protonmail.com> Co-authored-by: Wei Chen <wzc110@gmail.com>
c23120c
to
e538e66
Compare
Co-authored-by: Wei Chen <wzc110@gmail.com>
Also add `detect_receive_tx_cancel` test. Also rm `miniscript` dependency. Update ci to rm `miniscript/no-std` for `bdk_esplora`. Co-authored-by: Wei Chen <wzc110@gmail.com>
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.
ut ACK 1eb0c32
Thanks for the fix and all the new docs, looks like a solid solution. Before (or after) this goes in it would be nice to have some confirmation from @ErikDeSmedt that it fixes #1740 for him (at least using electrum and esplora).
@@ -94,7 +94,7 @@ jobs: | |||
- name: Check esplora | |||
working-directory: ./crates/esplora | |||
# TODO "--target thumbv6m-none-eabi" should work but currently does not | |||
run: cargo check --no-default-features --features miniscript/no-std,bdk_chain/hashbrown | |||
run: cargo check --no-default-features --features bdk_chain/hashbrown |
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.
👍 thanks for cleaning this up!
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.
It raised my attention, shouldn't we be using bdk_core/hashbrown
instead ?
//! methods only consider transactions that are "canonical" (i.e., in the best chain or in mempool). | ||
//! We decide which transactions are canonical based on the transaction's anchors and the | ||
//! `last_seen` (as unconfirmed) timestamp. | ||
//! # Canonicalization |
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.
👍 Good to see these new docs as this is core to how the the tx_graph
logic works.
@@ -1107,6 +1245,9 @@ pub struct ChangeSet<A = ()> { | |||
pub anchors: BTreeSet<(A, Txid)>, | |||
/// Added last-seen unix timestamps of transactions. | |||
pub last_seen: BTreeMap<Txid, u64>, | |||
/// Added timestamps of when a transaction is last evicted from the mempool. | |||
#[cfg_attr(feature = "serde", serde(default))] |
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.
Is this cfg_attr
only needed to load data from a pre-v2 serialized file?
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.
Yes, this ensures backwards compatibility of our serialized changeset (as we promised).
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.
cACK 1eb0c32
Great work! I left some minor comments/questions that if applicable could be done in a follow-up PR.
/// When transactions were discovered to be missing (evicted) from the mempool. | ||
/// | ||
/// [`SyncRequest::start_time`](crate::spk_client::SyncRequest::start_time) can be used to | ||
/// provide the `seen_at` value. |
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.
nit:
/// provide the `seen_at` value. | |
/// provide the `evicted_at` value. |
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.
It can be used to provide both actually. Will change.
const PK_BYTES: &[u8] = &[ | ||
12, 244, 72, 4, 163, 4, 211, 81, 159, 82, 153, 123, 125, 74, 142, 40, 55, 237, 191, 231, 31, | ||
114, 89, 165, 83, 141, 8, 203, 93, 240, 53, 101, | ||
]; | ||
|
||
#[allow(dead_code)] | ||
pub fn get_test_spk() -> ScriptBuf { | ||
let secp = Secp256k1::new(); | ||
let pk = UntweakedPublicKey::from_slice(PK_BYTES).expect("Must be valid PK"); | ||
ScriptBuf::new_p2tr(&secp, pk, None) | ||
} |
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.
nit: as it's common between bdk_esplora
and bdk_electrum
too, couldn't it be moved to bdk_testenv
instead?
/// Iterate over [`ScriptBuf`]s with corresponding [`Txid`] histories contained in this request. | ||
pub fn iter_spks_with_expected_txids( | ||
&mut self, | ||
) -> impl ExactSizeIterator<Item = SpkWithExpectedTxids> + '_ { | ||
SyncIter::<I, SpkWithExpectedTxids>::new(self) | ||
} |
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.
question: the iter_spks
fn above is not being used anymore, at least for SyncRequest
, do we need to keep it?
@@ -94,7 +94,7 @@ jobs: | |||
- name: Check esplora | |||
working-directory: ./crates/esplora | |||
# TODO "--target thumbv6m-none-eabi" should work but currently does not | |||
run: cargo check --no-default-features --features miniscript/no-std,bdk_chain/hashbrown | |||
run: cargo check --no-default-features --features bdk_chain/hashbrown |
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.
It raised my attention, shouldn't we be using bdk_core/hashbrown
instead ?
let (spks, expected_spk_txids): (Vec<(u32, ScriptBuf)>, HashMap<ScriptBuf, _>) = spks | ||
.iter() | ||
.map(|(spk_i, spk_with_expected_txids)| { | ||
( | ||
(*spk_i, spk_with_expected_txids.spk.clone()), | ||
( | ||
spk_with_expected_txids.spk.clone(), | ||
spk_with_expected_txids.expected_txids.clone(), | ||
), | ||
) | ||
}) | ||
.unzip(); |
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.
I'm curious about the reasoning behind this
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.
About what exactly? What the two variables are used for?
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.
why split the batch of spks into two new containers?
Partially Fixes #1740.
Replaces #1765.
Replaces #1811.
Description
This PR allows the receiving structures (
bdk_chain
,bdk_wallet
) to detect and evict incoming transactions that are double spent (cancelled).We add a new field to
TxUpdate
(TxUpdate::evicted_ats
), which in turn, updates thelast_evicted
timestamps that are tracked/persisted byTxGraph
. This is similar to howTxUpdate::seen_ats
update thelast_seen
timestamp inTxGraph
. Transactions with alast_evicted
timestamp higher than theirlast_seen
timestamp are considered evicted.SpkWithExpectedTxids
is introduced inSpkClient
to track expectedTxid
s for eachspk
. During a sync, if anyTxid
s fromSpkWithExpectedTxids
are not in the current history of anspk
obtained from the chain source, thoseTxid
s are considered missing. Support forSpkWithExpectedTxids
has been added to bothbdk_electrum
andbdk_esplora
chain source crates.The canonicalization algorithm is updated to disregard transactions with a
last_evicted
timestamp greater than or equal to theirlast_seen
timestamp, except in cases where transitivity rules apply.Notes to the reviewers
This PR does not fix #1740 for block-by-block chain source (such as
bdk_bitcoind_rpc
). This work is done in a separate PR (#1857).Changelog notice
TxUpdate::evicted_ats
which tracks transactions that have been replaced and are no longer present in mempool.TxGraph
to tracklast_evicted
timestamps. This is when a transaction is last marked as missing from the mempool.last_evicted
timestamp greater than or equal to it'slast_seen
timestamp, except when a canonical descendant exists due to rules of transitivity.SpkWithExpectedTxids
inspk_client
which keeps track of expectedTxid
s for eachspk
.bdk_electrum
andbdk_esplora
to understandSpkWithExpectedTxids
.SyncRequestBuilder::expected_txids_of_spk
method which adds an association betweentxid
s andspk
s.Checklists
All Submissions:
cargo fmt
andcargo clippy
before committingNew Features:
Bugfixes: