Skip to content

Commit

Permalink
Reject to run if the backend is still doing IBD
Browse files Browse the repository at this point in the history
  • Loading branch information
sr-gi committed Jul 28, 2023
1 parent bacac07 commit dfd0987
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions teos/src/bitcoin_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -138,14 +145,17 @@ impl<'a> BitcoindClient<'a> {
.await
}

/// Gets bitcoind's network.
pub async fn get_chain(&self) -> std::io::Result<String> {
/// 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<BtcNetwork> for JsonResponse {
type Error = std::io::Error;
fn try_into(self) -> std::io::Result<BtcNetwork> {
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(),
))
}
}

Expand All @@ -155,6 +165,6 @@ impl<'a> BitcoindClient<'a> {
.call_method::<BtcNetwork>("getblockchaininfo", &[])
.await?;

Ok(btc_network.0)
Ok((btc_network.0, btc_network.1))
}
}

0 comments on commit dfd0987

Please sign in to comment.