2019-02-19 15:54:38 -08:00
|
|
|
use bincode::deserialize;
|
2019-02-27 20:57:01 -08:00
|
|
|
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};
|
2019-02-22 14:44:23 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-03-12 14:38:29 -07:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
|
2019-02-22 14:44:23 -08:00
|
|
|
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
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
fn get_storage_entry_height(bank: &Bank, account: &Pubkey) -> u64 {
|
2019-02-22 14:44:23 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
fn get_storage_blockhash(bank: &Bank, account: &Pubkey) -> Hash {
|
2019-02-22 14:44:23 -08:00
|
|
|
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;
|
2019-03-02 09:46:19 -08:00
|
|
|
return state.hash;
|
2019-02-19 15:54:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Hash::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bank_storage() {
|
2019-03-12 11:44:41 -07:00
|
|
|
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
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
bank.transfer(10, &alice, &jill.pubkey(), blockhash)
|
|
|
|
.unwrap();
|
2019-02-19 15:54:38 -08:00
|
|
|
|
2019-03-09 19:28:43 -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
|
|
|
|
2019-02-22 14:44:23 -08:00
|
|
|
let tx = SystemTransaction::new_program_account(
|
|
|
|
&alice,
|
2019-03-09 19:28:43 -08:00
|
|
|
&bob.pubkey(),
|
2019-03-02 10:25:16 -08:00
|
|
|
blockhash,
|
2019-02-22 14:44:23 -08:00
|
|
|
1,
|
|
|
|
4 * 1024,
|
2019-03-09 19:28:43 -08:00
|
|
|
&solana_storage_api::id(),
|
2019-02-22 14:44:23 -08:00
|
|
|
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
|
|
|
|
2019-02-22 14:44:23 -08:00
|
|
|
assert_eq!(
|
2019-03-09 19:28:43 -08:00
|
|
|
get_storage_entry_height(&bank, &bob.pubkey()),
|
2019-02-22 14:44:23 -08:00
|
|
|
ENTRIES_PER_SEGMENT
|
|
|
|
);
|
2019-03-02 10:20:10 -08:00
|
|
|
assert_eq!(
|
2019-03-09 19:28:43 -08:00
|
|
|
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
|
|
|
}
|