ソースを参照

replace all data strings output as [123, 78, ...] with big endian hex strings.

zero 2 年 前
コミット
b6e8c00243

+ 6 - 3
src/runtime/import/merkle.rs

@@ -18,7 +18,10 @@
 
 use std::io::Cursor;
 
-use darkfi_sdk::crypto::{MerkleNode, MerkleTree};
+use darkfi_sdk::{
+    crypto::{MerkleNode, MerkleTree},
+    AsHex,
+};
 use darkfi_serial::{serialize, Decodable, Encodable, WriteExt};
 use log::{debug, error};
 use wasmer::{FunctionEnvMut, WasmPtr};
@@ -195,8 +198,8 @@ pub(crate) fn merkle_add(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u3
     );
     debug!(
         target: "runtime::merkle::merkle_add",
-        "                 {:02x?}",
-        return_data
+        "                 {}",
+        return_data.hex()
     );
 
     let mut decoder = Cursor::new(&return_data);

+ 7 - 7
src/runtime/vm_runtime.rs

@@ -21,7 +21,7 @@ use std::{
     sync::Arc,
 };
 
-use darkfi_sdk::{crypto::ContractId, entrypoint, tx::TransactionHash};
+use darkfi_sdk::{crypto::ContractId, entrypoint, tx::TransactionHash, AsHex};
 use darkfi_serial::serialize;
 use log::{debug, error, info};
 use wasmer::{
@@ -491,9 +491,9 @@ impl Runtime {
         let cid = self.ctx.as_ref(&self.store).contract_id;
         info!(target: "runtime::vm_runtime", "[WASM] Running metadata() for ContractID: {}", cid);
 
-        debug!(target: "runtime::vm_runtime", "metadata payload: {:?}", payload);
+        debug!(target: "runtime::vm_runtime", "metadata payload: {}", payload.hex());
         let ret = self.call(ContractSection::Metadata, payload)?;
-        debug!(target: "runtime::vm_runtime", "metadata returned: {:?}", ret);
+        debug!(target: "runtime::vm_runtime", "metadata returned: {:?}", ret.hex());
 
         info!(target: "runtime::vm_runtime", "[WASM] Successfully got metadata ContractID: {}", cid);
         Ok(ret)
@@ -508,9 +508,9 @@ impl Runtime {
         let cid = self.ctx.as_ref(&self.store).contract_id;
         info!(target: "runtime::vm_runtime", "[WASM] Running exec() for ContractID: {}", cid);
 
-        debug!(target: "runtime::vm_runtime", "exec payload: {:?}", payload);
+        debug!(target: "runtime::vm_runtime", "exec payload: {}", payload.hex());
         let ret = self.call(ContractSection::Exec, payload)?;
-        debug!(target: "runtime::vm_runtime", "exec returned: {:?}", ret);
+        debug!(target: "runtime::vm_runtime", "exec returned: {:?}", ret.hex());
 
         info!(target: "runtime::vm_runtime", "[WASM] Successfully executed ContractID: {}", cid);
         Ok(ret)
@@ -526,9 +526,9 @@ impl Runtime {
         let cid = self.ctx.as_ref(&self.store).contract_id;
         info!(target: "runtime::vm_runtime", "[WASM] Running apply() for ContractID: {}", cid);
 
-        debug!(target: "runtime::vm_runtime", "apply payload: {:?}", update);
+        debug!(target: "runtime::vm_runtime", "apply payload: {:?}", update.hex());
         let ret = self.call(ContractSection::Update, update)?;
-        debug!(target: "runtime::vm_runtime", "apply returned: {:?}", ret);
+        debug!(target: "runtime::vm_runtime", "apply returned: {:?}", ret.hex());
 
         info!(target: "runtime::vm_runtime", "[WASM] Successfully applied ContractID: {}", cid);
         Ok(())

+ 6 - 6
src/sdk/src/crypto/util.rs

@@ -25,7 +25,10 @@ use pasta_curves::{
 use std::io::Cursor;
 use subtle::CtOption;
 
-use crate::error::{ContractError, GenericResult};
+use crate::{
+    error::{ContractError, GenericResult},
+    hex_from_iter,
+};
 
 #[inline]
 fn hash_to_field_elem<F: FromUniformBytes<64>>(persona: &[u8], vals: &[&[u8]]) -> F {
@@ -90,11 +93,8 @@ pub fn fp_to_u64(value: pallas::Base) -> Option<u64> {
 // Not allowed to implement external traits for external crates
 pub trait FieldElemAsStr: PrimeField<Repr = [u8; 32]> {
     fn to_string(&self) -> String {
-        let mut repr = "0x".to_string();
-        for &b in self.to_repr().iter().rev() {
-            repr += &format!("{:02x}", b);
-        }
-        repr
+        // We reverse repr since it is little endian encoded
+        "0x".to_string() + &hex_from_iter(self.to_repr().iter().cloned().rev())
     }
 
     fn from_str(hex: &str) -> GenericResult<Self> {

+ 21 - 0
src/sdk/src/lib.rs

@@ -54,3 +54,24 @@ pub mod util;
 
 /// DarkTree structures
 pub mod dark_tree;
+
+/// Creates a hex formatted string of the data
+#[inline]
+pub fn hex_from_iter<I: Iterator<Item = u8>>(iter: I) -> String {
+    let mut repr = String::new();
+    for b in iter {
+        repr += &format!("{:02x}", b);
+    }
+    repr
+}
+
+pub trait AsHex {
+    fn hex(&self) -> String;
+}
+
+impl<T: AsRef<[u8]>> AsHex for T {
+    /// Creates a hex formatted string of the data (big endian)
+    fn hex(&self) -> String {
+        hex_from_iter(self.as_ref().iter().cloned())
+    }
+}

+ 3 - 3
src/sdk/src/tx.rs

@@ -22,7 +22,7 @@ use std::fmt::{self, Debug};
 use darkfi_serial::async_trait;
 use darkfi_serial::{SerialDecodable, SerialEncodable};
 
-use super::crypto::ContractId;
+use super::{crypto::ContractId, AsHex};
 
 #[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
 // We have to introduce a type rather than using an alias so we can implement Display
@@ -44,8 +44,8 @@ impl TransactionHash {
 }
 
 impl fmt::Display for TransactionHash {
-    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
-        self.0[..].fmt(formatter)
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.0.hex())
     }
 }
 

+ 3 - 2
src/tx/mod.rs

@@ -27,6 +27,7 @@ use darkfi_sdk::{
     error::DarkTreeResult,
     pasta::pallas,
     tx::{ContractCall, TransactionHash},
+    AsHex,
 };
 
 #[cfg(feature = "async-serial")]
@@ -128,7 +129,7 @@ impl Transaction {
 
         debug!(
             target: "tx::verify_sigs",
-            "tx.verify_sigs: data_hash: {:?}", data_hash.as_bytes(),
+            "tx.verify_sigs: data_hash: {}", data_hash.as_bytes().hex(),
         );
 
         assert_eq!(self.signatures.len(), pub_table.len());
@@ -166,7 +167,7 @@ impl Transaction {
 
         debug!(
             target: "tx::create_sigs",
-            "[TX] tx.create_sigs: data_hash: {:?}", data_hash.as_bytes(),
+            "[TX] tx.create_sigs: data_hash: {:?}", data_hash.as_bytes().hex(),
         );
 
         let mut sigs = vec![];