Просмотр исходного кода

prettify ContractCall and Transaction debug output

x 2 лет назад
Родитель
Сommit
87459ac644
2 измененных файлов с 33 добавлено и 2 удалено
  1. 13 1
      src/sdk/src/tx.rs
  2. 20 1
      src/tx/mod.rs

+ 13 - 1
src/sdk/src/tx.rs

@@ -25,7 +25,7 @@ use super::crypto::ContractId;
 // ANCHOR: contractcall
 // ANCHOR: contractcall
 /// A ContractCall is the part of a transaction that executes a certain
 /// A ContractCall is the part of a transaction that executes a certain
 /// `contract_id` with `data` as the call's payload.
 /// `contract_id` with `data` as the call's payload.
-#[derive(Debug, Clone, Eq, PartialEq, SerialEncodable, SerialDecodable)]
+#[derive(Clone, Eq, PartialEq, SerialEncodable, SerialDecodable)]
 pub struct ContractCall {
 pub struct ContractCall {
     /// ID of the contract invoked
     /// ID of the contract invoked
     pub contract_id: ContractId,
     pub contract_id: ContractId,
@@ -33,3 +33,15 @@ pub struct ContractCall {
     pub data: Vec<u8>,
     pub data: Vec<u8>,
 }
 }
 // ANCHOR_END: contractcall
 // ANCHOR_END: contractcall
+
+// Avoid showing the data in the debug output since often the calldata is very long.
+impl std::fmt::Debug for ContractCall {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "ContractCall(id={:?}", self.contract_id.inner())?;
+        let calldata = &self.data;
+        if calldata.len() > 0 {
+            write!(f, ", function_code={}", calldata[0])?;
+        }
+        write!(f, ")")
+    }
+}

+ 20 - 1
src/tx/mod.rs

@@ -53,7 +53,7 @@ macro_rules! zip {
 /// A Transaction contains an arbitrary number of `ContractCall` objects,
 /// A Transaction contains an arbitrary number of `ContractCall` objects,
 /// along with corresponding ZK proofs and Schnorr signatures. `DarkLeaf`
 /// along with corresponding ZK proofs and Schnorr signatures. `DarkLeaf`
 /// is used to map relations between contract calls in the transaciton.
 /// is used to map relations between contract calls in the transaciton.
-#[derive(Debug, Clone, Default, Eq, PartialEq, SerialEncodable, SerialDecodable)]
+#[derive(Clone, Default, Eq, PartialEq, SerialEncodable, SerialDecodable)]
 pub struct Transaction {
 pub struct Transaction {
     /// Calls executed in this transaction
     /// Calls executed in this transaction
     pub calls: Vec<DarkLeaf<ContractCall>>,
     pub calls: Vec<DarkLeaf<ContractCall>>,
@@ -166,6 +166,25 @@ impl Transaction {
     }
     }
 }
 }
 
 
+// Avoid showing the proofs and sigs in the debug output since often they are very long.
+impl std::fmt::Debug for Transaction {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "Transaction {{\n")?;
+        for (i, call) in self.calls.iter().enumerate() {
+            write!(f, "  Call {} {{\n", i)?;
+            write!(f, "    contract_id: {:?}\n", call.data.contract_id.inner())?;
+            let calldata = &call.data.data;
+            if calldata.len() > 0 {
+                write!(f, "    function_code: {}\n", calldata[0])?;
+            }
+            write!(f, "    parent: {:?}\n", call.parent_index)?;
+            write!(f, "    children: {:?}\n", call.children_indexes)?;
+            write!(f, "  }},\n")?;
+        }
+        write!(f, "}}\n")
+    }
+}
+
 #[cfg(feature = "net")]
 #[cfg(feature = "net")]
 use crate::net::Message;
 use crate::net::Message;