2019-10-14 15:02:24 -07:00
|
|
|
use crate::{account::KeyedAccount, instruction::InstructionError, pubkey::Pubkey};
|
2019-09-06 10:55:03 -07:00
|
|
|
use num_traits::{FromPrimitive, ToPrimitive};
|
2018-11-13 18:17:32 -08:00
|
|
|
|
|
|
|
// Native program ENTRYPOINT prototype
|
2018-11-17 17:02:14 -08:00
|
|
|
pub type Entrypoint = unsafe extern "C" fn(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<(), InstructionError>;
|
2018-11-13 18:38:51 -08:00
|
|
|
|
2019-11-20 10:12:43 -08:00
|
|
|
/// Convenience macro to declare a native program
|
|
|
|
///
|
2019-11-21 16:34:40 -08:00
|
|
|
/// bs58_string: bs58 string representation the program's id
|
2019-11-20 10:12:43 -08:00
|
|
|
/// name: Name of the program, must match the library name in Cargo.toml
|
|
|
|
/// entrypoint: Program's entrypoint, must be of `type Entrypoint`
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-12-03 17:55:18 -08:00
|
|
|
/// use std::str::FromStr;
|
|
|
|
/// # // wrapper is used so that the macro invocation occurs in the item position
|
|
|
|
/// # // rather than in the statement position which isn't allowed.
|
|
|
|
/// # mod item_wrapper {
|
2019-11-20 10:12:43 -08:00
|
|
|
/// use solana_sdk::account::KeyedAccount;
|
|
|
|
/// use solana_sdk::instruction::InstructionError;
|
|
|
|
/// use solana_sdk::pubkey::Pubkey;
|
|
|
|
/// use solana_sdk::declare_program;
|
|
|
|
///
|
|
|
|
/// fn my_process_instruction(
|
|
|
|
/// program_id: &Pubkey,
|
|
|
|
/// keyed_accounts: &mut [KeyedAccount],
|
|
|
|
/// data: &[u8],
|
|
|
|
/// ) -> Result<(), InstructionError> {
|
|
|
|
/// // Process an instruction
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
///
|
2019-12-03 17:55:18 -08:00
|
|
|
/// declare_program!(
|
|
|
|
/// "My11111111111111111111111111111111111111111",
|
2019-11-20 10:12:43 -08:00
|
|
|
/// solana_my_program,
|
|
|
|
/// my_process_instruction
|
|
|
|
/// );
|
2019-12-03 17:55:18 -08:00
|
|
|
///
|
|
|
|
/// # }
|
|
|
|
/// # use solana_sdk::pubkey::Pubkey;
|
|
|
|
/// # use item_wrapper::id;
|
|
|
|
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
|
|
|
|
/// assert_eq!(id(), my_id);
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
/// use std::str::FromStr;
|
|
|
|
/// # // wrapper is used so that the macro invocation occurs in the item position
|
|
|
|
/// # // rather than in the statement position which isn't allowed.
|
|
|
|
/// # mod item_wrapper {
|
|
|
|
/// use solana_sdk::account::KeyedAccount;
|
|
|
|
/// use solana_sdk::instruction::InstructionError;
|
|
|
|
/// use solana_sdk::pubkey::Pubkey;
|
|
|
|
/// use solana_sdk::declare_program;
|
|
|
|
///
|
|
|
|
/// fn my_process_instruction(
|
|
|
|
/// program_id: &Pubkey,
|
|
|
|
/// keyed_accounts: &mut [KeyedAccount],
|
|
|
|
/// data: &[u8],
|
|
|
|
/// ) -> Result<(), InstructionError> {
|
|
|
|
/// // Process an instruction
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// declare_program!(
|
|
|
|
/// solana_sdk::system_program::ID,
|
|
|
|
/// solana_my_program,
|
|
|
|
/// my_process_instruction
|
|
|
|
/// );
|
|
|
|
/// # }
|
|
|
|
///
|
|
|
|
/// # use item_wrapper::id;
|
|
|
|
/// assert_eq!(id(), solana_sdk::system_program::ID);
|
2019-11-20 10:12:43 -08:00
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_program(
|
2019-11-21 16:34:40 -08:00
|
|
|
($bs58_string:expr, $name:ident, $entrypoint:expr) => (
|
|
|
|
$crate::declare_id!($bs58_string);
|
2019-11-20 10:12:43 -08:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! $name {
|
|
|
|
() => {
|
|
|
|
(stringify!($name).to_string(), $crate::id())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn $name(
|
|
|
|
program_id: &$crate::pubkey::Pubkey,
|
2019-06-06 19:27:49 -07:00
|
|
|
keyed_accounts: &mut [$crate::account::KeyedAccount],
|
2018-11-17 17:02:14 -08:00
|
|
|
data: &[u8],
|
2019-06-06 19:27:49 -07:00
|
|
|
) -> Result<(), $crate::instruction::InstructionError> {
|
2019-05-31 15:29:21 -07:00
|
|
|
$entrypoint(program_id, keyed_accounts, data)
|
2018-11-13 18:38:51 -08:00
|
|
|
}
|
2019-04-30 15:11:08 -07:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-06 10:55:03 -07:00
|
|
|
impl<T> From<T> for InstructionError
|
|
|
|
where
|
|
|
|
T: ToPrimitive,
|
|
|
|
{
|
|
|
|
fn from(error: T) -> Self {
|
|
|
|
InstructionError::CustomError(error.to_u32().unwrap_or(0xbad_c0de))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 05:30:42 -07:00
|
|
|
/// Return the next KeyedAccount or a NotEnoughAccountKeys instruction error
|
|
|
|
pub fn next_keyed_account<I: Iterator>(iter: &mut I) -> Result<I::Item, InstructionError> {
|
|
|
|
iter.next().ok_or(InstructionError::NotEnoughAccountKeys)
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:20:15 -08:00
|
|
|
/// 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]) -> bool {
|
|
|
|
!keyed_accounts.is_empty() && keyed_accounts[0].account.executable
|
|
|
|
}
|
|
|
|
|
2019-11-14 11:27:01 -08:00
|
|
|
pub fn limited_deserialize<T>(data: &[u8]) -> Result<T, InstructionError>
|
2019-10-23 19:56:07 -07:00
|
|
|
where
|
|
|
|
T: serde::de::DeserializeOwned,
|
|
|
|
{
|
|
|
|
let limit = crate::packet::PACKET_DATA_SIZE as u64;
|
|
|
|
bincode::config()
|
|
|
|
.limit(limit)
|
|
|
|
.deserialize(data)
|
|
|
|
.map_err(|_| InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
|
2019-04-25 10:29:44 -07:00
|
|
|
pub trait DecodeError<E> {
|
2019-09-06 10:55:03 -07:00
|
|
|
fn decode_custom_error_to_enum(custom: u32) -> Option<E>
|
2019-04-25 10:29:44 -07:00
|
|
|
where
|
|
|
|
E: FromPrimitive,
|
|
|
|
{
|
2019-09-06 10:55:03 -07:00
|
|
|
E::from_u32(custom)
|
2019-04-25 10:29:44 -07:00
|
|
|
}
|
2019-09-06 10:55:03 -07:00
|
|
|
fn type_of() -> &'static str;
|
2019-04-25 10:29:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2019-09-06 10:55:03 -07:00
|
|
|
fn type_of() -> &'static str {
|
2019-04-25 10:29:44 -07:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
}
|