use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use solana_rbpf::{aligned_memory::AlignedMemory, ebpf::HOST_ALIGN};
use gemachain_sdk::{
account::{ReadableAccount, WritableAccount},
bpf_loader_deprecated,
entrypoint::MAX_PERMITTED_DATA_INCREASE,
instruction::InstructionError,
keyed_account::KeyedAccount,
pubkey::Pubkey,
};
use std::{
io::prelude::*,
mem::{align_of, size_of},
};
pub fn is_dup(accounts: &[KeyedAccount], keyed_account: &KeyedAccount) -> (bool, usize) {
for (i, account) in accounts.iter().enumerate() {
if account == keyed_account {
return (true, i);
}
}
(false, 0)
}
pub fn serialize_parameters(
loader_id: &Pubkey,
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
data: &[u8],
) -> Result<(AlignedMemory, Vec<usize>), InstructionError> {
if *loader_id == bpf_loader_deprecated::id() {
serialize_parameters_unaligned(program_id, keyed_accounts, data)
} else {
serialize_parameters_aligned(program_id, keyed_accounts, data)
}
.and_then(|buffer| {
let account_lengths = keyed_accounts
.iter()
.map(|keyed_account| keyed_account.data_len())
.collect::<Result<Vec<usize>, InstructionError>>()?;
Ok((buffer, account_lengths))
})
}
pub fn deserialize_parameters(
loader_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
buffer: &[u8],
account_lengths: &[usize],
) -> Result<(), InstructionError> {
if *loader_id == bpf_loader_deprecated::id() {
deserialize_parameters_unaligned(keyed_accounts, buffer, account_lengths)
} else {
deserialize_parameters_aligned(keyed_accounts, buffer, account_lengths)
}
}
pub fn get_serialized_account_size_unaligned(
keyed_account: &KeyedAccount,
) -> Result<usize, InstructionError> {
let data_len = keyed_account.data_len()?;
Ok(
size_of::<u8>() + size_of::<u8>() + size_of::<Pubkey>() + size_of::<u64>() + size_of::<u64>() + data_len + size_of::<Pubkey>() + size_of::<u8>() + size_of::<u64>(), )
}
pub fn serialize_parameters_unaligned(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<AlignedMemory, InstructionError> {
let mut size = size_of::<u64>();
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
size += 1; if !is_dup {
size += get_serialized_account_size_unaligned(keyed_account)?;
}
}
size += size_of::<u64>() + instruction_data.len() + size_of::<Pubkey>(); let mut v = AlignedMemory::new(size, HOST_ALIGN);
v.write_u64::<LittleEndian>(keyed_accounts.len() as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
let (is_dup, position) = is_dup(&keyed_accounts[..i], keyed_account);
if is_dup {
v.write_u8(position as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
} else {
v.write_u8(std::u8::MAX)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u8(keyed_account.signer_key().is_some() as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u8(keyed_account.is_writable() as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(keyed_account.unsigned_key().as_ref())
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u64::<LittleEndian>(keyed_account.carats()?)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(keyed_account.try_account_ref()?.data())
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(keyed_account.owner()?.as_ref())
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u8(keyed_account.executable()? as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
}
}
v.write_u64::<LittleEndian>(instruction_data.len() as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(instruction_data)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(program_id.as_ref())
.map_err(|_| InstructionError::InvalidArgument)?;
Ok(v)
}
pub fn deserialize_parameters_unaligned(
keyed_accounts: &[KeyedAccount],
buffer: &[u8],
account_lengths: &[usize],
) -> Result<(), InstructionError> {
let mut start = size_of::<u64>(); for (i, (keyed_account, _pre_len)) in keyed_accounts
.iter()
.zip(account_lengths.iter())
.enumerate()
{
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
start += 1; if !is_dup {
start += size_of::<u8>(); start += size_of::<u8>(); start += size_of::<Pubkey>(); keyed_account
.try_account_ref_mut()?
.set_carats(LittleEndian::read_u64(&buffer[start..]));
start += size_of::<u64>() + size_of::<u64>(); let end = start + keyed_account.data_len()?;
keyed_account
.try_account_ref_mut()?
.set_data_from_slice(&buffer[start..end]);
start += keyed_account.data_len()? + size_of::<Pubkey>() + size_of::<u8>() + size_of::<u64>(); }
}
Ok(())
}
pub fn get_serialized_account_size_aligned(
keyed_account: &KeyedAccount,
) -> Result<usize, InstructionError> {
let data_len = keyed_account.data_len()?;
Ok(
size_of::<u8>() + size_of::<u8>() + size_of::<u8>() + 4 + size_of::<Pubkey>() + size_of::<Pubkey>() + size_of::<u64>() + size_of::<u64>() + data_len
+ MAX_PERMITTED_DATA_INCREASE
+ (data_len as *const u8).align_offset(align_of::<u128>())
+ size_of::<u64>(), )
}
pub fn serialize_parameters_aligned(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<AlignedMemory, InstructionError> {
let mut size = size_of::<u64>();
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
size += 1; if is_dup {
size += 7; } else {
size += get_serialized_account_size_aligned(keyed_account)?;
}
}
size += size_of::<u64>() + instruction_data.len()
+ size_of::<Pubkey>(); let mut v = AlignedMemory::new(size, HOST_ALIGN);
v.write_u64::<LittleEndian>(keyed_accounts.len() as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
let (is_dup, position) = is_dup(&keyed_accounts[..i], keyed_account);
if is_dup {
v.write_u8(position as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(&[0u8, 0, 0, 0, 0, 0, 0])
.map_err(|_| InstructionError::InvalidArgument)?; } else {
v.write_u8(std::u8::MAX)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u8(keyed_account.signer_key().is_some() as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u8(keyed_account.is_writable() as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u8(keyed_account.executable()? as u8)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(&[0u8, 0, 0, 0])
.map_err(|_| InstructionError::InvalidArgument)?; v.write_all(keyed_account.unsigned_key().as_ref())
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(keyed_account.owner()?.as_ref())
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u64::<LittleEndian>(keyed_account.carats()?)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(keyed_account.try_account_ref()?.data())
.map_err(|_| InstructionError::InvalidArgument)?;
v.resize(
MAX_PERMITTED_DATA_INCREASE
+ (v.write_index() as *const u8).align_offset(align_of::<u128>()),
0,
)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
}
}
v.write_u64::<LittleEndian>(instruction_data.len() as u64)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(instruction_data)
.map_err(|_| InstructionError::InvalidArgument)?;
v.write_all(program_id.as_ref())
.map_err(|_| InstructionError::InvalidArgument)?;
Ok(v)
}
pub fn deserialize_parameters_aligned(
keyed_accounts: &[KeyedAccount],
buffer: &[u8],
account_lengths: &[usize],
) -> Result<(), InstructionError> {
let mut start = size_of::<u64>(); for (i, (keyed_account, pre_len)) in keyed_accounts
.iter()
.zip(account_lengths.iter())
.enumerate()
{
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
start += size_of::<u8>(); if is_dup {
start += 7; } else {
let mut account = keyed_account.try_account_ref_mut()?;
start += size_of::<u8>() + size_of::<u8>() + size_of::<u8>() + 4 + size_of::<Pubkey>(); account.copy_into_owner_from_slice(&buffer[start..start + size_of::<Pubkey>()]);
start += size_of::<Pubkey>(); account.set_carats(LittleEndian::read_u64(&buffer[start..]));
start += size_of::<u64>(); let post_len = LittleEndian::read_u64(&buffer[start..]) as usize;
start += size_of::<u64>(); let mut data_end = start + *pre_len;
if post_len != *pre_len
&& (post_len.saturating_sub(*pre_len)) <= MAX_PERMITTED_DATA_INCREASE
{
data_end = start + post_len;
}
account.set_data_from_slice(&buffer[start..data_end]);
start += *pre_len + MAX_PERMITTED_DATA_INCREASE; start += (start as *const u8).align_offset(align_of::<u128>());
start += size_of::<u64>(); }
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use gemachain_sdk::{
account::{Account, AccountSharedData},
account_info::AccountInfo,
bpf_loader,
entrypoint::deserialize,
};
use std::{
cell::RefCell,
rc::Rc,
slice::{from_raw_parts, from_raw_parts_mut},
};
#[test]
fn test_serialize_parameters() {
let program_id = gemachain_sdk::pubkey::new_rand();
let dup_key = gemachain_sdk::pubkey::new_rand();
let dup_key2 = gemachain_sdk::pubkey::new_rand();
let keys = vec![
dup_key,
dup_key,
gemachain_sdk::pubkey::new_rand(),
gemachain_sdk::pubkey::new_rand(),
dup_key2,
dup_key2,
gemachain_sdk::pubkey::new_rand(),
gemachain_sdk::pubkey::new_rand(),
];
let accounts = [
RefCell::new(AccountSharedData::from(Account {
carats: 1,
data: vec![1u8, 2, 3, 4, 5],
owner: bpf_loader::id(),
executable: false,
rent_epoch: 100,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 1,
data: vec![1u8, 2, 3, 4, 5],
owner: bpf_loader::id(),
executable: false,
rent_epoch: 100,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 2,
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
owner: bpf_loader::id(),
executable: true,
rent_epoch: 200,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 3,
data: vec![],
owner: bpf_loader::id(),
executable: false,
rent_epoch: 3100,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 4,
data: vec![1u8, 2, 3, 4, 5],
owner: bpf_loader::id(),
executable: false,
rent_epoch: 100,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 4,
data: vec![1u8, 2, 3, 4, 5],
owner: bpf_loader::id(),
executable: false,
rent_epoch: 100,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 5,
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
owner: bpf_loader::id(),
executable: true,
rent_epoch: 200,
})),
RefCell::new(AccountSharedData::from(Account {
carats: 6,
data: vec![],
owner: bpf_loader::id(),
executable: false,
rent_epoch: 3100,
})),
];
let keyed_accounts: Vec<_> = keys
.iter()
.zip(&accounts)
.enumerate()
.map(|(i, (key, account))| {
if i <= accounts.len() / 2 {
KeyedAccount::new_readonly(key, false, account)
} else {
KeyedAccount::new(key, false, account)
}
})
.collect();
let instruction_data = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let (mut serialized, account_lengths) = serialize_parameters(
&bpf_loader::id(),
&program_id,
&keyed_accounts,
&instruction_data,
)
.unwrap();
let (de_program_id, de_accounts, de_instruction_data) =
unsafe { deserialize(&mut serialized.as_slice_mut()[0] as *mut u8) };
assert_eq!(&program_id, de_program_id);
assert_eq!(instruction_data, de_instruction_data);
assert_eq!(
(&de_instruction_data[0] as *const u8).align_offset(align_of::<u128>()),
0
);
for ((account, account_info), key) in accounts.iter().zip(de_accounts).zip(keys.clone()) {
assert_eq!(key, *account_info.key);
let account = account.borrow();
assert_eq!(account.carats(), account_info.carats());
assert_eq!(account.data(), &account_info.data.borrow()[..]);
assert_eq!(account.owner(), account_info.owner);
assert_eq!(account.executable(), account_info.executable);
assert_eq!(account.rent_epoch(), account_info.rent_epoch);
assert_eq!(
(*account_info.carats.borrow() as *const u64).align_offset(align_of::<u64>()),
0
);
assert_eq!(
account_info
.data
.borrow()
.as_ptr()
.align_offset(align_of::<u128>()),
0
);
}
let de_accounts = accounts.clone();
let de_keyed_accounts: Vec<_> = keys
.iter()
.zip(&de_accounts)
.enumerate()
.map(|(i, (key, account))| {
if i <= accounts.len() / 2 {
KeyedAccount::new_readonly(key, false, account)
} else {
KeyedAccount::new(key, false, account)
}
})
.collect();
deserialize_parameters(
&bpf_loader::id(),
&de_keyed_accounts,
serialized.as_slice(),
&account_lengths,
)
.unwrap();
for ((account, de_keyed_account), key) in
accounts.iter().zip(de_keyed_accounts).zip(keys.clone())
{
assert_eq!(key, *de_keyed_account.unsigned_key());
let account = account.borrow();
assert_eq!(account.executable(), de_keyed_account.executable().unwrap());
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap());
}
let (mut serialized, account_lengths) = serialize_parameters(
&bpf_loader_deprecated::id(),
&program_id,
&keyed_accounts,
&instruction_data,
)
.unwrap();
let (de_program_id, de_accounts, de_instruction_data) =
unsafe { deserialize_unaligned(&mut serialized.as_slice_mut()[0] as *mut u8) };
assert_eq!(&program_id, de_program_id);
assert_eq!(instruction_data, de_instruction_data);
for ((account, account_info), key) in accounts.iter().zip(de_accounts).zip(keys.clone()) {
assert_eq!(key, *account_info.key);
let account = account.borrow();
assert_eq!(account.carats(), account_info.carats());
assert_eq!(account.data(), &account_info.data.borrow()[..]);
assert_eq!(account.owner(), account_info.owner);
assert_eq!(account.executable(), account_info.executable);
assert_eq!(account.rent_epoch(), account_info.rent_epoch);
}
let de_accounts = accounts.clone();
let de_keyed_accounts: Vec<_> = keys
.iter()
.zip(&de_accounts)
.enumerate()
.map(|(i, (key, account))| {
if i < accounts.len() / 2 {
KeyedAccount::new_readonly(key, false, account)
} else {
KeyedAccount::new(key, false, account)
}
})
.collect();
deserialize_parameters(
&bpf_loader_deprecated::id(),
&de_keyed_accounts,
serialized.as_slice(),
&account_lengths,
)
.unwrap();
for ((account, de_keyed_account), key) in
accounts.iter().zip(de_keyed_accounts).zip(keys.clone())
{
assert_eq!(key, *de_keyed_account.unsigned_key());
let account = account.borrow();
assert_eq!(account.carats(), de_keyed_account.carats().unwrap());
assert_eq!(
account.data(),
de_keyed_account.try_account_ref().unwrap().data()
);
assert_eq!(*account.owner(), de_keyed_account.owner().unwrap());
assert_eq!(account.executable(), de_keyed_account.executable().unwrap());
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap());
}
}
#[allow(clippy::type_complexity)]
pub unsafe fn deserialize_unaligned<'a>(
input: *mut u8,
) -> (&'a Pubkey, Vec<AccountInfo<'a>>, &'a [u8]) {
let mut offset: usize = 0;
#[allow(clippy::cast_ptr_alignment)]
let num_accounts = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let mut accounts = Vec::with_capacity(num_accounts);
for _ in 0..num_accounts {
let dup_info = *(input.add(offset) as *const u8);
offset += size_of::<u8>();
if dup_info == std::u8::MAX {
#[allow(clippy::cast_ptr_alignment)]
let is_signer = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();
#[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);
offset += size_of::<Pubkey>();
#[allow(clippy::cast_ptr_alignment)]
let carats = Rc::new(RefCell::new(&mut *(input.add(offset) as *mut u64)));
offset += size_of::<u64>();
#[allow(clippy::cast_ptr_alignment)]
let data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let data = Rc::new(RefCell::new({
from_raw_parts_mut(input.add(offset), data_len)
}));
offset += data_len;
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 {
key,
is_signer,
is_writable,
carats,
data,
owner,
executable,
rent_epoch,
});
} else {
accounts.push(accounts[dup_info as usize].clone());
}
}
#[allow(clippy::cast_ptr_alignment)]
let instruction_data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
offset += instruction_data_len;
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
(program_id, accounts, instruction_data)
}
}