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,

View File

@ -1,26 +1,32 @@
use assert_matches::assert_matches;
use bincode::deserialize;
use log::*;
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_runtime::genesis_utils::{create_genesis_block, GenesisBlockInfo};
use solana_sdk::account::{create_keyed_accounts, Account, KeyedAccount};
use solana_sdk::account_utils::State;
use solana_sdk::client::SyncClient;
use solana_sdk::clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT};
use solana_sdk::hash::{hash, Hash};
use solana_sdk::instruction::{Instruction, InstructionError};
use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::system_instruction;
use solana_sdk::sysvar::clock::{self, Clock};
use solana_sdk::sysvar::rewards::{self, Rewards};
use solana_storage_api::id;
use solana_storage_api::storage_contract::StorageAccount;
use solana_storage_api::storage_contract::{ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE};
use solana_storage_api::storage_instruction;
use solana_storage_api::storage_processor::process_instruction;
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
genesis_utils::{create_genesis_block, GenesisBlockInfo},
};
use solana_sdk::{
account::{create_keyed_accounts, Account, KeyedAccount},
account_utils::State,
client::SyncClient,
clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT},
hash::{hash, Hash},
instruction::{Instruction, InstructionError},
message::Message,
pubkey::Pubkey,
signature::{Keypair, KeypairUtil, Signature},
system_instruction,
sysvar::clock::{self, Clock},
sysvar::rewards::{self, Rewards},
};
use solana_storage_api::{
id,
storage_contract::StorageAccount,
storage_contract::{ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE},
storage_instruction::{self, StorageAccountType},
storage_processor::process_instruction,
};
use std::collections::HashMap;
use std::sync::Arc;
@ -61,11 +67,12 @@ fn test_account_owner() {
let bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);
let message = Message::new(storage_instruction::create_validator_storage_account(
let message = Message::new(storage_instruction::create_storage_account(
&mint_pubkey,
&account_owner,
&validator_storage_pubkey,
1,
StorageAccountType::Validator,
));
bank_client
.send_message(&[&mint_keypair], message)
@ -80,11 +87,12 @@ fn test_account_owner() {
assert!(false, "wrong account type found")
}
let message = Message::new(storage_instruction::create_replicator_storage_account(
let message = Message::new(storage_instruction::create_storage_account(
&mint_pubkey,
&account_owner,
&replicator_storage_pubkey,
1,
StorageAccountType::Replicator,
));
bank_client
.send_message(&[&mint_keypair], message)
@ -111,7 +119,7 @@ fn test_proof_bounds() {
{
let mut storage_account = StorageAccount::new(pubkey, &mut account);
storage_account
.initialize_replicator_storage(account_owner)
.initialize_storage(account_owner, StorageAccountType::Replicator)
.unwrap();
}
@ -224,7 +232,7 @@ fn test_submit_mining_ok() {
{
let mut storage_account = StorageAccount::new(pubkey, &mut account);
storage_account
.initialize_replicator_storage(account_owner)
.initialize_storage(account_owner, StorageAccountType::Replicator)
.unwrap();
}
@ -473,11 +481,12 @@ fn init_storage_accounts(
&mut validator_accounts_to_create
.into_iter()
.flat_map(|account| {
storage_instruction::create_validator_storage_account(
storage_instruction::create_storage_account(
&mint.pubkey(),
owner,
account,
lamports,
StorageAccountType::Validator,
)
})
.collect(),
@ -485,11 +494,12 @@ fn init_storage_accounts(
replicator_accounts_to_create
.into_iter()
.for_each(|account| {
ixs.append(&mut storage_instruction::create_replicator_storage_account(
ixs.append(&mut storage_instruction::create_storage_account(
&mint.pubkey(),
owner,
account,
lamports,
StorageAccountType::Replicator,
))
});
let message = Message::new(ixs);
@ -590,19 +600,21 @@ fn test_bank_storage() {
.transfer(10, &mint_keypair, &replicator_pubkey)
.unwrap();
let message = Message::new(storage_instruction::create_replicator_storage_account(
let message = Message::new(storage_instruction::create_storage_account(
&mint_pubkey,
&Pubkey::default(),
&replicator_pubkey,
1,
StorageAccountType::Replicator,
));
bank_client.send_message(&[&mint_keypair], message).unwrap();
let message = Message::new(storage_instruction::create_validator_storage_account(
let message = Message::new(storage_instruction::create_storage_account(
&mint_pubkey,
&Pubkey::default(),
&validator_pubkey,
1,
StorageAccountType::Validator,
));
bank_client.send_message(&[&mint_keypair], message).unwrap();