Skip to content

Commit dfd0987

Browse files
committed
Reject to run if the backend is still doing IBD
1 parent bacac07 commit dfd0987

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

teos/src/bitcoin_cli.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
use std::convert::TryInto;
1313
use std::io::{Error, ErrorKind};
14+
use std::str::FromStr;
1415
use std::sync::Arc;
1516
use tokio::sync::Mutex;
1617

1718
use bitcoin::base64;
1819
use bitcoin::hash_types::{BlockHash, Txid};
1920
use bitcoin::hashes::hex::ToHex;
21+
use bitcoin::network::constants::Network;
2022
use bitcoin::{Block, Transaction};
2123
use lightning::util::ser::Writeable;
2224
use lightning_block_sync::http::{HttpEndpoint, JsonResponse};
@@ -91,14 +93,19 @@ impl<'a> BitcoindClient<'a> {
9193
};
9294

9395
// Test that bitcoind is reachable.
94-
let btc_network = client.get_chain().await?;
96+
let (btc_network, ibd) = client.get_chain().await?;
9597

96-
// Assert teos runs on the same chain/network as bitcoind.
9798
if btc_network != teos_network {
99+
// Assert teos runs on the same chain/network as bitcoind.
98100
Err(Error::new(
99101
ErrorKind::InvalidInput,
100102
format!("bitcoind is running on {btc_network} but teosd is set to run on {teos_network}"),
101103
))
104+
} else if ibd && Network::from_str(&btc_network).unwrap() != Network::Regtest {
105+
Err(Error::new(
106+
ErrorKind::Interrupted,
107+
"bitcoind is still doing IDB, start the tower once it has finished",
108+
))
102109
} else {
103110
Ok(client)
104111
}
@@ -138,14 +145,17 @@ impl<'a> BitcoindClient<'a> {
138145
.await
139146
}
140147

141-
/// Gets bitcoind's network.
142-
pub async fn get_chain(&self) -> std::io::Result<String> {
148+
/// Gets the chain we are running in and whether or not we're on IBD
149+
pub async fn get_chain(&self) -> std::io::Result<(String, bool)> {
143150
// A wrapper type to extract "chain" key from getblockchaininfo JsonResponse.
144-
struct BtcNetwork(String);
151+
struct BtcNetwork(String, bool);
145152
impl TryInto<BtcNetwork> for JsonResponse {
146153
type Error = std::io::Error;
147154
fn try_into(self) -> std::io::Result<BtcNetwork> {
148-
Ok(BtcNetwork(self.0["chain"].as_str().unwrap().to_string()))
155+
Ok(BtcNetwork(
156+
self.0["chain"].as_str().unwrap().to_string(),
157+
self.0["initialblockdownload"].as_bool().unwrap(),
158+
))
149159
}
150160
}
151161

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

158-
Ok(btc_network.0)
168+
Ok((btc_network.0, btc_network.1))
159169
}
160170
}

0 commit comments

Comments
 (0)