use crate::{BlockId, ChainOracle};
/// This is a local implementation of [`ChainOracle`].
-///
-/// TODO: We need a cache/snapshot thing for chain oracle.
-/// * Minimize calls to remotes.
-/// * Can we cache it forever? Should we drop stuff?
-/// * Assume anything deeper than (i.e. 10) blocks won't be reorged.
-/// * Is this a cache on txs or block? or both?
-/// TODO: Parents of children are confirmed if children are confirmed.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct LocalChain {
blocks: BTreeMap<u32, BlockHash>,
}
}
+ /// Get a reference to the inner map of block height to hash.
+ pub fn inner(&self) -> &BTreeMap<u32, BlockHash> {
+ &self.blocks
+ }
+
pub fn tip(&self) -> Option<BlockId> {
self.blocks
.iter()
.map(|(&height, &hash)| BlockId { height, hash })
}
- /// Get a block at the given height.
- pub fn get_block(&self, height: u32) -> Option<BlockId> {
- self.blocks
- .get(&height)
- .map(|&hash| BlockId { height, hash })
+ /// Get a [`BlockHash`] at the given height.
+ pub fn get_blockhash(&self, height: u32) -> Option<BlockHash> {
+ self.blocks.get(&height).cloned()
}
/// This is like the sparsechain's logic, expect we must guarantee that all invalidated heights
keychain::{Balance, DerivationAdditions, KeychainTxOutIndex},
local_chain::LocalChain,
tx_graph::Additions,
- ConfirmationHeightAnchor, ObservedAs,
+ BlockId, ConfirmationHeightAnchor, ObservedAs,
};
use bitcoin::{secp256k1::Secp256k1, BlockHash, OutPoint, Script, Transaction, TxIn, TxOut};
use miniscript::Descriptor;
let _ = graph.insert_relevant_txs(
[&tx1, &tx2, &tx3, &tx6].iter().enumerate().map(|(i, tx)| {
+ let height = i as u32;
(
*tx,
local_chain
- .get_block(i as u32)
+ .get_blockhash(height)
+ .map(|hash| BlockId { height, hash })
.map(|anchor_block| ConfirmationHeightAnchor {
anchor_block,
confirmation_height: anchor_block.height,
// A helper lambda to extract and filter data from the graph.
let fetch =
- |ht: u32, graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
+ |height: u32,
+ graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
let txouts = graph
- .list_owned_txouts(&local_chain, local_chain.get_block(ht).unwrap())
+ .list_owned_txouts(
+ &local_chain,
+ local_chain
+ .get_blockhash(height)
+ .map(|hash| BlockId { height, hash })
+ .unwrap(),
+ )
.collect::<Vec<_>>();
let utxos = graph
- .list_owned_unspents(&local_chain, local_chain.get_block(ht).unwrap())
+ .list_owned_unspents(
+ &local_chain,
+ local_chain
+ .get_blockhash(height)
+ .map(|hash| BlockId { height, hash })
+ .unwrap(),
+ )
.collect::<Vec<_>>();
let balance = graph.balance(
&local_chain,
- local_chain.get_block(ht).unwrap(),
+ local_chain
+ .get_blockhash(height)
+ .map(|hash| BlockId { height, hash })
+ .unwrap(),
|spk: &Script| trusted_spks.contains(spk),
);