|
1 | 1 | use bdk_bitcoind_rpc::{ |
2 | 2 | bitcoincore_rpc::{Auth, Client, RpcApi}, |
3 | | - Emitter, |
| 3 | + Emitter, MempoolEvent, |
4 | 4 | }; |
5 | 5 | use bdk_wallet::{ |
6 | | - bitcoin::{Block, Network, Transaction}, |
| 6 | + bitcoin::{Block, Network}, |
7 | 7 | file_store::Store, |
8 | 8 | KeychainKind, Wallet, |
9 | 9 | }; |
10 | 10 | use clap::{self, Parser}; |
11 | | -use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant}; |
| 11 | +use std::{ |
| 12 | + path::PathBuf, |
| 13 | + sync::{mpsc::sync_channel, Arc}, |
| 14 | + thread::spawn, |
| 15 | + time::Instant, |
| 16 | +}; |
12 | 17 |
|
13 | 18 | const DB_MAGIC: &str = "bdk-rpc-wallet-example"; |
14 | 19 |
|
@@ -73,21 +78,21 @@ impl Args { |
73 | 78 | enum Emission { |
74 | 79 | SigTerm, |
75 | 80 | Block(bdk_bitcoind_rpc::BlockEvent<Block>), |
76 | | - Mempool(Vec<(Transaction, u64)>), |
| 81 | + Mempool(MempoolEvent), |
77 | 82 | } |
78 | 83 |
|
79 | 84 | fn main() -> anyhow::Result<()> { |
80 | 85 | let args = Args::parse(); |
81 | 86 |
|
82 | | - let rpc_client = args.client()?; |
| 87 | + let rpc_client = Arc::new(args.client()?); |
83 | 88 | println!( |
84 | 89 | "Connected to Bitcoin Core RPC at {:?}", |
85 | 90 | rpc_client.get_blockchain_info().unwrap() |
86 | 91 | ); |
87 | 92 |
|
88 | 93 | let start_load_wallet = Instant::now(); |
89 | | - let mut db = |
90 | | - Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), args.db_path)?; |
| 94 | + let (mut db, _) = |
| 95 | + Store::<bdk_wallet::ChangeSet>::load_or_create(DB_MAGIC.as_bytes(), args.db_path)?; |
91 | 96 | let wallet_opt = Wallet::load() |
92 | 97 | .descriptor(KeychainKind::External, Some(args.descriptor.clone())) |
93 | 98 | .descriptor(KeychainKind::Internal, args.change_descriptor.clone()) |
@@ -129,9 +134,15 @@ fn main() -> anyhow::Result<()> { |
129 | 134 | .expect("failed to send sigterm") |
130 | 135 | }); |
131 | 136 |
|
132 | | - let emitter_tip = wallet_tip.clone(); |
| 137 | + let mut emitter = Emitter::new( |
| 138 | + rpc_client, |
| 139 | + wallet_tip, |
| 140 | + args.start_height, |
| 141 | + wallet |
| 142 | + .transactions() |
| 143 | + .filter(|tx| tx.chain_position.is_unconfirmed()), |
| 144 | + ); |
133 | 145 | spawn(move || -> Result<(), anyhow::Error> { |
134 | | - let mut emitter = Emitter::new(&rpc_client, emitter_tip, args.start_height); |
135 | 146 | while let Some(emission) = emitter.next_block()? { |
136 | 147 | sender.send(Emission::Block(emission))?; |
137 | 148 | } |
@@ -160,9 +171,10 @@ fn main() -> anyhow::Result<()> { |
160 | 171 | hash, height, elapsed |
161 | 172 | ); |
162 | 173 | } |
163 | | - Emission::Mempool(mempool_emission) => { |
| 174 | + Emission::Mempool(event) => { |
164 | 175 | let start_apply_mempool = Instant::now(); |
165 | | - wallet.apply_unconfirmed_txs(mempool_emission); |
| 176 | + wallet.apply_evicted_txs(event.evicted_ats()); |
| 177 | + wallet.apply_unconfirmed_txs(event.new_txs); |
166 | 178 | wallet.persist(&mut db)?; |
167 | 179 | println!( |
168 | 180 | "Applied unconfirmed transactions in {}s", |
|
0 commit comments