Skip to content

Commit a28076a

Browse files
committed
refactor: move sync to a helper function
This is a pre-requisite for adding Payjoin support. When the receiver sends the Payjoin proposal to the sender to be broadcasted, they need to sync the blockchain before checking if the Payjoin has indeed been broadcasted. To do that, the sync function will need to be shared between the two online commands.
1 parent 7df419a commit a28076a

File tree

1 file changed

+92
-77
lines changed

1 file changed

+92
-77
lines changed

src/handlers.rs

Lines changed: 92 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -683,83 +683,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
683683
Ok(serde_json::to_string_pretty(&json!({}))?)
684684
}
685685
Sync => {
686-
#[cfg(any(feature = "electrum", feature = "esplora"))]
687-
let request = wallet
688-
.start_sync_with_revealed_spks()
689-
.inspect(|item, progress| {
690-
let pc = (100 * progress.consumed()) as f32 / progress.total() as f32;
691-
eprintln!("[ SCANNING {pc:03.0}% ] {item}");
692-
});
693-
match client {
694-
#[cfg(feature = "electrum")]
695-
Electrum { client, batch_size } => {
696-
// Populate the electrum client's transaction cache so it doesn't re-download transaction we
697-
// already have.
698-
client
699-
.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
700-
701-
let update = client.sync(request, batch_size, false)?;
702-
wallet.apply_update(update)?;
703-
}
704-
#[cfg(feature = "esplora")]
705-
Esplora {
706-
client,
707-
parallel_requests,
708-
} => {
709-
let update = client
710-
.sync(request, parallel_requests)
711-
.await
712-
.map_err(|e| *e)?;
713-
wallet.apply_update(update)?;
714-
}
715-
#[cfg(feature = "rpc")]
716-
RpcClient { client } => {
717-
let blockchain_info = client.get_blockchain_info()?;
718-
let wallet_cp = wallet.latest_checkpoint();
719-
720-
// reload the last 200 blocks in case of a reorg
721-
let emitter_height = wallet_cp.height().saturating_sub(200);
722-
let mut emitter = Emitter::new(
723-
&*client,
724-
wallet_cp,
725-
emitter_height,
726-
wallet
727-
.tx_graph()
728-
.list_canonical_txs(
729-
wallet.local_chain(),
730-
wallet.local_chain().tip().block_id(),
731-
CanonicalizationParams::default(),
732-
)
733-
.filter(|tx| tx.chain_position.is_unconfirmed()),
734-
);
735-
736-
while let Some(block_event) = emitter.next_block()? {
737-
if block_event.block_height() % 10_000 == 0 {
738-
let percent_done = f64::from(block_event.block_height())
739-
/ f64::from(blockchain_info.headers as u32)
740-
* 100f64;
741-
println!(
742-
"Applying block at height: {}, {:.2}% done.",
743-
block_event.block_height(),
744-
percent_done
745-
);
746-
}
747-
748-
wallet.apply_block_connected_to(
749-
&block_event.block,
750-
block_event.block_height(),
751-
block_event.connected_to(),
752-
)?;
753-
}
754-
755-
let mempool_txs = emitter.mempool()?;
756-
wallet.apply_unconfirmed_txs(mempool_txs.update);
757-
}
758-
#[cfg(feature = "cbf")]
759-
KyotoClient { client } => {
760-
sync_kyoto_client(wallet, client).await?;
761-
}
762-
}
686+
sync_wallet(client, wallet).await?;
763687
Ok(serde_json::to_string_pretty(&json!({}))?)
764688
}
765689
Broadcast { psbt, tx } => {
@@ -1325,6 +1249,97 @@ async fn respond(
13251249
}
13261250
}
13271251

1252+
#[cfg(any(
1253+
feature = "electrum",
1254+
feature = "esplora",
1255+
feature = "cbf",
1256+
feature = "rpc"
1257+
))]
1258+
/// Syncs a given wallet using the blockchain client.
1259+
pub async fn sync_wallet(client: BlockchainClient, wallet: &mut Wallet) -> Result<(), Error> {
1260+
#[cfg(any(feature = "electrum", feature = "esplora"))]
1261+
let request = wallet
1262+
.start_sync_with_revealed_spks()
1263+
.inspect(|item, progress| {
1264+
let pc = (100 * progress.consumed()) as f32 / progress.total() as f32;
1265+
eprintln!("[ SCANNING {pc:03.0}% ] {item}");
1266+
});
1267+
match client {
1268+
#[cfg(feature = "electrum")]
1269+
Electrum { client, batch_size } => {
1270+
// Populate the electrum client's transaction cache so it doesn't re-download transaction we
1271+
// already have.
1272+
client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
1273+
1274+
let update = client.sync(request, batch_size, false)?;
1275+
wallet
1276+
.apply_update(update)
1277+
.map_err(|e| Error::Generic(e.to_string()))
1278+
}
1279+
#[cfg(feature = "esplora")]
1280+
Esplora {
1281+
client,
1282+
parallel_requests,
1283+
} => {
1284+
let update = client
1285+
.sync(request, parallel_requests)
1286+
.await
1287+
.map_err(|e| *e)?;
1288+
wallet
1289+
.apply_update(update)
1290+
.map_err(|e| Error::Generic(e.to_string()))
1291+
}
1292+
#[cfg(feature = "rpc")]
1293+
RpcClient { client } => {
1294+
let blockchain_info = client.get_blockchain_info()?;
1295+
let wallet_cp = wallet.latest_checkpoint();
1296+
1297+
// reload the last 200 blocks in case of a reorg
1298+
let emitter_height = wallet_cp.height().saturating_sub(200);
1299+
let mut emitter = Emitter::new(
1300+
&*client,
1301+
wallet_cp,
1302+
emitter_height,
1303+
wallet
1304+
.tx_graph()
1305+
.list_canonical_txs(
1306+
wallet.local_chain(),
1307+
wallet.local_chain().tip().block_id(),
1308+
CanonicalizationParams::default(),
1309+
)
1310+
.filter(|tx| tx.chain_position.is_unconfirmed()),
1311+
);
1312+
1313+
while let Some(block_event) = emitter.next_block()? {
1314+
if block_event.block_height() % 10_000 == 0 {
1315+
let percent_done = f64::from(block_event.block_height())
1316+
/ f64::from(blockchain_info.headers as u32)
1317+
* 100f64;
1318+
println!(
1319+
"Applying block at height: {}, {:.2}% done.",
1320+
block_event.block_height(),
1321+
percent_done
1322+
);
1323+
}
1324+
1325+
wallet.apply_block_connected_to(
1326+
&block_event.block,
1327+
block_event.block_height(),
1328+
block_event.connected_to(),
1329+
)?;
1330+
}
1331+
1332+
let mempool_txs = emitter.mempool()?;
1333+
wallet.apply_unconfirmed_txs(mempool_txs.update);
1334+
Ok(())
1335+
}
1336+
#[cfg(feature = "cbf")]
1337+
KyotoClient { client } => sync_kyoto_client(wallet, client)
1338+
.await
1339+
.map_err(|e| Error::Generic(e.to_string())),
1340+
}
1341+
}
1342+
13281343
#[cfg(feature = "repl")]
13291344
fn readline() -> Result<String, Error> {
13301345
write!(std::io::stdout(), "> ").map_err(|e| Error::Generic(e.to_string()))?;

0 commit comments

Comments
 (0)