From 75d94240ed006501c72a33fb8bcb8875b88a1071 Mon Sep 17 00:00:00 2001 From: Jack May Date: Fri, 3 Jan 2020 09:14:51 -0800 Subject: [PATCH] account_info utilities (#7666) --- sdk/src/account.rs | 18 ++++----- sdk/src/account_info.rs | 88 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/sdk/src/account.rs b/sdk/src/account.rs index 3d66123d9..1219fcce5 100644 --- a/sdk/src/account.rs +++ b/sdk/src/account.rs @@ -134,8 +134,8 @@ impl<'a> KeyedAccount<'a> { self.is_writable } - pub fn new(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> KeyedAccount<'a> { - KeyedAccount { + pub fn new(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> Self { + Self { is_signer, is_writable: true, key, @@ -143,12 +143,8 @@ impl<'a> KeyedAccount<'a> { } } - pub fn new_readonly( - key: &'a Pubkey, - is_signer: bool, - account: &'a mut Account, - ) -> KeyedAccount<'a> { - KeyedAccount { + pub fn new_readonly(key: &'a Pubkey, is_signer: bool, account: &'a mut Account) -> Self { + Self { is_signer, is_writable: false, key, @@ -159,7 +155,7 @@ impl<'a> KeyedAccount<'a> { impl<'a> From<(&'a Pubkey, &'a mut Account)> for KeyedAccount<'a> { fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self { - KeyedAccount { + Self { is_signer: false, is_writable: true, key, @@ -170,7 +166,7 @@ impl<'a> From<(&'a Pubkey, &'a mut Account)> for KeyedAccount<'a> { impl<'a> From<(&'a Pubkey, bool, &'a mut Account)> for KeyedAccount<'a> { fn from((key, is_signer, account): (&'a Pubkey, bool, &'a mut Account)) -> Self { - KeyedAccount { + Self { is_signer, is_writable: true, key, @@ -181,7 +177,7 @@ impl<'a> From<(&'a Pubkey, bool, &'a mut Account)> for KeyedAccount<'a> { impl<'a> From<&'a mut (Pubkey, Account)> for KeyedAccount<'a> { fn from((key, account): &'a mut (Pubkey, Account)) -> Self { - KeyedAccount { + Self { is_signer: false, is_writable: true, key, diff --git a/sdk/src/account_info.rs b/sdk/src/account_info.rs index a557ff629..8dfac18f0 100644 --- a/sdk/src/account_info.rs +++ b/sdk/src/account_info.rs @@ -1,11 +1,12 @@ -use crate::pubkey::Pubkey; +use crate::{account::Account, pubkey::Pubkey}; + use std::{cmp, fmt}; /// AccountInfo pub struct AccountInfo<'a> { /// Public key of the account pub key: &'a Pubkey, - /// Public key of the account + /// Was the transaction signed by this account's public key? pub is_signer: bool, /// Number of lamports owned by this account pub lamports: &'a mut u64, @@ -35,6 +36,34 @@ impl<'a> fmt::Debug for AccountInfo<'a> { } impl<'a> AccountInfo<'a> { + pub fn signer_key(&self) -> Option<&Pubkey> { + if self.is_signer { + Some(self.key) + } else { + None + } + } + + pub fn unsigned_key(&self) -> &Pubkey { + self.key + } + + pub fn new( + key: &'a Pubkey, + is_signer: bool, + lamports: &'a mut u64, + data: &'a mut [u8], + owner: &'a Pubkey, + ) -> Self { + Self { + key, + is_signer, + lamports, + data, + owner, + } + } + pub fn deserialize_data(&self) -> Result { bincode::deserialize(&self.data) } @@ -46,3 +75,58 @@ impl<'a> AccountInfo<'a> { bincode::serialize_into(&mut self.data[..], state) } } + +impl<'a> From<(&'a Pubkey, &'a mut Account)> for AccountInfo<'a> { + fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self { + Self { + key, + is_signer: false, + lamports: &mut account.lamports, + data: &mut account.data, + owner: &account.owner, + } + } +} + +impl<'a> From<(&'a Pubkey, bool, &'a mut Account)> for AccountInfo<'a> { + fn from((key, is_signer, account): (&'a Pubkey, bool, &'a mut Account)) -> Self { + Self { + key, + is_signer, + lamports: &mut account.lamports, + data: &mut account.data, + owner: &account.owner, + } + } +} + +impl<'a> From<&'a mut (Pubkey, Account)> for AccountInfo<'a> { + fn from((key, account): &'a mut (Pubkey, Account)) -> Self { + Self { + key, + is_signer: false, + lamports: &mut account.lamports, + data: &mut account.data, + owner: &account.owner, + } + } +} + +pub fn create_account_infos(accounts: &mut [(Pubkey, Account)]) -> Vec { + accounts.iter_mut().map(Into::into).collect() +} + +pub fn create_is_signer_account_infos<'a>( + accounts: &'a mut [(&'a Pubkey, bool, &'a mut Account)], +) -> Vec> { + accounts + .iter_mut() + .map(|(key, is_signer, account)| AccountInfo { + key, + is_signer: *is_signer, + lamports: &mut account.lamports, + data: &mut account.data, + owner: &account.owner, + }) + .collect() +}