Evan Lin
I write code. I like Bitcoin.
@evanlinjin
Example of ambiguous representation:
blockchain.scripthash.get_history(scripthash)
[
{ "height": 200004, "tx_hash": "acc3758bd2a26f869fcc67d48ff30b96464d476bca82c1cd6656e7d506816412" },
{ "height": 215008, "tx_hash": "f3e1bf48975b8d6060a9de8884296abb80be618dc00ae3cb2f6cee3085e09403" }
]
get_history(scripthash) -> [ { confirmation_height, txid }, ... ]
Why is this bad?
In bdk_chain
, you need to "anchor" a transaction to a block_height
AND block_hash
.
pub struct BlockId { pub height: u32, pub hash: BlockHash }
pub trait Anchor {
fn anchor_block(&self) -> BlockId;
/* Hidden methods */
}
Does this mean BDK is incompatible with Electrum?
No.
`get_history`
calls.`blockchain.transaction.get_merkle`
against fetched transactions.crate `bdk_electrum`
Is the transaction anchored to a block that belongs in the chain identified by `chain_tip`?
pub trait ChainOracle {
fn is_block_in_chain(
&self,
block: BlockId,
chain_tip: BlockId,
) -> Result<Option<bool>, Self::Error>;
/* ... */
}
pub struct BlockId { pub height: u32, pub hash: BlockHash }
`bdk_chain::TxGraph`
Example of a monotone data structure.
`bitcoin::Transaction`
s`Anchor`
s`last_seen`
(in mempool) timestamppub struct TxGraph<A> {
txs: HashMap<Txid, (TxNodeInternal, BTreeSet<A>, u64)>,
spends: BTreeMap<OutPoint, HashSet<Txid>>,
anchors: BTreeSet<(A, Txid)>,
}
Extrapolating a consistent history of transactions
impl<A: Anchor> TxGraph<A> {
pub fn list_chain_txs<'a, C: ChainOracle + 'a>(
&'a self,
chain: &'a C,
chain_tip: BlockId,
) -> impl Iterator<Item = CanonicalTx<'a, Transaction, A>> { /* … */ }
}
`TxGraph::list_chain_txs`
worksFor each transaction Tn:
pub fn list_chain_txs(&self, chain: &impl ChainOracle, chain_tip: BlockId) -> impl Iterator<Item = CanonicalTx> { /* … */ }
`TxGraph`
is a set of transactions, `Anchor`
s and last_seen timestamps.`TxGraph`
is monotone and can be updated in any order. No invalid representation.`ChainOracle`
gives us a view of the chain identified with `chain_tip`
(blockhash+height).`TxGraph::list_chain_txs`
.Join us on Discord
By Evan Lin