-
Notifications
You must be signed in to change notification settings - Fork 338
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
#1811
base: master
Are you sure you want to change the base?
Conversation
287b146
to
fe09eab
Compare
MissingMarker
missing-at
/last-missing
timestamps
0f52ead
to
2e31bc7
Compare
Looks like only breaking changes are on |
2c37d06
to
bbd6340
Compare
@notmandatory core breaking change is also a wallet crate breaking change unfortunately. It is a minor breaking change (some struct from core is changing that no one inspects directly in their code probably) but it technically is a breaking change so it would have to be in bdk_wallet v2. This is a pretty important improvement so I'd say it justifies a v2 release by itself when it's merged. |
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.
ConceptACK. Nice ideas here. I think chain API needs a clean up.
crates/chain/src/tx_graph.rs
Outdated
@@ -145,6 +145,7 @@ pub struct TxGraph<A = ConfirmationBlockTime> { | |||
spends: BTreeMap<OutPoint, HashSet<Txid>>, | |||
anchors: HashMap<Txid, BTreeSet<A>>, | |||
last_seen: HashMap<Txid, u64>, | |||
last_missing: HashMap<Txid, u64>, |
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 think it would be nice if we had a data structure like this and then last_seen
would become HashMap<Txid, LastSeen>
#[derive(Debug, Clone, Copy, Default, PartialEq)]
struct LastSeen {
/// seen at
seen_at: u64,
/// evicted at
evicted_at: Option<u64>,
}
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 quite like this idea since there has been talks about adding more timestamps.
I.e. first-seen
timestamp, which will help with tx ordering when listing transactions.
struct MempoolTimestamps {
pub first_seen: u64,
pub last_seen: u64,
pub last_evicted: Option<u64>,
}
I also like the term evicted
as it's more descriptive than missing
.
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 agree, and +1 on this idea, it makes it clear, and easy to "derive" its lifecycle and it's and can be useful info to expose to the users.
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.
After some discussion with @evanlinjin, it was decided that this was best done in a separate PR as it may be too breaking of a change.
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. On second thought, this introduces an invariant that last_evicted
must exist only if we have had a seen_at
value. Because of the existence of this invariant, this invariant must be reflected in tx_graph::ChangeSet
by changing ChangeSet::seen_at
to have values of MempoolTimestamps
. This change breaks our policy of changes to changeset which was to only add new fields.
Edit: Maybe we can make it a user-facing API.
.flat_map(|(txid, anchors)| anchors.into_iter().map(move |a| (a, txid))) | ||
.collect(); | ||
tx_update.seen_ats = graph.last_seen.into_iter().collect(); | ||
tx_update |
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 seems like TxUpdate
would need another field evicted_at
in order to maintain roundtrip convertibility between TxUpdate
and TxGraph
.
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.
Do you think the convertibility between TxGraph
and TxUpdate
is important? I'm thinking maybe we should just get rid of it. One can just construct an empty TxGraph
and apply an TxUpdate
.
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's probably ok to forego complete fungibility between graphs and updates, but at the same time I wonder how useful are the seen-ats without the corresponding evictions?
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.
but at the same time I wonder how useful are the seen-ats without the corresponding evictions?
@ValuedMammal what do you mean by this?
I think it makes sense to completely remove convertibility of TxGraph -> TxUpdate
and TxUpdate -> TxGraph
.
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.
Just pointing out that we lose information going from TxGraph
to TxUpdate
. Since both last-seen and last-evicted can influence canonicalization, it might make sense to include both (or neither).
Maybe it depends on the use case for TxUpdate
. If updating a tx graph from a spk_client
based chain source, the seen_ats
don't seem to be as important compared to treating graphs and updates as interchangeable. If we remove convertibility, would it make sense to change this From
impl to a method on TxGraph
that returns a TxUpdate
? We could potentially develop different kinds of updates, like one that is only relevant to a subset of keychains.
crates/chain/src/tx_graph.rs
Outdated
@@ -145,6 +145,7 @@ pub struct TxGraph<A = ConfirmationBlockTime> { | |||
spends: BTreeMap<OutPoint, HashSet<Txid>>, | |||
anchors: HashMap<Txid, BTreeSet<A>>, | |||
last_seen: HashMap<Txid, u64>, | |||
last_missing: HashMap<Txid, u64>, |
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 quite like this idea since there has been talks about adding more timestamps.
I.e. first-seen
timestamp, which will help with tx ordering when listing transactions.
struct MempoolTimestamps {
pub first_seen: u64,
pub last_seen: u64,
pub last_evicted: Option<u64>,
}
I also like the term evicted
as it's more descriptive than missing
.
.flat_map(|(txid, anchors)| anchors.into_iter().map(move |a| (a, txid))) | ||
.collect(); | ||
tx_update.seen_ats = graph.last_seen.into_iter().collect(); | ||
tx_update |
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.
Do you think the convertibility between TxGraph
and TxUpdate
is important? I'm thinking maybe we should just get rid of it. One can just construct an empty TxGraph
and apply an TxUpdate
.
bbd6340
to
8c0d5c3
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.
A general ConceptACK, some small nits.
crates/chain/src/tx_graph.rs
Outdated
@@ -145,6 +145,7 @@ pub struct TxGraph<A = ConfirmationBlockTime> { | |||
spends: BTreeMap<OutPoint, HashSet<Txid>>, | |||
anchors: HashMap<Txid, BTreeSet<A>>, | |||
last_seen: HashMap<Txid, u64>, | |||
last_missing: HashMap<Txid, u64>, |
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 agree, and +1 on this idea, it makes it clear, and easy to "derive" its lifecycle and it's and can be useful info to expose to the users.
7f3b6d4
to
be60ada
Compare
missing-at
/last-missing
timestampsevicted-at
/last-evicted
timestamps
.flat_map(|(txid, anchors)| anchors.into_iter().map(move |a| (a, txid))) | ||
.collect(); | ||
tx_update.seen_ats = graph.last_seen.into_iter().collect(); | ||
tx_update |
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.
but at the same time I wonder how useful are the seen-ats without the corresponding evictions?
@ValuedMammal what do you mean by this?
I think it makes sense to completely remove convertibility of TxGraph -> TxUpdate
and TxUpdate -> TxGraph
.
25b005c
to
203c97d
Compare
203c97d
to
7568867
Compare
This is for conveniently adding associations of txid <-> spk. We expect that these txids exist in the spk history. Otherwise, it means the tx is evicted from the mempool and we need to update the `missing_at` value in the sync response.
This is a convenience method for adding unconfirmed txs alongside their associated spks the the sync request. This way, we will be able to detect evictions of these transactions from the mempool.
7568867
to
c209d1a
Compare
c209d1a
to
18fad27
Compare
|
||
env.mine_blocks(101, None)?; | ||
|
||
// Select a UTXO to use as an input for constructing our test transactions. |
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.
Which test transaction?
abae44b
to
9cc40f6
Compare
98af6cd
to
75adeee
Compare
crates/chain/src/tx_graph.rs
Outdated
let chain_tip = chain.get_chain_tip().unwrap(); | ||
|
||
self.list_canonical_txs(chain, chain_tip) | ||
.filter(|c| !c.chain_position.is_confirmed() && indexer.is_tx_relevant(&c.tx_node)) |
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.
Any confirmed tx that is missing from the Electrum's spk history should also be evicted. Since this means that there is a reorg and the tx is not re-introduced to the mempool (evicted).
Make this method work when the indexer is `KeychainTxOutIndex`. We reintroduce the ability to get the internal `SpkTxOutIndex` from `KeychainTxOutIndex` so that `SpkTxOutIndex::relevant_spks_of_tx` is callable from `KeychainTxOutIndex`. This commit renames `iter_spks_with_expected_txids` to `expected_unconfirmed_spk_txids` for `TxGraph`, `IndexedTxGraph` and `SyncRequestBuilder`. Docs are also improved to explain how these methods are useful. Remove unused `SyncRequestBuilder` methods.
* 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.
75adeee
to
ec90121
Compare
Fixes #1740.
Description
This PR replaces #1765. For context and the original discussion that led to this change, please refer to this comment.
This PR addresses a potential malicious double-spending issue by introducing improvements to unconfirmed transaction tracking. Key changes include the addition of
TxUpdate::missing
that tracks transactions that have been replaced and are no longer in the mempool, and the inclusion oflast_evicted
andevicted_at
timestamps inTxGraph
to track when a transaction was last deemed missing.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.Changelog notice
TxUpdate::missing
tracks transactions that have been replaced and are no longer present in mempool.last_evicted
andevicted_at
timestamps inTxGraph
track when a transaction was 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
inSpkClient
keeps track of expectedTxid
s for eachspk
.SpkWithExpectedTxids
support added forbdk_electrum
andbdk_esplora
.SyncRequestBuilder::expected_txids_of_spk
adds an association betweenTxid
andspk
.SyncRequestExt::check_unconfirmed_statuses
adds unconfirmed transactions alongside theirspk
s during sync.Checklists
All Submissions:
cargo fmt
andcargo clippy
before committingNew Features:
Bugfixes: