diff --git a/teos/src/bitcoin_cli.rs b/teos/src/bitcoin_cli.rs index 80b805c2..568a1356 100644 --- a/teos/src/bitcoin_cli.rs +++ b/teos/src/bitcoin_cli.rs @@ -11,12 +11,14 @@ use std::convert::TryInto; use std::io::{Error, ErrorKind}; +use std::str::FromStr; use std::sync::Arc; use tokio::sync::Mutex; use bitcoin::base64; use bitcoin::hash_types::{BlockHash, Txid}; use bitcoin::hashes::hex::ToHex; +use bitcoin::network::constants::Network; use bitcoin::{Block, Transaction}; use lightning::util::ser::Writeable; use lightning_block_sync::http::{HttpEndpoint, JsonResponse}; @@ -91,14 +93,19 @@ impl<'a> BitcoindClient<'a> { }; // Test that bitcoind is reachable. - let btc_network = client.get_chain().await?; + let (btc_network, ibd) = client.get_chain().await?; - // Assert teos runs on the same chain/network as bitcoind. if btc_network != teos_network { + // Assert teos runs on the same chain/network as bitcoind. Err(Error::new( ErrorKind::InvalidInput, format!("bitcoind is running on {btc_network} but teosd is set to run on {teos_network}"), )) + } else if ibd && Network::from_str(&btc_network).unwrap() != Network::Regtest { + Err(Error::new( + ErrorKind::Interrupted, + "bitcoind is still doing IDB, start the tower once it has finished", + )) } else { Ok(client) } @@ -138,14 +145,17 @@ impl<'a> BitcoindClient<'a> { .await } - /// Gets bitcoind's network. - pub async fn get_chain(&self) -> std::io::Result { + /// Gets the chain we are running in and whether or not we're on IBD + pub async fn get_chain(&self) -> std::io::Result<(String, bool)> { // A wrapper type to extract "chain" key from getblockchaininfo JsonResponse. - struct BtcNetwork(String); + struct BtcNetwork(String, bool); impl TryInto for JsonResponse { type Error = std::io::Error; fn try_into(self) -> std::io::Result { - Ok(BtcNetwork(self.0["chain"].as_str().unwrap().to_string())) + Ok(BtcNetwork( + self.0["chain"].as_str().unwrap().to_string(), + self.0["initialblockdownload"].as_bool().unwrap(), + )) } } @@ -155,6 +165,6 @@ impl<'a> BitcoindClient<'a> { .call_method::("getblockchaininfo", &[]) .await?; - Ok(btc_network.0) + Ok((btc_network.0, btc_network.1)) } }