Reorg Storage program to look more like the others

This commit is contained in:
Greg Fitzgerald
2019-03-22 14:30:18 -06:00
committed by Grimes
parent acedf4ca5a
commit 8ff1987d2d
7 changed files with 143 additions and 130 deletions

View File

@ -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<ProofStatus>,
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct StorageProgramState {
pub entry_height: u64,
pub hash: Hash,
pub proofs: Vec<Vec<ProofInfo>>,
pub previous_proofs: Vec<Vec<ProofInfo>>,
pub lockout_validations: Vec<Vec<ValidationInfo>>,
pub reward_validations: Vec<Vec<ValidationInfo>>,
}
#[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<ProofStatus>,
},
}
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<ProofStatus>,
) -> 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)
}
}

View File

@ -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<ProofStatus>,
},
}

View File

@ -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;
}
}

View File

@ -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<ProofStatus>,
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct StorageState {
pub entry_height: u64,
pub hash: Hash,
pub proofs: Vec<Vec<ProofInfo>>,
pub previous_proofs: Vec<Vec<ProofInfo>>,
pub lockout_validations: Vec<Vec<ValidationInfo>>,
pub reward_validations: Vec<Vec<ValidationInfo>>,
}

View File

@ -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<ProofStatus>,
) -> 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)
}
}