Skip to content

Commit

Permalink
prettify ContractCall and Transaction debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxxxxxxxxxxx committed Dec 22, 2023
1 parent ff76f6e commit 87459ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/sdk/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ use super::crypto::ContractId;
// ANCHOR: contractcall
/// A ContractCall is the part of a transaction that executes a certain
/// `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 {
/// ID of the contract invoked
pub contract_id: ContractId,
/// Call data passed to the contract
pub data: Vec<u8>,
}
// 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, ")")
}
}
21 changes: 20 additions & 1 deletion src/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ macro_rules! zip {
/// A Transaction contains an arbitrary number of `ContractCall` objects,
/// along with corresponding ZK proofs and Schnorr signatures. `DarkLeaf`
/// 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 {
/// Calls executed in this transaction
pub calls: Vec<DarkLeaf<ContractCall>>,
Expand Down Expand Up @@ -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")]
use crate::net::Message;

Expand Down

0 comments on commit 87459ac

Please sign in to comment.