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:
parent
e130b2cffc
commit
c4a9c8b5e9
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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(),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<T: ReadableAccount>(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 {
|
||||
|
|
Loading…
Reference in New Issue