feat!: LocalChain with hardwired genesis checkpoint
This ensures that `LocalChain` will always have a tip. The `ChainOracle` trait's `get_chain_tip` method no longer needs to return an option.
This commit is contained in:
@@ -131,7 +131,7 @@ fn main() -> anyhow::Result<()> {
|
||||
start.elapsed().as_secs_f32()
|
||||
);
|
||||
|
||||
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.0));
|
||||
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.0)?);
|
||||
println!(
|
||||
"[{:>10}s] loaded local chain from changeset",
|
||||
start.elapsed().as_secs_f32()
|
||||
@@ -170,10 +170,7 @@ fn main() -> anyhow::Result<()> {
|
||||
|
||||
let chain_tip = chain.lock().unwrap().tip();
|
||||
let rpc_client = rpc_args.new_client()?;
|
||||
let mut emitter = match chain_tip {
|
||||
Some(cp) => Emitter::from_checkpoint(&rpc_client, cp),
|
||||
None => Emitter::from_height(&rpc_client, fallback_height),
|
||||
};
|
||||
let mut emitter = Emitter::new(&rpc_client, chain_tip, fallback_height);
|
||||
|
||||
let mut last_db_commit = Instant::now();
|
||||
let mut last_print = Instant::now();
|
||||
@@ -205,23 +202,22 @@ fn main() -> anyhow::Result<()> {
|
||||
// print synced-to height and current balance in intervals
|
||||
if last_print.elapsed() >= STDOUT_PRINT_DELAY {
|
||||
last_print = Instant::now();
|
||||
if let Some(synced_to) = chain.tip() {
|
||||
let balance = {
|
||||
graph.graph().balance(
|
||||
&*chain,
|
||||
synced_to.block_id(),
|
||||
graph.index.outpoints().iter().cloned(),
|
||||
|(k, _), _| k == &Keychain::Internal,
|
||||
)
|
||||
};
|
||||
println!(
|
||||
"[{:>10}s] synced to {} @ {} | total: {} sats",
|
||||
start.elapsed().as_secs_f32(),
|
||||
synced_to.hash(),
|
||||
synced_to.height(),
|
||||
balance.total()
|
||||
);
|
||||
}
|
||||
let synced_to = chain.tip();
|
||||
let balance = {
|
||||
graph.graph().balance(
|
||||
&*chain,
|
||||
synced_to.block_id(),
|
||||
graph.index.outpoints().iter().cloned(),
|
||||
|(k, _), _| k == &Keychain::Internal,
|
||||
)
|
||||
};
|
||||
println!(
|
||||
"[{:>10}s] synced to {} @ {} | total: {} sats",
|
||||
start.elapsed().as_secs_f32(),
|
||||
synced_to.hash(),
|
||||
synced_to.height(),
|
||||
balance.total()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,10 +249,7 @@ fn main() -> anyhow::Result<()> {
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel::<Emission>(CHANNEL_BOUND);
|
||||
let emission_jh = std::thread::spawn(move || -> anyhow::Result<()> {
|
||||
let rpc_client = rpc_args.new_client()?;
|
||||
let mut emitter = match last_cp {
|
||||
Some(cp) => Emitter::from_checkpoint(&rpc_client, cp),
|
||||
None => Emitter::from_height(&rpc_client, fallback_height),
|
||||
};
|
||||
let mut emitter = Emitter::new(&rpc_client, last_cp, fallback_height);
|
||||
|
||||
let mut block_count = rpc_client.get_block_count()? as u32;
|
||||
tx.send(Emission::Tip(block_count))?;
|
||||
@@ -335,24 +328,23 @@ fn main() -> anyhow::Result<()> {
|
||||
|
||||
if last_print.map_or(Duration::MAX, |i| i.elapsed()) >= STDOUT_PRINT_DELAY {
|
||||
last_print = Some(Instant::now());
|
||||
if let Some(synced_to) = chain.tip() {
|
||||
let balance = {
|
||||
graph.graph().balance(
|
||||
&*chain,
|
||||
synced_to.block_id(),
|
||||
graph.index.outpoints().iter().cloned(),
|
||||
|(k, _), _| k == &Keychain::Internal,
|
||||
)
|
||||
};
|
||||
println!(
|
||||
"[{:>10}s] synced to {} @ {} / {} | total: {} sats",
|
||||
start.elapsed().as_secs_f32(),
|
||||
synced_to.hash(),
|
||||
synced_to.height(),
|
||||
tip_height,
|
||||
balance.total()
|
||||
);
|
||||
}
|
||||
let synced_to = chain.tip();
|
||||
let balance = {
|
||||
graph.graph().balance(
|
||||
&*chain,
|
||||
synced_to.block_id(),
|
||||
graph.index.outpoints().iter().cloned(),
|
||||
|(k, _), _| k == &Keychain::Internal,
|
||||
)
|
||||
};
|
||||
println!(
|
||||
"[{:>10}s] synced to {} @ {} / {} | total: {} sats",
|
||||
start.elapsed().as_secs_f32(),
|
||||
synced_to.hash(),
|
||||
synced_to.height(),
|
||||
tip_height,
|
||||
balance.total()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -315,10 +315,8 @@ where
|
||||
version: 0x02,
|
||||
// because the temporary planning module does not support timelocks, we can use the chain
|
||||
// tip as the `lock_time` for anti-fee-sniping purposes
|
||||
lock_time: chain
|
||||
.get_chain_tip()?
|
||||
.and_then(|block_id| absolute::LockTime::from_height(block_id.height).ok())
|
||||
.unwrap_or(absolute::LockTime::ZERO),
|
||||
lock_time: absolute::LockTime::from_height(chain.get_chain_tip()?.height)
|
||||
.expect("invalid height"),
|
||||
input: selected_txos
|
||||
.iter()
|
||||
.map(|(_, utxo)| TxIn {
|
||||
@@ -404,7 +402,7 @@ pub fn planned_utxos<A: Anchor, O: ChainOracle, K: Clone + bdk_tmp_plan::CanDeri
|
||||
chain: &O,
|
||||
assets: &bdk_tmp_plan::Assets<K>,
|
||||
) -> Result<Vec<(bdk_tmp_plan::Plan<K>, FullTxOut<A>)>, O::Error> {
|
||||
let chain_tip = chain.get_chain_tip()?.unwrap_or_default();
|
||||
let chain_tip = chain.get_chain_tip()?;
|
||||
let outpoints = graph.index.outpoints().iter().cloned();
|
||||
graph
|
||||
.graph()
|
||||
@@ -509,7 +507,7 @@ where
|
||||
|
||||
let balance = graph.graph().try_balance(
|
||||
chain,
|
||||
chain.get_chain_tip()?.unwrap_or_default(),
|
||||
chain.get_chain_tip()?,
|
||||
graph.index.outpoints().iter().cloned(),
|
||||
|(k, _), _| k == &Keychain::Internal,
|
||||
)?;
|
||||
@@ -539,7 +537,7 @@ where
|
||||
Commands::TxOut { txout_cmd } => {
|
||||
let graph = &*graph.lock().unwrap();
|
||||
let chain = &*chain.lock().unwrap();
|
||||
let chain_tip = chain.get_chain_tip()?.unwrap_or_default();
|
||||
let chain_tip = chain.get_chain_tip()?;
|
||||
let outpoints = graph.index.outpoints().iter().cloned();
|
||||
|
||||
match txout_cmd {
|
||||
|
||||
@@ -112,7 +112,7 @@ fn main() -> anyhow::Result<()> {
|
||||
graph
|
||||
});
|
||||
|
||||
let chain = Mutex::new(LocalChain::from_changeset(disk_local_chain));
|
||||
let chain = Mutex::new(LocalChain::from_changeset(disk_local_chain)?);
|
||||
|
||||
let electrum_cmd = match &args.command {
|
||||
example_cli::Commands::ChainSpecific(electrum_cmd) => electrum_cmd,
|
||||
@@ -193,7 +193,7 @@ fn main() -> anyhow::Result<()> {
|
||||
// Get a short lock on the tracker to get the spks we're interested in
|
||||
let graph = graph.lock().unwrap();
|
||||
let chain = chain.lock().unwrap();
|
||||
let chain_tip = chain.tip().map(|cp| cp.block_id()).unwrap_or_default();
|
||||
let chain_tip = chain.tip().block_id();
|
||||
|
||||
if !(all_spks || unused_spks || utxos || unconfirmed) {
|
||||
unused_spks = true;
|
||||
|
||||
@@ -5,10 +5,10 @@ use std::{
|
||||
};
|
||||
|
||||
use bdk_chain::{
|
||||
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
|
||||
bitcoin::{constants::genesis_block, Address, Network, OutPoint, ScriptBuf, Txid},
|
||||
indexed_tx_graph::{self, IndexedTxGraph},
|
||||
keychain,
|
||||
local_chain::{self, CheckPoint, LocalChain},
|
||||
local_chain::{self, LocalChain},
|
||||
Append, ConfirmationTimeHeightAnchor,
|
||||
};
|
||||
|
||||
@@ -102,6 +102,8 @@ fn main() -> anyhow::Result<()> {
|
||||
let (args, keymap, index, db, init_changeset) =
|
||||
example_cli::init::<EsploraCommands, EsploraArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;
|
||||
|
||||
let genesis_hash = genesis_block(args.network).block_hash();
|
||||
|
||||
let (init_chain_changeset, init_indexed_tx_graph_changeset) = init_changeset;
|
||||
|
||||
// Contruct `IndexedTxGraph` and `LocalChain` with our initial changeset. They are wrapped in
|
||||
@@ -113,8 +115,8 @@ fn main() -> anyhow::Result<()> {
|
||||
graph
|
||||
});
|
||||
let chain = Mutex::new({
|
||||
let mut chain = LocalChain::default();
|
||||
chain.apply_changeset(&init_chain_changeset);
|
||||
let (mut chain, _) = LocalChain::from_genesis_hash(genesis_hash);
|
||||
chain.apply_changeset(&init_chain_changeset)?;
|
||||
chain
|
||||
});
|
||||
|
||||
@@ -234,7 +236,7 @@ fn main() -> anyhow::Result<()> {
|
||||
{
|
||||
let graph = graph.lock().unwrap();
|
||||
let chain = chain.lock().unwrap();
|
||||
let chain_tip = chain.tip().map(|cp| cp.block_id()).unwrap_or_default();
|
||||
let chain_tip = chain.tip().block_id();
|
||||
|
||||
if *all_spks {
|
||||
let all_spks = graph
|
||||
@@ -332,7 +334,7 @@ fn main() -> anyhow::Result<()> {
|
||||
(missing_block_heights, tip)
|
||||
};
|
||||
|
||||
println!("prev tip: {}", tip.as_ref().map_or(0, CheckPoint::height));
|
||||
println!("prev tip: {}", tip.height());
|
||||
println!("missing block heights: {:?}", missing_block_heights);
|
||||
|
||||
// Here, we actually fetch the missing blocks and create a `local_chain::Update`.
|
||||
|
||||
Reference in New Issue
Block a user