diff --git a/core/src/replicator.rs b/core/src/replicator.rs index 616725c6fd..81dcbe4480 100644 --- a/core/src/replicator.rs +++ b/core/src/replicator.rs @@ -22,7 +22,7 @@ use solana_client::thin_client::{create_client, ThinClient}; use solana_drone::drone::{request_airdrop_transaction, DRONE_PORT}; use solana_sdk::hash::{Hash, Hasher}; use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; -use solana_storage_api::StorageTransaction; +use solana_storage_api::storage_transaction::StorageTransaction; use std::fs::File; use std::io; use std::io::BufReader; diff --git a/core/src/storage_stage.rs b/core/src/storage_stage.rs index 4e99c8dfa7..fd0328cf94 100644 --- a/core/src/storage_stage.rs +++ b/core/src/storage_stage.rs @@ -17,7 +17,8 @@ use solana_sdk::hash::Hash; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, Signature}; use solana_sdk::transaction::Transaction; -use solana_storage_api::{self, StorageProgram, StorageTransaction}; +use solana_storage_api::storage_instruction::StorageInstruction; +use solana_storage_api::storage_transaction::StorageTransaction; use std::collections::HashSet; use std::io; use std::mem::size_of; @@ -374,7 +375,7 @@ impl StorageStage { for (i, program_id) in tx.program_ids.iter().enumerate() { if solana_storage_api::check_id(&program_id) { match deserialize(&tx.instructions[i].data) { - Ok(StorageProgram::SubmitMiningProof { + Ok(StorageInstruction::SubmitMiningProof { entry_height: proof_entry_height, signature, .. @@ -453,23 +454,17 @@ impl Service for StorageStage { #[cfg(test)] mod tests { + use super::*; use crate::blocktree::{create_new_tmp_ledger, Blocktree}; use crate::cluster_info::ClusterInfo; use crate::contact_info::ContactInfo; use crate::entry::{make_tiny_test_entries, Entry}; use crate::service::Service; - use crate::storage_stage::StorageState; - use crate::storage_stage::NUM_IDENTITIES; - use crate::storage_stage::{ - get_identity_index_from_signature, StorageStage, STORAGE_ROTATE_TEST_COUNT, - }; use rayon::prelude::*; use solana_sdk::genesis_block::GenesisBlock; - use solana_sdk::hash::Hash; - use solana_sdk::hash::Hasher; + use solana_sdk::hash::{Hash, Hasher}; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; - use solana_storage_api::StorageTransaction; use std::cmp::{max, min}; use std::fs::remove_dir_all; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; diff --git a/programs/storage_api/src/lib.rs b/programs/storage_api/src/lib.rs index a9435982c0..1c41828b27 100644 --- a/programs/storage_api/src/lib.rs +++ b/programs/storage_api/src/lib.rs @@ -1,10 +1,9 @@ +pub mod storage_instruction; pub mod storage_processor; +pub mod storage_state; +pub mod storage_transaction; -use serde_derive::{Deserialize, Serialize}; -use solana_sdk::hash::Hash; use solana_sdk::pubkey::Pubkey; -use solana_sdk::signature::{Keypair, Signature}; -use solana_sdk::transaction::Transaction; pub const ENTRIES_PER_SEGMENT: u64 = 16; @@ -12,58 +11,6 @@ pub fn get_segment_from_entry(entry_height: u64) -> usize { (entry_height / ENTRIES_PER_SEGMENT) as usize } -#[derive(Debug, Serialize, Deserialize, Clone)] -pub enum ProofStatus { - Valid, - NotValid, - Skipped, -} - -#[derive(Default, Debug, Serialize, Deserialize, Clone)] -pub struct ProofInfo { - pub id: Pubkey, - pub signature: Signature, - pub sha_state: Hash, -} - -#[derive(Default, Debug, Serialize, Deserialize, Clone)] -pub struct ValidationInfo { - pub id: Pubkey, - pub proof_mask: Vec, -} - -#[derive(Default, Debug, Serialize, Deserialize)] -pub struct StorageProgramState { - pub entry_height: u64, - pub hash: Hash, - - pub proofs: Vec>, - pub previous_proofs: Vec>, - - pub lockout_validations: Vec>, - pub reward_validations: Vec>, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub enum StorageProgram { - SubmitMiningProof { - sha_state: Hash, - entry_height: u64, - signature: Signature, - }, - AdvertiseStorageRecentBlockhash { - hash: Hash, - entry_height: u64, - }, - ClaimStorageReward { - entry_height: u64, - }, - ProofValidation { - entry_height: u64, - proof_mask: Vec, - }, -} - 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, @@ -76,57 +23,3 @@ pub fn check_id(program_id: &Pubkey) -> bool { pub fn id() -> Pubkey { Pubkey::new(&STORAGE_PROGRAM_ID) } - -pub struct StorageTransaction {} - -impl StorageTransaction { - pub fn new_mining_proof( - from_keypair: &Keypair, - sha_state: Hash, - recent_blockhash: Hash, - entry_height: u64, - signature: Signature, - ) -> Transaction { - let program = StorageProgram::SubmitMiningProof { - sha_state, - entry_height, - signature, - }; - Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) - } - - pub fn new_advertise_recent_blockhash( - from_keypair: &Keypair, - storage_hash: Hash, - recent_blockhash: Hash, - entry_height: u64, - ) -> Transaction { - let program = StorageProgram::AdvertiseStorageRecentBlockhash { - hash: storage_hash, - entry_height, - }; - Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) - } - - pub fn new_proof_validation( - from_keypair: &Keypair, - recent_blockhash: Hash, - entry_height: u64, - proof_mask: Vec, - ) -> Transaction { - let program = StorageProgram::ProofValidation { - entry_height, - proof_mask, - }; - Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) - } - - pub fn new_reward_claim( - from_keypair: &Keypair, - recent_blockhash: Hash, - entry_height: u64, - ) -> Transaction { - let program = StorageProgram::ClaimStorageReward { entry_height }; - Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) - } -} diff --git a/programs/storage_api/src/storage_instruction.rs b/programs/storage_api/src/storage_instruction.rs new file mode 100644 index 0000000000..c93f7516ea --- /dev/null +++ b/programs/storage_api/src/storage_instruction.rs @@ -0,0 +1,24 @@ +use crate::storage_state::ProofStatus; +use serde_derive::{Deserialize, Serialize}; +use solana_sdk::hash::Hash; +use solana_sdk::signature::Signature; + +#[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, + proof_mask: Vec, + }, +} diff --git a/programs/storage_api/src/storage_processor.rs b/programs/storage_api/src/storage_processor.rs index cf360c606c..6d13403958 100644 --- a/programs/storage_api/src/storage_processor.rs +++ b/programs/storage_api/src/storage_processor.rs @@ -2,7 +2,9 @@ //! Receive mining proofs from miners, validate the answers //! and give reward for good proofs. -use crate::*; +use crate::storage_instruction::StorageInstruction; +use crate::storage_state::{ProofInfo, ProofStatus, StorageState, ValidationInfo}; +use crate::{get_segment_from_entry, ENTRIES_PER_SEGMENT}; use log::*; use solana_sdk::account::KeyedAccount; use solana_sdk::instruction::InstructionError; @@ -47,7 +49,7 @@ pub fn process_instruction( { storage_account_state } else { - StorageProgramState::default() + StorageState::default() }; debug!( @@ -55,7 +57,7 @@ pub fn process_instruction( storage_account_state.entry_height ); match syscall { - StorageProgram::SubmitMiningProof { + StorageInstruction::SubmitMiningProof { sha_state, entry_height, signature, @@ -79,7 +81,7 @@ pub fn process_instruction( }; storage_account_state.proofs[segment_index].push(proof_info); } - StorageProgram::AdvertiseStorageRecentBlockhash { hash, entry_height } => { + StorageInstruction::AdvertiseStorageRecentBlockhash { hash, entry_height } => { let original_segments = storage_account_state.entry_height / ENTRIES_PER_SEGMENT; let segments = entry_height / ENTRIES_PER_SEGMENT; debug!( @@ -108,7 +110,7 @@ pub fn process_instruction( .lockout_validations .resize(segments as usize, Vec::new()); } - StorageProgram::ProofValidation { + StorageInstruction::ProofValidation { entry_height, proof_mask, } => { @@ -134,7 +136,7 @@ pub fn process_instruction( }; storage_account_state.lockout_validations[segment_index].push(info); } - StorageProgram::ClaimStorageReward { entry_height } => { + StorageInstruction::ClaimStorageReward { entry_height } => { let claims_index = get_segment_from_entry(entry_height); let account_key = keyed_accounts[0].signer_key().unwrap(); let mut num_validations = 0; @@ -173,7 +175,10 @@ pub fn process_instruction( #[cfg(test)] mod tests { use super::*; - use crate::{ProofStatus, StorageTransaction, ENTRIES_PER_SEGMENT}; + use crate::id; + use crate::storage_state::ProofStatus; + use crate::storage_transaction::StorageTransaction; + use crate::ENTRIES_PER_SEGMENT; use bincode::deserialize; use solana_runtime::bank::Bank; use solana_sdk::account::{create_keyed_accounts, Account}; @@ -370,7 +375,7 @@ mod tests { Some(storage_system_account) => { let state = deserialize(&storage_system_account.data); if let Ok(state) = state { - let state: StorageProgramState = state; + let state: StorageState = state; return state.entry_height; } } @@ -385,7 +390,7 @@ mod tests { if let Some(storage_system_account) = bank.get_account(&account) { let state = deserialize(&storage_system_account.data); if let Ok(state) = state { - let state: StorageProgramState = state; + let state: StorageState = state; return state.hash; } } diff --git a/programs/storage_api/src/storage_state.rs b/programs/storage_api/src/storage_state.rs new file mode 100644 index 0000000000..9738bffa39 --- /dev/null +++ b/programs/storage_api/src/storage_state.rs @@ -0,0 +1,36 @@ +use serde_derive::{Deserialize, Serialize}; +use solana_sdk::hash::Hash; +use solana_sdk::pubkey::Pubkey; +use solana_sdk::signature::Signature; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum ProofStatus { + Valid, + NotValid, + Skipped, +} + +#[derive(Default, Debug, Serialize, Deserialize, Clone)] +pub struct ProofInfo { + pub id: Pubkey, + pub signature: Signature, + pub sha_state: Hash, +} + +#[derive(Default, Debug, Serialize, Deserialize, Clone)] +pub struct ValidationInfo { + pub id: Pubkey, + pub proof_mask: Vec, +} + +#[derive(Default, Debug, Serialize, Deserialize)] +pub struct StorageState { + pub entry_height: u64, + pub hash: Hash, + + pub proofs: Vec>, + pub previous_proofs: Vec>, + + pub lockout_validations: Vec>, + pub reward_validations: Vec>, +} diff --git a/programs/storage_api/src/storage_transaction.rs b/programs/storage_api/src/storage_transaction.rs new file mode 100644 index 0000000000..41ff1bc6cf --- /dev/null +++ b/programs/storage_api/src/storage_transaction.rs @@ -0,0 +1,60 @@ +use crate::id; +use crate::storage_instruction::StorageInstruction; +use crate::storage_state::ProofStatus; +use solana_sdk::hash::Hash; +use solana_sdk::signature::{Keypair, Signature}; +use solana_sdk::transaction::Transaction; + +pub struct StorageTransaction {} + +impl StorageTransaction { + pub fn new_mining_proof( + from_keypair: &Keypair, + sha_state: Hash, + recent_blockhash: Hash, + entry_height: u64, + signature: Signature, + ) -> Transaction { + let program = StorageInstruction::SubmitMiningProof { + sha_state, + entry_height, + signature, + }; + Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) + } + + pub fn new_advertise_recent_blockhash( + from_keypair: &Keypair, + storage_hash: Hash, + recent_blockhash: Hash, + entry_height: u64, + ) -> Transaction { + let program = StorageInstruction::AdvertiseStorageRecentBlockhash { + hash: storage_hash, + entry_height, + }; + Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) + } + + pub fn new_proof_validation( + from_keypair: &Keypair, + recent_blockhash: Hash, + entry_height: u64, + proof_mask: Vec, + ) -> Transaction { + let program = StorageInstruction::ProofValidation { + entry_height, + proof_mask, + }; + Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) + } + + pub fn new_reward_claim( + from_keypair: &Keypair, + recent_blockhash: Hash, + entry_height: u64, + ) -> Transaction { + let program = StorageInstruction::ClaimStorageReward { entry_height }; + Transaction::new_signed(from_keypair, &[], &id(), &program, recent_blockhash, 0) + } +}