2019-03-22 06:47:05 -06:00
|
|
|
//! storage program
|
|
|
|
//! Receive mining proofs from miners, validate the answers
|
|
|
|
//! and give reward for good proofs.
|
2019-04-04 12:01:09 -07:00
|
|
|
use crate::storage_contract::StorageAccount;
|
2019-03-22 14:30:18 -06:00
|
|
|
use crate::storage_instruction::StorageInstruction;
|
2019-04-04 12:01:09 -07:00
|
|
|
use solana_sdk::account::KeyedAccount;
|
2019-03-23 21:12:27 -06:00
|
|
|
use solana_sdk::instruction::InstructionError;
|
2019-03-22 06:47:05 -06:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-06-11 18:27:47 -07:00
|
|
|
use solana_sdk::syscall::current::Current;
|
2019-04-02 13:08:55 -07:00
|
|
|
|
2019-03-22 06:47:05 -06:00
|
|
|
pub fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-04-04 12:01:09 -07:00
|
|
|
let (me, rest) = keyed_accounts.split_at_mut(1);
|
2019-05-23 14:50:23 -07:00
|
|
|
let me_unsigned = me[0].signer_key().is_none();
|
2019-04-04 12:01:09 -07:00
|
|
|
let mut storage_account = StorageAccount::new(&mut me[0].account);
|
|
|
|
|
|
|
|
match bincode::deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? {
|
2019-05-23 14:50:23 -07:00
|
|
|
StorageInstruction::InitializeMiningPool => {
|
|
|
|
if !rest.is_empty() {
|
|
|
|
Err(InstructionError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
storage_account.initialize_mining_pool()
|
|
|
|
}
|
2019-06-04 14:52:52 -07:00
|
|
|
StorageInstruction::InitializeReplicatorStorage { owner } => {
|
2019-05-23 14:50:23 -07:00
|
|
|
if !rest.is_empty() {
|
|
|
|
Err(InstructionError::InvalidArgument)?;
|
|
|
|
}
|
2019-06-04 14:52:52 -07:00
|
|
|
storage_account.initialize_replicator_storage(owner)
|
2019-05-23 14:50:23 -07:00
|
|
|
}
|
2019-06-04 14:52:52 -07:00
|
|
|
StorageInstruction::InitializeValidatorStorage { owner } => {
|
2019-05-23 14:50:23 -07:00
|
|
|
if !rest.is_empty() {
|
|
|
|
Err(InstructionError::InvalidArgument)?;
|
|
|
|
}
|
2019-06-04 14:52:52 -07:00
|
|
|
storage_account.initialize_validator_storage(owner)
|
2019-05-23 14:50:23 -07:00
|
|
|
}
|
2019-04-04 12:01:09 -07:00
|
|
|
StorageInstruction::SubmitMiningProof {
|
|
|
|
sha_state,
|
2019-06-11 18:27:47 -07:00
|
|
|
segment_index,
|
2019-04-04 12:01:09 -07:00
|
|
|
signature,
|
|
|
|
} => {
|
2019-05-31 16:29:21 -06:00
|
|
|
if me_unsigned || rest.len() != 1 {
|
2019-05-23 14:50:23 -07:00
|
|
|
// This instruction must be signed by `me`
|
2019-04-04 12:01:09 -07:00
|
|
|
Err(InstructionError::InvalidArgument)?;
|
|
|
|
}
|
2019-06-11 18:27:47 -07:00
|
|
|
let current = Current::from(&rest[0].account).unwrap();
|
|
|
|
storage_account.submit_mining_proof(sha_state, segment_index, signature, current.slot)
|
2019-04-04 12:01:09 -07:00
|
|
|
}
|
2019-05-03 16:27:53 -07:00
|
|
|
StorageInstruction::AdvertiseStorageRecentBlockhash { hash, slot } => {
|
2019-05-31 16:29:21 -06:00
|
|
|
if me_unsigned || rest.len() != 1 {
|
2019-05-23 14:50:23 -07:00
|
|
|
// This instruction must be signed by `me`
|
2019-04-04 12:01:09 -07:00
|
|
|
Err(InstructionError::InvalidArgument)?;
|
2019-03-22 06:47:05 -06:00
|
|
|
}
|
2019-06-11 18:27:47 -07:00
|
|
|
let current = Current::from(&rest[0].account).unwrap();
|
|
|
|
storage_account.advertise_storage_recent_blockhash(hash, slot, current.slot)
|
2019-04-04 12:01:09 -07:00
|
|
|
}
|
2019-06-11 18:27:47 -07:00
|
|
|
StorageInstruction::ClaimStorageReward => {
|
|
|
|
if rest.len() != 1 {
|
2019-04-04 12:01:09 -07:00
|
|
|
Err(InstructionError::InvalidArgument)?;
|
2019-04-02 13:08:55 -07:00
|
|
|
}
|
2019-06-11 18:27:47 -07:00
|
|
|
storage_account.claim_storage_reward(&mut rest[0])
|
2019-04-04 12:01:09 -07:00
|
|
|
}
|
2019-05-24 14:49:10 -07:00
|
|
|
StorageInstruction::ProofValidation { segment, proofs } => {
|
2019-05-23 14:50:23 -07:00
|
|
|
if me_unsigned || rest.is_empty() {
|
2019-05-24 14:49:10 -07:00
|
|
|
// This instruction must be signed by `me` and `rest` cannot be empty
|
2019-04-04 12:01:09 -07:00
|
|
|
Err(InstructionError::InvalidArgument)?;
|
2019-03-22 06:47:05 -06:00
|
|
|
}
|
2019-05-23 14:50:23 -07:00
|
|
|
let mut rest: Vec<_> = rest
|
|
|
|
.iter_mut()
|
|
|
|
.map(|keyed_account| StorageAccount::new(&mut keyed_account.account))
|
|
|
|
.collect();
|
2019-05-24 14:49:10 -07:00
|
|
|
storage_account.proof_validation(segment, proofs, &mut rest)
|
2019-03-22 06:47:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|