solana/programs/storage/tests/storage.rs

105 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;
2019-03-12 14:38:29 -07:00
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::system_transaction::SystemTransaction;
2019-03-04 19:45:26 -08:00
use solana_storage_api::{StorageTransaction, ENTRIES_PER_SEGMENT};
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 {
2019-03-04 19:45:26 -08:00
let state: solana_storage_api::StorageProgramState = state;
2019-02-19 15:54:38 -08:00
return state.entry_height;
}
}
None => {
info!("error in reading entry_height");
}
}
0
}
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 {
2019-03-04 19:45:26 -08:00
let state: solana_storage_api::StorageProgramState = state;
return state.hash;
2019-02-19 15:54:38 -08:00
}
}
Hash::default()
}
#[test]
fn test_bank_storage() {
let (mut genesis_block, alice) = GenesisBlock::new(1000);
genesis_block.native_programs.push((
"solana_storage_program".to_string(),
solana_storage_api::id(),
));
2019-02-19 15:54:38 -08:00
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
bank.transfer(10, &alice, &jill.pubkey(), blockhash)
.unwrap();
2019-02-19 15:54:38 -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,
&solana_storage_api::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();
2019-03-12 14:38:29 -07:00
let entry_height = 0;
let tx = StorageTransaction::new_mining_proof(
&bob,
Hash::default(),
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!(
get_storage_blockhash(&bank, &bob.pubkey()),
2019-03-02 10:25:16 -08:00
storage_blockhash
2019-03-02 10:20:10 -08:00
);
2019-02-19 15:54:38 -08:00
}