Optimize account copies and use RefCell to handle duplicate accounts in BPF programs (#7958)
This commit is contained in:
@ -226,31 +226,42 @@ SOL_FN_PREFIX bool sol_deserialize(
|
||||
return false;
|
||||
}
|
||||
params->ka_num = *(uint64_t *) input;
|
||||
if (ka_num < *(uint64_t *) input) {
|
||||
input += sizeof(uint64_t);
|
||||
if (ka_num < params->ka_num) {
|
||||
return false;
|
||||
}
|
||||
|
||||
input += sizeof(uint64_t);
|
||||
for (int i = 0; i < params->ka_num; i++) {
|
||||
// key
|
||||
params->ka[i].is_signer = *(uint64_t *) input != 0;
|
||||
input += sizeof(uint64_t);
|
||||
params->ka[i].key = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
uint8_t dup_info = input[0];
|
||||
input += sizeof(uint8_t);
|
||||
if (dup_info == 0) {
|
||||
// key
|
||||
params->ka[i].is_signer = *(uint64_t *) input != 0;
|
||||
input += sizeof(uint64_t);
|
||||
params->ka[i].key = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
|
||||
// lamports
|
||||
params->ka[i].lamports = (uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
// lamports
|
||||
params->ka[i].lamports = (uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
|
||||
// account userdata
|
||||
params->ka[i].userdata_len = *(uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
params->ka[i].userdata = (uint8_t *) input;
|
||||
input += params->ka[i].userdata_len;
|
||||
// account userdata
|
||||
params->ka[i].userdata_len = *(uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
params->ka[i].userdata = (uint8_t *) input;
|
||||
input += params->ka[i].userdata_len;
|
||||
|
||||
// owner
|
||||
params->ka[i].owner = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
// owner
|
||||
params->ka[i].owner = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
} 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].userdata_len = params->ka[dup_info].userdata_len;
|
||||
params->ka[i].userdata = params->ka[dup_info].userdata;
|
||||
params->ka[i].owner = params->ka[dup_info].owner;
|
||||
}
|
||||
}
|
||||
|
||||
params->data_len = *(uint64_t *) input;
|
||||
|
@ -1,33 +1,46 @@
|
||||
use crate::{account::Account, pubkey::Pubkey};
|
||||
use std::{cmp, fmt};
|
||||
use std::{
|
||||
cell::{Ref, RefCell, RefMut},
|
||||
cmp, fmt,
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
/// AccountInfo
|
||||
pub struct AccountInfo<'a> {
|
||||
/// Public key of the account
|
||||
pub key: &'a Pubkey,
|
||||
/// Was the transaction signed by this account's public key?
|
||||
pub is_signer: bool,
|
||||
/// Account information that is mutable by a program
|
||||
pub struct AccountInfoMut<'a> {
|
||||
/// Number of lamports owned by this account
|
||||
pub lamports: &'a mut u64,
|
||||
/// On-chain data within this account
|
||||
pub data: &'a mut [u8],
|
||||
}
|
||||
/// Account information
|
||||
#[derive(Clone)]
|
||||
pub struct AccountInfo<'a> {
|
||||
/// Public key of the account
|
||||
pub key: &'a Pubkey,
|
||||
// Was the transaction signed by this account's public key?
|
||||
pub is_signer: bool,
|
||||
/// Account members that are mutable by the program
|
||||
pub m: Rc<RefCell<AccountInfoMut<'a>>>,
|
||||
/// Program that owns this account
|
||||
pub owner: &'a Pubkey,
|
||||
}
|
||||
|
||||
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_len = cmp::min(64, self.m.borrow().data.len());
|
||||
let data_str = if data_len > 0 {
|
||||
format!(" data: {}", hex::encode(self.data[..data_len].to_vec()))
|
||||
format!(
|
||||
" data: {}",
|
||||
hex::encode(self.m.borrow().data[..data_len].to_vec())
|
||||
)
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
write!(
|
||||
f,
|
||||
"AccountInfo {{ lamports: {} data.len: {} owner: {} {} }}",
|
||||
self.lamports,
|
||||
self.data.len(),
|
||||
self.m.borrow().lamports,
|
||||
self.m.borrow().data.len(),
|
||||
self.owner,
|
||||
data_str,
|
||||
)
|
||||
@ -47,6 +60,22 @@ impl<'a> AccountInfo<'a> {
|
||||
self.key
|
||||
}
|
||||
|
||||
pub fn try_account_ref(&'a self) -> Result<Ref<AccountInfoMut>, u32> {
|
||||
self.try_borrow()
|
||||
}
|
||||
|
||||
pub fn try_account_ref_mut(&'a self) -> Result<RefMut<'a, AccountInfoMut>, u32> {
|
||||
self.try_borrow_mut()
|
||||
}
|
||||
|
||||
fn try_borrow(&self) -> Result<Ref<AccountInfoMut>, u32> {
|
||||
self.m.try_borrow().map_err(|_| std::u32::MAX)
|
||||
}
|
||||
|
||||
fn try_borrow_mut(&self) -> Result<RefMut<'a, AccountInfoMut>, u32> {
|
||||
self.m.try_borrow_mut().map_err(|_| std::u32::MAX)
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
key: &'a Pubkey,
|
||||
is_signer: bool,
|
||||
@ -57,57 +86,56 @@ impl<'a> AccountInfo<'a> {
|
||||
Self {
|
||||
key,
|
||||
is_signer,
|
||||
lamports,
|
||||
data,
|
||||
m: Rc::new(RefCell::new(AccountInfoMut { lamports, data })),
|
||||
owner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
|
||||
bincode::deserialize(&self.data)
|
||||
bincode::deserialize(&self.m.borrow().data)
|
||||
}
|
||||
|
||||
pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> {
|
||||
if bincode::serialized_size(state)? > self.data.len() as u64 {
|
||||
if bincode::serialized_size(state)? > self.m.borrow().data.len() as u64 {
|
||||
return Err(Box::new(bincode::ErrorKind::SizeLimit));
|
||||
}
|
||||
bincode::serialize_into(&mut self.data[..], state)
|
||||
bincode::serialize_into(&mut self.m.borrow_mut().data[..], state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<(&'a Pubkey, &'a mut Account)> for AccountInfo<'a> {
|
||||
fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self {
|
||||
Self {
|
||||
Self::new(
|
||||
key,
|
||||
is_signer: false,
|
||||
lamports: &mut account.lamports,
|
||||
data: &mut account.data,
|
||||
owner: &account.owner,
|
||||
}
|
||||
false,
|
||||
&mut account.lamports,
|
||||
&mut account.data,
|
||||
&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 {
|
||||
Self::new(
|
||||
key,
|
||||
is_signer,
|
||||
lamports: &mut account.lamports,
|
||||
data: &mut account.data,
|
||||
owner: &account.owner,
|
||||
}
|
||||
&mut account.lamports,
|
||||
&mut account.data,
|
||||
&account.owner,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a mut (Pubkey, Account)> for AccountInfo<'a> {
|
||||
fn from((key, account): &'a mut (Pubkey, Account)) -> Self {
|
||||
Self {
|
||||
Self::new(
|
||||
key,
|
||||
is_signer: false,
|
||||
lamports: &mut account.lamports,
|
||||
data: &mut account.data,
|
||||
owner: &account.owner,
|
||||
}
|
||||
false,
|
||||
&mut account.lamports,
|
||||
&mut account.data,
|
||||
&account.owner,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,12 +148,14 @@ pub fn create_is_signer_account_infos<'a>(
|
||||
) -> Vec<AccountInfo<'a>> {
|
||||
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,
|
||||
.map(|(key, is_signer, account)| {
|
||||
AccountInfo::new(
|
||||
key,
|
||||
*is_signer,
|
||||
&mut account.lamports,
|
||||
&mut account.data,
|
||||
&account.owner,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
@ -4,10 +4,15 @@
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use crate::{account_info::AccountInfo, pubkey::Pubkey};
|
||||
use crate::{
|
||||
account_info::{AccountInfo, AccountInfoMut},
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
use alloc::vec::Vec;
|
||||
use core::mem::size_of;
|
||||
use core::slice::{from_raw_parts, from_raw_parts_mut};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// User implemented program entrypoint
|
||||
///
|
||||
@ -15,7 +20,7 @@ use core::slice::{from_raw_parts, from_raw_parts_mut};
|
||||
/// accounts: Accounts passed as part of the instruction
|
||||
/// data: Instruction data
|
||||
pub type ProcessInstruction =
|
||||
fn(program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> bool;
|
||||
fn(program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32;
|
||||
|
||||
/// Programs indicate success with a return value of 0
|
||||
pub const SUCCESS: u32 = 0;
|
||||
@ -57,37 +62,45 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
|
||||
|
||||
let mut accounts = Vec::with_capacity(num_accounts);
|
||||
for _ in 0..num_accounts {
|
||||
let is_signer = {
|
||||
let dup_info = *(input.add(offset) as *const u8) as usize;
|
||||
offset += size_of::<u8>();
|
||||
if dup_info == 0 {
|
||||
let is_signer = {
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
let is_signer_val = *(input.add(offset) as *const u64);
|
||||
(is_signer_val != 0)
|
||||
};
|
||||
offset += size_of::<u64>();
|
||||
|
||||
let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
||||
offset += size_of::<Pubkey>();
|
||||
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
let is_signer_val = *(input.add(offset) as *const u64);
|
||||
(is_signer_val != 0)
|
||||
};
|
||||
offset += size_of::<u64>();
|
||||
let lamports = &mut *(input.add(offset) as *mut u64);
|
||||
offset += size_of::<u64>();
|
||||
|
||||
let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
||||
offset += size_of::<Pubkey>();
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
let data_length = *(input.add(offset) as *const u64) as usize;
|
||||
offset += size_of::<u64>();
|
||||
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
let lamports = &mut *(input.add(offset) as *mut u64);
|
||||
offset += size_of::<u64>();
|
||||
let data = { from_raw_parts_mut(input.add(offset), data_length) };
|
||||
offset += data_length;
|
||||
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
let data_length = *(input.add(offset) as *const u64) as usize;
|
||||
offset += size_of::<u64>();
|
||||
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
||||
offset += size_of::<Pubkey>();
|
||||
|
||||
let data = { from_raw_parts_mut(input.add(offset), data_length) };
|
||||
offset += data_length;
|
||||
let m = Rc::new(RefCell::new(AccountInfoMut { lamports, data }));
|
||||
|
||||
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
||||
offset += size_of::<Pubkey>();
|
||||
|
||||
accounts.push(AccountInfo {
|
||||
key,
|
||||
is_signer,
|
||||
lamports,
|
||||
data,
|
||||
owner,
|
||||
});
|
||||
accounts.push(AccountInfo {
|
||||
key,
|
||||
is_signer,
|
||||
m,
|
||||
owner,
|
||||
});
|
||||
} else {
|
||||
// Duplicate account, clone the original
|
||||
accounts.push(accounts[dup_info].clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Instruction data
|
||||
|
@ -2,7 +2,6 @@
|
||||
extern crate self as solana_sdk;
|
||||
|
||||
pub mod account;
|
||||
pub mod account_info;
|
||||
pub mod account_utils;
|
||||
pub mod bpf_loader;
|
||||
pub mod clock;
|
||||
@ -56,6 +55,7 @@ pub mod timing;
|
||||
pub use solana_sdk_macro::declare_id;
|
||||
|
||||
// On-chain program specific modules
|
||||
pub mod account_info;
|
||||
pub mod entrypoint;
|
||||
pub mod log;
|
||||
|
||||
|
@ -89,9 +89,9 @@ pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
|
||||
sol_log("- Key");
|
||||
account.key.log();
|
||||
sol_log("- Lamports");
|
||||
sol_log_64(0, 0, 0, 0, *account.lamports);
|
||||
sol_log_64(0, 0, 0, 0, *account.m.borrow().lamports);
|
||||
sol_log("- Account data length");
|
||||
sol_log_64(0, 0, 0, 0, account.data.len() as u64);
|
||||
sol_log_64(0, 0, 0, 0, account.m.borrow().data.len() as u64);
|
||||
sol_log("- Owner");
|
||||
account.owner.log();
|
||||
}
|
||||
|
@ -71,10 +71,10 @@ pub trait Sysvar:
|
||||
bincode::serialize_into(&mut account.data[..], self).ok()
|
||||
}
|
||||
fn from_account_info(account_info: &AccountInfo) -> Option<Self> {
|
||||
bincode::deserialize(&account_info.data).ok()
|
||||
bincode::deserialize(&account_info.m.borrow().data).ok()
|
||||
}
|
||||
fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
|
||||
bincode::serialize_into(&mut account_info.data[..], self).ok()
|
||||
bincode::serialize_into(&mut account_info.m.borrow_mut().data[..], self).ok()
|
||||
}
|
||||
fn from_keyed_account(keyed_account: &KeyedAccount) -> Result<Self, InstructionError> {
|
||||
if !Self::check_id(keyed_account.unsigned_key()) {
|
||||
|
Reference in New Issue
Block a user