From c4a9c8b5e993ce5e854244d28d5fd3ccc7c5b233 Mon Sep 17 00:00:00 2001 From: fee1-dead Date: Sat, 4 Dec 2021 11:53:35 +0800 Subject: [PATCH] Remove dependency on `hex` (#21567) * Remove dependency on `hex` * Update lock file * Use `debug_struct` instead of own format * Share code, add test, and fix rent_epoch spelling Co-authored-by: Tyera Eulberg --- Cargo.lock | 2 - programs/bpf/Cargo.lock | 2 - sdk/Cargo.toml | 1 - sdk/program/Cargo.toml | 3 +- sdk/program/src/account_info.rs | 126 +++++++++++++++++++++----- sdk/program/src/debug_account_data.rs | 20 ++++ sdk/program/src/lib.rs | 1 + sdk/src/account.rs | 30 +++--- 8 files changed, 136 insertions(+), 49 deletions(-) create mode 100644 sdk/program/src/debug_account_data.rs diff --git a/Cargo.lock b/Cargo.lock index ceb63ee7f2..a4305500ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5339,7 +5339,6 @@ dependencies = [ "bv", "bytemuck", "curve25519-dalek 3.2.0", - "hex", "itertools 0.10.1", "lazy_static", "libsecp256k1 0.6.0", @@ -5624,7 +5623,6 @@ dependencies = [ "ed25519-dalek", "ed25519-dalek-bip32", "generic-array 0.14.4", - "hex", "hmac 0.11.0", "itertools 0.10.1", "lazy_static", diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index 5ebba4f178..0cf4523d35 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -3181,7 +3181,6 @@ dependencies = [ "bv", "bytemuck", "curve25519-dalek 3.2.0", - "hex", "itertools 0.10.1", "lazy_static", "libsecp256k1 0.6.0", @@ -3336,7 +3335,6 @@ dependencies = [ "ed25519-dalek", "ed25519-dalek-bip32", "generic-array 0.14.4", - "hex", "hmac 0.11.0", "itertools 0.10.1", "lazy_static", diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 54391e10bc..8ce62e7971 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -51,7 +51,6 @@ digest = { version = "0.9.0", optional = true } ed25519-dalek = { version = "=1.0.1", optional = true } ed25519-dalek-bip32 = { version = "0.1.1", optional = true } generic-array = { version = "0.14.4", default-features = false, features = ["serde", "more_lengths"], optional = true } -hex = "0.4.3" hmac = "0.11.0" itertools = "0.10.1" lazy_static = "1.4.0" diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index 566162c9e3..94b65aad0e 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -18,8 +18,7 @@ borsh-derive = "0.9.1" bs58 = "0.4.0" bytemuck = { version = "1.7.2", features = ["derive"] } bv = { version = "0.11.1", features = ["serde"] } -hex = "0.4.2" -itertools = "0.10.1" +itertools = "0.10.1" lazy_static = "1.4.0" log = "0.4.14" num-derive = "0.3" diff --git a/sdk/program/src/account_info.rs b/sdk/program/src/account_info.rs index 27150a7d0f..cba05d8345 100644 --- a/sdk/program/src/account_info.rs +++ b/sdk/program/src/account_info.rs @@ -1,10 +1,11 @@ use { crate::{ - clock::Epoch, program_error::ProgramError, program_memory::sol_memset, pubkey::Pubkey, + clock::Epoch, debug_account_data::*, program_error::ProgramError, + program_memory::sol_memset, pubkey::Pubkey, }, std::{ cell::{Ref, RefCell, RefMut}, - cmp, fmt, + fmt, rc::Rc, }, }; @@ -32,28 +33,19 @@ pub struct AccountInfo<'a> { impl<'a> fmt::Debug for AccountInfo<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let data_len = cmp::min(64, self.data_len()); - let data_str = if data_len > 0 { - format!( - " data: {} ...", - hex::encode(self.data.borrow()[..data_len].to_vec()) - ) - } else { - "".to_string() - }; - write!( - f, - "AccountInfo {{ key: {} owner: {} is_signer: {} is_writable: {} executable: {} rent_epoch: {} lamports: {} data.len: {} {} }}", - self.key, - self.owner, - self.is_signer, - self.is_writable, - self.executable, - self.rent_epoch, - self.lamports(), - self.data_len(), - data_str, - ) + let mut f = f.debug_struct("AccountInfo"); + + f.field("key", &self.key) + .field("owner", &self.owner) + .field("is_signer", &self.is_signer) + .field("is_writable", &self.is_writable) + .field("executable", &self.executable) + .field("rent_epoch", &self.rent_epoch) + .field("lamports", &self.lamports()) + .field("data.len", &self.data_len()); + debug_account_data(&self.data.borrow(), &mut f); + + f.finish_non_exhaustive() } } @@ -415,4 +407,90 @@ mod tests { let info = AccountInfo::new(&k, false, false, l, d, &k, false, 0); assert_eq!(info.key, info.as_ref().key); } + + #[test] + fn test_account_info_debug_data() { + let key = Pubkey::new_unique(); + let mut lamports = 42; + let mut data = vec![5; 80]; + let data_str = format!("{:?}", Hex(&data[..MAX_DEBUG_ACCOUNT_DATA])); + let info = AccountInfo::new(&key, false, false, &mut lamports, &mut data, &key, false, 0); + assert_eq!( + format!("{:?}", info), + format!( + "AccountInfo {{ \ + key: {}, \ + owner: {}, \ + is_signer: {}, \ + is_writable: {}, \ + executable: {}, \ + rent_epoch: {}, \ + lamports: {}, \ + data.len: {}, \ + data: {}, .. }}", + key, + key, + false, + false, + false, + 0, + lamports, + data.len(), + data_str, + ) + ); + + let mut data = vec![5; 40]; + let data_str = format!("{:?}", Hex(&data)); + let info = AccountInfo::new(&key, false, false, &mut lamports, &mut data, &key, false, 0); + assert_eq!( + format!("{:?}", info), + format!( + "AccountInfo {{ \ + key: {}, \ + owner: {}, \ + is_signer: {}, \ + is_writable: {}, \ + executable: {}, \ + rent_epoch: {}, \ + lamports: {}, \ + data.len: {}, \ + data: {}, .. }}", + key, + key, + false, + false, + false, + 0, + lamports, + data.len(), + data_str, + ) + ); + + let mut data = vec![]; + let info = AccountInfo::new(&key, false, false, &mut lamports, &mut data, &key, false, 0); + assert_eq!( + format!("{:?}", info), + format!( + "AccountInfo {{ \ + key: {}, \ + owner: {}, \ + is_signer: {}, \ + is_writable: {}, \ + executable: {}, \ + rent_epoch: {}, \ + lamports: {}, \ + data.len: {}, .. }}", + key, + key, + false, + false, + false, + 0, + lamports, + data.len(), + ) + ); + } } diff --git a/sdk/program/src/debug_account_data.rs b/sdk/program/src/debug_account_data.rs new file mode 100644 index 0000000000..898b060223 --- /dev/null +++ b/sdk/program/src/debug_account_data.rs @@ -0,0 +1,20 @@ +use std::{cmp, fmt}; + +pub(crate) const MAX_DEBUG_ACCOUNT_DATA: usize = 64; + +pub fn debug_account_data(data: &[u8], f: &mut fmt::DebugStruct<'_, '_>) { + let data_len = cmp::min(MAX_DEBUG_ACCOUNT_DATA, data.len()); + if data_len > 0 { + f.field("data", &Hex(&data[..data_len])); + } +} + +pub(crate) struct Hex<'a>(pub(crate) &'a [u8]); +impl fmt::Debug for Hex<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for &byte in self.0 { + write!(f, "{:02x}", byte)?; + } + Ok(()) + } +} diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index 5914aa842b..3d5768a6d9 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -13,6 +13,7 @@ pub mod bpf_loader; pub mod bpf_loader_deprecated; pub mod bpf_loader_upgradeable; pub mod clock; +pub mod debug_account_data; pub mod decode_error; pub mod ed25519_program; pub mod entrypoint; diff --git a/sdk/src/account.rs b/sdk/src/account.rs index 0ec77fc6d0..ca19f91a85 100644 --- a/sdk/src/account.rs +++ b/sdk/src/account.rs @@ -4,10 +4,10 @@ use { lamports::LamportsError, pubkey::Pubkey, }, - solana_program::{account_info::AccountInfo, sysvar::Sysvar}, + solana_program::{account_info::AccountInfo, debug_account_data::*, sysvar::Sysvar}, std::{ cell::{Ref, RefCell}, - cmp, fmt, + fmt, rc::Rc, sync::Arc, }, @@ -280,22 +280,16 @@ impl ReadableAccount for Ref<'_, Account> { } fn debug_fmt(item: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let data_len = cmp::min(64, item.data().len()); - let data_str = if data_len > 0 { - format!(" data: {}", hex::encode(item.data()[..data_len].to_vec())) - } else { - "".to_string() - }; - write!( - f, - "Account {{ lamports: {} data.len: {} owner: {} executable: {} rent_epoch: {}{} }}", - item.lamports(), - item.data().len(), - item.owner(), - item.executable(), - item.rent_epoch(), - data_str, - ) + let mut f = f.debug_struct("Account"); + + f.field("lamports", &item.lamports()) + .field("data.len", &item.data().len()) + .field("owner", &item.owner()) + .field("executable", &item.executable()) + .field("rent_epoch", &item.rent_epoch()); + debug_account_data(item.data(), &mut f); + + f.finish() } impl fmt::Debug for Account {