Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend trace decoder err info #148

Merged
merged 14 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions trace_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
OtherBlockData, TriePathIter, TrieRootHash, TxnIdx, TxnProofGenIR,
EMPTY_ACCOUNT_BYTES_RLPED, ZERO_STORAGE_SLOT_VAL_RLPED,
},
utils::{hash, update_val_if_some, write_optional},
utils::{hash, optional_field, optional_field_hex, update_val_if_some},
};

/// Stores the result of parsing tries. Returns a [TraceParsingError] upon
Expand Down Expand Up @@ -57,23 +57,24 @@ pub struct TraceParsingError {

impl std::fmt::Display for TraceParsingError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
vladimir-trifonov marked this conversation as resolved.
Show resolved Hide resolved
writeln!(f, "Error processing trace: {}", self.reason)?;
write_optional(f, "Block num", self.block_num)?;
write_optional(f, "Block chain id", self.block_chain_id)?;
write_optional(f, "Txn idx", self.txn_idx)?;
write_optional(f, "Address", self.addr.as_ref())?;
write_optional(f, "Hashed address", self.h_addr.as_ref())?;
if let Some(slot) = self.slot {
writeln!(f, "Slot: {:x}", slot)?;
let h_slot = self.slot.map(|slot| {
let mut buf = [0u8; 64];
slot.to_big_endian(&mut buf);
writeln!(f, "Hashed Slot: {}", hash(&buf))?;
}
if let Some(slot_value) = self.slot_value {
writeln!(f, "Slot value: {:x}", slot_value)?;
}

Ok(())
hash(&buf)
});
write!(
f,
"Error processing trace: {}\n{}{}{}{}{}{}{}{}",
self.reason,
optional_field("Block num", self.block_num),
optional_field("Block chain id", self.block_chain_id),
optional_field("Txn idx", self.txn_idx),
optional_field("Address", self.addr.as_ref()),
optional_field("Hashed address", self.h_addr.as_ref()),
optional_field_hex("Slot", self.slot),
optional_field("Hashed Slot", h_slot),
optional_field_hex("Slot value", self.slot_value),
)
}
}

Expand Down
15 changes: 6 additions & 9 deletions trace_decoder/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ pub(crate) fn h_addr_nibs_to_h256(h_addr_nibs: &Nibbles) -> H256 {
H256::from_slice(&nib_bytes)
}

pub(crate) fn write_optional<T: std::fmt::Display>(
f: &mut std::fmt::Formatter,
label: &str,
value: Option<T>,
) -> std::fmt::Result {
if let Some(v) = value {
writeln!(f, "{}: {}", label, v)?;
}
Ok(())
pub(crate) fn optional_field<T: std::fmt::Debug>(label: &str, value: Option<T>) -> String {
value.map_or(String::new(), |v| format!("{}: {:?}\n", label, v))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually prefer your original direct call to write! just because it avoids the Vec allocation 😅, but this is totally fine! I know we've already had a back an fourth on this.


pub(crate) fn optional_field_hex<T: std::fmt::UpperHex>(label: &str, value: Option<T>) -> String {
value.map_or(String::new(), |v| format!("{}: 0x{:064X}\n", label, v))
}