Test bitcoin-serai

Also resolves a few rough edges.
This commit is contained in:
Luke Parker
2023-03-20 04:46:27 -04:00
parent 6a2a353b91
commit 7fc8630d39
10 changed files with 372 additions and 35 deletions

View File

@@ -14,11 +14,17 @@ use bitcoin::{
Txid, Transaction, BlockHash, Block,
};
#[derive(Clone, PartialEq, Eq, Debug, Deserialize)]
pub struct Error {
code: isize,
message: String,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
enum RpcResponse<T> {
Ok { result: T },
Err { error: String },
Err { error: Error },
}
/// A minimal asynchronous Bitcoin RPC client.
@@ -29,8 +35,8 @@ pub struct Rpc(String);
pub enum RpcError {
#[error("couldn't connect to node")]
ConnectionError,
#[error("request had an error: {0}")]
RequestError(String),
#[error("request had an error: {0:?}")]
RequestError(Error),
#[error("node sent an invalid response")]
InvalidResponse,
}
@@ -69,7 +75,14 @@ impl Rpc {
}
/// Get the latest block's number.
///
/// The genesis block's 'number' is zero. They increment from there.
pub async fn get_latest_block_number(&self) -> Result<usize, RpcError> {
// getblockcount doesn't return the amount of blocks on the current chain, yet the "height"
// of the current chain. The "height" of the current chain is defined as the "height" of the
// tip block of the current chain. The "height" of a block is defined as the amount of blocks
// present when the block was created. Accordingly, the genesis block has height 0, and
// getblockcount will return 0 when it's only the only block, despite their being one block.
self.rpc_call("getblockcount", json!([])).await
}