Fix storage program space issues and limit storage transaction data (#4677)

This commit is contained in:
Sagar Dhawan
2019-06-13 17:53:54 -07:00
committed by GitHub
parent 9e3758983d
commit b1ac8f933b
11 changed files with 456 additions and 249 deletions

View File

@ -12,6 +12,8 @@ edition = "2018"
assert_matches = "1.3.0"
bincode = "1.1.4"
log = "0.4.2"
num-derive = "0.2"
num-traits = "0.2"
serde = "1.0.92"
serde_derive = "1.0.92"
solana-logger = { path = "../../logger", version = "0.16.0" }

View File

@ -1,5 +1,6 @@
use crate::get_segment_from_slot;
use log::*;
use num_derive::FromPrimitive;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::account::Account;
use solana_sdk::account::KeyedAccount;
@ -10,10 +11,22 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature;
use std::collections::HashMap;
pub const TOTAL_VALIDATOR_REWARDS: u64 = 1;
pub const TOTAL_REPLICATOR_REWARDS: u64 = 1;
pub const VALIDATOR_REWARD: u64 = 25;
pub const REPLICATOR_REWARD: u64 = 25;
// Todo Tune this for actual use cases when replicators are feature complete
pub const STORAGE_ACCOUNT_SPACE: u64 = 1024 * 8;
pub const MAX_PROOFS_PER_SEGMENT: usize = 80;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, FromPrimitive)]
pub enum StorageError {
InvalidSegment,
InvalidBlockhash,
InvalidProofMask,
DuplicateProof,
RewardPoolDepleted,
InvalidOwner,
ProofLimitReached,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum ProofStatus {
@ -30,18 +43,16 @@ impl Default for ProofStatus {
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Proof {
/// The encryption key the replicator used (also used to generate offsets)
pub signature: Signature,
/// A "recent" blockhash used to generate the seed
pub blockhash: Hash,
/// The resulting sampled state
pub sha_state: Hash,
/// The start index of the segment proof is for
pub segment_index: usize,
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct CheckedProof {
pub proof: Proof,
pub status: ProofStatus,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum StorageContract {
Uninitialized, // Must be first (aka, 0)
@ -52,16 +63,20 @@ pub enum StorageContract {
slot: u64,
// Most recently advertised blockhash
hash: Hash,
lockout_validations: HashMap<usize, HashMap<Hash, ProofStatus>>,
reward_validations: HashMap<usize, HashMap<Hash, ProofStatus>>,
// Lockouts and Rewards are per segment per replicator. It needs to remain this way until
// the challenge stage is added. Once challenges are in rewards can just be a number
lockout_validations: HashMap<usize, HashMap<Pubkey, Vec<ProofStatus>>>,
// lamports that are ready to be claimed
pending_lamports: u64,
},
ReplicatorStorage {
owner: Pubkey,
/// Map of Proofs per segment, in a HashMap based on the sha_state
proofs: HashMap<usize, HashMap<Hash, Proof>>,
/// Map of Rewards per segment, in a HashMap based on the sha_state
/// Multiple validators can validate the same set of proofs so it needs a Vec
reward_validations: HashMap<usize, HashMap<Hash, Vec<ProofStatus>>>,
// TODO what to do about duplicate proofs across segments? - Check the blockhashes
// Map of Proofs per segment, in a Vec
proofs: HashMap<usize, Vec<Proof>>,
// Map of Rewards per segment, in a HashMap based on the validator account that verified
// the proof. This can be used for challenge stage when its added
reward_validations: HashMap<usize, HashMap<Pubkey, Vec<ProofStatus>>>,
},
MiningPool,
@ -77,7 +92,7 @@ pub fn create_validator_storage_account(owner: Pubkey, lamports: u64) -> Account
slot: 0,
hash: Hash::default(),
lockout_validations: HashMap::new(),
reward_validations: HashMap::new(),
pending_lamports: 0,
})
.expect("set_state");
@ -85,12 +100,13 @@ pub fn create_validator_storage_account(owner: Pubkey, lamports: u64) -> Account
}
pub struct StorageAccount<'a> {
pub(crate) id: Pubkey,
account: &'a mut Account,
}
impl<'a> StorageAccount<'a> {
pub fn new(account: &'a mut Account) -> Self {
Self { account }
pub fn new(id: Pubkey, account: &'a mut Account) -> Self {
Self { id, account }
}
pub fn initialize_mining_pool(&mut self) -> Result<(), InstructionError> {
@ -125,7 +141,7 @@ impl<'a> StorageAccount<'a> {
slot: 0,
hash: Hash::default(),
lockout_validations: HashMap::new(),
reward_validations: HashMap::new(),
pending_lamports: 0,
};
self.account.set_state(storage_contract)
} else {
@ -138,15 +154,28 @@ impl<'a> StorageAccount<'a> {
sha_state: Hash,
segment_index: usize,
signature: Signature,
blockhash: Hash,
current_slot: u64,
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
if let StorageContract::ReplicatorStorage { proofs, .. } = &mut storage_contract {
if let StorageContract::ReplicatorStorage {
proofs,
reward_validations,
..
} = &mut storage_contract
{
let current_segment = get_segment_from_slot(current_slot);
// clean up the account
// TODO check for time correctness - storage seems to run at a delay of about 3
proofs.retain(|segment, _| *segment >= current_segment.saturating_sub(5));
reward_validations.retain(|segment, _| *segment >= current_segment.saturating_sub(10));
if segment_index >= current_segment {
// attempt to submit proof for unconfirmed segment
return Err(InstructionError::InvalidArgument);
return Err(InstructionError::CustomError(
StorageError::InvalidSegment as u32,
));
}
debug!(
@ -154,23 +183,33 @@ impl<'a> StorageAccount<'a> {
sha_state, segment_index
);
// TODO check that this blockhash is valid and recent
// if !is_valid(&blockhash) {
// // proof isn't using a recent blockhash
// return Err(InstructionError::CustomError(InvalidBlockhash as u32));
// }
let proof = Proof {
sha_state,
signature,
blockhash,
segment_index,
};
// store the proofs in the "current" segment's entry in the hash map.
let segment_proofs = proofs.entry(current_segment).or_default();
if segment_proofs.contains_key(&sha_state) {
if segment_proofs.contains(&proof) {
// do not accept duplicate proofs
return Err(InstructionError::InvalidArgument);
return Err(InstructionError::CustomError(
StorageError::DuplicateProof as u32,
));
}
segment_proofs.insert(
sha_state,
Proof {
sha_state,
signature,
segment_index,
},
);
// TODO check for time correctness
proofs.retain(|segment, _| *segment >= current_segment.saturating_sub(5));
if segment_proofs.len() >= MAX_PROOFS_PER_SEGMENT {
// do not accept more than MAX_PROOFS_PER_SEGMENT
return Err(InstructionError::CustomError(
StorageError::ProofLimitReached as u32,
));
}
segment_proofs.push(proof);
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
@ -187,8 +226,8 @@ impl<'a> StorageAccount<'a> {
if let StorageContract::ValidatorStorage {
slot: state_slot,
hash: state_hash,
reward_validations,
lockout_validations,
pending_lamports,
..
} = &mut storage_contract
{
@ -200,14 +239,27 @@ impl<'a> StorageAccount<'a> {
segment, current_segment
);
if segment < original_segment || segment >= current_segment {
return Err(InstructionError::InvalidArgument);
return Err(InstructionError::CustomError(
StorageError::InvalidSegment as u32,
));
}
*state_slot = slot;
*state_hash = hash;
// move storage epoch updated, move the lockout_validations to reward_validations
reward_validations.extend(lockout_validations.drain());
// storage epoch updated, move the lockout_validations to pending_lamports
let num_validations = count_valid_proofs(
&lockout_validations
.drain()
.flat_map(|(_segment, mut proofs)| {
proofs
.drain()
.flat_map(|(_, proof)| proof)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>(),
);
*pending_lamports += VALIDATOR_REWARD * num_validations;
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
@ -216,8 +268,9 @@ impl<'a> StorageAccount<'a> {
pub fn proof_validation(
&mut self,
me: &Pubkey,
segment: u64,
proofs: Vec<(Pubkey, Vec<CheckedProof>)>,
proofs_per_account: Vec<Vec<ProofStatus>>,
replicator_accounts: &mut [StorageAccount],
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
@ -231,56 +284,72 @@ impl<'a> StorageAccount<'a> {
let state_segment = get_segment_from_slot(*state_slot);
if segment_index > state_segment {
return Err(InstructionError::InvalidArgument);
return Err(InstructionError::CustomError(
StorageError::InvalidSegment as u32,
));
}
let accounts_and_proofs = replicator_accounts
let accounts = replicator_accounts
.iter_mut()
.filter_map(|account| {
account
.account
.state()
.ok()
.map(move |contract| match contract {
StorageContract::ReplicatorStorage { proofs, .. } => {
if let Some(proofs) = proofs.get(&segment_index).cloned() {
Some((account, proofs))
.enumerate()
.filter_map(|(i, account)| {
account.account.state().ok().map(|contract| match contract {
StorageContract::ReplicatorStorage {
proofs: account_proofs,
..
} => {
//TODO do this better
if let Some(segment_proofs) =
account_proofs.get(&segment_index).cloned()
{
if proofs_per_account
.get(i)
.filter(|proofs| proofs.len() == segment_proofs.len())
.is_some()
{
Some(account)
} else {
None
}
} else {
None
}
_ => None,
})
}
_ => None,
})
})
.flatten()
.collect::<Vec<_>>();
if accounts_and_proofs.len() != proofs.len() {
// don't have all the accounts to validate the proofs against
return Err(InstructionError::InvalidArgument);
if accounts.len() != proofs_per_account.len() {
// don't have all the accounts to validate the proofs_per_account against
return Err(InstructionError::CustomError(
StorageError::InvalidProofMask as u32,
));
}
let valid_proofs: Vec<_> = proofs
let stored_proofs: Vec<_> = proofs_per_account
.into_iter()
.zip(accounts_and_proofs.into_iter())
.flat_map(|((_id, checked_proofs), (account, proofs))| {
checked_proofs.into_iter().filter_map(move |checked_proof| {
proofs.get(&checked_proof.proof.sha_state).map(|proof| {
process_validation(account, segment_index, &proof, &checked_proof)
.map(|_| checked_proof)
})
})
.zip(accounts.into_iter())
.filter_map(|(checked_proofs, account)| {
if store_validation_result(me, account, segment_index, &checked_proofs).is_ok()
{
Some((account.id, checked_proofs))
} else {
None
}
})
.flatten()
.collect();
// allow validators to store successful validations
valid_proofs.into_iter().for_each(|proof| {
lockout_validations
.entry(segment_index)
.or_default()
.insert(proof.proof.sha_state, proof.status);
});
stored_proofs
.into_iter()
.for_each(|(replicator_account_id, proof_mask)| {
lockout_validations
.entry(segment_index)
.or_default()
.insert(replicator_account_id, proof_mask);
});
self.account.set_state(storage_contract)
} else {
@ -291,59 +360,59 @@ impl<'a> StorageAccount<'a> {
pub fn claim_storage_reward(
&mut self,
mining_pool: &mut KeyedAccount,
owner: &mut StorageAccount,
) -> Result<(), InstructionError> {
let mut storage_contract = &mut self.account.state()?;
if let StorageContract::ValidatorStorage {
reward_validations, ..
owner: account_owner,
pending_lamports,
..
} = &mut storage_contract
{
let num_validations = count_valid_proofs(
&reward_validations
.drain()
.flat_map(|(_segment, mut proofs)| {
proofs.drain().map(|(_, proof)| proof).collect::<Vec<_>>()
})
.collect::<Vec<_>>(),
);
let reward = TOTAL_VALIDATOR_REWARDS * num_validations;
mining_pool.account.lamports -= reward;
self.account.lamports += reward;
if owner.id != *account_owner {
Err(InstructionError::CustomError(
StorageError::InvalidOwner as u32,
))?
}
let pending = *pending_lamports;
if mining_pool.account.lamports < pending {
Err(InstructionError::CustomError(
StorageError::RewardPoolDepleted as u32,
))?
}
mining_pool.account.lamports -= pending;
owner.account.lamports += pending;
//clear pending_lamports
*pending_lamports = 0;
self.account.set_state(storage_contract)
} else if let StorageContract::ReplicatorStorage {
proofs,
owner: account_owner,
reward_validations,
..
} = &mut storage_contract
{
// remove proofs for which rewards have already been collected
let segment_proofs = proofs;
if owner.id != *account_owner {
Err(InstructionError::CustomError(
StorageError::InvalidOwner as u32,
))?
}
let checked_proofs = reward_validations
.drain()
.flat_map(|(segment, mut proofs)| {
.flat_map(|(_, mut proofs)| {
proofs
.drain()
.map(|(sha_state, proof)| {
proof
.into_iter()
.map(|proof| {
segment_proofs.get_mut(&segment).and_then(|segment_proofs| {
segment_proofs.remove(&sha_state)
});
proof
})
.collect::<Vec<_>>()
})
.flatten()
.flat_map(|(_, proofs)| proofs)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let total_proofs = checked_proofs.len() as u64;
let num_validations = count_valid_proofs(&checked_proofs);
let reward =
num_validations * TOTAL_REPLICATOR_REWARDS * (num_validations / total_proofs);
let reward = num_validations * REPLICATOR_REWARD * (num_validations / total_proofs);
mining_pool.account.lamports -= reward;
self.account.lamports += reward;
owner.account.lamports += reward;
self.account.set_state(storage_contract)
} else {
Err(InstructionError::InvalidArgument)?
@ -353,9 +422,10 @@ impl<'a> StorageAccount<'a> {
/// Store the result of a proof validation into the replicator account
fn store_validation_result(
me: &Pubkey,
storage_account: &mut StorageAccount,
segment: usize,
checked_proof: CheckedProof,
proof_mask: &[ProofStatus],
) -> Result<(), InstructionError> {
let mut storage_contract = storage_account.account.state()?;
match &mut storage_contract {
@ -368,20 +438,14 @@ fn store_validation_result(
return Err(InstructionError::InvalidAccountData);
}
if proofs
.get(&segment)
.unwrap()
.contains_key(&checked_proof.proof.sha_state)
{
reward_validations
.entry(segment)
.or_default()
.entry(checked_proof.proof.sha_state)
.or_default()
.push(checked_proof.status);
} else {
if proofs.get(&segment).unwrap().len() != proof_mask.len() {
return Err(InstructionError::InvalidAccountData);
}
reward_validations
.entry(segment)
.or_default()
.insert(*me, proof_mask.to_vec());
}
_ => return Err(InstructionError::InvalidAccountData),
}
@ -398,21 +462,6 @@ fn count_valid_proofs(proofs: &[ProofStatus]) -> u64 {
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.clone())?;
if proof.signature != checked_proof.proof.signature
|| checked_proof.status != ProofStatus::Valid
{
return Err(InstructionError::GenericError);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@ -423,7 +472,7 @@ mod tests {
solana_logger::setup();
let mut account = Account::default();
account.data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
let storage_account = StorageAccount::new(&mut account);
let storage_account = StorageAccount::new(Pubkey::default(), &mut account);
// pretend it's a validator op code
let mut contract = storage_account.account.state().unwrap();
if let StorageContract::ValidatorStorage { .. } = contract {
@ -438,7 +487,7 @@ mod tests {
slot: 0,
hash: Hash::default(),
lockout_validations: HashMap::new(),
reward_validations: HashMap::new(),
pending_lamports: 0,
};
storage_account.account.set_state(&contract).unwrap();
if let StorageContract::ReplicatorStorage { .. } = contract {
@ -458,6 +507,7 @@ mod tests {
#[test]
fn test_process_validation() {
let mut account = StorageAccount {
id: Pubkey::default(),
account: &mut Account {
lamports: 0,
data: vec![],
@ -467,17 +517,18 @@ mod tests {
};
let segment_index = 0_usize;
let proof = Proof {
signature: Signature::default(),
sha_state: Hash::default(),
segment_index,
};
let mut checked_proof = CheckedProof {
proof: proof.clone(),
status: ProofStatus::Valid,
..Proof::default()
};
// account has no space
process_validation(&mut account, segment_index, &proof, &checked_proof).unwrap_err();
store_validation_result(
&Pubkey::default(),
&mut account,
segment_index,
&vec![ProofStatus::default(); 1],
)
.unwrap_err();
account
.account
@ -485,10 +536,8 @@ mod tests {
.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
let storage_contract = &mut account.account.state().unwrap();
if let StorageContract::Uninitialized = storage_contract {
let mut proof_map = HashMap::new();
proof_map.insert(proof.sha_state, proof.clone());
let mut proofs = HashMap::new();
proofs.insert(0, proof_map);
proofs.insert(0, vec![proof.clone()]);
*storage_contract = StorageContract::ReplicatorStorage {
owner: Pubkey::default(),
proofs,
@ -498,11 +547,21 @@ mod tests {
account.account.set_state(storage_contract).unwrap();
// proof is valid
process_validation(&mut account, segment_index, &proof, &checked_proof).unwrap();
store_validation_result(
&Pubkey::default(),
&mut account,
segment_index,
&vec![ProofStatus::Valid],
)
.unwrap();
checked_proof.status = ProofStatus::NotValid;
// proof failed verification
process_validation(&mut account, segment_index, &proof, &checked_proof).unwrap_err();
// proof failed verification but we should still be able to store it
store_validation_result(
&Pubkey::default(),
&mut account,
segment_index,
&vec![ProofStatus::NotValid],
)
.unwrap();
}
}

View File

@ -1,5 +1,5 @@
use crate::id;
use crate::storage_contract::{CheckedProof, STORAGE_ACCOUNT_SPACE};
use crate::storage_contract::{ProofStatus, STORAGE_ACCOUNT_SPACE};
use serde_derive::{Deserialize, Serialize};
use solana_sdk::hash::Hash;
use solana_sdk::instruction::{AccountMeta, Instruction};
@ -7,7 +7,6 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature;
use solana_sdk::syscall::current;
use solana_sdk::system_instruction;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum StorageInstruction {
@ -27,6 +26,7 @@ pub enum StorageInstruction {
sha_state: Hash,
segment_index: usize,
signature: Signature,
blockhash: Hash,
},
AdvertiseStorageRecentBlockhash {
hash: Hash,
@ -37,13 +37,49 @@ pub enum StorageInstruction {
/// Expects 1 Account:
/// 0 - Storage account with credits to redeem
/// 1 - MiningPool account to redeem credits from
/// 2 - Replicator account to credit - this account *must* be the owner
ClaimStorageReward,
ProofValidation {
/// The segment during which this proof was generated
segment: u64,
proofs: Vec<(Pubkey, Vec<CheckedProof>)>,
/// A Vec of proof masks per keyed replicator account loaded by the instruction
proofs: Vec<Vec<ProofStatus>>,
},
}
fn get_ratios() -> (u64, u64) {
// max number bytes available for account metas and proofs
// The maximum transaction size is == `PACKET_DATA_SIZE` (1232 bytes)
// There are approx. 900 bytes left over after the storage instruction is wrapped into
// a signed transaction.
static MAX_BYTES: u64 = 900;
let account_meta_size: u64 =
bincode::serialized_size(&AccountMeta::new(Pubkey::new_rand(), false)).unwrap_or(0);
let proof_size: u64 = bincode::serialized_size(&ProofStatus::default()).unwrap_or(0);
// the ratio between account meta size and a single proof status
let ratio = (account_meta_size + proof_size - 1) / proof_size;
let bytes = (MAX_BYTES + ratio - 1) / ratio;
(ratio, bytes)
}
/// Returns how many accounts and their proofs will fit in a single proof validation tx
///
/// # Arguments
///
/// * `proof_mask_max` - The largest proof mask across all accounts intended for submission
///
pub fn validation_account_limit(proof_mask_max: usize) -> u64 {
let (ratio, bytes) = get_ratios();
// account_meta_count * (ratio + proof_mask_max) = bytes
bytes / (ratio + proof_mask_max as u64)
}
pub fn proof_mask_limit() -> u64 {
let (ratio, bytes) = get_ratios();
bytes - ratio
}
pub fn create_validator_storage_account(
from_pubkey: &Pubkey,
storage_owner: &Pubkey,
@ -118,11 +154,13 @@ pub fn mining_proof(
sha_state: Hash,
segment_index: usize,
signature: Signature,
blockhash: Hash,
) -> Instruction {
let storage_instruction = StorageInstruction::SubmitMiningProof {
sha_state,
segment_index,
signature,
blockhash,
};
let account_metas = vec![
AccountMeta::new(*storage_pubkey, true),
@ -147,26 +185,42 @@ pub fn advertise_recent_blockhash(
Instruction::new(id(), &storage_instruction, account_metas)
}
pub fn proof_validation<S: std::hash::BuildHasher>(
pub fn proof_validation(
storage_pubkey: &Pubkey,
segment: u64,
checked_proofs: HashMap<Pubkey, Vec<CheckedProof>, S>,
checked_proofs: Vec<(Pubkey, Vec<ProofStatus>)>,
) -> Instruction {
let mut account_metas = vec![AccountMeta::new(*storage_pubkey, true)];
let mut proofs = vec![];
checked_proofs.into_iter().for_each(|(id, p)| {
proofs.push((id, p));
proofs.push(p);
account_metas.push(AccountMeta::new(id, false))
});
let storage_instruction = StorageInstruction::ProofValidation { segment, proofs };
Instruction::new(id(), &storage_instruction, account_metas)
}
pub fn claim_reward(storage_pubkey: &Pubkey, mining_pool_pubkey: &Pubkey) -> Instruction {
pub fn claim_reward(
owner_pubkey: &Pubkey,
storage_pubkey: &Pubkey,
mining_pool_pubkey: &Pubkey,
) -> Instruction {
let storage_instruction = StorageInstruction::ClaimStorageReward;
let account_metas = vec![
AccountMeta::new(*storage_pubkey, false),
AccountMeta::new(*mining_pool_pubkey, false),
AccountMeta::new(*owner_pubkey, false),
];
Instruction::new(id(), &storage_instruction, account_metas)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_size() {
// check that if there's 50 proof per account, only 1 account can fit in a single tx
assert_eq!(validation_account_limit(50), 1);
}
}

View File

@ -17,7 +17,7 @@ pub fn process_instruction(
let (me, rest) = keyed_accounts.split_at_mut(1);
let me_unsigned = me[0].signer_key().is_none();
let mut storage_account = StorageAccount::new(&mut me[0].account);
let mut storage_account = StorageAccount::new(*me[0].unsigned_key(), &mut me[0].account);
match bincode::deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? {
StorageInstruction::InitializeMiningPool => {
@ -42,13 +42,20 @@ pub fn process_instruction(
sha_state,
segment_index,
signature,
blockhash,
} => {
if me_unsigned || rest.len() != 1 {
// This instruction must be signed by `me`
Err(InstructionError::InvalidArgument)?;
}
let current = Current::from(&rest[0].account).unwrap();
storage_account.submit_mining_proof(sha_state, segment_index, signature, current.slot)
storage_account.submit_mining_proof(
sha_state,
segment_index,
signature,
blockhash,
current.slot,
)
}
StorageInstruction::AdvertiseStorageRecentBlockhash { hash, slot } => {
if me_unsigned || rest.len() != 1 {
@ -59,21 +66,27 @@ pub fn process_instruction(
storage_account.advertise_storage_recent_blockhash(hash, slot, current.slot)
}
StorageInstruction::ClaimStorageReward => {
if rest.len() != 1 {
if rest.len() != 2 {
Err(InstructionError::InvalidArgument)?;
}
storage_account.claim_storage_reward(&mut rest[0])
let (mining_pool, owner) = rest.split_at_mut(1);
let mut owner = StorageAccount::new(*owner[0].unsigned_key(), &mut owner[0].account);
storage_account.claim_storage_reward(&mut mining_pool[0], &mut owner)
}
StorageInstruction::ProofValidation { segment, proofs } => {
if me_unsigned || rest.is_empty() {
// This instruction must be signed by `me` and `rest` cannot be empty
Err(InstructionError::InvalidArgument)?;
}
let me_id = storage_account.id;
let mut rest: Vec<_> = rest
.iter_mut()
.map(|keyed_account| StorageAccount::new(&mut keyed_account.account))
.map(|keyed_account| {
StorageAccount::new(*keyed_account.unsigned_key(), &mut keyed_account.account)
})
.collect();
storage_account.proof_validation(segment, proofs, &mut rest)
storage_account.proof_validation(&me_id, segment, proofs, &mut rest)
}
}
}

View File

@ -14,11 +14,11 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::syscall::current;
use solana_sdk::syscall::current::Current;
use solana_sdk::system_instruction;
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;
use solana_storage_api::storage_contract::StorageAccount;
use solana_storage_api::storage_contract::{
CheckedProof, Proof, ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE,
TOTAL_REPLICATOR_REWARDS, TOTAL_VALIDATOR_REWARDS,
ProofStatus, StorageContract, REPLICATOR_REWARD, STORAGE_ACCOUNT_SPACE, VALIDATOR_REWARD,
};
use solana_storage_api::storage_instruction;
use solana_storage_api::storage_processor::process_instruction;
@ -108,13 +108,19 @@ fn test_proof_bounds() {
..Account::default()
};
{
let mut storage_account = StorageAccount::new(&mut account);
let mut storage_account = StorageAccount::new(pubkey, &mut account);
storage_account
.initialize_replicator_storage(account_owner)
.unwrap();
}
let ix = storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default());
let ix = storage_instruction::mining_proof(
&pubkey,
Hash::default(),
0,
Signature::default(),
Hash::default(),
);
// the proof is for segment 0, need to move the slot into segment 2
let mut current_account = current::create_account(1);
Current::to(
@ -167,7 +173,13 @@ 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());
let ix = storage_instruction::mining_proof(
&pubkey,
Hash::default(),
0,
Signature::default(),
Hash::default(),
);
// move tick height into segment 1
let mut current_account = current::create_account(1);
Current::to(
@ -194,7 +206,13 @@ fn test_submit_mining_invalid_slot() {
accounts[0].data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
accounts[1].data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
let ix = storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default());
let ix = storage_instruction::mining_proof(
&pubkey,
Hash::default(),
0,
Signature::default(),
Hash::default(),
);
// submitting a proof for a slot in the past, so this should fail
assert!(test_instruction(&ix, &mut accounts).is_err());
@ -208,13 +226,19 @@ fn test_submit_mining_ok() {
let mut account = Account::default();
account.data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
{
let mut storage_account = StorageAccount::new(&mut account);
let mut storage_account = StorageAccount::new(pubkey, &mut account);
storage_account
.initialize_replicator_storage(account_owner)
.unwrap();
}
let ix = storage_instruction::mining_proof(&pubkey, Hash::default(), 0, Signature::default());
let ix = storage_instruction::mining_proof(
&pubkey,
Hash::default(),
0,
Signature::default(),
Hash::default(),
);
// move slot into segment 1
let mut current_account = current::create_account(1);
Current::to(
@ -235,11 +259,13 @@ fn test_submit_mining_ok() {
#[test]
fn test_validate_mining() {
solana_logger::setup();
let (mut genesis_block, mint_keypair) = create_genesis_block(1000);
let (mut genesis_block, mint_keypair) = create_genesis_block(100_000);
genesis_block
.native_instruction_processors
.push(solana_storage_program::solana_storage_program!());
let mint_pubkey = mint_keypair.pubkey();
// 1 owner for all replicator and validator accounts for the test
let owner_pubkey = Pubkey::new_rand();
let replicator_1_storage_keypair = Keypair::new();
let replicator_1_storage_id = replicator_1_storage_keypair.pubkey();
@ -258,6 +284,7 @@ fn test_validate_mining() {
let bank_client = BankClient::new_shared(&bank);
init_storage_accounts(
&owner_pubkey,
&bank_client,
&mint_keypair,
&[&validator_storage_id],
@ -267,7 +294,7 @@ fn test_validate_mining() {
let message = Message::new(storage_instruction::create_mining_pool_account(
&mint_pubkey,
&mining_pool_pubkey,
100,
10_000,
));
bank_client.send_message(&[&mint_keypair], message).unwrap();
@ -342,7 +369,7 @@ fn test_validate_mining() {
vec![storage_instruction::proof_validation(
&validator_storage_id,
proof_segment as u64,
checked_proofs,
checked_proofs.into_iter().map(|entry| entry).collect(),
)],
Some(&mint_pubkey),
);
@ -378,6 +405,7 @@ fn test_validate_mining() {
let message = Message::new_with_payer(
vec![storage_instruction::claim_reward(
&owner_pubkey,
&validator_storage_id,
&mining_pool_pubkey,
)],
@ -385,8 +413,8 @@ fn test_validate_mining() {
);
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
assert_eq!(
bank_client.get_balance(&validator_storage_id).unwrap(),
10 + (TOTAL_VALIDATOR_REWARDS * 10)
bank_client.get_balance(&owner_pubkey).unwrap(),
1 + (VALIDATOR_REWARD * 10)
);
// tick the bank into the next storage epoch so that rewards can be claimed
@ -401,15 +429,23 @@ fn test_validate_mining() {
let message = Message::new_with_payer(
vec![storage_instruction::claim_reward(
&owner_pubkey,
&replicator_1_storage_id,
&mining_pool_pubkey,
)],
Some(&mint_pubkey),
);
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
assert_eq!(
bank_client.get_balance(&owner_pubkey).unwrap(),
1 + (VALIDATOR_REWARD * 10) + (REPLICATOR_REWARD * 5)
);
let message = Message::new_with_payer(
vec![storage_instruction::claim_reward(
&owner_pubkey,
&replicator_2_storage_id,
&mining_pool_pubkey,
)],
@ -417,37 +453,44 @@ fn test_validate_mining() {
);
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
// TODO enable when rewards are working
assert_eq!(
bank_client.get_balance(&replicator_1_storage_id).unwrap(),
10 + (TOTAL_REPLICATOR_REWARDS * 5)
bank_client.get_balance(&owner_pubkey).unwrap(),
1 + (VALIDATOR_REWARD * 10) + (REPLICATOR_REWARD * 10)
);
}
fn init_storage_accounts(
owner: &Pubkey,
client: &BankClient,
mint: &Keypair,
validator_accounts_to_create: &[&Pubkey],
replicator_accounts_to_create: &[&Pubkey],
lamports: u64,
) {
let mut ixs: Vec<_> = validator_accounts_to_create
.into_iter()
.flat_map(|account| {
storage_instruction::create_validator_storage_account(
&mint.pubkey(),
&Pubkey::default(),
account,
lamports,
)
})
.collect();
let mut ixs: Vec<_> = vec![system_instruction::create_user_account(
&mint.pubkey(),
owner,
1,
)];
ixs.append(
&mut validator_accounts_to_create
.into_iter()
.flat_map(|account| {
storage_instruction::create_validator_storage_account(
&mint.pubkey(),
owner,
account,
lamports,
)
})
.collect(),
);
replicator_accounts_to_create
.into_iter()
.for_each(|account| {
ixs.append(&mut storage_instruction::create_replicator_storage_account(
&mint.pubkey(),
&Pubkey::default(),
owner,
account,
lamports,
))
@ -481,7 +524,7 @@ fn submit_proof(
storage_keypair: &Keypair,
bank_client: &BankClient,
segment_index: u64,
) -> CheckedProof {
) -> ProofStatus {
let sha_state = Hash::new(Pubkey::new_rand().as_ref());
let message = Message::new_with_payer(
vec![storage_instruction::mining_proof(
@ -489,6 +532,7 @@ fn submit_proof(
sha_state,
segment_index as usize,
Signature::default(),
bank_client.get_recent_blockhash().unwrap().0,
)],
Some(&mint_keypair.pubkey()),
);
@ -497,14 +541,7 @@ fn submit_proof(
bank_client.send_message(&[&mint_keypair, &storage_keypair], message),
Ok(_)
);
CheckedProof {
proof: Proof {
signature: Signature::default(),
sha_state,
segment_index: segment_index as usize,
},
status: ProofStatus::Valid,
}
ProofStatus::Valid
}
fn get_storage_blockhash<C: SyncClient>(client: &C, account: &Pubkey) -> Hash {
@ -585,6 +622,7 @@ fn test_bank_storage() {
Hash::default(),
slot,
Signature::default(),
bank_client.get_recent_blockhash().unwrap().0,
)],
Some(&mint_pubkey),
);