Skip to content

Commit 886d72e

Browse files
committed
chore(chain)!: rm missing_heights and missing_heights_from methods
These methods are no longer needed as we can determine missing heights directly from the `CheckPoint` tip.
1 parent bd62aa0 commit 886d72e

File tree

2 files changed

+2
-218
lines changed

2 files changed

+2
-218
lines changed

crates/chain/src/tx_graph.rs

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
//! [`insert_txout`]: TxGraph::insert_txout
9090
9191
use crate::{
92-
collections::*, keychain::Balance, local_chain::LocalChain, Anchor, Append, BlockId,
93-
ChainOracle, ChainPosition, FullTxOut,
92+
collections::*, keychain::Balance, Anchor, Append, BlockId, ChainOracle, ChainPosition,
93+
FullTxOut,
9494
};
9595
use alloc::collections::vec_deque::VecDeque;
9696
use alloc::sync::Arc;
@@ -759,69 +759,6 @@ impl<A: Clone + Ord> TxGraph<A> {
759759
}
760760

761761
impl<A: Anchor> TxGraph<A> {
762-
/// Find missing block heights of `chain`.
763-
///
764-
/// This works by scanning through anchors, and seeing whether the anchor block of the anchor
765-
/// exists in the [`LocalChain`]. The returned iterator does not output duplicate heights.
766-
pub fn missing_heights<'a>(&'a self, chain: &'a LocalChain) -> impl Iterator<Item = u32> + 'a {
767-
// Map of txids to skip.
768-
//
769-
// Usually, if a height of a tx anchor is missing from the chain, we would want to return
770-
// this height in the iterator. The exception is when the tx is confirmed in chain. All the
771-
// other missing-height anchors of this tx can be skipped.
772-
//
773-
// * Some(true) => skip all anchors of this txid
774-
// * Some(false) => do not skip anchors of this txid
775-
// * None => we do not know whether we can skip this txid
776-
let mut txids_to_skip = HashMap::<Txid, bool>::new();
777-
778-
// Keeps track of the last height emitted so we don't double up.
779-
let mut last_height_emitted = Option::<u32>::None;
780-
781-
self.anchors
782-
.iter()
783-
.filter(move |(_, txid)| {
784-
let skip = *txids_to_skip.entry(*txid).or_insert_with(|| {
785-
let tx_anchors = match self.txs.get(txid) {
786-
Some((_, anchors, _)) => anchors,
787-
None => return true,
788-
};
789-
let mut has_missing_height = false;
790-
for anchor_block in tx_anchors.iter().map(Anchor::anchor_block) {
791-
match chain.get(anchor_block.height) {
792-
None => {
793-
has_missing_height = true;
794-
continue;
795-
}
796-
Some(chain_cp) => {
797-
if chain_cp.hash() == anchor_block.hash {
798-
return true;
799-
}
800-
}
801-
}
802-
}
803-
!has_missing_height
804-
});
805-
#[cfg(feature = "std")]
806-
debug_assert!({
807-
println!("txid={} skip={}", txid, skip);
808-
true
809-
});
810-
!skip
811-
})
812-
.filter_map(move |(a, _)| {
813-
let anchor_block = a.anchor_block();
814-
if Some(anchor_block.height) != last_height_emitted
815-
&& chain.get(anchor_block.height).is_none()
816-
{
817-
last_height_emitted = Some(anchor_block.height);
818-
Some(anchor_block.height)
819-
} else {
820-
None
821-
}
822-
})
823-
}
824-
825762
/// Get the position of the transaction in `chain` with tip `chain_tip`.
826763
///
827764
/// Chain data is fetched from `chain`, a [`ChainOracle`] implementation.
@@ -1330,8 +1267,6 @@ impl<A> ChangeSet<A> {
13301267
///
13311268
/// This is useful if you want to find which heights you need to fetch data about in order to
13321269
/// confirm or exclude these anchors.
1333-
///
1334-
/// See also: [`TxGraph::missing_heights`]
13351270
pub fn anchor_heights(&self) -> impl Iterator<Item = u32> + '_
13361271
where
13371272
A: Anchor,
@@ -1346,24 +1281,6 @@ impl<A> ChangeSet<A> {
13461281
!duplicate
13471282
})
13481283
}
1349-
1350-
/// Returns an iterator for the [`anchor_heights`] in this changeset that are not included in
1351-
/// `local_chain`. This tells you which heights you need to include in `local_chain` in order
1352-
/// for it to conclusively act as a [`ChainOracle`] for the transaction anchors this changeset
1353-
/// will add.
1354-
///
1355-
/// [`ChainOracle`]: crate::ChainOracle
1356-
/// [`anchor_heights`]: Self::anchor_heights
1357-
pub fn missing_heights_from<'a>(
1358-
&'a self,
1359-
local_chain: &'a LocalChain,
1360-
) -> impl Iterator<Item = u32> + 'a
1361-
where
1362-
A: Anchor,
1363-
{
1364-
self.anchor_heights()
1365-
.filter(move |&height| local_chain.get(height).is_none())
1366-
}
13671284
}
13681285

