Skip to content

Commit e9cfbce

Browse files
committed
Use block header from IndexedFilter event
After the new kyoto release (v0.6.3) `Indexedfilter` event has a `header` field which is used directly (previously we fetched header as an additional action). Also renamed import of kyoto `Event` into `KyotoEvent` for readability.
1 parent 539d7bd commit e9cfbce

2 files changed

Lines changed: 13 additions & 43 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bdk_chain = { version = "0.23.3", default-features = false, features = ["std"] }
5858
bdk_esplora = { version = "0.22.2", default-features = false, features = ["async-https-rustls", "tokio"]}
5959
bdk_electrum = { version = "0.24.0", default-features = false, features = ["use-rustls-ring"]}
6060
bdk_wallet = { version = "3.1.0", default-features = false, features = ["std", "keys-bip39"]}
61-
bip157 = { version = "0.6.1", default-features = false }
61+
bip157 = { version = "0.6.3", default-features = false }
6262

6363
bitreq = { version = "0.3", default-features = false, features = ["async-https", "json-using-serde"] }
6464
rustls = { version = "0.23", default-features = false }

src/chain/cbf.rs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
55

66
use bip157::chain::ChainState;
77
use bip157::{
8-
chain::BlockHeaderChanges, Builder as KyotoBuilder, Client, Event, HashCheckpoint, Header,
9-
IndexedBlock, Info, Node as KyotoNode, Package, Requester, TrustedPeer, Warning,
8+
chain::BlockHeaderChanges, Builder as KyotoBuilder, Client, Event as KyotoEvent,
9+
HashCheckpoint, Header, IndexedBlock, Info, Node as KyotoNode, Package, Requester, TrustedPeer,
10+
Warning,
1011
};
1112
use bitcoin::{BlockHash, FeeRate, Network, Script, ScriptBuf, Transaction, Txid};
1213
use electrum_client::{Client as ElectrumClient, ConfigBuilder as ElectrumConfigBuilder};
@@ -577,14 +578,14 @@ impl CbfChainSource {
577578
}
578579

579580
async fn process_kyoto_events(
580-
logger: Arc<Logger>, mut event_rx: mpsc::UnboundedReceiver<Event>,
581+
logger: Arc<Logger>, mut event_rx: mpsc::UnboundedReceiver<KyotoEvent>,
581582
registered_scripts: Arc<Mutex<HashSet<ScriptBuf>>>,
582583
cbf_runtime_status: Arc<Mutex<CbfRuntimeStatus>>, ops_tx: mpsc::UnboundedSender<ChainOp>,
583584
onchain_wallet: Arc<Wallet>, sync_state_tx: watch::Sender<CbfSyncState>,
584585
) {
585586
while let Some(event) = event_rx.recv().await {
586587
match event {
587-
Event::IndexedFilter(indexed_filter) => {
588+
KyotoEvent::IndexedFilter(indexed_filter) => {
588589
// A new block's filter arrived, so we're behind by at least this block until it
589590
// is fetched (if matched) and applied. Flip this before the fetch, not after,
590591
// so a `sync_wallets` call issued in between doesn't return on a stale
@@ -668,61 +669,30 @@ impl CbfChainSource {
668669
};
669670
ChainOp::ConnectFull { block }
670671
} else {
671-
let height = indexed_filter.height();
672-
//TODO we need to recheck that a particular height has not been
673-
//reorganized, and we retrieve indeed the same block header that we
674-
//received `IndexedFilter` event of.
675-
match requester.get_header(height).await {
676-
Ok(Some(indexed_header)) => {
677-
if indexed_header.block_hash() != block_hash {
678-
log_debug!(
679-
logger,
680-
"Filter for {} reorged; skipping",
681-
block_hash
682-
);
683-
continue;
684-
}
685-
ChainOp::ConnectFiltered {
686-
header: indexed_header.header,
687-
height: indexed_header.height,
688-
}
689-
},
690-
Ok(None) => {
691-
log_error!(logger, "No header at height {}", height,);
692-
let _ = ops_tx.send(ChainOp::Failed { error: Error::TxSyncFailed });
693-
break;
694-
},
695-
Err(e) => {
696-
log_error!(
697-
logger,
698-
"Failed to fetch header at height {}: {:?}",
699-
height,
700-
e,
701-
);
702-
let _ = ops_tx.send(ChainOp::Failed { error: Error::TxSyncFailed });
703-
break;
704-
},
672+
ChainOp::ConnectFiltered {
673+
header: indexed_filter.header(),
674+
height: indexed_filter.height(),
705675
}
706676
};
707677
if let Err(e) = ops_tx.send(chop) {
708678
log_debug!(logger, "ops_rx gone: {}", e);
709679
}
710680
},
711-
Event::FiltersSynced(sync_update) => {
681+
KyotoEvent::FiltersSynced(sync_update) => {
712682
//Because application of blocks is async, the fact that kyoto synced up to the
713683
//tip does NOT mean that we caught everything up, that's why we send a ChainOp,
714684
//only processing of which means we processed all blocks up to the tip.
715685
log_info!(logger, "Kyoto synced up to the tip {}", sync_update.tip().height);
716686
let _ = ops_tx.send(ChainOp::Synced { tip_height: sync_update.tip().height });
717687
},
718-
Event::ChainUpdate(BlockHeaderChanges::Connected(indexed_header)) => {
688+
KyotoEvent::ChainUpdate(BlockHeaderChanges::Connected(indexed_header)) => {
719689
log_debug!(
720690
logger,
721691
"Kyoto connected header at height {}",
722692
indexed_header.height
723693
);
724694
},
725-
Event::ChainUpdate(BlockHeaderChanges::Reorganized {
695+
KyotoEvent::ChainUpdate(BlockHeaderChanges::Reorganized {
726696
reorganized,
727697
accepted: _,
728698
}) => {
@@ -735,7 +705,7 @@ impl CbfChainSource {
735705
let _ = ops_tx.send(ChainOp::Disconnect { fork_point });
736706
}
737707
},
738-
Event::ChainUpdate(BlockHeaderChanges::ForkAdded(fork)) => {
708+
KyotoEvent::ChainUpdate(BlockHeaderChanges::ForkAdded(fork)) => {
739709
log_debug!(logger, "Kyoto added fork header at height {}", fork.height);
740710
},
741711
}

0 commit comments

Comments
 (0)