Don't use global storage account

Other accounts would not be able to modify the system accounts userdata.
This commit is contained in:
Stephen Akridge
2019-02-22 14:44:23 -08:00
committed by sakridge
parent 6bca577d6d
commit 66891d9d4e
4 changed files with 33 additions and 115 deletions

View File

@ -33,7 +33,7 @@ fn entrypoint(
) -> Result<(), ProgramError> {
solana_logger::setup();
if keyed_accounts.len() != 2 {
if keyed_accounts.len() != 1 {
// keyed_accounts[1] should be the main storage key
// to access its userdata
Err(ProgramError::InvalidArgument)?;
@ -45,26 +45,9 @@ fn entrypoint(
Err(ProgramError::GenericError)?;
}
// Following https://github.com/solana-labs/solana/pull/2773,
// Modifications to userdata can only be made by accounts owned
// by this program. TODO: Add this check:
//if !check_id(&keyed_accounts[1].account.owner) {
// error!("account[1] is not assigned to the STORAGE_PROGRAM");
// Err(ProgramError::InvalidArgument)?;
//}
if *keyed_accounts[1].unsigned_key() != system_id() {
info!(
"invalid account id owner: {:?} system_id: {:?}",
keyed_accounts[1].unsigned_key(),
system_id()
);
Err(ProgramError::InvalidArgument)?;
}
if let Ok(syscall) = bincode::deserialize(data) {
let mut storage_account_state = if let Ok(storage_account_state) =
bincode::deserialize(&keyed_accounts[1].account.userdata)
bincode::deserialize(&keyed_accounts[0].account.userdata)
{
storage_account_state
} else {
@ -176,7 +159,7 @@ fn entrypoint(
}
if bincode::serialize_into(
&mut keyed_accounts[1].account.userdata[..],
&mut keyed_accounts[0].account.userdata[..],
&storage_account_state,
)
.is_err()
@ -197,7 +180,6 @@ mod test {
use solana_sdk::account::{create_keyed_accounts, Account};
use solana_sdk::hash::Hash;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::storage_program;
use solana_sdk::storage_program::ProofStatus;
use solana_sdk::storage_program::StorageTransaction;
use solana_sdk::transaction::{Instruction, Transaction};
@ -244,11 +226,8 @@ mod test {
let keypair = Keypair::new();
let mut keyed_accounts = Vec::new();
let mut user_account = Account::default();
let mut system_account = Account::default();
let pubkey = keypair.pubkey();
let system_key = storage_program::system_id();
keyed_accounts.push(KeyedAccount::new(&pubkey, true, &mut user_account));
keyed_accounts.push(KeyedAccount::new(&system_key, false, &mut system_account));
let tx = StorageTransaction::new_advertise_last_id(
&keypair,
@ -306,7 +285,7 @@ mod test {
solana_logger::setup();
let keypair = Keypair::new();
let mut accounts = [Account::default(), Account::default()];
accounts[1].userdata.resize(16 * 1024, 0);
accounts[0].userdata.resize(16 * 1024, 0);
let tx = StorageTransaction::new_advertise_last_id(
&keypair,
@ -333,7 +312,7 @@ mod test {
solana_logger::setup();
let keypair = Keypair::new();
let mut accounts = [Account::default(), Account::default()];
accounts[1].userdata.resize(16 * 1024, 0);
accounts[0].userdata.resize(16 * 1024, 0);
let entry_height = 0;

View File

@ -3,12 +3,14 @@ use log::info;
use solana_runtime::bank::Bank;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::hash::{hash, Hash};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::storage_program;
use solana_sdk::storage_program::{StorageTransaction, ENTRIES_PER_SEGMENT};
use solana_sdk::system_transaction::SystemTransaction;
fn get_storage_entry_height(bank: &Bank) -> u64 {
match bank.get_account(&storage_program::system_id()) {
fn get_storage_entry_height(bank: &Bank, account: Pubkey) -> u64 {
match bank.get_account(&account) {
Some(storage_system_account) => {
let state = deserialize(&storage_system_account.userdata);
if let Ok(state) = state {
@ -23,8 +25,8 @@ fn get_storage_entry_height(bank: &Bank) -> u64 {
0
}
fn get_storage_last_id(bank: &Bank) -> Hash {
if let Some(storage_system_account) = bank.get_account(&storage_program::system_id()) {
fn get_storage_last_id(bank: &Bank, account: Pubkey) -> Hash {
if let Some(storage_system_account) = bank.get_account(&account) {
let state = deserialize(&storage_system_account.userdata);
if let Ok(state) = state {
let state: storage_program::StorageProgramState = state;
@ -35,7 +37,6 @@ fn get_storage_last_id(bank: &Bank) -> Hash {
}
#[test]
#[ignore]
fn test_bank_storage() {
let (genesis_block, alice) = GenesisBlock::new(1000);
let bank = Bank::new(&genesis_block);
@ -56,6 +57,18 @@ fn test_bank_storage() {
bank.transfer(10, &alice, bob.pubkey(), last_id).unwrap();
bank.transfer(10, &alice, jack.pubkey(), last_id).unwrap();
let tx = SystemTransaction::new_program_account(
&alice,
bob.pubkey(),
last_id,
1,
4 * 1024,
storage_program::id(),
0,
);
bank.process_transaction(&tx).unwrap();
let tx = StorageTransaction::new_advertise_last_id(
&bob,
storage_last_id,
@ -77,6 +90,9 @@ fn test_bank_storage() {
bank.process_transaction(&tx).unwrap();
assert_eq!(get_storage_entry_height(&bank), ENTRIES_PER_SEGMENT);
assert_eq!(get_storage_last_id(&bank), storage_last_id);
assert_eq!(
get_storage_entry_height(&bank, bob.pubkey()),
ENTRIES_PER_SEGMENT
);
assert_eq!(get_storage_last_id(&bank, bob.pubkey()), storage_last_id);
}