13691286
impl<A: Ord> Append for ChangeSet<A> {

crates/chain/tests/test_tx_graph.rs

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,139 +1087,6 @@ fn update_last_seen_unconfirmed() {
10871087
assert_eq!(graph.full_txs().next().unwrap().last_seen_unconfirmed, 2);
10881088
}
10891089

1090-
#[test]
1091-
fn test_missing_blocks() {
1092-
/// An anchor implementation for testing, made up of `(the_anchor_block, random_data)`.
1093-
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, core::hash::Hash)]
1094-
struct TestAnchor(BlockId);
1095-
1096-
impl Anchor for TestAnchor {
1097-
fn anchor_block(&self) -> BlockId {
1098-
self.0
1099-
}
1100-
}
1101-
1102-
struct Scenario<'a> {
1103-
name: &'a str,
1104-
graph: TxGraph<TestAnchor>,
1105-
chain: LocalChain,
1106-
exp_heights: &'a [u32],
1107-
}
1108-
1109-
const fn new_anchor(height: u32, hash: BlockHash) -> TestAnchor {
1110-
TestAnchor(BlockId { height, hash })
1111-
}
1112-
1113-
fn new_scenario<'a>(
1114-
name: &'a str,
1115-
graph_anchors: &'a [(Txid, TestAnchor)],
1116-
chain: &'a [(u32, BlockHash)],
1117-
exp_heights: &'a [u32],
1118-
) -> Scenario<'a> {
1119-
Scenario {
1120-
name,
1121-
graph: {
1122-
let mut g = TxGraph::default();
1123-
for (txid, anchor) in graph_anchors {
1124-
let _ = g.insert_anchor(*txid, anchor.clone());
1125-
}
1126-
g
1127-
},
1128-
chain: {
1129-
let (mut c, _) = LocalChain::from_genesis_hash(h!("genesis"));
1130-
for (height, hash) in chain {
1131-
let _ = c.insert_block(BlockId {
1132-
height: *height,
1133-
hash: *hash,
1134-
});
1135-
}
1136-
c
1137-
},
1138-
exp_heights,
1139-
}
1140-
}
1141-
1142-
fn run(scenarios: &[Scenario]) {
1143-
for scenario in scenarios {
1144-
let Scenario {
1145-
name,
1146-
graph,
1147-
chain,
1148-
exp_heights,
1149-
} = scenario;
1150-
1151-
let heights = graph.missing_heights(chain).collect::<Vec<_>>();
1152-
assert_eq!(&heights, exp_heights, "scenario: {}", name);
1153-
}
1154-
}
1155-
1156-
run(&[
1157-
new_scenario(
1158-
"2 txs with the same anchor (2:B) which is missing from chain",
1159-
&[
1160-
(h!("tx_1"), new_anchor(2, h!("B"))),
1161-
(h!("tx_2"), new_anchor(2, h!("B"))),
1162-
],
1163-
&[(1, h!("A")), (3, h!("C"))],
1164-
&[2],
1165-
),
1166-
new_scenario(
1167-
"2 txs with different anchors at the same height, one of the anchors is missing",
1168-
&[
1169-
(h!("tx_1"), new_anchor(2, h!("B1"))),
1170-
(h!("tx_2"), new_anchor(2, h!("B2"))),
1171-
],
1172-
&[(1, h!("A")), (2, h!("B1"))],
1173-
&[],
1174-
),
1175-
new_scenario(
1176-
"tx with 2 anchors of same height which are missing from the chain",
1177-
&[
1178-
(h!("tx"), new_anchor(3, h!("C1"))),
1179-
(h!("tx"), new_anchor(3, h!("C2"))),
1180-
],
1181-
&[(1, h!("A")), (4, h!("D"))],
1182-
&[3],
1183-
),
1184-
new_scenario(
1185-
"tx with 2 anchors at the same height, chain has this height but does not match either anchor",
1186-
&[
1187-
(h!("tx"), new_anchor(4, h!("D1"))),
1188-
(h!("tx"), new_anchor(4, h!("D2"))),
1189-
],
1190-
&[(4, h!("D3")), (5, h!("E"))],
1191-
&[],
1192-
),
1193-
new_scenario(
1194-
"tx with 2 anchors at different heights, one anchor exists in chain, should return nothing",
1195-
&[
1196-
(h!("tx"), new_anchor(3, h!("C"))),
1197-
(h!("tx"), new_anchor(4, h!("D"))),
1198-
],
1199-
&[(4, h!("D")), (5, h!("E"))],
1200-
&[],
1201-
),
1202-
new_scenario(
1203-
"tx with 2 anchors at different heights, first height is already in chain with different hash, iterator should only return 2nd height",
1204-
&[
1205-
(h!("tx"), new_anchor(5, h!("E1"))),
1206-
(h!("tx"), new_anchor(6, h!("F1"))),
1207-
],
1208-
&[(4, h!("D")), (5, h!("E")), (7, h!("G"))],
1209-
&[6],
1210-
),
1211-
new_scenario(
1212-
"tx with 2 anchors at different heights, neither height is in chain, both heights should be returned",
1213-
&[
1214-
(h!("tx"), new_anchor(3, h!("C"))),
1215-
(h!("tx"), new_anchor(4, h!("D"))),
1216-
],
1217-
&[(1, h!("A")), (2, h!("B"))],
1218-
&[3, 4],
1219-
),
1220-
]);
1221-
}
1222-
12231090
#[test]
12241091
/// The `map_anchors` allow a caller to pass a function to reconstruct the [`TxGraph`] with any [`Anchor`],
12251092
/// even though the function is non-deterministic.

0 commit comments

Comments
 (0)