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 <tyera@solana.com>
This commit is contained in:
fee1-dead 2021-12-04 11:53:35 +08:00 committed by GitHub
parent e130b2cffc
commit c4a9c8b5e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 136 additions and 49 deletions

2
Cargo.lock generated
View File

@ -5339,7 +5339,6 @@ dependencies = [
"bv", "bv",
"bytemuck", "bytemuck",
"curve25519-dalek 3.2.0", "curve25519-dalek 3.2.0",
"hex",
"itertools 0.10.1", "itertools 0.10.1",
"lazy_static", "lazy_static",
"libsecp256k1 0.6.0", "libsecp256k1 0.6.0",
@ -5624,7 +5623,6 @@ dependencies = [
"ed25519-dalek", "ed25519-dalek",
"ed25519-dalek-bip32", "ed25519-dalek-bip32",
"generic-array 0.14.4", "generic-array 0.14.4",
"hex",
"hmac 0.11.0", "hmac 0.11.0",
"itertools 0.10.1", "itertools 0.10.1",
"lazy_static", "lazy_static",

View File

@ -3181,7 +3181,6 @@ dependencies = [
"bv", "bv",
"bytemuck", "bytemuck",
"curve25519-dalek 3.2.0", "curve25519-dalek 3.2.0",
"hex",
"itertools 0.10.1", "itertools 0.10.1",
"lazy_static", "lazy_static",
"libsecp256k1 0.6.0", "libsecp256k1 0.6.0",
@ -3336,7 +3335,6 @@ dependencies = [
"ed25519-dalek", "ed25519-dalek",
"ed25519-dalek-bip32", "ed25519-dalek-bip32",
"generic-array 0.14.4", "generic-array 0.14.4",
"hex",
"hmac 0.11.0", "hmac 0.11.0",
"itertools 0.10.1", "itertools 0.10.1",
"lazy_static", "lazy_static",

View File

@ -51,7 +51,6 @@ digest = { version = "0.9.0", optional = true }
ed25519-dalek = { version = "=1.0.1", optional = true } ed25519-dalek = { version = "=1.0.1", optional = true }
ed25519-dalek-bip32 = { version = "0.1.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 } generic-array = { version = "0.14.4", default-features = false, features = ["serde", "more_lengths"], optional = true }
hex = "0.4.3"
hmac = "0.11.0" hmac = "0.11.0"
itertools = "0.10.1" itertools = "0.10.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"

View File

@ -18,7 +18,6 @@ borsh-derive = "0.9.1"
bs58 = "0.4.0" bs58 = "0.4.0"
bytemuck = { version = "1.7.2", features = ["derive"] } bytemuck = { version = "1.7.2", features = ["derive"] }
bv = { version = "0.11.1", features = ["serde"] } bv = { version = "0.11.1", features = ["serde"] }
hex = "0.4.2"
itertools = "0.10.1" itertools = "0.10.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.14" log = "0.4.14"

View File

@ -1,10 +1,11 @@
use { use {
crate::{ 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::{ std::{
cell::{Ref, RefCell, RefMut}, cell::{Ref, RefCell, RefMut},
cmp, fmt, fmt,
rc::Rc, rc::Rc,
}, },
}; };
@ -32,28 +33,19 @@ pub struct AccountInfo<'a> {
impl<'a> fmt::Debug for AccountInfo<'a> { impl<'a> fmt::Debug for AccountInfo<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let data_len = cmp::min(64, self.data_len()); let mut f = f.debug_struct("AccountInfo");
let data_str = if data_len > 0 {
format!( f.field("key", &self.key)
" data: {} ...", .field("owner", &self.owner)
hex::encode(self.data.borrow()[..data_len].to_vec()) .field("is_signer", &self.is_signer)
) .field("is_writable", &self.is_writable)
} else { .field("executable", &self.executable)
"".to_string() .field("rent_epoch", &self.rent_epoch)
}; .field("lamports", &self.lamports())
write!( .field("data.len", &self.data_len());
f, debug_account_data(&self.data.borrow(), &mut f);
"AccountInfo {{ key: {} owner: {} is_signer: {} is_writable: {} executable: {} rent_epoch: {} lamports: {} data.len: {} {} }}",
self.key, f.finish_non_exhaustive()
self.owner,
self.is_signer,
self.is_writable,
self.executable,
self.rent_epoch,
self.lamports(),
self.data_len(),
data_str,
)
} }
} }
@ -415,4 +407,90 @@ mod tests {
let info = AccountInfo::new(&k, false, false, l, d, &k, false, 0); let info = AccountInfo::new(&k, false, false, l, d, &k, false, 0);
assert_eq!(info.key, info.as_ref().key); 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(),
)
);
}
} }

View File

@ -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(())
}
}

View File

@ -13,6 +13,7 @@ pub mod bpf_loader;
pub mod bpf_loader_deprecated; pub mod bpf_loader_deprecated;
pub mod bpf_loader_upgradeable; pub mod bpf_loader_upgradeable;
pub mod clock; pub mod clock;
pub mod debug_account_data;
pub mod decode_error; pub mod decode_error;
pub mod ed25519_program; pub mod ed25519_program;
pub mod entrypoint; pub mod entrypoint;

View File

@ -4,10 +4,10 @@ use {
lamports::LamportsError, lamports::LamportsError,
pubkey::Pubkey, pubkey::Pubkey,
}, },
solana_program::{account_info::AccountInfo, sysvar::Sysvar}, solana_program::{account_info::AccountInfo, debug_account_data::*, sysvar::Sysvar},
std::{ std::{
cell::{Ref, RefCell}, cell::{Ref, RefCell},
cmp, fmt, fmt,
rc::Rc, rc::Rc,
sync::Arc, sync::Arc,
}, },
@ -280,22 +280,16 @@ impl ReadableAccount for Ref<'_, Account> {
} }
fn debug_fmt<T: ReadableAccount>(item: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn debug_fmt<T: ReadableAccount>(item: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let data_len = cmp::min(64, item.data().len()); let mut f = f.debug_struct("Account");
let data_str = if data_len > 0 {
format!(" data: {}", hex::encode(item.data()[..data_len].to_vec())) f.field("lamports", &item.lamports())
} else { .field("data.len", &item.data().len())
"".to_string() .field("owner", &item.owner())
}; .field("executable", &item.executable())
write!( .field("rent_epoch", &item.rent_epoch());
f, debug_account_data(item.data(), &mut f);
"Account {{ lamports: {} data.len: {} owner: {} executable: {} rent_epoch: {}{} }}",
item.lamports(), f.finish()
item.data().len(),
item.owner(),
item.executable(),
item.rent_epoch(),
data_str,
)
} }
impl fmt::Debug for Account { impl fmt::Debug for Account {