]> Untitled Git - bdk/commitdiff
Remove EsploraHeader json in favor of raw hex block header
authorRiccardo Casatta <riccardo@casatta.it>
Fri, 4 Dec 2020 09:59:06 +0000 (10:59 +0100)
committerRiccardo Casatta <riccardo@casatta.it>
Fri, 4 Dec 2020 10:04:31 +0000 (11:04 +0100)
src/blockchain/esplora.rs

index bbc18cd4bd3e52f9be623b6b48c7bf140e1d4c49..a6c4ffa561076b9f7bde9d5fd7a79894d109459e 100644 (file)
@@ -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::<EsploraHeader>().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<BlockHeader> 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<bitcoin::consensus::encode::Error> 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<bitcoin::hashes::hex::Error> for EsploraError {
+    fn from(other: bitcoin::hashes::hex::Error) -> Self {
+        EsploraError::Hex(other)
     }
 }