Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Expose executable and rent_epoch in AccountInfo (#8619)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored Mar 4, 2020
1 parent b85d7c1 commit 25df95b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
7 changes: 6 additions & 1 deletion programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ pub fn serialize_parameters(
.unwrap();
v.write_all(&keyed_account.try_account_ref()?.data).unwrap();
v.write_all(keyed_account.owner()?.as_ref()).unwrap();
v.write_u8(keyed_account.executable()? as u8).unwrap();
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
.unwrap();
}
}
v.write_u64::<LittleEndian>(data.len() as u64).unwrap();
Expand Down Expand Up @@ -129,7 +132,9 @@ pub fn deserialize_parameters(
.data
.clone_from_slice(&buffer[start..end]);
start += keyed_account.data_len()? // data
+ mem::size_of::<Pubkey>(); // owner
+ mem::size_of::<Pubkey>() // owner
+ mem::size_of::<u8>() // executable
+ mem::size_of::<u64>(); // rent_epoch
}
}
Ok(())
Expand Down
20 changes: 18 additions & 2 deletions sdk/bpf/c/inc/solana_sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ SOL_FN_PREFIX bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two) {
*/
typedef struct {
SolPubkey *key; /** Public key of the account */
bool is_signer; /** Transaction was signed by this account's key? */
bool is_writable; /** Is the account writable? */
uint64_t *lamports; /** Number of lamports owned by this account */
uint64_t data_len; /** Length of data in bytes */
uint8_t *data; /** On-chain data within this account */
SolPubkey *owner; /** Program that owns this account */
uint64_t rent_epoch; /** The epoch at which this account will next owe rent */
bool is_signer; /** Transaction was signed by this account's key? */
bool is_writable; /** Is the account writable? */
bool executable; /** This account's data contains a loaded program (and is now read-only) */
} SolKeyedAccount;

/**
Expand Down Expand Up @@ -315,13 +317,23 @@ SOL_FN_PREFIX bool sol_deserialize(
// owner
params->ka[i].owner = (SolPubkey *) input;
input += sizeof(SolPubkey);

// executable?
params->ka[i].executable = *(uint8_t *) input;
input += sizeof(uint8_t);

// rent epoch
params->ka[i].rent_epoch = *(uint64_t *) input;
input += sizeof(uint64_t);
} else {
params->ka[i].is_signer = params->ka[dup_info].is_signer;
params->ka[i].key = params->ka[dup_info].key;
params->ka[i].lamports = params->ka[dup_info].lamports;
params->ka[i].data_len = params->ka[dup_info].data_len;
params->ka[i].data = params->ka[dup_info].data;
params->ka[i].owner = params->ka[dup_info].owner;
params->ka[i].executable = params->ka[dup_info].executable;
params->ka[i].rent_epoch = params->ka[dup_info].rent_epoch;
}
}

Expand Down Expand Up @@ -387,6 +399,10 @@ SOL_FN_PREFIX void sol_log_params(const SolParameters *params) {
sol_log_array(params->ka[i].data, params->ka[i].data_len);
sol_log(" - Owner");
sol_log_key(params->ka[i].owner);
sol_log(" - Executable");
sol_log_64(0, 0, 0, 0, params->ka[i].executable);
sol_log(" - Rent Epoch");
sol_log_64(0, 0, 0, 0, params->ka[i].rent_epoch);
}
sol_log("- Instruction data\0");
sol_log_array(params->data, params->data_len);
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::{
hash::Hash,
instruction::InstructionError,
{clock::Epoch, pubkey::Pubkey},
};
use crate::{clock::Epoch, hash::Hash, instruction::InstructionError, pubkey::Pubkey};
use std::{
cell::{Ref, RefCell, RefMut},
cmp, fmt,
Expand Down Expand Up @@ -183,6 +179,10 @@ impl<'a> KeyedAccount<'a> {
Ok(self.try_borrow()?.executable)
}

pub fn rent_epoch(&self) -> Result<Epoch, InstructionError> {
Ok(self.try_borrow()?.rent_epoch)
}

pub fn try_account_ref(&'a self) -> Result<Ref<Account>, InstructionError> {
self.try_borrow()
}
Expand Down
33 changes: 25 additions & 8 deletions sdk/src/account_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{account::Account, program_error::ProgramError, pubkey::Pubkey};
use crate::{account::Account, clock::Epoch, program_error::ProgramError, pubkey::Pubkey};
use std::{
cell::{Ref, RefCell, RefMut},
cmp, fmt,
Expand All @@ -20,25 +20,34 @@ pub struct AccountInfo<'a> {
pub data: Rc<RefCell<&'a mut [u8]>>,
/// Program that owns this account
pub owner: &'a Pubkey,
/// This account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// The epoch at which this account will next owe rent
pub rent_epoch: Epoch,
}

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: {}",
" data: {} ...",
hex::encode(self.data.borrow()[..data_len].to_vec())
)
} else {
"".to_string()
};
write!(
f,
"AccountInfo {{ lamports: {} data.len: {} owner: {} {} }}",
"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(),
self.owner,
data_str,
)
}
Expand All @@ -57,10 +66,6 @@ impl<'a> AccountInfo<'a> {
self.key
}

pub fn is_writable(&self) -> bool {
self.is_writable
}

pub fn lamports(&self) -> u64 {
**self.lamports.borrow()
}
Expand Down Expand Up @@ -116,6 +121,8 @@ impl<'a> AccountInfo<'a> {
lamports: &'a mut u64,
data: &'a mut [u8],
owner: &'a Pubkey,
executable: bool,
rent_epoch: Epoch,
) -> Self {
Self {
key,
Expand All @@ -124,6 +131,8 @@ impl<'a> AccountInfo<'a> {
lamports: Rc::new(RefCell::new(lamports)),
data: Rc::new(RefCell::new(data)),
owner,
executable,
rent_epoch,
}
}

Expand All @@ -148,6 +157,8 @@ impl<'a> From<(&'a Pubkey, &'a mut Account)> for AccountInfo<'a> {
&mut account.lamports,
&mut account.data,
&account.owner,
account.executable,
account.rent_epoch,
)
}
}
Expand All @@ -161,6 +172,8 @@ impl<'a> From<(&'a Pubkey, bool, &'a mut Account)> for AccountInfo<'a> {
&mut account.lamports,
&mut account.data,
&account.owner,
account.executable,
account.rent_epoch,
)
}
}
Expand All @@ -174,6 +187,8 @@ impl<'a> From<&'a mut (Pubkey, Account)> for AccountInfo<'a> {
&mut account.lamports,
&mut account.data,
&account.owner,
account.executable,
account.rent_epoch,
)
}
}
Expand All @@ -195,6 +210,8 @@ pub fn create_is_signer_account_infos<'a>(
&mut account.lamports,
&mut account.data,
&account.owner,
account.executable,
account.rent_epoch,
)
})
.collect()
Expand Down
24 changes: 14 additions & 10 deletions sdk/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,12 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
let dup_info = *(input.add(offset) as *const u8);
offset += size_of::<u8>();
if dup_info == std::u8::MAX {
let is_signer = {
#[allow(clippy::cast_ptr_alignment)]
let is_signer = *(input.add(offset) as *const u8);
is_signer != 0
};
#[allow(clippy::cast_ptr_alignment)]
let is_signer = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();

let is_writable = {
#[allow(clippy::cast_ptr_alignment)]
let is_writable = *(input.add(offset) as *const u8);
is_writable != 0
};
#[allow(clippy::cast_ptr_alignment)]
let is_writable = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();

let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
Expand All @@ -100,13 +94,23 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<Pubkey>();

#[allow(clippy::cast_ptr_alignment)]
let executable = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();

#[allow(clippy::cast_ptr_alignment)]
let rent_epoch = *(input.add(offset) as *const u64);
offset += size_of::<u64>();

accounts.push(AccountInfo {
is_signer,
is_writable,
key,
lamports,
data,
owner,
executable,
rent_epoch,
});
} else {
// Duplicate account, clone the original
Expand Down

0 comments on commit 25df95b

Please sign in to comment.