Move types to more appropriate files (#10638)

This commit is contained in:
Jack May 2020-06-17 10:39:14 -07:00 committed by GitHub
parent 39984cdcc3
commit dac7dc2f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 97 additions and 98 deletions

View File

@ -39,13 +39,13 @@ use solana_sdk::{
bpf_loader,
clock::{Epoch, Slot, DEFAULT_TICKS_PER_SECOND},
commitment_config::CommitmentConfig,
decode_error::DecodeError,
fee_calculator::FeeCalculator,
hash::Hash,
instruction::InstructionError,
loader_instruction,
message::Message,
native_token::lamports_to_sol,
program_utils::DecodeError,
pubkey::{Pubkey, MAX_SEED_LEN},
signature::{Keypair, Signature, Signer, SignerError},
signers::Signers,

View File

@ -5,11 +5,11 @@ use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use solana_sdk::{
account_info::AccountInfo,
decode_error::DecodeError,
entrypoint,
entrypoint::ProgramResult,
info,
program_error::{PrintProgramError, ProgramError},
program_utils::DecodeError,
pubkey::Pubkey,
};
use thiserror::Error;

View File

@ -12,14 +12,14 @@ use solana_rbpf::{
EbpfVm,
};
use solana_sdk::{
account::KeyedAccount,
account::{is_executable, next_keyed_account, KeyedAccount},
bpf_loader,
decode_error::DecodeError,
entrypoint::SUCCESS,
entrypoint_native::InvokeContext,
instruction::InstructionError,
loader_instruction::LoaderInstruction,
program_utils::DecodeError,
program_utils::{is_executable, limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
};
use std::{io::prelude::*, mem};

View File

@ -4,9 +4,9 @@ use chrono::prelude::{DateTime, Utc};
use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::{Deserialize, Serialize};
use solana_sdk::{
decode_error::DecodeError,
hash::Hash,
instruction::{AccountMeta, Instruction},
program_utils::DecodeError,
pubkey::Pubkey,
system_instruction,
};

View File

@ -7,10 +7,10 @@ use crate::{
use chrono::prelude::{DateTime, Utc};
use log::*;
use solana_sdk::{
account::KeyedAccount,
account::{next_keyed_account, KeyedAccount},
hash::hash,
instruction::InstructionError,
program_utils::{limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
};

View File

@ -3,9 +3,9 @@
use crate::ConfigKeys;
use bincode::deserialize;
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::account::{next_keyed_account, KeyedAccount};
use solana_sdk::instruction::InstructionError;
use solana_sdk::program_utils::{limited_deserialize, next_keyed_account};
use solana_sdk::program_utils::limited_deserialize;
use solana_sdk::pubkey::Pubkey;
pub fn process_instruction(

View File

@ -8,8 +8,8 @@ use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::Serialize;
use solana_metrics::inc_new_counter_info;
use solana_sdk::{
account::KeyedAccount, instruction::InstructionError, program_utils::limited_deserialize,
program_utils::DecodeError, pubkey::Pubkey,
account::KeyedAccount, decode_error::DecodeError, instruction::InstructionError,
program_utils::limited_deserialize, pubkey::Pubkey,
};
use std::cmp;
use thiserror::Error;

View File

@ -6,13 +6,13 @@ use log::*;
use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::{Deserialize, Serialize};
use solana_sdk::{
account::KeyedAccount,
account::{is_executable, next_keyed_account, KeyedAccount},
account_utils::State,
decode_error::DecodeError,
entrypoint_native::InvokeContext,
instruction::InstructionError,
move_loader::id,
program_utils::DecodeError,
program_utils::{is_executable, limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
};
use thiserror::Error;

View File

@ -1,7 +1,7 @@
use num_derive::{FromPrimitive, ToPrimitive};
use solana_sdk::{
decode_error::DecodeError,
instruction::{AccountMeta, Instruction},
program_utils::DecodeError,
pubkey::Pubkey,
system_instruction,
};

View File

@ -3,9 +3,9 @@
use crate::ownable_instruction::OwnableError;
use bincode::serialize_into;
use solana_sdk::{
account::KeyedAccount,
account::{next_keyed_account, KeyedAccount},
instruction::InstructionError,
program_utils::{limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
};

View File

@ -6,10 +6,11 @@ use log::*;
use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::{Deserialize, Serialize};
use solana_sdk::{
account::{get_signers, KeyedAccount},
account::{get_signers, next_keyed_account, KeyedAccount},
clock::{Epoch, UnixTimestamp},
decode_error::DecodeError,
instruction::{AccountMeta, Instruction, InstructionError},
program_utils::{limited_deserialize, next_keyed_account, DecodeError},
program_utils::limited_deserialize,
pubkey::Pubkey,
system_instruction,
sysvar::{self, clock::Clock, rent::Rent, stake_history::StakeHistory, Sysvar},

View File

@ -4,8 +4,8 @@ use chrono::prelude::{Date, DateTime, Utc};
use num_derive::FromPrimitive;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::{
decode_error::DecodeError,
instruction::{AccountMeta, Instruction, InstructionError},
program_utils::DecodeError,
pubkey::Pubkey,
system_instruction,
};

View File

@ -7,9 +7,9 @@ use chrono::prelude::*;
use solana_config_program::date_instruction::DateConfig;
use solana_config_program::get_config_data;
use solana_sdk::{
account::{Account, KeyedAccount},
account::{next_keyed_account, Account, KeyedAccount},
instruction::InstructionError,
program_utils::{limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
};
use std::cell::RefMut;

View File

@ -10,10 +10,11 @@ use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::{Deserialize, Serialize};
use solana_metrics::inc_new_counter_info;
use solana_sdk::{
account::{get_signers, KeyedAccount},
account::{get_signers, next_keyed_account, KeyedAccount},
decode_error::DecodeError,
hash::Hash,
instruction::{AccountMeta, Instruction, InstructionError},
program_utils::{limited_deserialize, next_keyed_account, DecodeError},
program_utils::limited_deserialize,
pubkey::Pubkey,
system_instruction,
sysvar::{self, clock::Clock, slot_hashes::SlotHashes, Sysvar},

View File

@ -738,10 +738,9 @@ mod tests {
use super::*;
use crate::vote_state;
use solana_sdk::{
account::{get_signers, Account},
account::{get_signers, next_keyed_account, Account},
account_utils::StateMut,
hash::hash,
program_utils::next_keyed_account,
};
use std::cell::RefCell;

View File

@ -1,9 +1,9 @@
use log::*;
use solana_sdk::{
account::{get_signers, Account, KeyedAccount},
account::{get_signers, next_keyed_account, Account, KeyedAccount},
instruction::InstructionError,
nonce::Account as NonceAccount,
program_utils::{limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
system_instruction::{SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH},
system_program,

View File

@ -6,10 +6,10 @@ use libloading::os::windows::*;
use log::*;
use num_derive::{FromPrimitive, ToPrimitive};
use solana_sdk::{
account::KeyedAccount,
account::{next_keyed_account, KeyedAccount},
decode_error::DecodeError,
entrypoint_native::{InvokeContext, LoaderEntrypoint, ProgramEntrypoint},
instruction::InstructionError,
program_utils::{next_keyed_account, DecodeError},
pubkey::Pubkey,
};
use std::{collections::HashMap, env, path::PathBuf, str, sync::RwLock};

View File

@ -1,10 +1,10 @@
use log::*;
use solana_sdk::{
account::{get_signers, Account, KeyedAccount},
account::{get_signers, next_keyed_account, Account, KeyedAccount},
account_utils::StateMut,
instruction::InstructionError,
nonce::{self, Account as NonceAccount},
program_utils::{limited_deserialize, next_keyed_account},
program_utils::limited_deserialize,
pubkey::Pubkey,
system_instruction::{SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH},
system_program,

View File

@ -286,3 +286,16 @@ where
.cloned()
.collect::<A>()
}
/// Return the next KeyedAccount or a NotEnoughAccountKeys error
pub fn next_keyed_account<'a, 'b, I: Iterator<Item = &'a KeyedAccount<'b>>>(
iter: &mut I,
) -> Result<I::Item, InstructionError> {
iter.next().ok_or(InstructionError::NotEnoughAccountKeys)
}
/// Return true if the first keyed_account is executable, used to determine if
/// the loader should call a program's 'main'
pub fn is_executable(keyed_accounts: &[KeyedAccount]) -> Result<bool, InstructionError> {
Ok(!keyed_accounts.is_empty() && keyed_accounts[0].executable()?)
}

View File

@ -216,3 +216,10 @@ pub fn create_is_signer_account_infos<'a>(
})
.collect()
}
/// Return the next AccountInfo or a NotEnoughAccountKeys error
pub fn next_account_info<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
iter: &mut I,
) -> Result<I::Item, ProgramError> {
iter.next().ok_or(ProgramError::NotEnoughAccountKeys)
}

38
sdk/src/decode_error.rs Normal file
View File

@ -0,0 +1,38 @@
use num_traits::FromPrimitive;
/// Allows customer errors to be decoded back to their original enum
pub trait DecodeError<E> {
fn decode_custom_error_to_enum(custom: u32) -> Option<E>
where
E: FromPrimitive,
{
E::from_u32(custom)
}
fn type_of() -> &'static str;
}
#[cfg(test)]
mod tests {
use super::*;
use num_derive::FromPrimitive;
#[test]
fn test_decode_custom_error_to_enum() {
#[derive(Debug, FromPrimitive, PartialEq)]
enum TestEnum {
A,
B,
C,
}
impl<T> DecodeError<T> for TestEnum {
fn type_of() -> &'static str {
"TestEnum"
}
}
assert_eq!(TestEnum::decode_custom_error_to_enum(0), Some(TestEnum::A));
assert_eq!(TestEnum::decode_custom_error_to_enum(1), Some(TestEnum::B));
assert_eq!(TestEnum::decode_custom_error_to_enum(2), Some(TestEnum::C));
let option: Option<TestEnum> = TestEnum::decode_custom_error_to_enum(3);
assert_eq!(option, None);
}
}

View File

@ -13,6 +13,7 @@ pub mod account_utils;
pub mod bpf_loader;
pub mod clock;
pub mod commitment_config;
pub mod decode_error;
pub mod entrypoint_native;
pub mod epoch_info;
pub mod epoch_schedule;

View File

@ -1,4 +1,4 @@
use crate::{instruction::InstructionError, program_utils::DecodeError};
use crate::{decode_error::DecodeError, instruction::InstructionError};
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryFrom;
use thiserror::Error;

View File

@ -1,28 +1,4 @@
use crate::{
account::KeyedAccount, account_info::AccountInfo, instruction::InstructionError,
program_error::ProgramError,
};
use num_traits::FromPrimitive;
/// Return the next KeyedAccount or a NotEnoughAccountKeys error
pub fn next_keyed_account<'a, 'b, I: Iterator<Item = &'a KeyedAccount<'b>>>(
iter: &mut I,
) -> Result<I::Item, InstructionError> {
iter.next().ok_or(InstructionError::NotEnoughAccountKeys)
}
/// Return the next AccountInfo or a NotEnoughAccountKeys error
pub fn next_account_info<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
iter: &mut I,
) -> Result<I::Item, ProgramError> {
iter.next().ok_or(ProgramError::NotEnoughAccountKeys)
}
/// Return true if the first keyed_account is executable, used to determine if
/// the loader should call a program's 'main'
pub fn is_executable(keyed_accounts: &[KeyedAccount]) -> Result<bool, InstructionError> {
Ok(!keyed_accounts.is_empty() && keyed_accounts[0].executable()?)
}
use crate::instruction::InstructionError;
/// Deserialize with a limit based the maximum amount of data a program can expect to get.
/// This function should be used in place of direct deserialization to help prevent OOM errors
@ -36,40 +12,3 @@ where
.deserialize(instruction_data)
.map_err(|_| InstructionError::InvalidInstructionData)
}
/// Allows customer errors to be decoded back to their original enum
pub trait DecodeError<E> {
fn decode_custom_error_to_enum(custom: u32) -> Option<E>
where
E: FromPrimitive,
{
E::from_u32(custom)
}
fn type_of() -> &'static str;
}
#[cfg(test)]
mod tests {
use super::*;
use num_derive::FromPrimitive;
#[test]
fn test_decode_custom_error_to_enum() {
#[derive(Debug, FromPrimitive, PartialEq)]
enum TestEnum {
A,
B,
C,
}
impl<T> DecodeError<T> for TestEnum {
fn type_of() -> &'static str {
"TestEnum"
}
}
assert_eq!(TestEnum::decode_custom_error_to_enum(0), Some(TestEnum::A));
assert_eq!(TestEnum::decode_custom_error_to_enum(1), Some(TestEnum::B));
assert_eq!(TestEnum::decode_custom_error_to_enum(2), Some(TestEnum::C));
let option: Option<TestEnum> = TestEnum::decode_custom_error_to_enum(3);
assert_eq!(option, None);
}
}

View File

@ -1,6 +1,6 @@
use crate::{
decode_error::DecodeError,
hash::{hash, hashv, Hasher},
program_utils::DecodeError,
};
use num_derive::{FromPrimitive, ToPrimitive};
#[cfg(not(feature = "program"))]

View File

@ -1,7 +1,7 @@
use crate::{
decode_error::DecodeError,
instruction::{AccountMeta, Instruction},
nonce,
program_utils::DecodeError,
pubkey::Pubkey,
system_program,
sysvar::{recent_blockhashes, rent},