Rename programs to instruction_processors (#3789)

* Rename programs to instruction_processors

* Updates around the code base to support instruction_processors rename

* Kabab instruction_processors

* Update Cargo.toml files and scripts to use instruction-processors

* Update Cargo.toml to use instruction-processors

* Update CI scripts to use instruction-processors
This commit is contained in:
Amr Ali
2019-04-16 22:39:00 +02:00
committed by GitHub
parent f73d38739a
commit 34344982a9
93 changed files with 44 additions and 44 deletions

View File

@@ -1,24 +0,0 @@
[package]
name = "solana-storage-api"
version = "0.14.0"
description = "Solana Storage program API"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
edition = "2018"
[dependencies]
bincode = "1.1.3"
log = "0.4.2"
serde = "1.0.90"
serde_derive = "1.0.90"
solana-logger = { path = "../../logger", version = "0.14.0" }
solana-sdk = { path = "../../sdk", version = "0.14.0" }
[dev-dependencies]
solana-runtime = { path = "../../runtime", version = "0.14.0" }
[lib]
name = "solana_storage_api"
crate-type = ["lib"]

View File

@@ -1,24 +0,0 @@
pub mod storage_contract;
pub mod storage_instruction;
pub mod storage_processor;
use solana_sdk::pubkey::Pubkey;
pub const ENTRIES_PER_SEGMENT: u64 = 16;
pub fn get_segment_from_entry(entry_height: u64) -> usize {
(entry_height / ENTRIES_PER_SEGMENT) as usize
}
const STORAGE_PROGRAM_ID: [u8; 32] = [
130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
];
pub fn check_id(program_id: &Pubkey) -> bool {
program_id.as_ref() == STORAGE_PROGRAM_ID
}
pub fn id() -> Pubkey {
Pubkey::new(&STORAGE_PROGRAM_ID)
}

View File

@@ -1,412 +0,0 @@
use crate::{get_segment_from_entry, ENTRIES_PER_SEGMENT};
use log::*;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::account::Account;
use solana_sdk::hash::Hash;
use solana_sdk::instruction::InstructionError;
use solana_sdk::instruction_processor_utils::State;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature;
use std::cmp;
pub const TOTAL_VALIDATOR_REWARDS: u64 = 1;
pub const TOTAL_REPLICATOR_REWARDS: u64 = 1;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum ProofStatus {
Skipped,
Valid,
NotValid,
}
impl Default for ProofStatus {
fn default() -> Self {
ProofStatus::Skipped
}
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct Proof {
pub id: Pubkey,
pub signature: Signature,
pub sha_state: Hash,
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct CheckedProof {
pub proof: Proof,
pub status: ProofStatus,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum StorageContract {
//don't move this
Default,
ValidatorStorage {
entry_height: u64,
hash: Hash,
lockout_validations: Vec<Vec<CheckedProof>>,
reward_validations: Vec<Vec<CheckedProof>>,
},
ReplicatorStorage {
proofs: Vec<Proof>,
reward_validations: Vec<Vec<CheckedProof>>,
},
}
pub struct StorageAccount<'a> {
account: &'a mut Account,
}
impl<'a> StorageAccount<'a> {
pub fn new(account: &'a mut Account) -> Self {
Self { account }
}
pub fn submit_mining_proof(
&mut self,
id: Pubkey,
sha_state: Hash,
entry_height: u64,
signature: Signature,
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
if let StorageContract::Default = storage_contract {
*storage_contract = StorageContract::ReplicatorStorage {
proofs: vec![],
reward_validations: vec![],
};
};
if let StorageContract::ReplicatorStorage { proofs, .. } = &mut storage_contract {
let segment_index = get_segment_from_entry(entry_height);
if segment_index > proofs.len() || proofs.is_empty() {
proofs.resize(cmp::max(1, segment_index), Proof::default());
}
if segment_index > proofs.len() {
// only possible if usize max < u64 max
return Err(InstructionError::InvalidArgument);
}
debug!(
"Mining proof submitted with contract {:?} entry_height: {}",
sha_state, entry_height
);
let proof_info = Proof {
id,
sha_state,
signature,
};
proofs[segment_index] = proof_info;
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
}
}
pub fn advertise_storage_recent_blockhash(
&mut self,
hash: Hash,
entry_height: u64,
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
if let StorageContract::Default = storage_contract {
*storage_contract = StorageContract::ValidatorStorage {
entry_height: 0,
hash: Hash::default(),
lockout_validations: vec![],
reward_validations: vec![],
};
};
if let StorageContract::ValidatorStorage {
entry_height: state_entry_height,
hash: state_hash,
reward_validations,
lockout_validations,
} = &mut storage_contract
{
let original_segments = *state_entry_height / ENTRIES_PER_SEGMENT;
let segments = entry_height / ENTRIES_PER_SEGMENT;
debug!(
"advertise new last id segments: {} orig: {}",
segments, original_segments
);
if segments <= original_segments {
return Err(InstructionError::InvalidArgument);
}
*state_entry_height = entry_height;
*state_hash = hash;
// move lockout_validations to reward_validations
*reward_validations = lockout_validations.clone();
lockout_validations.clear();
lockout_validations.resize(segments as usize, Vec::new());
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
}
}
pub fn proof_validation(
&mut self,
entry_height: u64,
proofs: Vec<CheckedProof>,
replicator_accounts: &mut [StorageAccount],
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
if let StorageContract::Default = storage_contract {
*storage_contract = StorageContract::ValidatorStorage {
entry_height: 0,
hash: Hash::default(),
lockout_validations: vec![],
reward_validations: vec![],
};
};
if let StorageContract::ValidatorStorage {
entry_height: current_entry_height,
lockout_validations,
..
} = &mut storage_contract
{
if entry_height >= *current_entry_height {
return Err(InstructionError::InvalidArgument);
}
let segment_index = get_segment_from_entry(entry_height);
let mut previous_proofs = replicator_accounts
.iter_mut()
.filter_map(|account| {
account
.account
.state()
.ok()
.map(move |contract| match contract {
StorageContract::ReplicatorStorage { proofs, .. } => {
Some((account, proofs[segment_index].clone()))
}
_ => None,
})
})
.flatten()
.collect::<Vec<_>>();
if previous_proofs.len() != proofs.len() {
// don't have all the accounts to validate the proofs against
return Err(InstructionError::InvalidArgument);
}
let mut valid_proofs: Vec<_> = proofs
.into_iter()
.enumerate()
.filter_map(|(i, entry)| {
let (account, proof) = &mut previous_proofs[i];
if process_validation(account, segment_index, &proof, &entry).is_ok() {
Some(entry)
} else {
None
}
})
.collect();
// allow validators to store successful validations
lockout_validations[segment_index].append(&mut valid_proofs);
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
}
}
pub fn claim_storage_reward(
&mut self,
entry_height: u64,
tick_height: u64,
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
if let StorageContract::Default = storage_contract {
Err(InstructionError::InvalidArgument)?
};
if let StorageContract::ValidatorStorage {
reward_validations, ..
} = &mut storage_contract
{
let claims_index = get_segment_from_entry(entry_height);
let _num_validations = count_valid_proofs(&reward_validations[claims_index]);
// TODO can't just create lamports out of thin air
// self.account.lamports += TOTAL_VALIDATOR_REWARDS * num_validations;
reward_validations.clear();
self.account.set_state(storage_contract)
} else if let StorageContract::ReplicatorStorage {
reward_validations, ..
} = &mut storage_contract
{
// if current tick height is a full segment away? then allow reward collection
// storage needs to move to tick heights too, until then this makes little sense
let current_index = get_segment_from_entry(tick_height);
let claims_index = get_segment_from_entry(entry_height);
if current_index <= claims_index || claims_index >= reward_validations.len() {
debug!(
"current {:?}, claim {:?}, rewards {:?}",
current_index,
claims_index,
reward_validations.len()
);
return Err(InstructionError::InvalidArgument);
}
let _num_validations = count_valid_proofs(&reward_validations[claims_index]);
// TODO can't just create lamports out of thin air
// self.account.lamports += num_validations
// * TOTAL_REPLICATOR_REWARDS
// * (num_validations / reward_validations[claims_index].len() as u64);
reward_validations.clear();
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
}
}
}
/// Store the result of a proof validation into the replicator account
fn store_validation_result(
storage_account: &mut StorageAccount,
segment_index: usize,
status: ProofStatus,
) -> Result<(), InstructionError> {
let mut storage_contract = storage_account.account.state()?;
match &mut storage_contract {
StorageContract::ReplicatorStorage {
proofs,
reward_validations,
..
} => {
if segment_index >= proofs.len() {
return Err(InstructionError::InvalidAccountData);
}
if segment_index > reward_validations.len() || reward_validations.is_empty() {
reward_validations.resize(cmp::max(1, segment_index), vec![]);
}
let result = proofs[segment_index].clone();
reward_validations[segment_index].push(CheckedProof {
proof: result,
status,
});
}
_ => return Err(InstructionError::InvalidAccountData),
}
storage_account.account.set_state(&storage_contract)
}
fn count_valid_proofs(proofs: &[CheckedProof]) -> u64 {
let mut num = 0;
for proof in proofs {
if let ProofStatus::Valid = proof.status {
num += 1;
}
}
num
}
fn process_validation(
account: &mut StorageAccount,
segment_index: usize,
proof: &Proof,
checked_proof: &CheckedProof,
) -> Result<(), InstructionError> {
store_validation_result(account, segment_index, checked_proof.status.clone())?;
if proof.signature != checked_proof.proof.signature
|| checked_proof.status != ProofStatus::Valid
{
return Err(InstructionError::GenericError);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::id;
#[test]
fn test_account_data() {
solana_logger::setup();
let mut account = Account::default();
account.data.resize(4 * 1024, 0);
let storage_account = StorageAccount::new(&mut account);
// pretend it's a validator op code
let mut contract = storage_account.account.state().unwrap();
if let StorageContract::ValidatorStorage { .. } = contract {
assert!(true)
}
if let StorageContract::ReplicatorStorage { .. } = &mut contract {
panic!("Contract should not decode into two types");
}
contract = StorageContract::ValidatorStorage {
entry_height: 0,
hash: Hash::default(),
lockout_validations: vec![],
reward_validations: vec![],
};
storage_account.account.set_state(&contract).unwrap();
if let StorageContract::ReplicatorStorage { .. } = contract {
panic!("Wrong contract type");
}
contract = StorageContract::ReplicatorStorage {
proofs: vec![],
reward_validations: vec![],
};
storage_account.account.set_state(&contract).unwrap();
if let StorageContract::ValidatorStorage { .. } = contract {
panic!("Wrong contract type");
}
}
#[test]
fn test_process_validation() {
let mut account = StorageAccount {
account: &mut Account {
lamports: 0,
data: vec![],
owner: id(),
executable: false,
},
};
let segment_index = 0_usize;
let proof = Proof {
id: Pubkey::default(),
signature: Signature::default(),
sha_state: Hash::default(),
};
let mut checked_proof = CheckedProof {
proof: proof.clone(),
status: ProofStatus::Valid,
};
// account has no space
process_validation(&mut account, segment_index, &proof, &checked_proof).unwrap_err();
account.account.data.resize(4 * 1024, 0);
let storage_contract = &mut account.account.state().unwrap();
if let StorageContract::Default = storage_contract {
*storage_contract = StorageContract::ReplicatorStorage {
proofs: vec![proof.clone()],
reward_validations: vec![],
};
};
account.account.set_state(storage_contract).unwrap();
// proof is valid
process_validation(&mut account, segment_index, &proof, &checked_proof).unwrap();
checked_proof.status = ProofStatus::NotValid;
// proof failed verification
process_validation(&mut account, segment_index, &proof, &checked_proof).unwrap_err();
}
}

View File

@@ -1,78 +0,0 @@
use crate::id;
use crate::storage_contract::CheckedProof;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::hash::Hash;
use solana_sdk::instruction::{AccountMeta, Instruction};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature;
// TODO maybe split this into StorageReplicator and StorageValidator
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum StorageInstruction {
SubmitMiningProof {
sha_state: Hash,
entry_height: u64,
signature: Signature,
},
AdvertiseStorageRecentBlockhash {
hash: Hash,
entry_height: u64,
},
ClaimStorageReward {
entry_height: u64,
},
ProofValidation {
entry_height: u64,
proofs: Vec<CheckedProof>,
},
}
pub fn mining_proof(
from_pubkey: &Pubkey,
sha_state: Hash,
entry_height: u64,
signature: Signature,
) -> Instruction {
let storage_instruction = StorageInstruction::SubmitMiningProof {
sha_state,
entry_height,
signature,
};
let account_metas = vec![AccountMeta::new(*from_pubkey, true)];
Instruction::new(id(), &storage_instruction, account_metas)
}
pub fn advertise_recent_blockhash(
from_pubkey: &Pubkey,
storage_hash: Hash,
entry_height: u64,
) -> Instruction {
let storage_instruction = StorageInstruction::AdvertiseStorageRecentBlockhash {
hash: storage_hash,
entry_height,
};
let account_metas = vec![AccountMeta::new(*from_pubkey, true)];
Instruction::new(id(), &storage_instruction, account_metas)
}
pub fn proof_validation(
from_pubkey: &Pubkey,
entry_height: u64,
proofs: Vec<CheckedProof>,
) -> Instruction {
let mut account_metas = vec![AccountMeta::new(*from_pubkey, true)];
proofs.iter().for_each(|checked_proof| {
account_metas.push(AccountMeta::new(checked_proof.proof.id, false))
});
let storage_instruction = StorageInstruction::ProofValidation {
entry_height,
proofs,
};
Instruction::new(id(), &storage_instruction, account_metas)
}
pub fn reward_claim(from_pubkey: &Pubkey, entry_height: u64) -> Instruction {
let storage_instruction = StorageInstruction::ClaimStorageReward { entry_height };
let account_metas = vec![AccountMeta::new(*from_pubkey, true)];
Instruction::new(id(), &storage_instruction, account_metas)
}

View File

@@ -1,387 +0,0 @@
//! storage program
//! Receive mining proofs from miners, validate the answers
//! and give reward for good proofs.
use crate::storage_contract::StorageAccount;
use crate::storage_instruction::StorageInstruction;
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError;
use solana_sdk::pubkey::Pubkey;
pub fn process_instruction(
_program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
tick_height: u64,
) -> Result<(), InstructionError> {
solana_logger::setup();
let num_keyed_accounts = keyed_accounts.len();
let (me, rest) = keyed_accounts.split_at_mut(1);
// accounts_keys[0] must be signed
let storage_account_pubkey = me[0].signer_key();
if storage_account_pubkey.is_none() {
info!("account[0] is unsigned");
Err(InstructionError::MissingRequiredSignature)?;
}
let storage_account_pubkey = *storage_account_pubkey.unwrap();
let mut storage_account = StorageAccount::new(&mut me[0].account);
let mut rest: Vec<_> = rest
.iter_mut()
.map(|keyed_account| StorageAccount::new(&mut keyed_account.account))
.collect();
match bincode::deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? {
StorageInstruction::SubmitMiningProof {
sha_state,
entry_height,
signature,
} => {
if num_keyed_accounts != 1 {
Err(InstructionError::InvalidArgument)?;
}
storage_account.submit_mining_proof(
storage_account_pubkey,
sha_state,
entry_height,
signature,
)
}
StorageInstruction::AdvertiseStorageRecentBlockhash { hash, entry_height } => {
if num_keyed_accounts != 1 {
// keyed_accounts[0] should be the main storage key
// to access its data
Err(InstructionError::InvalidArgument)?;
}
storage_account.advertise_storage_recent_blockhash(hash, entry_height)
}
StorageInstruction::ClaimStorageReward { entry_height } => {
if num_keyed_accounts != 1 {
// keyed_accounts[0] should be the main storage key
// to access its data
Err(InstructionError::InvalidArgument)?;
}
storage_account.claim_storage_reward(entry_height, tick_height)
}
StorageInstruction::ProofValidation {
entry_height,
proofs,
} => {
if num_keyed_accounts == 1 {
// have to have at least 1 replicator to do any verification
Err(InstructionError::InvalidArgument)?;
}
storage_account.proof_validation(entry_height, proofs, &mut rest)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::id;
use crate::storage_contract::{CheckedProof, Proof, ProofStatus, StorageContract};
use crate::storage_instruction;
use crate::ENTRIES_PER_SEGMENT;
use bincode::deserialize;
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::account::{create_keyed_accounts, Account};
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::hash::{hash, Hash};
use solana_sdk::instruction::Instruction;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::system_instruction;
fn test_instruction(
ix: &Instruction,
program_accounts: &mut [Account],
) -> Result<(), InstructionError> {
let mut keyed_accounts: Vec<_> = ix
.accounts
.iter()
.zip(program_accounts.iter_mut())
.map(|(account_meta, account)| {
KeyedAccount::new(&account_meta.pubkey, account_meta.is_signer, account)
})
.collect();
let ret = process_instruction(&id(), &mut keyed_accounts, &ix.data, 42);
info!("ret: {:?}", ret);
ret
}
#[test]
fn test_storage_tx() {
let pubkey = Pubkey::new_rand();
let mut accounts = [(pubkey, Account::default())];
let mut keyed_accounts = create_keyed_accounts(&mut accounts);
assert!(process_instruction(&id(), &mut keyed_accounts, &[], 42).is_err());
}
#[test]
fn test_serialize_overflow() {
let pubkey = Pubkey::new_rand();
let mut keyed_accounts = Vec::new();
let mut user_account = Account::default();
keyed_accounts.push(KeyedAccount::new(&pubkey, true, &mut user_account));
let ix = storage_instruction::advertise_recent_blockhash(
&pubkey,
Hash::default(),
ENTRIES_PER_SEGMENT,
);
assert_eq!(
process_instruction(&id(), &mut keyed_accounts, &ix.data, 42),
Err(InstructionError::InvalidAccountData)
);
}
#[test]
fn test_invalid_accounts_len() {
let pubkey = Pubkey::new_rand();
let mut accounts = [Account::default()];
let ix =
storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default());
assert!(test_instruction(&ix, &mut accounts).is_err());
let mut accounts = [Account::default(), Account::default(), Account::default()];
assert!(test_instruction(&ix, &mut accounts).is_err());
}
#[test]
fn test_submit_mining_invalid_entry_height() {
solana_logger::setup();
let pubkey = Pubkey::new_rand();
let mut accounts = [Account::default(), Account::default()];
accounts[1].data.resize(16 * 1024, 0);
let ix =
storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default());
// Haven't seen a transaction to roll over the epoch, so this should fail
assert!(test_instruction(&ix, &mut accounts).is_err());
}
#[test]
fn test_submit_mining_ok() {
solana_logger::setup();
let pubkey = Pubkey::new_rand();
let mut accounts = [Account::default(), Account::default()];
accounts[0].data.resize(16 * 1024, 0);
let ix =
storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default());
test_instruction(&ix, &mut accounts).unwrap();
}
#[test]
#[ignore]
fn test_validate_mining() {
solana_logger::setup();
let (genesis_block, mint_keypair) = GenesisBlock::new(1000);
let mint_pubkey = mint_keypair.pubkey();
let replicator_keypair = Keypair::new();
let replicator = replicator_keypair.pubkey();
let validator_keypair = Keypair::new();
let validator = validator_keypair.pubkey();
let mut bank = Bank::new(&genesis_block);
bank.add_instruction_processor(id(), process_instruction);
let entry_height = 0;
let bank_client = BankClient::new(bank);
let ix = system_instruction::create_account(&mint_pubkey, &validator, 10, 4 * 1042, &id());
bank_client.send_instruction(&mint_keypair, ix).unwrap();
let ix = system_instruction::create_account(&mint_pubkey, &replicator, 10, 4 * 1042, &id());
bank_client.send_instruction(&mint_keypair, ix).unwrap();
let ix = storage_instruction::advertise_recent_blockhash(
&validator,
Hash::default(),
ENTRIES_PER_SEGMENT,
);
bank_client
.send_instruction(&validator_keypair, ix)
.unwrap();
let ix = storage_instruction::mining_proof(
&replicator,
Hash::default(),
entry_height,
Signature::default(),
);
bank_client
.send_instruction(&replicator_keypair, ix)
.unwrap();
let ix = storage_instruction::advertise_recent_blockhash(
&validator,
Hash::default(),
ENTRIES_PER_SEGMENT * 2,
);
bank_client
.send_instruction(&validator_keypair, ix)
.unwrap();
let ix = storage_instruction::proof_validation(
&validator,
entry_height,
vec![CheckedProof {
proof: Proof {
id: replicator,
signature: Signature::default(),
sha_state: Hash::default(),
},
status: ProofStatus::Valid,
}],
);
bank_client
.send_instruction(&validator_keypair, ix)
.unwrap();
let ix = storage_instruction::advertise_recent_blockhash(
&validator,
Hash::default(),
ENTRIES_PER_SEGMENT * 3,
);
bank_client
.send_instruction(&validator_keypair, ix)
.unwrap();
let ix = storage_instruction::reward_claim(&validator, entry_height);
bank_client
.send_instruction(&validator_keypair, ix)
.unwrap();
// TODO enable when rewards are working
// assert_eq!(bank_client.get_balance(&validator).unwrap(), TOTAL_VALIDATOR_REWARDS);
// TODO extend BankClient with a method to force a block boundary
// tick the bank into the next storage epoch so that rewards can be claimed
//for _ in 0..=ENTRIES_PER_SEGMENT {
// bank.register_tick(&bank.last_blockhash());
//}
let ix = storage_instruction::reward_claim(&replicator, entry_height);
bank_client
.send_instruction(&replicator_keypair, ix)
.unwrap();
// TODO enable when rewards are working
// assert_eq!(bank_client.get_balance(&replicator).unwrap(), TOTAL_REPLICATOR_REWARDS);
}
fn get_storage_entry_height<C: SyncClient>(client: &C, account: &Pubkey) -> u64 {
match client.get_account_data(&account).unwrap() {
Some(storage_system_account_data) => {
let contract = deserialize(&storage_system_account_data);
if let Ok(contract) = contract {
match contract {
StorageContract::ValidatorStorage { entry_height, .. } => {
return entry_height;
}
_ => info!("error in reading entry_height"),
}
}
}
None => {
info!("error in reading entry_height");
}
}
0
}
fn get_storage_blockhash<C: SyncClient>(client: &C, account: &Pubkey) -> Hash {
if let Some(storage_system_account_data) = client.get_account_data(&account).unwrap() {
let contract = deserialize(&storage_system_account_data);
if let Ok(contract) = contract {
match contract {
StorageContract::ValidatorStorage { hash, .. } => {
return hash;
}
_ => (),
}
}
}
Hash::default()
}
#[test]
fn test_bank_storage() {
let (genesis_block, mint_keypair) = GenesisBlock::new(1000);
let mint_pubkey = mint_keypair.pubkey();
let replicator_keypair = Keypair::new();
let replicator_pubkey = replicator_keypair.pubkey();
let validator_keypair = Keypair::new();
let validator_pubkey = validator_keypair.pubkey();
let mut bank = Bank::new(&genesis_block);
bank.add_instruction_processor(id(), process_instruction);
let bank_client = BankClient::new(bank);
let x = 42;
let x2 = x * 2;
let storage_blockhash = hash(&[x2]);
bank_client
.transfer(10, &mint_keypair, &replicator_pubkey)
.unwrap();
let ix = system_instruction::create_account(
&mint_pubkey,
&replicator_pubkey,
1,
4 * 1024,
&id(),
);
bank_client.send_instruction(&mint_keypair, ix).unwrap();
let ix =
system_instruction::create_account(&mint_pubkey, &validator_pubkey, 1, 4 * 1024, &id());
bank_client.send_instruction(&mint_keypair, ix).unwrap();
let ix = storage_instruction::advertise_recent_blockhash(
&validator_pubkey,
storage_blockhash,
ENTRIES_PER_SEGMENT,
);
bank_client
.send_instruction(&validator_keypair, ix)
.unwrap();
let entry_height = 0;
let ix = storage_instruction::mining_proof(
&replicator_pubkey,
Hash::default(),
entry_height,
Signature::default(),
);
let _result = bank_client
.send_instruction(&replicator_keypair, ix)
.unwrap();
assert_eq!(
get_storage_entry_height(&bank_client, &validator_pubkey),
ENTRIES_PER_SEGMENT
);
assert_eq!(
get_storage_blockhash(&bank_client, &validator_pubkey),
storage_blockhash
);
}
}