fix(example): bitcoind_rpc_polling now initializes local_chain properly

Previously, the genesis block is not initialized properly. Thank you
@notmandatory for identifying this bug.
This commit is contained in:
志宇 2023-12-28 12:49:04 +08:00
parent 4fd539b647
commit bc796f412a
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8

View File

@ -12,7 +12,7 @@ use bdk_bitcoind_rpc::{
Emitter,
};
use bdk_chain::{
bitcoin::{Block, Transaction},
bitcoin::{constants::genesis_block, Block, Transaction},
indexed_tx_graph, keychain,
local_chain::{self, CheckPoint, LocalChain},
ConfirmationTimeHeightAnchor, IndexedTxGraph,
@ -117,10 +117,11 @@ fn main() -> anyhow::Result<()> {
"[{:>10}s] loaded initial changeset from db",
start.elapsed().as_secs_f32()
);
let (init_chain_changeset, init_graph_changeset) = init_changeset;
let graph = Mutex::new({
let mut graph = IndexedTxGraph::new(index);
graph.apply_changeset(init_changeset.1);
graph.apply_changeset(init_graph_changeset);
graph
});
println!(
@ -128,7 +129,16 @@ fn main() -> anyhow::Result<()> {
start.elapsed().as_secs_f32()
);
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.0)?);
let chain = Mutex::new(if init_chain_changeset.is_empty() {
let genesis_hash = genesis_block(args.network).block_hash();
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
let mut db = db.lock().unwrap();
db.stage((chain_changeset, Default::default()));
db.commit()?;
chain
} else {
LocalChain::from_changeset(init_chain_changeset)?
});
println!(
"[{:>10}s] loaded local chain from changeset",
start.elapsed().as_secs_f32()