From d56688db378d2596f665ec9f8154fdc59096b30f Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Sat, 30 Dec 2023 09:44:10 +0800 Subject: [PATCH] chore: improve scanning job --- crates/ns-indexer/Cargo.toml | 2 +- crates/ns-indexer/src/bin/main.rs | 56 ++++++++++++++++--------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/crates/ns-indexer/Cargo.toml b/crates/ns-indexer/Cargo.toml index 2b54011..75d8356 100644 --- a/crates/ns-indexer/Cargo.toml +++ b/crates/ns-indexer/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ns-indexer" -version = "0.3.0" +version = "0.3.1" edition = "2021" rust-version = "1.64" description = "Name & Service Protocol indexer service in Rust" diff --git a/crates/ns-indexer/src/bin/main.rs b/crates/ns-indexer/src/bin/main.rs index fc030d9..733690c 100644 --- a/crates/ns-indexer/src/bin/main.rs +++ b/crates/ns-indexer/src/bin/main.rs @@ -30,16 +30,6 @@ fn main() -> anyhow::Result<()> { } tokio_builder.enable_all().build().unwrap().block_on(async { - let rpcurl = std::env::var("BITCOIN_RPC_URL").unwrap(); - let rpcuser = std::env::var("BITCOIN_RPC_USER").unwrap(); - let rpcpassword = std::env::var("BITCOIN_RPC_PASSWORD").unwrap(); - - // 709632: This block marks the moment Taproot was activated on the Bitcoin network - let start_height = std::env::var("INDEXER_START_HEIGHT") - .unwrap_or("709632".to_string()) - .parse::() - .unwrap(); - let scylla = ScyllaDBOptions { nodes: std::env::var("SCYLLA_NODES") .unwrap() @@ -51,13 +41,6 @@ fn main() -> anyhow::Result<()> { keyspace: std::env::var("SCYLLA_KEYSPACE").unwrap_or_default(), }; - let bitcoin = BitcoinRPC::new(&BitcoinRPCOptions { - rpcurl, - rpcuser, - rpcpassword, - }) - .await?; - let indexer = Indexer::new(&IndexerOptions { scylla, index_utxo: std::env::var("INDEXER_UTXO") @@ -68,14 +51,7 @@ fn main() -> anyhow::Result<()> { .await?; let last_accepted_height = indexer.initialize().await?; - let start_height = if last_accepted_height > 0 { - last_accepted_height + 1 - } else { - start_height - }; - let indexer = Arc::new(indexer); - let scanner = Scanner::new(Arc::new(bitcoin), indexer.clone()); let indexer_api = IndexerAPI::new(indexer.clone()); let app = router::new(Arc::new(indexer_api)); @@ -98,7 +74,10 @@ fn main() -> anyhow::Result<()> { .await { Ok(_) => log::info!(target: "server", "indexer api finished"), - Err(err) => log::error!(target: "server", "indexer api error: {}", err), + Err(err) => { + log::error!(target: "server", "indexer api error: {}", err); + Err(err)?; + } } Ok::<(), anyhow::Error>(()) @@ -107,12 +86,35 @@ fn main() -> anyhow::Result<()> { let scanning = std::env::var("INDEXER_SERVER_NOSCAN").unwrap_or_default() != "true"; let background_job = async { if scanning { + let rpcurl = std::env::var("BITCOIN_RPC_URL").unwrap(); + let rpcuser = std::env::var("BITCOIN_RPC_USER").unwrap(); + let rpcpassword = std::env::var("BITCOIN_RPC_PASSWORD").unwrap(); + + // 709632: This block marks the moment Taproot was activated on the Bitcoin network + let start_height = std::env::var("INDEXER_START_HEIGHT") + .unwrap_or("709632".to_string()) + .parse::() + .unwrap(); + let start_height = if last_accepted_height > 0 { + last_accepted_height + 1 + } else { + start_height + }; + + let bitcoin = BitcoinRPC::new(&BitcoinRPCOptions { + rpcurl, + rpcuser, + rpcpassword, + }) + .await?; + + let scanner = Scanner::new(Arc::new(bitcoin), indexer.clone()); + match scanner.run(shutdown.clone(), start_height).await { Ok(_) => log::info!(target: "server", "scanner finished"), Err(err) => { log::error!(target: "server", "scanner error: {}", err); - // should exit the process and restart - return Err(err); + Err(err)?; } } }