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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 456 additions and 249 deletions

2
Cargo.lock generated
View File

@ -2734,6 +2734,8 @@ dependencies = [
"assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-logger 0.16.0", "solana-logger 0.16.0",

View File

@ -52,7 +52,7 @@ pub struct Replicator {
ledger_path: String, ledger_path: String,
keypair: Arc<Keypair>, keypair: Arc<Keypair>,
storage_keypair: Arc<Keypair>, storage_keypair: Arc<Keypair>,
blockhash: String, blockhash: Hash,
signature: ed25519_dalek::Signature, signature: ed25519_dalek::Signature,
cluster_info: Arc<RwLock<ClusterInfo>>, cluster_info: Arc<RwLock<ClusterInfo>>,
ledger_data_file_encrypted: PathBuf, ledger_data_file_encrypted: PathBuf,
@ -204,7 +204,7 @@ impl Replicator {
let client = crate::gossip_service::get_client(&nodes); let client = crate::gossip_service::get_client(&nodes);
let (storage_blockhash, storage_slot) = let (storage_blockhash, storage_slot) =
match Self::poll_for_blockhash_and_slot(&cluster_info, &Hash::default().to_string()) { match Self::poll_for_blockhash_and_slot(&cluster_info, &Hash::default()) {
Ok(blockhash_and_slot) => blockhash_and_slot, Ok(blockhash_and_slot) => blockhash_and_slot,
Err(e) => { Err(e) => {
//shutdown services before exiting //shutdown services before exiting
@ -301,9 +301,9 @@ impl Replicator {
pub fn run(&mut self) { pub fn run(&mut self) {
info!("waiting for ledger download"); info!("waiting for ledger download");
self.thread_handles.pop().unwrap().join().unwrap(); self.thread_handles.pop().unwrap().join().unwrap();
self.encrypt_ledger()
.expect("ledger encrypt not successful");
loop { loop {
self.encrypt_ledger()
.expect("ledger encrypt not successful");
self.create_sampling_offsets(); self.create_sampling_offsets();
if let Err(err) = self.sample_file_to_create_mining_hash() { if let Err(err) = self.sample_file_to_create_mining_hash() {
warn!("Error sampling file, exiting: {:?}", err); warn!("Error sampling file, exiting: {:?}", err);
@ -311,6 +311,7 @@ impl Replicator {
} }
self.submit_mining_proof(); self.submit_mining_proof();
// Todo make this a lot more frequent by picking a "new" blockhash instead of picking a storage blockhash
// prep the next proof // prep the next proof
let (storage_blockhash, _) = let (storage_blockhash, _) =
match Self::poll_for_blockhash_and_slot(&self.cluster_info, &self.blockhash) { match Self::poll_for_blockhash_and_slot(&self.cluster_info, &self.blockhash) {
@ -323,7 +324,6 @@ impl Replicator {
break; break;
} }
}; };
self.signature = self.storage_keypair.sign(storage_blockhash.as_ref());
self.blockhash = storage_blockhash; self.blockhash = storage_blockhash;
} }
} }
@ -473,6 +473,7 @@ impl Replicator {
self.sha_state, self.sha_state,
get_segment_from_slot(self.slot), get_segment_from_slot(self.slot),
Signature::new(&self.signature.to_bytes()), Signature::new(&self.signature.to_bytes()),
self.blockhash,
); );
let message = Message::new_with_payer(vec![instruction], Some(&self.keypair.pubkey())); let message = Message::new_with_payer(vec![instruction], Some(&self.keypair.pubkey()));
let mut transaction = Transaction::new( let mut transaction = Transaction::new(
@ -507,8 +508,8 @@ impl Replicator {
/// Poll for a different blockhash and associated max_slot than `previous_blockhash` /// Poll for a different blockhash and associated max_slot than `previous_blockhash`
fn poll_for_blockhash_and_slot( fn poll_for_blockhash_and_slot(
cluster_info: &Arc<RwLock<ClusterInfo>>, cluster_info: &Arc<RwLock<ClusterInfo>>,
previous_blockhash: &str, previous_blockhash: &Hash,
) -> result::Result<(String, u64), Error> { ) -> result::Result<(Hash, u64), Error> {
for _ in 0..10 { for _ in 0..10 {
let rpc_client = { let rpc_client = {
let cluster_info = cluster_info.read().unwrap(); let cluster_info = cluster_info.read().unwrap();
@ -517,13 +518,28 @@ impl Replicator {
let node_index = thread_rng().gen_range(0, rpc_peers.len()); let node_index = thread_rng().gen_range(0, rpc_peers.len());
RpcClient::new_socket(rpc_peers[node_index].rpc) RpcClient::new_socket(rpc_peers[node_index].rpc)
}; };
let storage_blockhash = rpc_client let response = rpc_client
.retry_make_rpc_request(&RpcRequest::GetStorageBlockhash, None, 0) .retry_make_rpc_request(&RpcRequest::GetStorageBlockhash, None, 0)
.map_err(|err| { .map_err(|err| {
warn!("Error while making rpc request {:?}", err); warn!("Error while making rpc request {:?}", err);
Error::IO(io::Error::new(ErrorKind::Other, "rpc error")) Error::IO(io::Error::new(ErrorKind::Other, "rpc error"))
})? })?;
.to_string(); let storage_blockhash =
serde_json::from_value::<(String)>(response).map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Couldn't parse response: {:?}", err),
)
})?;
let storage_blockhash = storage_blockhash.parse().map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!(
"Blockhash parse failure: {:?} on {:?}",
err, storage_blockhash
),
)
})?;
if storage_blockhash != *previous_blockhash { if storage_blockhash != *previous_blockhash {
let storage_slot = rpc_client let storage_slot = rpc_client
.retry_make_rpc_request(&RpcRequest::GetStorageSlot, None, 0) .retry_make_rpc_request(&RpcRequest::GetStorageSlot, None, 0)

View File

@ -21,11 +21,10 @@ use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::transaction::Transaction; use solana_sdk::transaction::Transaction;
use solana_storage_api::storage_contract::{CheckedProof, Proof, ProofStatus, StorageContract}; use solana_storage_api::storage_contract::{Proof, ProofStatus, StorageContract};
use solana_storage_api::storage_instruction::proof_validation; use solana_storage_api::storage_instruction::proof_validation;
use solana_storage_api::{get_segment_from_slot, storage_instruction}; use solana_storage_api::{get_segment_from_slot, storage_instruction};
use std::collections::HashMap; use std::collections::HashMap;
use std::io;
use std::mem::size_of; use std::mem::size_of;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
@ -33,6 +32,7 @@ use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::thread::{self, sleep, Builder, JoinHandle}; use std::thread::{self, sleep, Builder, JoinHandle};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::{cmp, io};
// Block of hash answers to validate against // Block of hash answers to validate against
// Vec of [ledger blocks] x [keys] // Vec of [ledger blocks] x [keys]
@ -86,7 +86,7 @@ fn get_identity_index_from_signature(key: &Signature) -> usize {
} }
impl StorageState { impl StorageState {
pub fn new() -> Self { pub fn new(hash: &Hash) -> Self {
let storage_keys = vec![0u8; KEY_SIZE * NUM_IDENTITIES]; let storage_keys = vec![0u8; KEY_SIZE * NUM_IDENTITIES];
let storage_results = vec![Hash::default(); NUM_IDENTITIES]; let storage_results = vec![Hash::default(); NUM_IDENTITIES];
let replicator_map = vec![]; let replicator_map = vec![];
@ -96,7 +96,7 @@ impl StorageState {
storage_results, storage_results,
replicator_map, replicator_map,
slot: 0, slot: 0,
storage_blockhash: Hash::default(), storage_blockhash: *hash,
}; };
StorageState { StorageState {
@ -439,7 +439,7 @@ impl StorageStage {
//convert slot to segment //convert slot to segment
let segment = get_segment_from_slot(slot); let segment = get_segment_from_slot(slot);
if let Some(proofs) = proofs.get(&segment) { if let Some(proofs) = proofs.get(&segment) {
for (_, proof) in proofs.iter() { for proof in proofs.iter() {
{ {
debug!( debug!(
"generating storage_keys from storage txs current_key_idx: {}", "generating storage_keys from storage txs current_key_idx: {}",
@ -543,6 +543,8 @@ impl StorageStage {
// bundle up mining submissions from replicators // bundle up mining submissions from replicators
// and submit them in a tx to the leader to get rewarded. // and submit them in a tx to the leader to get rewarded.
let mut w_state = storage_state.write().unwrap(); let mut w_state = storage_state.write().unwrap();
let mut max_proof_mask = 0;
let proof_mask_limit = storage_instruction::proof_mask_limit();
let instructions: Vec<_> = w_state let instructions: Vec<_> = w_state
.replicator_map .replicator_map
.iter_mut() .iter_mut()
@ -552,32 +554,44 @@ impl StorageStage {
.iter_mut() .iter_mut()
.filter_map(|(id, proofs)| { .filter_map(|(id, proofs)| {
if !proofs.is_empty() { if !proofs.is_empty() {
Some(( if (proofs.len() as u64) >= proof_mask_limit {
*id, proofs.clear();
proofs None
.drain(..) } else {
.map(|proof| CheckedProof { max_proof_mask = cmp::max(max_proof_mask, proofs.len());
proof, Some((
status: ProofStatus::Valid, *id,
}) proofs
.collect::<Vec<_>>(), .drain(..)
)) .map(|_| ProofStatus::Valid)
.collect::<Vec<_>>(),
))
}
} else { } else {
None None
} }
}) })
.collect::<HashMap<_, _>>(); .collect::<Vec<(_, _)>>();
if !checked_proofs.is_empty() { if !checked_proofs.is_empty() {
let ix = proof_validation( let max_accounts_per_ix =
&storage_keypair.pubkey(), storage_instruction::validation_account_limit(max_proof_mask);
current_segment as u64, let ixs = checked_proofs
checked_proofs, .chunks(max_accounts_per_ix as usize)
); .map(|checked_proofs| {
Some(ix) proof_validation(
&storage_keypair.pubkey(),
current_segment as u64,
checked_proofs.to_vec(),
)
})
.collect::<Vec<_>>();
Some(ixs)
} else { } else {
None None
} }
}) })
.flatten()
.collect(); .collect();
let res: std::result::Result<_, _> = instructions let res: std::result::Result<_, _> = instructions
.into_iter() .into_iter()
@ -633,9 +647,9 @@ mod tests {
let cluster_info = test_cluster_info(&keypair.pubkey()); let cluster_info = test_cluster_info(&keypair.pubkey());
let GenesisBlockInfo { genesis_block, .. } = create_genesis_block(1000); let GenesisBlockInfo { genesis_block, .. } = create_genesis_block(1000);
let bank = Arc::new(Bank::new(&genesis_block)); let bank = Arc::new(Bank::new(&genesis_block));
let bank_forks = Arc::new(RwLock::new(BankForks::new_from_banks(&[bank], 0))); let bank_forks = Arc::new(RwLock::new(BankForks::new_from_banks(&[bank.clone()], 0)));
let (_slot_sender, slot_receiver) = channel(); let (_slot_sender, slot_receiver) = channel();
let storage_state = StorageState::new(); let storage_state = StorageState::new(&bank.last_blockhash());
let storage_stage = StorageStage::new( let storage_stage = StorageStage::new(
&storage_state, &storage_state,
slot_receiver, slot_receiver,
@ -674,7 +688,7 @@ mod tests {
let cluster_info = test_cluster_info(&keypair.pubkey()); let cluster_info = test_cluster_info(&keypair.pubkey());
let (bank_sender, bank_receiver) = channel(); let (bank_sender, bank_receiver) = channel();
let storage_state = StorageState::new(); let storage_state = StorageState::new(&bank.last_blockhash());
let storage_stage = StorageStage::new( let storage_stage = StorageStage::new(
&storage_state, &storage_state,
bank_receiver, bank_receiver,
@ -763,7 +777,7 @@ mod tests {
let cluster_info = test_cluster_info(&keypair.pubkey()); let cluster_info = test_cluster_info(&keypair.pubkey());
let (bank_sender, bank_receiver) = channel(); let (bank_sender, bank_receiver) = channel();
let storage_state = StorageState::new(); let storage_state = StorageState::new(&bank.last_blockhash());
let storage_stage = StorageStage::new( let storage_stage = StorageStage::new(
&storage_state, &storage_state,
bank_receiver, bank_receiver,
@ -808,6 +822,7 @@ mod tests {
Hash::default(), Hash::default(),
0, 0,
keypair.sign_message(b"test"), keypair.sign_message(b"test"),
bank.last_blockhash(),
); );
let next_bank = Arc::new(Bank::new_from_parent(&bank, &keypair.pubkey(), 2)); let next_bank = Arc::new(Bank::new_from_parent(&bank, &keypair.pubkey(), 2));

View File

@ -153,7 +153,7 @@ impl Validator {
keypair.clone(), keypair.clone(),
))); )));
let storage_state = StorageState::new(); let storage_state = StorageState::new(&bank.last_blockhash());
let rpc_service = if node.info.rpc.port() == 0 { let rpc_service = if node.info.rpc.port() == 0 {
None None

View File

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

View File

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

View File

@ -1,5 +1,5 @@
use crate::id; 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 serde_derive::{Deserialize, Serialize};
use solana_sdk::hash::Hash; use solana_sdk::hash::Hash;
use solana_sdk::instruction::{AccountMeta, Instruction}; use solana_sdk::instruction::{AccountMeta, Instruction};
@ -7,7 +7,6 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature; use solana_sdk::signature::Signature;
use solana_sdk::syscall::current; use solana_sdk::syscall::current;
use solana_sdk::system_instruction; use solana_sdk::system_instruction;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub enum StorageInstruction { pub enum StorageInstruction {
@ -27,6 +26,7 @@ pub enum StorageInstruction {
sha_state: Hash, sha_state: Hash,
segment_index: usize, segment_index: usize,
signature: Signature, signature: Signature,
blockhash: Hash,
}, },
AdvertiseStorageRecentBlockhash { AdvertiseStorageRecentBlockhash {
hash: Hash, hash: Hash,
@ -37,13 +37,49 @@ pub enum StorageInstruction {
/// Expects 1 Account: /// Expects 1 Account:
/// 0 - Storage account with credits to redeem /// 0 - Storage account with credits to redeem
/// 1 - MiningPool account to redeem credits from /// 1 - MiningPool account to redeem credits from
/// 2 - Replicator account to credit - this account *must* be the owner
ClaimStorageReward, ClaimStorageReward,
ProofValidation { ProofValidation {
/// The segment during which this proof was generated
segment: u64, 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( pub fn create_validator_storage_account(
from_pubkey: &Pubkey, from_pubkey: &Pubkey,
storage_owner: &Pubkey, storage_owner: &Pubkey,
@ -118,11 +154,13 @@ pub fn mining_proof(
sha_state: Hash, sha_state: Hash,
segment_index: usize, segment_index: usize,
signature: Signature, signature: Signature,
blockhash: Hash,
) -> Instruction { ) -> Instruction {
let storage_instruction = StorageInstruction::SubmitMiningProof { let storage_instruction = StorageInstruction::SubmitMiningProof {
sha_state, sha_state,
segment_index, segment_index,
signature, signature,
blockhash,
}; };
let account_metas = vec![ let account_metas = vec![
AccountMeta::new(*storage_pubkey, true), AccountMeta::new(*storage_pubkey, true),
@ -147,26 +185,42 @@ pub fn advertise_recent_blockhash(
Instruction::new(id(), &storage_instruction, account_metas) Instruction::new(id(), &storage_instruction, account_metas)
} }
pub fn proof_validation<S: std::hash::BuildHasher>( pub fn proof_validation(
storage_pubkey: &Pubkey, storage_pubkey: &Pubkey,
segment: u64, segment: u64,
checked_proofs: HashMap<Pubkey, Vec<CheckedProof>, S>, checked_proofs: Vec<(Pubkey, Vec<ProofStatus>)>,
) -> Instruction { ) -> Instruction {
let mut account_metas = vec![AccountMeta::new(*storage_pubkey, true)]; let mut account_metas = vec![AccountMeta::new(*storage_pubkey, true)];
let mut proofs = vec![]; let mut proofs = vec![];
checked_proofs.into_iter().for_each(|(id, p)| { checked_proofs.into_iter().for_each(|(id, p)| {
proofs.push((id, p)); proofs.push(p);
account_metas.push(AccountMeta::new(id, false)) account_metas.push(AccountMeta::new(id, false))
}); });
let storage_instruction = StorageInstruction::ProofValidation { segment, proofs }; let storage_instruction = StorageInstruction::ProofValidation { segment, proofs };
Instruction::new(id(), &storage_instruction, account_metas) 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 storage_instruction = StorageInstruction::ClaimStorageReward;
let account_metas = vec![ let account_metas = vec![
AccountMeta::new(*storage_pubkey, false), AccountMeta::new(*storage_pubkey, false),
AccountMeta::new(*mining_pool_pubkey, false), AccountMeta::new(*mining_pool_pubkey, false),
AccountMeta::new(*owner_pubkey, false),
]; ];
Instruction::new(id(), &storage_instruction, account_metas) 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, rest) = keyed_accounts.split_at_mut(1);
let me_unsigned = me[0].signer_key().is_none(); 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)? { match bincode::deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? {
StorageInstruction::InitializeMiningPool => { StorageInstruction::InitializeMiningPool => {
@ -42,13 +42,20 @@ pub fn process_instruction(
sha_state, sha_state,
segment_index, segment_index,
signature, signature,
blockhash,
} => { } => {
if me_unsigned || rest.len() != 1 { if me_unsigned || rest.len() != 1 {
// This instruction must be signed by `me` // This instruction must be signed by `me`
Err(InstructionError::InvalidArgument)?; Err(InstructionError::InvalidArgument)?;
} }
let current = Current::from(&rest[0].account).unwrap(); 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 } => { StorageInstruction::AdvertiseStorageRecentBlockhash { hash, slot } => {
if me_unsigned || rest.len() != 1 { if me_unsigned || rest.len() != 1 {
@ -59,21 +66,27 @@ pub fn process_instruction(
storage_account.advertise_storage_recent_blockhash(hash, slot, current.slot) storage_account.advertise_storage_recent_blockhash(hash, slot, current.slot)
} }
StorageInstruction::ClaimStorageReward => { StorageInstruction::ClaimStorageReward => {
if rest.len() != 1 { if rest.len() != 2 {
Err(InstructionError::InvalidArgument)?; 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 } => { StorageInstruction::ProofValidation { segment, proofs } => {
if me_unsigned || rest.is_empty() { if me_unsigned || rest.is_empty() {
// This instruction must be signed by `me` and `rest` cannot be empty // This instruction must be signed by `me` and `rest` cannot be empty
Err(InstructionError::InvalidArgument)?; Err(InstructionError::InvalidArgument)?;
} }
let me_id = storage_account.id;
let mut rest: Vec<_> = rest let mut rest: Vec<_> = rest
.iter_mut() .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(); .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::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::syscall::current; use solana_sdk::syscall::current;
use solana_sdk::syscall::current::Current; use solana_sdk::syscall::current::Current;
use solana_sdk::system_instruction;
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;
use solana_storage_api::storage_contract::StorageAccount; use solana_storage_api::storage_contract::StorageAccount;
use solana_storage_api::storage_contract::{ use solana_storage_api::storage_contract::{
CheckedProof, Proof, ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE, ProofStatus, StorageContract, REPLICATOR_REWARD, STORAGE_ACCOUNT_SPACE, VALIDATOR_REWARD,
TOTAL_REPLICATOR_REWARDS, TOTAL_VALIDATOR_REWARDS,
}; };
use solana_storage_api::storage_instruction; use solana_storage_api::storage_instruction;
use solana_storage_api::storage_processor::process_instruction; use solana_storage_api::storage_processor::process_instruction;
@ -108,13 +108,19 @@ fn test_proof_bounds() {
..Account::default() ..Account::default()
}; };
{ {
let mut storage_account = StorageAccount::new(&mut account); let mut storage_account = StorageAccount::new(pubkey, &mut account);
storage_account storage_account
.initialize_replicator_storage(account_owner) .initialize_replicator_storage(account_owner)
.unwrap(); .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 // the proof is for segment 0, need to move the slot into segment 2
let mut current_account = current::create_account(1); let mut current_account = current::create_account(1);
Current::to( Current::to(
@ -167,7 +173,13 @@ fn test_invalid_accounts_len() {
let pubkey = Pubkey::new_rand(); let pubkey = Pubkey::new_rand();
let mut accounts = [Account::default()]; 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 // move tick height into segment 1
let mut current_account = current::create_account(1); let mut current_account = current::create_account(1);
Current::to( Current::to(
@ -194,7 +206,13 @@ fn test_submit_mining_invalid_slot() {
accounts[0].data.resize(STORAGE_ACCOUNT_SPACE as usize, 0); accounts[0].data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
accounts[1].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 // submitting a proof for a slot in the past, so this should fail
assert!(test_instruction(&ix, &mut accounts).is_err()); assert!(test_instruction(&ix, &mut accounts).is_err());
@ -208,13 +226,19 @@ fn test_submit_mining_ok() {
let mut account = Account::default(); let mut account = Account::default();
account.data.resize(STORAGE_ACCOUNT_SPACE as usize, 0); 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 storage_account
.initialize_replicator_storage(account_owner) .initialize_replicator_storage(account_owner)
.unwrap(); .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 // move slot into segment 1
let mut current_account = current::create_account(1); let mut current_account = current::create_account(1);
Current::to( Current::to(
@ -235,11 +259,13 @@ fn test_submit_mining_ok() {
#[test] #[test]
fn test_validate_mining() { fn test_validate_mining() {
solana_logger::setup(); 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 genesis_block
.native_instruction_processors .native_instruction_processors
.push(solana_storage_program::solana_storage_program!()); .push(solana_storage_program::solana_storage_program!());
let mint_pubkey = mint_keypair.pubkey(); 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_keypair = Keypair::new();
let replicator_1_storage_id = replicator_1_storage_keypair.pubkey(); 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); let bank_client = BankClient::new_shared(&bank);
init_storage_accounts( init_storage_accounts(
&owner_pubkey,
&bank_client, &bank_client,
&mint_keypair, &mint_keypair,
&[&validator_storage_id], &[&validator_storage_id],
@ -267,7 +294,7 @@ fn test_validate_mining() {
let message = Message::new(storage_instruction::create_mining_pool_account( let message = Message::new(storage_instruction::create_mining_pool_account(
&mint_pubkey, &mint_pubkey,
&mining_pool_pubkey, &mining_pool_pubkey,
100, 10_000,
)); ));
bank_client.send_message(&[&mint_keypair], message).unwrap(); bank_client.send_message(&[&mint_keypair], message).unwrap();
@ -342,7 +369,7 @@ fn test_validate_mining() {
vec![storage_instruction::proof_validation( vec![storage_instruction::proof_validation(
&validator_storage_id, &validator_storage_id,
proof_segment as u64, proof_segment as u64,
checked_proofs, checked_proofs.into_iter().map(|entry| entry).collect(),
)], )],
Some(&mint_pubkey), Some(&mint_pubkey),
); );
@ -378,6 +405,7 @@ fn test_validate_mining() {
let message = Message::new_with_payer( let message = Message::new_with_payer(
vec![storage_instruction::claim_reward( vec![storage_instruction::claim_reward(
&owner_pubkey,
&validator_storage_id, &validator_storage_id,
&mining_pool_pubkey, &mining_pool_pubkey,
)], )],
@ -385,8 +413,8 @@ fn test_validate_mining() {
); );
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_)); assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
assert_eq!( assert_eq!(
bank_client.get_balance(&validator_storage_id).unwrap(), bank_client.get_balance(&owner_pubkey).unwrap(),
10 + (TOTAL_VALIDATOR_REWARDS * 10) 1 + (VALIDATOR_REWARD * 10)
); );
// tick the bank into the next storage epoch so that rewards can be claimed // 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( let message = Message::new_with_payer(
vec![storage_instruction::claim_reward( vec![storage_instruction::claim_reward(
&owner_pubkey,
&replicator_1_storage_id, &replicator_1_storage_id,
&mining_pool_pubkey, &mining_pool_pubkey,
)], )],
Some(&mint_pubkey), Some(&mint_pubkey),
); );
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_)); 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( let message = Message::new_with_payer(
vec![storage_instruction::claim_reward( vec![storage_instruction::claim_reward(
&owner_pubkey,
&replicator_2_storage_id, &replicator_2_storage_id,
&mining_pool_pubkey, &mining_pool_pubkey,
)], )],
@ -417,37 +453,44 @@ fn test_validate_mining() {
); );
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_)); assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
// TODO enable when rewards are working
assert_eq!( assert_eq!(
bank_client.get_balance(&replicator_1_storage_id).unwrap(), bank_client.get_balance(&owner_pubkey).unwrap(),
10 + (TOTAL_REPLICATOR_REWARDS * 5) 1 + (VALIDATOR_REWARD * 10) + (REPLICATOR_REWARD * 10)
); );
} }
fn init_storage_accounts( fn init_storage_accounts(
owner: &Pubkey,
client: &BankClient, client: &BankClient,
mint: &Keypair, mint: &Keypair,
validator_accounts_to_create: &[&Pubkey], validator_accounts_to_create: &[&Pubkey],
replicator_accounts_to_create: &[&Pubkey], replicator_accounts_to_create: &[&Pubkey],
lamports: u64, lamports: u64,
) { ) {
let mut ixs: Vec<_> = validator_accounts_to_create let mut ixs: Vec<_> = vec![system_instruction::create_user_account(
.into_iter() &mint.pubkey(),
.flat_map(|account| { owner,
storage_instruction::create_validator_storage_account( 1,
&mint.pubkey(), )];
&Pubkey::default(), ixs.append(
account, &mut validator_accounts_to_create
lamports, .into_iter()
) .flat_map(|account| {
}) storage_instruction::create_validator_storage_account(
.collect(); &mint.pubkey(),
owner,
account,
lamports,
)
})
.collect(),
);
replicator_accounts_to_create replicator_accounts_to_create
.into_iter() .into_iter()
.for_each(|account| { .for_each(|account| {
ixs.append(&mut storage_instruction::create_replicator_storage_account( ixs.append(&mut storage_instruction::create_replicator_storage_account(
&mint.pubkey(), &mint.pubkey(),
&Pubkey::default(), owner,
account, account,
lamports, lamports,
)) ))
@ -481,7 +524,7 @@ fn submit_proof(
storage_keypair: &Keypair, storage_keypair: &Keypair,
bank_client: &BankClient, bank_client: &BankClient,
segment_index: u64, segment_index: u64,
) -> CheckedProof { ) -> ProofStatus {
let sha_state = Hash::new(Pubkey::new_rand().as_ref()); let sha_state = Hash::new(Pubkey::new_rand().as_ref());
let message = Message::new_with_payer( let message = Message::new_with_payer(
vec![storage_instruction::mining_proof( vec![storage_instruction::mining_proof(
@ -489,6 +532,7 @@ fn submit_proof(
sha_state, sha_state,
segment_index as usize, segment_index as usize,
Signature::default(), Signature::default(),
bank_client.get_recent_blockhash().unwrap().0,
)], )],
Some(&mint_keypair.pubkey()), Some(&mint_keypair.pubkey()),
); );
@ -497,14 +541,7 @@ fn submit_proof(
bank_client.send_message(&[&mint_keypair, &storage_keypair], message), bank_client.send_message(&[&mint_keypair, &storage_keypair], message),
Ok(_) Ok(_)
); );
CheckedProof { ProofStatus::Valid
proof: Proof {
signature: Signature::default(),
sha_state,
segment_index: segment_index as usize,
},
status: ProofStatus::Valid,
}
} }
fn get_storage_blockhash<C: SyncClient>(client: &C, account: &Pubkey) -> Hash { fn get_storage_blockhash<C: SyncClient>(client: &C, account: &Pubkey) -> Hash {
@ -585,6 +622,7 @@ fn test_bank_storage() {
Hash::default(), Hash::default(),
slot, slot,
Signature::default(), Signature::default(),
bank_client.get_recent_blockhash().unwrap().0,
)], )],
Some(&mint_pubkey), Some(&mint_pubkey),
); );

View File

@ -2,8 +2,8 @@ use clap::{crate_description, crate_name, crate_version, App, Arg};
use solana::cluster_info::{Node, FULLNODE_PORT_RANGE}; use solana::cluster_info::{Node, FULLNODE_PORT_RANGE};
use solana::contact_info::ContactInfo; use solana::contact_info::ContactInfo;
use solana::replicator::Replicator; use solana::replicator::Replicator;
use solana::socketaddr;
use solana_sdk::signature::{read_keypair, Keypair, KeypairUtil}; use solana_sdk::signature::{read_keypair, Keypair, KeypairUtil};
use std::net::SocketAddr;
use std::process::exit; use std::process::exit;
use std::sync::Arc; use std::sync::Arc;
@ -77,7 +77,8 @@ fn main() {
.unwrap(); .unwrap();
let gossip_addr = { let gossip_addr = {
let mut addr = socketaddr!([127, 0, 0, 1], 8700); let ip = solana_netutil::get_public_ip_addr(&entrypoint_addr).unwrap();
let mut addr = SocketAddr::new(ip, 0);
addr.set_ip(solana_netutil::get_public_ip_addr(&entrypoint_addr).unwrap()); addr.set_ip(solana_netutil::get_public_ip_addr(&entrypoint_addr).unwrap());
addr addr
}; };

View File

@ -58,7 +58,7 @@ pub enum WalletCommand {
CreateStorageMiningPoolAccount(Pubkey, u64), CreateStorageMiningPoolAccount(Pubkey, u64),
CreateReplicatorStorageAccount(Pubkey, Pubkey), CreateReplicatorStorageAccount(Pubkey, Pubkey),
CreateValidatorStorageAccount(Pubkey, Pubkey), CreateValidatorStorageAccount(Pubkey, Pubkey),
ClaimStorageReward(Pubkey, Pubkey), ClaimStorageReward(Pubkey, Pubkey, Pubkey),
ShowStorageAccount(Pubkey), ShowStorageAccount(Pubkey),
Deploy(String), Deploy(String),
GetTransactionCount, GetTransactionCount,
@ -299,10 +299,12 @@ pub fn parse_command(
)) ))
} }
("claim-storage-reward", Some(matches)) => { ("claim-storage-reward", Some(matches)) => {
let node_account_pubkey = value_of(matches, "node_account_pubkey").unwrap();
let storage_mining_pool_account_pubkey = let storage_mining_pool_account_pubkey =
value_of(matches, "storage_mining_pool_account_pubkey").unwrap(); value_of(matches, "storage_mining_pool_account_pubkey").unwrap();
let storage_account_pubkey = value_of(matches, "storage_account_pubkey").unwrap(); let storage_account_pubkey = value_of(matches, "storage_account_pubkey").unwrap();
Ok(WalletCommand::ClaimStorageReward( Ok(WalletCommand::ClaimStorageReward(
node_account_pubkey,
storage_mining_pool_account_pubkey, storage_mining_pool_account_pubkey,
storage_account_pubkey, storage_account_pubkey,
)) ))
@ -734,12 +736,14 @@ fn process_create_validator_storage_account(
fn process_claim_storage_reward( fn process_claim_storage_reward(
rpc_client: &RpcClient, rpc_client: &RpcClient,
config: &WalletConfig, config: &WalletConfig,
node_account_pubkey: &Pubkey,
storage_mining_pool_account_pubkey: &Pubkey, storage_mining_pool_account_pubkey: &Pubkey,
storage_account_pubkey: &Pubkey, storage_account_pubkey: &Pubkey,
) -> ProcessResult { ) -> ProcessResult {
let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?; let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?;
let instruction = storage_instruction::claim_reward( let instruction = storage_instruction::claim_reward(
node_account_pubkey,
storage_account_pubkey, storage_account_pubkey,
storage_mining_pool_account_pubkey, storage_mining_pool_account_pubkey,
); );
@ -763,7 +767,7 @@ fn process_show_storage_account(
format!("Unable to deserialize storage account: {:?}", err).to_string(), format!("Unable to deserialize storage account: {:?}", err).to_string(),
) )
})?; })?;
println!("{:?}", storage_contract); println!("{:#?}", storage_contract);
println!("account lamports: {}", account.lamports); println!("account lamports: {}", account.lamports);
Ok("".to_string()) Ok("".to_string())
} }
@ -1126,11 +1130,13 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
} }
WalletCommand::ClaimStorageReward( WalletCommand::ClaimStorageReward(
node_account_pubkey,
storage_mining_pool_account_pubkey, storage_mining_pool_account_pubkey,
storage_account_pubkey, storage_account_pubkey,
) => process_claim_storage_reward( ) => process_claim_storage_reward(
&rpc_client, &rpc_client,
config, config,
node_account_pubkey,
&storage_mining_pool_account_pubkey, &storage_mining_pool_account_pubkey,
&storage_account_pubkey, &storage_account_pubkey,
), ),
@ -1591,8 +1597,17 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
SubCommand::with_name("claim-storage-reward") SubCommand::with_name("claim-storage-reward")
.about("Redeem storage reward credits") .about("Redeem storage reward credits")
.arg( .arg(
Arg::with_name("storage_mining_pool_account_pubkey") Arg::with_name("node_account_pubkey")
.index(1) .index(1)
.value_name("NODE PUBKEY")
.takes_value(true)
.required(true)
.validator(is_pubkey)
.help("The node account to credit the rewards to"),
)
.arg(
Arg::with_name("storage_mining_pool_account_pubkey")
.index(2)
.value_name("MINING POOL PUBKEY") .value_name("MINING POOL PUBKEY")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
@ -1601,21 +1616,13 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
) )
.arg( .arg(
Arg::with_name("storage_account_pubkey") Arg::with_name("storage_account_pubkey")
.index(2) .index(3)
.value_name("STORAGE ACCOUNT PUBKEY") .value_name("STORAGE ACCOUNT PUBKEY")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.validator(is_pubkey) .validator(is_pubkey)
.help("Storage account address to redeem credits for"), .help("Storage account address to redeem credits for"),
) ))
.arg(
Arg::with_name("slot")
.index(3)
.value_name("SLOT")
.takes_value(true)
.required(true)
.help("The slot to claim rewards for"),
),)
.subcommand( .subcommand(
SubCommand::with_name("show-storage-account") SubCommand::with_name("show-storage-account")