solana-with-rpc-optimizations/programs/storage/tests/storage.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2019-02-19 15:54:38 -08:00
use bincode::deserialize;
use log::*;
2019-02-19 15:54:38 -08:00
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};
2019-02-19 15:54:38 -08:00
use solana_sdk::storage_program;
use solana_sdk::storage_program::{StorageTransaction, ENTRIES_PER_SEGMENT};
use solana_sdk::system_transaction::SystemTransaction;
2019-02-19 15:54:38 -08:00
fn get_storage_entry_height(bank: &Bank, account: Pubkey) -> u64 {
match bank.get_account(&account) {
2019-02-19 15:54:38 -08:00
Some(storage_system_account) => {
let state = deserialize(&storage_system_account.userdata);
if let Ok(state) = state {
let state: storage_program::StorageProgramState = state;
return state.entry_height;
}
}
None => {
info!("error in reading entry_height");
}
}
0
}
2019-03-02 10:25:16 -08:00
fn get_storage_blockhash(bank: &Bank, account: Pubkey) -> Hash {
if let Some(storage_system_account) = bank.get_account(&account) {
2019-02-19 15:54:38 -08:00
let state = deserialize(&storage_system_account.userdata);
if let Ok(state) = state {
let state: storage_program::StorageProgramState = state;
return state.hash;
2019-02-19 15:54:38 -08:00
}
}
Hash::default()
}
#[test]
fn test_bank_storage() {
let (genesis_block, alice) = GenesisBlock::new(1000);
let bank = Bank::new(&genesis_block);
let bob = Keypair::new();
let jack = Keypair::new();
let jill = Keypair::new();
let x = 42;
2019-03-02 10:25:16 -08:00
let blockhash = genesis_block.hash();
2019-02-19 15:54:38 -08:00
let x2 = x * 2;
2019-03-02 10:25:16 -08:00
let storage_blockhash = hash(&[x2]);
2019-02-19 15:54:38 -08:00
2019-03-02 10:25:16 -08:00
bank.register_tick(&blockhash);
2019-02-19 15:54:38 -08:00
2019-03-02 10:25:16 -08:00
bank.transfer(10, &alice, jill.pubkey(), blockhash).unwrap();
2019-02-19 15:54:38 -08:00
2019-03-02 10:25:16 -08:00
bank.transfer(10, &alice, bob.pubkey(), blockhash).unwrap();
bank.transfer(10, &alice, jack.pubkey(), blockhash).unwrap();
2019-02-19 15:54:38 -08:00
let tx = SystemTransaction::new_program_account(
&alice,
bob.pubkey(),
2019-03-02 10:25:16 -08:00
blockhash,
1,
4 * 1024,
storage_program::id(),
0,
);
bank.process_transaction(&tx).unwrap();
2019-03-02 10:25:16 -08:00
let tx = StorageTransaction::new_advertise_recent_blockhash(
2019-02-19 15:54:38 -08:00
&bob,
2019-03-02 10:25:16 -08:00
storage_blockhash,
blockhash,
2019-02-19 15:54:38 -08:00
ENTRIES_PER_SEGMENT,
);
bank.process_transaction(&tx).unwrap();
// TODO: This triggers a ProgramError(0, InvalidArgument). Why?
// Oddly, the assertions below it succeed without running this code.
//let entry_height = 0;
//let tx = StorageTransaction::new_mining_proof(
// &jack,
// Hash::default(),
2019-03-02 10:25:16 -08:00
// blockhash,
// entry_height,
// Signature::default(),
//);
//let _result = bank.process_transaction(&tx).unwrap();
2019-02-19 15:54:38 -08:00
assert_eq!(
get_storage_entry_height(&bank, bob.pubkey()),
ENTRIES_PER_SEGMENT
);
2019-03-02 10:20:10 -08:00
assert_eq!(
2019-03-02 10:25:16 -08:00
get_storage_blockhash(&bank, bob.pubkey()),
storage_blockhash
2019-03-02 10:20:10 -08:00
);
2019-02-19 15:54:38 -08:00
}