Cli refactor: vote and storage program functionalities (#6242)

automerge
This commit is contained in:
Tyera Eulberg
2019-10-04 15:18:19 -06:00
committed by Grimes
parent 7f53737000
commit 0c3ff6b75c
12 changed files with 611 additions and 543 deletions

View File

@@ -1,3 +1,4 @@
use crate::storage_instruction::StorageAccountType;
use log::*;
use num_derive::FromPrimitive;
use serde_derive::{Deserialize, Serialize};
@@ -130,30 +131,27 @@ impl<'a> StorageAccount<'a> {
Self { id, account }
}
pub fn initialize_replicator_storage(&mut self, owner: Pubkey) -> Result<(), InstructionError> {
pub fn initialize_storage(
&mut self,
owner: Pubkey,
account_type: StorageAccountType,
) -> Result<(), InstructionError> {
let storage_contract = &mut self.account.state()?;
if let StorageContract::Uninitialized = storage_contract {
*storage_contract = StorageContract::ReplicatorStorage {
owner,
proofs: BTreeMap::new(),
validations: BTreeMap::new(),
credits: Credits::default(),
};
self.account.set_state(storage_contract)
} else {
Err(InstructionError::AccountAlreadyInitialized)
}
}
pub fn initialize_validator_storage(&mut self, owner: Pubkey) -> Result<(), InstructionError> {
let storage_contract = &mut self.account.state()?;
if let StorageContract::Uninitialized = storage_contract {
*storage_contract = StorageContract::ValidatorStorage {
owner,
segment: 0,
hash: Hash::default(),
lockout_validations: BTreeMap::new(),
credits: Credits::default(),
*storage_contract = match account_type {
StorageAccountType::Replicator => StorageContract::ReplicatorStorage {
owner,
proofs: BTreeMap::new(),
validations: BTreeMap::new(),
credits: Credits::default(),
},
StorageAccountType::Validator => StorageContract::ValidatorStorage {
owner,
segment: 0,
hash: Hash::default(),
lockout_validations: BTreeMap::new(),
credits: Credits::default(),
},
};
self.account.set_state(storage_contract)
} else {

View File

@@ -8,17 +8,21 @@ use solana_sdk::signature::Signature;
use solana_sdk::system_instruction;
use solana_sdk::sysvar::{clock, rewards};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
pub enum StorageAccountType {
Replicator,
Validator,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum StorageInstruction {
/// Initialize the account as a validator or replicator
///
/// Expects 1 Account:
/// 0 - Account to be initialized
InitializeValidatorStorage {
owner: Pubkey,
},
InitializeReplicatorStorage {
InitializeStorage {
owner: Pubkey,
account_type: StorageAccountType,
},
SubmitMiningProof {
@@ -81,11 +85,12 @@ pub fn proof_mask_limit() -> u64 {
bytes - ratio
}
pub fn create_validator_storage_account(
pub fn create_storage_account(
from_pubkey: &Pubkey,
storage_owner: &Pubkey,
storage_pubkey: &Pubkey,
lamports: u64,
account_type: StorageAccountType,
) -> Vec<Instruction> {
vec![
system_instruction::create_account(
@@ -97,32 +102,9 @@ pub fn create_validator_storage_account(
),
Instruction::new(
id(),
&StorageInstruction::InitializeValidatorStorage {
owner: *storage_owner,
},
vec![AccountMeta::new(*storage_pubkey, false)],
),
]
}
pub fn create_replicator_storage_account(
from_pubkey: &Pubkey,
storage_owner: &Pubkey,
storage_pubkey: &Pubkey,
lamports: u64,
) -> Vec<Instruction> {
vec![
system_instruction::create_account(
from_pubkey,
storage_pubkey,
lamports,
STORAGE_ACCOUNT_SPACE,
&id(),
),
Instruction::new(
id(),
&StorageInstruction::InitializeReplicatorStorage {
&StorageInstruction::InitializeStorage {
owner: *storage_owner,
account_type,
},
vec![AccountMeta::new(*storage_pubkey, false)],
),

View File

@@ -20,17 +20,14 @@ pub fn process_instruction(
let mut storage_account = StorageAccount::new(*me[0].unsigned_key(), &mut me[0].account);
match bincode::deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? {
StorageInstruction::InitializeReplicatorStorage { owner } => {
StorageInstruction::InitializeStorage {
owner,
account_type,
} => {
if !rest.is_empty() {
return Err(InstructionError::InvalidArgument);
}
storage_account.initialize_replicator_storage(owner)
}
StorageInstruction::InitializeValidatorStorage { owner } => {
if !rest.is_empty() {
return Err(InstructionError::InvalidArgument);
}
storage_account.initialize_validator_storage(owner)
storage_account.initialize_storage(owner, account_type)
}
StorageInstruction::SubmitMiningProof {
sha_state,