From 586b874a19244897102beea8170205123f2dfc96 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 4 Dec 2020 10:59:06 +0100 Subject: [PATCH] Remove EsploraHeader json in favor of raw hex block header --- src/blockchain/esplora.rs | 62 +++++++-------------------------------- 1 file changed, 10 insertions(+), 52 deletions(-) diff --git a/src/blockchain/esplora.rs b/src/blockchain/esplora.rs index bbc18cd4..a6c4ffa5 100644 --- a/src/blockchain/esplora.rs +++ b/src/blockchain/esplora.rs @@ -48,9 +48,9 @@ use serde::Deserialize; use reqwest::{Client, StatusCode}; use bitcoin::consensus::{deserialize, serialize}; -use bitcoin::hashes::hex::ToHex; +use bitcoin::hashes::hex::{FromHex, ToHex}; use bitcoin::hashes::{sha256, Hash}; -use bitcoin::{BlockHash, BlockHeader, Script, Transaction, TxMerkleNode, Txid}; +use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid}; use self::utils::{ELSGetHistoryRes, ElectrumLikeSync}; use super::*; @@ -190,13 +190,13 @@ impl UrlClient { let resp = self .client - .get(&format!("{}/block/{}", self.url, hash)) + .get(&format!("{}/block/{}/header", self.url, hash)) .send() .await?; - let esplora_header = resp.json::().await?; + let header = deserialize(&Vec::from_hex(&resp.text().await?)?)?; - Ok(esplora_header.into()) + Ok(header) } async fn _broadcast(&self, transaction: &Transaction) -> Result<(), EsploraError> { @@ -373,37 +373,6 @@ struct EsploraGetHistory { status: EsploraGetHistoryStatus, } -/// `EsploraHeader` maps the json returned from esplora endpoint `GET /block/:hash` -/// It will be removed when `GET /block/:hash/header` will be deployed -#[derive(Default, Debug, Clone, PartialEq, Deserialize)] -struct EsploraHeader { - id: String, - height: u32, - version: i32, - timestamp: u32, - tx_count: u32, - size: u32, - weight: u32, - merkle_root: TxMerkleNode, - previousblockhash: BlockHash, - nonce: u32, - bits: u32, - difficulty: u32, -} - -impl Into for EsploraHeader { - fn into(self) -> BlockHeader { - BlockHeader { - version: self.version, - prev_blockhash: self.previousblockhash, - merkle_root: self.merkle_root, - time: self.timestamp, - bits: self.bits, - nonce: self.nonce, - } - } -} - /// Configuration for an [`EsploraBlockchain`] #[derive(Debug, serde::Deserialize, serde::Serialize)] pub struct EsploraBlockchainConfig { @@ -435,6 +404,8 @@ pub enum EsploraError { Parsing(std::num::ParseIntError), /// Invalid Bitcoin data returned BitcoinEncoding(bitcoin::consensus::encode::Error), + /// Invalid Hex data returned + Hex(bitcoin::hashes::hex::Error), /// Transaction not found TransactionNotFound(Txid), @@ -470,21 +441,8 @@ impl From for EsploraError { } } -#[cfg(test)] -mod test { - use crate::blockchain::esplora::EsploraHeader; - use bitcoin::hashes::hex::FromHex; - use bitcoin::{BlockHash, BlockHeader}; - - #[test] - fn test_esplora_header() { - let json_str = r#"{"id":"00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206","height":1,"version":1,"timestamp":1296688928,"tx_count":1,"size":190,"weight":760,"merkle_root":"f0315ffc38709d70ad5647e22048358dd3745f3ce3874223c80a7c92fab0c8ba","previousblockhash":"000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943","nonce":1924588547,"bits":486604799,"difficulty":1}"#; - let json: EsploraHeader = serde_json::from_str(&json_str).unwrap(); - let header: BlockHeader = json.into(); - assert_eq!( - header.block_hash(), - BlockHash::from_hex("00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206") - .unwrap() - ); +impl From for EsploraError { + fn from(other: bitcoin::hashes::hex::Error) -> Self { + EsploraError::Hex(other) } }