Skip to content

Commit adc9513

Browse files
committed
fix(chain)! Re-order fields in anchors so Ord DWIM
1 parent 7aca884 commit adc9513

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

crates/chain/src/chain_data.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{Anchor, AnchorFromBlockPosition, COINBASE_MATURITY};
99
pub enum ChainPosition<A> {
1010
/// The chain data is seen as confirmed, and in anchored by `A`.
1111
Confirmed(A),
12-
/// The chain data is seen in mempool at this given timestamp.
12+
/// The chain data is not confirmed and last seen in the mempool at this timestamp.
1313
Unconfirmed(u64),
1414
}
1515

@@ -48,14 +48,14 @@ impl<A: Anchor> ChainPosition<A> {
4848
serde(crate = "serde_crate")
4949
)]
5050
pub enum ConfirmationTime {
51-
/// The confirmed variant.
51+
/// The transaction is confirmed
5252
Confirmed {
5353
/// Confirmation height.
5454
height: u32,
5555
/// Confirmation time in unix seconds.
5656
time: u64,
5757
},
58-
/// The unconfirmed variant.
58+
/// The transaction is unconfirmed
5959
Unconfirmed {
6060
/// The last-seen timestamp in unix seconds.
6161
last_seen: u64,
@@ -157,13 +157,12 @@ impl From<(&u32, &BlockHash)> for BlockId {
157157
serde(crate = "serde_crate")
158158
)]
159159
pub struct ConfirmationHeightAnchor {
160-
/// The anchor block.
161-
pub anchor_block: BlockId,
162-
163160
/// The exact confirmation height of the transaction.
164161
///
165162
/// It is assumed that this value is never larger than the height of the anchor block.
166163
pub confirmation_height: u32,
164+
/// The anchor block.
165+
pub anchor_block: BlockId,
167166
}
168167

169168
impl Anchor for ConfirmationHeightAnchor {
@@ -198,12 +197,12 @@ impl AnchorFromBlockPosition for ConfirmationHeightAnchor {
198197
serde(crate = "serde_crate")
199198
)]
200199
pub struct ConfirmationTimeHeightAnchor {
201-
/// The anchor block.
202-
pub anchor_block: BlockId,
203-
/// The confirmation height of the chain data being anchored.
200+
/// The confirmation height of the transaction being anchored.
204201
pub confirmation_height: u32,
205-
/// The confirmation time of the chain data being anchored.
202+
/// The confirmation time of the transaction being anchored.
206203
pub confirmation_time: u64,
204+
/// The anchor block.
205+
pub anchor_block: BlockId,
207206
}
208207

209208
impl Anchor for ConfirmationTimeHeightAnchor {
@@ -229,12 +228,12 @@ impl AnchorFromBlockPosition for ConfirmationTimeHeightAnchor {
229228
/// A `TxOut` with as much data as we can retrieve about it
230229
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
231230
pub struct FullTxOut<A> {
231+
/// The position of the transaction in `outpoint` in the overall chain.
232+
pub chain_position: ChainPosition<A>,
232233
/// The location of the `TxOut`.
233234
pub outpoint: OutPoint,
234235
/// The `TxOut`.
235236
pub txout: TxOut,
236-
/// The position of the transaction in `outpoint` in the overall chain.
237-
pub chain_position: ChainPosition<A>,
238237
/// The txid and chain position of the transaction (if any) that has spent this output.
239238
pub spent_by: Option<(ChainPosition<A>, Txid)>,
240239
/// Whether this output is on a coinbase transaction.
@@ -299,3 +298,35 @@ impl<A: Anchor> FullTxOut<A> {
299298
true
300299
}
301300
}
301+
302+
#[cfg(test)]
303+
mod test {
304+
use super::*;
305+
306+
#[test]
307+
fn chain_position_ord() {
308+
let unconf1 = ChainPosition::<ConfirmationHeightAnchor>::Unconfirmed(10);
309+
let unconf2 = ChainPosition::<ConfirmationHeightAnchor>::Unconfirmed(20);
310+
let conf1 = ChainPosition::Confirmed(ConfirmationHeightAnchor {
311+
confirmation_height: 9,
312+
anchor_block: BlockId {
313+
height: 20,
314+
..Default::default()
315+
},
316+
});
317+
let conf2 = ChainPosition::Confirmed(ConfirmationHeightAnchor {
318+
confirmation_height: 12,
319+
anchor_block: BlockId {
320+
height: 15,
321+
..Default::default()
322+
},
323+
});
324+
325+
assert!(unconf2 > unconf1, "higher last_seen means higher ord");
326+
assert!(unconf1 > conf1, "unconfirmed is higher ord than confirmed");
327+
assert!(
328+
conf2 > conf1,
329+
"confirmation_height is higher then it should be higher ord"
330+
);
331+
}
332+
}

0 commit comments

Comments
 (0)