Skip to content

Commit

Permalink
utils: Move hex serializers to utils from rpc_adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Aug 22, 2024
1 parent c7dd696 commit 97ad0a1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 57 deletions.
10 changes: 5 additions & 5 deletions src/rpc/adapter/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! # Blockchain RPCs
use super::{decode_from_hex, encode_decode_to_rpc_error, encode_to_hex};
use crate::utils::{decode_from_hex, encode_decode_to_rpc_error, encode_to_hex};
use crate::Client;
use bitcoin::{consensus::Decodable, BlockHash, Txid};
use bitcoincore_rpc::{Error, RpcApi};

pub fn getbestblockhash(client: &Client) -> Result<String, Error> {
let res = client.get_best_block_hash()?;

Ok(encode_to_hex(res))
Ok(encode_to_hex(&res))
}

pub fn getblock(
Expand All @@ -23,7 +23,7 @@ pub fn getblock(
};

let res = client.get_block(&blockhash)?;
let encoded = encode_to_hex(res);
let encoded = encode_to_hex(&res);

match verbosity {
None | Some(1) => Ok(encoded),
Expand All @@ -38,7 +38,7 @@ pub fn getblockcount(client: &Client) -> Result<usize, Error> {
pub fn getblockhash(client: &Client, height: usize) -> Result<String, Error> {
let block_hash = client.get_block_hash(height as u64)?;

Ok(encode_to_hex(block_hash))
Ok(encode_to_hex(&block_hash))
}

pub fn getblockheader(
Expand All @@ -51,7 +51,7 @@ pub fn getblockheader(

match verbose {
None | Some(true) => Ok(serde_json::to_string(&header).unwrap()),
Some(false) => Ok(encode_to_hex(header)),
Some(false) => Ok(encode_to_hex(&header)),
}
}

Expand Down
44 changes: 0 additions & 44 deletions src/rpc/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! This crate provides an adapter interface that aims to mimic real Bitcoin
//! RPC interface.
use bitcoin::consensus::encode::{deserialize_hex, serialize_hex};

mod blockchain;
mod generating;
mod rawtransactions;
Expand All @@ -14,45 +12,3 @@ pub use blockchain::*;
pub use generating::*;
pub use rawtransactions::*;
pub use wallet::*;

/// Encodes given Rust struct to hex string.
fn encode_to_hex<T>(strct: T) -> String
where
T: bitcoin::consensus::Encodable,
{
serialize_hex::<T>(&strct)
}

/// Decodes given hex string to a Rust struct.
fn decode_from_hex<T>(hex: String) -> Result<T, bitcoincore_rpc::Error>
where
T: bitcoin::consensus::Decodable,
{
Ok(deserialize_hex::<T>(&hex)?)
}

fn encode_decode_to_rpc_error(error: bitcoin::consensus::encode::Error) -> bitcoincore_rpc::Error {
bitcoincore_rpc::Error::BitcoinSerialization(bitcoin::consensus::encode::FromHexError::Decode(
bitcoin::consensus::DecodeError::Consensus(error),
))
}

#[cfg(test)]
mod tests {
use super::{decode_from_hex, encode_to_hex};
use bitcoin::{hashes::sha256d::Hash, Txid};
use std::str::FromStr;

#[test]
fn encode_decode_txid() {
let txid = Txid::from_raw_hash(
Hash::from_str("e6d467860551868fe599889ea9e622ae1ff08891049e934f83a783a3ea5fbc12")
.unwrap(),
);

let encoded_txid = encode_to_hex(txid);
let decoded_txid = decode_from_hex::<Txid>(encoded_txid).unwrap();

assert_eq!(txid, decoded_txid);
}
}
12 changes: 6 additions & 6 deletions src/rpc/adapter/rawtransactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # Rawtransactions RPCs
use super::{decode_from_hex, encode_to_hex};
use crate::utils::{decode_from_hex, encode_to_hex};
use crate::Client;
use bitcoin::{BlockHash, Transaction, Txid};
use bitcoincore_rpc::{Error, RpcApi};
Expand All @@ -17,7 +17,7 @@ pub fn getrawtransaction(
None | Some(false) => {
let tx = client.get_raw_transaction(&txid, blockhash.as_ref())?;

encode_to_hex(tx)
encode_to_hex(&tx)
}
Some(true) => {
let tx = client.get_raw_transaction_info(&txid, blockhash.as_ref())?;
Expand All @@ -37,15 +37,15 @@ pub fn sendrawtransaction(
let tx = decode_from_hex::<Transaction>(hexstring)?;

let txid = client.send_raw_transaction(&tx)?;
let txid = encode_to_hex(txid);
let txid = encode_to_hex(&txid);

Ok(txid)
}

#[cfg(test)]
mod tests {
use crate::{
rpc::adapter::{decode_from_hex, encode_to_hex},
utils::{decode_from_hex, encode_to_hex},
Client, RpcApiWrapper,
};
use bitcoin::{
Expand Down Expand Up @@ -74,7 +74,7 @@ mod tests {
let tx = client.get_raw_transaction(&txid, None).unwrap();

let encoded_tx =
super::getrawtransaction(&client, encode_to_hex(txid), None, None).unwrap();
super::getrawtransaction(&client, encode_to_hex(&txid), None, None).unwrap();
let encoded_tx = decode_from_hex(encoded_tx).unwrap();

assert_eq!(tx, encoded_tx);
Expand Down Expand Up @@ -114,7 +114,7 @@ mod tests {
lock_time: LockTime::ZERO,
};

let txid = super::sendrawtransaction(&client, encode_to_hex(tx.clone()), None).unwrap();
let txid = super::sendrawtransaction(&client, encode_to_hex(&tx.clone()), None).unwrap();
let txid = decode_from_hex::<Txid>(txid).unwrap();

let read_tx = client.get_raw_transaction(&txid, None).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/adapter/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # Wallet RPCs
use super::{decode_from_hex, encode_to_hex};
use crate::utils::{decode_from_hex, encode_to_hex};
use crate::Client;
use bitcoin::{Address, Amount, Txid};
use bitcoincore_rpc::{json, Error, RpcApi};
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn sendtoaddress(
None,
)?;

Ok(encode_to_hex::<Txid>(txid))
Ok(encode_to_hex::<Txid>(&txid))
}

#[cfg(test)]
Expand Down
14 changes: 14 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ pub fn initialize_logger() -> Result<(), tracing_subscriber::util::TryInitError>

#[cfg(test)]
mod tests {
use super::{decode_from_hex, encode_to_hex};
use bitcoin::{hashes::sha256d::Hash, TxMerkleNode, Txid};
use std::str::FromStr;

Expand Down Expand Up @@ -244,4 +245,17 @@ mod tests {
merkle_root
);
}

#[test]
fn encode_decode_txid() {
let txid = Txid::from_raw_hash(
Hash::from_str("e6d467860551868fe599889ea9e622ae1ff08891049e934f83a783a3ea5fbc12")
.unwrap(),
);

let encoded_txid = encode_to_hex(&txid);
let decoded_txid = decode_from_hex::<Txid>(encoded_txid).unwrap();

assert_eq!(txid, decoded_txid);
}
}

0 comments on commit 97ad0a1

Please sign in to comment.