-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate and wait for bitcoind block height responses (#340)
* fix: validate and wait for bitcoind block height responses * chore: comments
- Loading branch information
Showing
7 changed files
with
99 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use chainhook_sdk::{bitcoincore_rpc::{Auth, Client, RpcApi}, utils::Context}; | ||
|
||
use crate::{config::Config, try_error}; | ||
|
||
fn bitcoind_get_client(config: &Config, ctx: &Context) -> Client { | ||
loop { | ||
let auth = Auth::UserPass( | ||
config.network.bitcoind_rpc_username.clone(), | ||
config.network.bitcoind_rpc_password.clone(), | ||
); | ||
match Client::new(&config.network.bitcoind_rpc_url, auth) { | ||
Ok(con) => { | ||
return con; | ||
} | ||
Err(e) => { | ||
try_error!(ctx, "bitcoind unable to get client: {}", e.to_string()); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Retrieves the block height from bitcoind. | ||
pub fn bitcoind_get_block_height(config: &Config, ctx: &Context) -> u64 { | ||
let bitcoin_rpc = bitcoind_get_client(config, ctx); | ||
loop { | ||
match bitcoin_rpc.get_blockchain_info() { | ||
Ok(result) => { | ||
return result.blocks; | ||
} | ||
Err(e) => { | ||
try_error!( | ||
ctx, | ||
"bitcoind unable to get block height: {}", | ||
e.to_string() | ||
); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#[macro_export] | ||
macro_rules! try_info { | ||
($a:expr, $tag:expr, $($args:tt)*) => { | ||
$a.try_log(|l| info!(l, $tag, $($args)*)); | ||
}; | ||
($a:expr, $tag:expr) => { | ||
$a.try_log(|l| info!(l, $tag)); | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! try_debug { | ||
($a:expr, $tag:expr, $($args:tt)*) => { | ||
$a.try_log(|l| debug!(l, $tag, $($args)*)); | ||
}; | ||
($a:expr, $tag:expr) => { | ||
$a.try_log(|l| debug!(l, $tag)); | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! try_warn { | ||
($a:expr, $tag:expr, $($args:tt)*) => { | ||
$a.try_log(|l| warn!(l, $tag, $($args)*)); | ||
}; | ||
($a:expr, $tag:expr) => { | ||
$a.try_log(|l| warn!(l, $tag)); | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! try_error { | ||
($a:expr, $tag:expr, $($args:tt)*) => { | ||
$a.try_log(|l| error!(l, $tag, $($args)*)); | ||
}; | ||
($a:expr, $tag:expr) => { | ||
$a.try_log(|l| error!(l, $tag)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub mod bitcoind; | ||
pub mod logger; | ||
|
||
use std::{ | ||
fs, | ||
io::{Read, Write}, | ||
|