Debug for Account

Derive prints the full userdata vec which is questionably useful.
This commit is contained in:
Stephen Akridge
2019-02-21 10:55:29 -08:00
committed by Grimes
parent d25fc7a649
commit f77788447c
3 changed files with 33 additions and 1 deletions

View File

@ -1,8 +1,9 @@
use crate::pubkey::Pubkey;
use std::{cmp, fmt};
/// An Account with userdata that is stored on chain
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
#[derive(Serialize, Deserialize, Clone, Default, Eq, PartialEq)]
pub struct Account {
/// tokens in the account
pub tokens: u64,
@ -14,6 +15,29 @@ pub struct Account {
pub executable: bool,
}
impl fmt::Debug for Account {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let userdata_len = cmp::min(64, self.userdata.len());
let userdata_str = if userdata_len > 0 {
format!(
" userdata: {}",
hex::encode(self.userdata[..userdata_len].to_vec())
)
} else {
"".to_string()
};
write!(
f,
"Account {{ tokens: {} userdata.len: {} owner: {} executable: {}{} }}",
self.tokens,
self.userdata.len(),
self.owner,
self.executable,
userdata_str,
)
}
}
impl Account {
// TODO do we want to add executable and leader_owner even though they should always be false/default?
pub fn new(tokens: u64, space: usize, owner: Pubkey) -> Account {