Add account accessor functions (#7966)

This commit is contained in:
Jack May
2020-01-24 14:34:59 -08:00
committed by GitHub
parent 917067741a
commit 1b391dd36b
4 changed files with 33 additions and 29 deletions

View File

@ -16,32 +16,32 @@ fn process_instruction(
match instruction_data[0] { match instruction_data[0] {
1 => { 1 => {
info!("modify first account data"); info!("modify first account data");
accounts[2].m.borrow_mut().data[0] = 1; accounts[2].borrow_mut().data[0] = 1;
} }
2 => { 2 => {
info!("modify first account data"); info!("modify first account data");
accounts[3].m.borrow_mut().data[0] = 2; accounts[3].borrow_mut().data[0] = 2;
} }
3 => { 3 => {
info!("modify both account data"); info!("modify both account data");
accounts[2].m.borrow_mut().data[0] += 1; accounts[2].borrow_mut().data[0] += 1;
accounts[3].m.borrow_mut().data[0] += 2; accounts[3].borrow_mut().data[0] += 2;
} }
4 => { 4 => {
info!("modify first account lamports"); info!("modify first account lamports");
*accounts[1].m.borrow_mut().lamports -= 1; *accounts[1].borrow_mut().lamports -= 1;
*accounts[2].m.borrow_mut().lamports += 1; *accounts[2].borrow_mut().lamports += 1;
} }
5 => { 5 => {
info!("modify first account lamports"); info!("modify first account lamports");
*accounts[1].m.borrow_mut().lamports -= 2; *accounts[1].borrow_mut().lamports -= 2;
*accounts[3].m.borrow_mut().lamports += 2; *accounts[3].borrow_mut().lamports += 2;
} }
6 => { 6 => {
info!("modify both account lamports"); info!("modify both account lamports");
*accounts[1].m.borrow_mut().lamports -= 3; *accounts[1].borrow_mut().lamports -= 3;
*accounts[2].m.borrow_mut().lamports += 1; *accounts[2].borrow_mut().lamports += 1;
*accounts[3].m.borrow_mut().lamports += 2; *accounts[3].borrow_mut().lamports += 2;
} }
_ => { _ => {
info!("Unrecognized command"); info!("Unrecognized command");

View File

@ -12,7 +12,7 @@ fn process_instruction(
// account 0 is the mint and not owned by this program, any debit of its lamports // account 0 is the mint and not owned by this program, any debit of its lamports
// should result in a failed program execution. Test to ensure that this debit // should result in a failed program execution. Test to ensure that this debit
// is seen by the runtime and fails as expected // is seen by the runtime and fails as expected
*accounts[0].m.borrow_mut().lamports -= 1; *accounts[0].borrow_mut().lamports -= 1;
SUCCESS SUCCESS
} }

View File

@ -27,11 +27,11 @@ 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.m.borrow().data.len()); let data_len = cmp::min(64, self.data_len());
let data_str = if data_len > 0 { let data_str = if data_len > 0 {
format!( format!(
" data: {}", " data: {}",
hex::encode(self.m.borrow().data[..data_len].to_vec()) hex::encode(self.borrow().data[..data_len].to_vec())
) )
} else { } else {
"".to_string() "".to_string()
@ -39,8 +39,8 @@ impl<'a> fmt::Debug for AccountInfo<'a> {
write!( write!(
f, f,
"AccountInfo {{ lamports: {} data.len: {} owner: {} {} }}", "AccountInfo {{ lamports: {} data.len: {} owner: {} {} }}",
self.m.borrow().lamports, self.lamports(),
self.m.borrow().data.len(), self.data_len(),
self.owner, self.owner,
data_str, data_str,
) )
@ -60,20 +60,24 @@ impl<'a> AccountInfo<'a> {
self.key self.key
} }
pub fn try_account_ref(&'a self) -> Result<Ref<AccountInfoMut>, u32> { pub fn lamports(&self) -> u64 {
self.try_borrow() *self.borrow().lamports
} }
pub fn try_account_ref_mut(&'a self) -> Result<RefMut<'a, AccountInfoMut>, u32> { pub fn data_len(&self) -> usize {
self.try_borrow_mut() self.borrow().data.len()
} }
fn try_borrow(&self) -> Result<Ref<AccountInfoMut>, u32> { pub fn data_is_empty(&self) -> bool {
self.m.try_borrow().map_err(|_| std::u32::MAX) self.borrow().data.is_empty()
} }
fn try_borrow_mut(&self) -> Result<RefMut<'a, AccountInfoMut>, u32> { pub fn borrow(&self) -> Ref<AccountInfoMut> {
self.m.try_borrow_mut().map_err(|_| std::u32::MAX) self.m.borrow()
}
pub fn borrow_mut(&self) -> RefMut<AccountInfoMut<'a>> {
self.m.borrow_mut()
} }
pub fn new( pub fn new(
@ -92,14 +96,14 @@ impl<'a> AccountInfo<'a> {
} }
pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> { pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
bincode::deserialize(&self.m.borrow().data) bincode::deserialize(&self.borrow().data)
} }
pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> { pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> {
if bincode::serialized_size(state)? > self.m.borrow().data.len() as u64 { if bincode::serialized_size(state)? > self.data_len() as u64 {
return Err(Box::new(bincode::ErrorKind::SizeLimit)); return Err(Box::new(bincode::ErrorKind::SizeLimit));
} }
bincode::serialize_into(&mut self.m.borrow_mut().data[..], state) bincode::serialize_into(&mut self.borrow_mut().data[..], state)
} }
} }

View File

@ -89,9 +89,9 @@ pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
info!("- Key"); info!("- Key");
account.key.log(); account.key.log();
info!("- Lamports"); info!("- Lamports");
info!(0, 0, 0, 0, *account.m.borrow().lamports); info!(0, 0, 0, 0, account.lamports());
info!("- Account data length"); info!("- Account data length");
info!(0, 0, 0, 0, account.m.borrow().data.len()); info!(0, 0, 0, 0, account.data_len());
info!("- Owner"); info!("- Owner");
account.owner.log(); account.owner.log();
} }