Refactor Storage Program (#3622)

* Refactor Storage Program

* Replace KeyedAccount trait with StorageAccount struct

* Implement State for Account, not StorageAccount

* Make State trait more generic

* Move validation check into function
This commit is contained in:
Sagar Dhawan
2019-04-04 12:01:09 -07:00
committed by GitHub
parent 1598a02a7a
commit 0b23af324b
5 changed files with 467 additions and 382 deletions

View File

@@ -5,10 +5,10 @@
//use crate::{check_id, id};
//use log::*;
use bincode::{deserialize, serialize_into, ErrorKind};
use serde_derive::{Deserialize, Serialize};
use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError;
use solana_sdk::instruction_processor_utils::State;
use solana_sdk::pubkey::Pubkey;
use solana_vote_api::vote_state::VoteState;
@@ -39,26 +39,6 @@ pub trait StakeAccount {
) -> Result<(), InstructionError>;
}
pub trait State<T> {
fn state(&self) -> Result<T, InstructionError>;
fn set_state(&mut self, state: &T) -> Result<(), InstructionError>;
}
impl<'a, T> State<T> for KeyedAccount<'a>
where
T: serde::Serialize + serde::de::DeserializeOwned,
{
fn state(&self) -> Result<T, InstructionError> {
deserialize(&self.account.data).map_err(|_| InstructionError::InvalidAccountData)
}
fn set_state(&mut self, state: &T) -> Result<(), InstructionError> {
serialize_into(&mut self.account.data[..], state).map_err(|err| match *err {
ErrorKind::SizeLimit => InstructionError::AccountDataTooSmall,
_ => InstructionError::GenericError,
})
}
}
impl<'a> StakeAccount for KeyedAccount<'a> {
fn delegate_stake(&mut self, vote_account: &KeyedAccount) -> Result<(), InstructionError> {
if self.signer_key().is_none() {