diff --git a/programs/storage_api/src/storage_contract.rs b/programs/storage_api/src/storage_contract.rs index ccb7839a14..e144211f5f 100644 --- a/programs/storage_api/src/storage_contract.rs +++ b/programs/storage_api/src/storage_contract.rs @@ -17,17 +17,16 @@ impl Default for ProofStatus { } #[derive(Default, Debug, Serialize, Deserialize, Clone)] -pub struct ProofInfo { +pub struct Proof { pub id: Pubkey, pub signature: Signature, pub sha_state: Hash, - pub status: ProofStatus, } #[derive(Default, Debug, Serialize, Deserialize, Clone)] -pub struct ValidationInfo { - pub id: Pubkey, - pub proof_mask: Vec, +pub struct CheckedProof { + pub proof: Proof, + pub status: ProofStatus, } #[derive(Debug, Serialize, Deserialize)] @@ -38,11 +37,11 @@ pub enum StorageContract { ValidatorStorage { entry_height: u64, hash: Hash, - lockout_validations: Vec>, - reward_validations: Vec>, + lockout_validations: Vec>, + reward_validations: Vec>, }, ReplicatorStorage { - proofs: Vec, - reward_validations: Vec>, + proofs: Vec, + reward_validations: Vec>, }, } diff --git a/programs/storage_api/src/storage_instruction.rs b/programs/storage_api/src/storage_instruction.rs index e633433d11..7fd22b1908 100644 --- a/programs/storage_api/src/storage_instruction.rs +++ b/programs/storage_api/src/storage_instruction.rs @@ -1,5 +1,5 @@ use crate::id; -use crate::storage_contract::ProofInfo; +use crate::storage_contract::CheckedProof; use serde_derive::{Deserialize, Serialize}; use solana_sdk::hash::Hash; use solana_sdk::instruction::{AccountMeta, Instruction}; @@ -23,7 +23,7 @@ pub enum StorageInstruction { }, ProofValidation { entry_height: u64, - proofs: Vec, + proofs: Vec, }, } @@ -58,12 +58,12 @@ pub fn advertise_recent_blockhash( pub fn proof_validation( from_pubkey: &Pubkey, entry_height: u64, - proofs: Vec, + proofs: Vec, ) -> Instruction { let mut account_metas = vec![AccountMeta::new(*from_pubkey, true)]; - proofs - .iter() - .for_each(|proof| account_metas.push(AccountMeta::new(proof.id, false))); + proofs.iter().for_each(|checked_proof| { + account_metas.push(AccountMeta::new(checked_proof.proof.id, false)) + }); let storage_instruction = StorageInstruction::ProofValidation { entry_height, proofs, diff --git a/programs/storage_api/src/storage_processor.rs b/programs/storage_api/src/storage_processor.rs index d58a2f4b2d..e5f5150aba 100644 --- a/programs/storage_api/src/storage_processor.rs +++ b/programs/storage_api/src/storage_processor.rs @@ -2,7 +2,7 @@ //! Receive mining proofs from miners, validate the answers //! and give reward for good proofs. -use crate::storage_contract::{ProofInfo, ProofStatus, StorageContract}; +use crate::storage_contract::{CheckedProof, Proof, ProofStatus, StorageContract}; use crate::storage_instruction::StorageInstruction; use crate::{get_segment_from_entry, ENTRIES_PER_SEGMENT}; use log::*; @@ -17,7 +17,7 @@ use std::cmp; pub const TOTAL_VALIDATOR_REWARDS: u64 = 1; pub const TOTAL_REPLICATOR_REWARDS: u64 = 1; -fn count_valid_proofs(proofs: &[ProofInfo]) -> u64 { +fn count_valid_proofs(proofs: &[CheckedProof]) -> u64 { let mut num = 0; for proof in proofs { if let ProofStatus::Valid = proof.status { @@ -92,9 +92,11 @@ fn store_validation_result( if segment_index > reward_validations.len() || reward_validations.is_empty() { reward_validations.resize(cmp::max(1, segment_index), vec![]); } - let mut result = proofs[segment_index].clone(); - result.status = status; - reward_validations[segment_index].push(result); + let result = proofs[segment_index].clone(); + reward_validations[segment_index].push(CheckedProof { + proof: result, + status, + }); } _ => return Err(InstructionError::InvalidAccountData), } @@ -134,7 +136,7 @@ pub fn process_instruction( let segment_index = get_segment_from_entry(entry_height); if segment_index > proofs.len() || proofs.is_empty() { - proofs.resize(cmp::max(1, segment_index), ProofInfo::default()); + proofs.resize(cmp::max(1, segment_index), Proof::default()); } if segment_index > proofs.len() { @@ -147,12 +149,10 @@ pub fn process_instruction( sha_state, entry_height ); - let proof_info = ProofInfo { + let proof_info = Proof { id: *keyed_accounts[0].signer_key().unwrap(), sha_state, signature, - // don't care - status: ProofStatus::Skipped, }; proofs[segment_index] = proof_info; } else { @@ -281,7 +281,7 @@ pub fn process_instruction( .into_iter() .enumerate() .filter_map(|(i, entry)| { - if previous_proofs[i].1.signature != entry.signature + if previous_proofs[i].1.signature != entry.proof.signature || entry.status != ProofStatus::Valid { let _ = store_validation_result( @@ -416,14 +416,6 @@ mod tests { let mut accounts = [Account::default(), Account::default()]; accounts[0].data.resize(16 * 1024, 0); - let ix = storage_instruction::advertise_recent_blockhash( - &pubkey, - Hash::default(), - ENTRIES_PER_SEGMENT, - ); - - test_instruction(&ix, &mut accounts).unwrap(); - let ix = storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default()); @@ -481,10 +473,10 @@ mod tests { let entry_height = 0; let bank_client = BankClient::new(&bank); - let ix = SystemInstruction::new_account(&mint_pubkey, &validator, 10, 4 * 1042, &id()); + let ix = system_instruction::create_account(&mint_pubkey, &validator, 10, 4 * 1042, &id()); bank_client.process_instruction(&mint_keypair, ix).unwrap(); - let ix = SystemInstruction::new_account(&mint_pubkey, &replicator, 10, 4 * 1042, &id()); + let ix = system_instruction::create_account(&mint_pubkey, &replicator, 10, 4 * 1042, &id()); bank_client.process_instruction(&mint_keypair, ix).unwrap(); let ix = storage_instruction::advertise_recent_blockhash( @@ -519,10 +511,12 @@ mod tests { let ix = storage_instruction::proof_validation( &validator, entry_height, - vec![ProofInfo { - id: replicator, - signature: Signature::default(), - sha_state: Hash::default(), + vec![CheckedProof { + proof: Proof { + id: replicator, + signature: Signature::default(), + sha_state: Hash::default(), + }, status: ProofStatus::Valid, }], ); @@ -552,7 +546,7 @@ mod tests { bank.register_tick(&bank.last_blockhash()); } - let ix = StorageInstruction::new_reward_claim(&replicator, entry_height); + let ix = storage_instruction::reward_claim(&replicator, entry_height); bank_client .process_instruction(&replicator_keypair, ix) .unwrap(); @@ -630,13 +624,8 @@ mod tests { bank_client.process_instruction(&mint_keypair, ix).unwrap(); - let ix = system_instruction::create_account( - &mint_pubkey, - &validator_pubkey, - 1, - 4 * 1024, - &id(), - ); + let ix = + system_instruction::create_account(&mint_pubkey, &validator_pubkey, 1, 4 * 1024, &id()); bank_client.process_instruction(&mint_keypair, ix).unwrap(); @@ -651,7 +640,7 @@ mod tests { .unwrap(); let entry_height = 0; - let ix = storage_instruction::mining_proof() + let ix = storage_instruction::mining_proof( &replicator_pubkey, Hash::default(), entry_height,