Преглед изворни кода

chore: use blake3 directly in its Hash wrappers

skoupidi пре 1 година
родитељ
комит
e1165419e0
3 измењених фајлова са 17 додато и 33 уклоњено
  1. 5 7
      src/blockchain/header_store.rs
  2. 7 8
      src/sdk/src/tx.rs
  3. 5 18
      src/tx/mod.rs

+ 5 - 7
src/blockchain/header_store.rs

@@ -21,9 +21,7 @@ use std::{fmt, str::FromStr};
 use darkfi_sdk::{
     blockchain::block_version,
     crypto::{MerkleNode, MerkleTree},
-    hex::decode_hex_arr,
     monotree::{Hash as StateHash, EMPTY_HASH},
-    AsHex,
 };
 #[cfg(feature = "async-serial")]
 use darkfi_serial::async_trait;
@@ -52,7 +50,7 @@ impl HeaderHash {
     }
 
     pub fn as_string(&self) -> String {
-        self.0.hex().to_string()
+        format!("{}", blake3::hash(&self.0))
     }
 }
 
@@ -60,13 +58,13 @@ impl FromStr for HeaderHash {
     type Err = Error;
 
     fn from_str(header_hash_str: &str) -> Result<Self> {
-        Ok(Self(decode_hex_arr(header_hash_str)?))
+        Ok(Self(*blake3::Hash::from_str(header_hash_str)?.as_bytes()))
     }
 }
 
 impl fmt::Display for HeaderHash {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}", self.0.hex())
+        write!(f, "{}", blake3::hash(&self.0))
     }
 }
 
@@ -240,7 +238,7 @@ impl HeaderStore {
                 continue
             }
             if strict {
-                return Err(Error::HeaderNotFound(hash.inner().hex()))
+                return Err(Error::HeaderNotFound(hash.as_string()))
             }
             ret.push(None);
         }
@@ -395,7 +393,7 @@ impl HeaderStoreOverlay {
                 continue
             }
             if strict {
-                return Err(Error::HeaderNotFound(hash.inner().hex()))
+                return Err(Error::HeaderNotFound(hash.as_string()))
             }
             ret.push(None);
         }

+ 7 - 8
src/sdk/src/tx.rs

@@ -25,11 +25,7 @@ use std::{
 use darkfi_serial::async_trait;
 use darkfi_serial::{SerialDecodable, SerialEncodable};
 
-use super::{
-    crypto::ContractId,
-    hex::{decode_hex_arr, AsHex},
-    ContractError, GenericResult,
-};
+use super::{crypto::ContractId, ContractError, GenericResult};
 use crate::crypto::{DAO_CONTRACT_ID, DEPLOYOOOR_CONTRACT_ID, MONEY_CONTRACT_ID};
 
 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, SerialEncodable, SerialDecodable)]
@@ -51,7 +47,7 @@ impl TransactionHash {
     }
 
     pub fn as_string(&self) -> String {
-        self.0.hex().to_string()
+        format!("{}", blake3::hash(&self.0))
     }
 }
 
@@ -59,13 +55,16 @@ impl FromStr for TransactionHash {
     type Err = ContractError;
 
     fn from_str(tx_hash_str: &str) -> GenericResult<Self> {
-        Ok(Self(decode_hex_arr(tx_hash_str)?))
+        let Ok(hash) = blake3::Hash::from_str(tx_hash_str) else {
+            return Err(ContractError::HexFmtErr);
+        };
+        Ok(Self(*hash.as_bytes()))
     }
 }
 
 impl fmt::Display for TransactionHash {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}", self.0.hex())
+        write!(f, "{}", blake3::hash(&self.0))
     }
 }
 

+ 5 - 18
src/tx/mod.rs

@@ -27,7 +27,6 @@ use darkfi_sdk::{
     error::DarkTreeResult,
     pasta::pallas,
     tx::{ContractCall, TransactionHash},
-    AsHex,
 };
 
 #[cfg(feature = "async-serial")]
@@ -128,10 +127,7 @@ impl Transaction {
         self.proofs.encode(&mut hasher)?;
         let data_hash = hasher.finalize();
 
-        debug!(
-            target: "tx::verify_sigs",
-            "tx.verify_sigs: data_hash: {}", data_hash.as_bytes().hex(),
-        );
+        debug!(target: "tx::verify_sigs", "tx.verify_sigs: data_hash: {data_hash}");
 
         assert_eq!(self.signatures.len(), pub_table.len());
 
@@ -139,20 +135,14 @@ impl Transaction {
             assert_eq!(sigs.len(), pubkeys.len());
 
             for (pubkey, signature) in pubkeys.iter().zip(sigs) {
-                debug!(
-                    target: "tx::verify_sigs",
-                    "[TX] Verifying signature with public key: {}", pubkey,
-                );
+                debug!(target: "tx::verify_sigs", "[TX] Verifying signature with public key: {pubkey}");
                 if !pubkey.verify(&data_hash.as_bytes()[..], signature) {
-                    error!(
-                        target: "tx::verify_sigs",
-                        "[TX] tx::verify_sigs[{}] failed to verify signature", i,
-                    );
+                    error!(target: "tx::verify_sigs", "[TX] tx::verify_sigs[{i}] failed to verify signature");
                     return Err(Error::InvalidSignature)
                 }
             }
 
-            debug!(target: "tx::verify_sigs", "[TX] tx::verify_sigs[{}] passed", i);
+            debug!(target: "tx::verify_sigs", "[TX] tx::verify_sigs[{i}] passed");
         }
 
         Ok(())
@@ -166,10 +156,7 @@ impl Transaction {
         self.proofs.encode(&mut hasher)?;
         let data_hash = hasher.finalize();
 
-        debug!(
-            target: "tx::create_sigs",
-            "[TX] tx.create_sigs: data_hash: {:?}", data_hash.as_bytes().hex(),
-        );
+        debug!(target: "tx::create_sigs", "[TX] tx.create_sigs: data_hash: {data_hash}");
 
         let mut sigs = vec![];
         for secret in secret_keys {