Implement Wrap for Data, fix solitaire version string

Change-Id: I28dcfc334995572ab2c864d045334d124aca1e31
This commit is contained in:
Stan Drozd 2021-06-11 14:29:43 +02:00
parent 70b49e3525
commit 388743fa6c
4 changed files with 37 additions and 7 deletions

View File

@ -77,6 +77,9 @@ pub use crate::{
types::*, types::*,
}; };
/// Library name and version to print in entrypoint. Must be evaluated in this crate in order to do the right thing
pub const PKG_NAME_VERSION: &'static str = concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"));
pub struct ExecutionContext<'a, 'b: 'a> { pub struct ExecutionContext<'a, 'b: 'a> {
/// A reference to the program_id of the current program. /// A reference to the program_id of the current program.
pub program_id: &'a Pubkey, pub program_id: &'a Pubkey,

View File

@ -53,7 +53,7 @@ macro_rules! solitaire {
} }
pub fn solitaire<'a, 'b: 'a>(p: &Pubkey, a: &'a [AccountInfo<'b>], d: &[u8]) -> ProgramResult { pub fn solitaire<'a, 'b: 'a>(p: &Pubkey, a: &'a [AccountInfo<'b>], d: &[u8]) -> ProgramResult {
solana_program::msg!(concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"))); solana_program::msg!(solitaire::PKG_NAME_VERSION);
if let Err(err) = dispatch(p, a, d) { if let Err(err) = dispatch(p, a, d) {
solana_program::msg!("Error: {:?}", err); solana_program::msg!("Error: {:?}", err);

View File

@ -28,7 +28,7 @@ use crate::{
/// A short alias for AccountInfo. /// A short alias for AccountInfo.
pub type Info<'r> = AccountInfo<'r>; pub type Info<'r> = AccountInfo<'r>;
#[derive(Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum AccountState { pub enum AccountState {
Initialized, Initialized,
Uninitialized, Uninitialized,

View File

@ -34,27 +34,34 @@ type StdResult<T, E> = std::result::Result<T, E>;
pub type ErrBox = Box<dyn std::error::Error>; pub type ErrBox = Box<dyn std::error::Error>;
/// The sum type for clearly specifying the accounts required on client side. /// The sum type for clearly specifying the accounts required on client side.
#[derive(Debug)]
pub enum AccEntry { pub enum AccEntry {
/// Least privileged account. /// Least privileged account.
Unprivileged(Pubkey), Unprivileged(Pubkey),
/// Least privileged account, read-only.
UnprivilegedRO(Pubkey),
/// Accounts that need to sign a Solana call /// Accounts that need to sign a Solana call
Signer(Keypair), Signer(Keypair),
/// Accounts that need to sign a Solana call, read-only.
SignerRO(Keypair), SignerRO(Keypair),
/// Program addresses for privileged/unprivileged cross calls /// Program addresses for unprivileged cross calls
CPIProgram(Pubkey), CPIProgram(Pubkey),
/// Program addresses for privileged cross calls
CPIProgramSigner(Keypair), CPIProgramSigner(Keypair),
/// Key decided by Wrap implementation /// Key decided from SPL constants
Sysvar, Sysvar,
/// Key derived from constants and/or program address
Derived(Pubkey), Derived(Pubkey),
/// Key derived from constants and/or program address, read-only.
DerivedRO(Pubkey), DerivedRO(Pubkey),
} }
/// Types implementing Wrap are those that can be turned into a /// Types implementing Wrap are those that can be turned into a
/// partial account vector tha /// partial account vector for a program call.
/// payload.
pub trait Wrap { pub trait Wrap {
fn wrap(_: &AccEntry) -> StdResult<Vec<AccountMeta>, ErrBox>; fn wrap(_: &AccEntry) -> StdResult<Vec<AccountMeta>, ErrBox>;
@ -115,7 +122,27 @@ where
T: BorshSerialize + Owned + Default, T: BorshSerialize + Owned + Default,
{ {
fn wrap(a: &AccEntry) -> StdResult<Vec<AccountMeta>, ErrBox> { fn wrap(a: &AccEntry) -> StdResult<Vec<AccountMeta>, ErrBox> {
todo!(); use AccEntry::*;
use AccountState::*;
match IsInitialized {
Initialized => match a {
Unprivileged(k) => Ok(vec![AccountMeta::new(*k, false)]),
UnprivilegedRO(k) => Ok(vec![AccountMeta::new_readonly(*k, false)]),
Signer(pair) => Ok(vec![AccountMeta::new(pair.pubkey(), true)]),
SignerRO(pair) => Ok(vec![AccountMeta::new_readonly(pair.pubkey(), true)]),
_other => Err(format!("{} with IsInitialized = {:?} must be passed as Unprivileged, Signer or the respective read-only variant", std::any::type_name::<Self>(), a).into())
},
Uninitialized => match a {
Unprivileged(k) => Ok(vec![AccountMeta::new(*k, false)]),
Signer(pair) => Ok(vec![AccountMeta::new(pair.pubkey(), true)]),
_other => Err(format!("{} with IsInitialized = {:?} must be passed as Unprivileged or Signer (write access required for initialization)", std::any::type_name::<Self>(), a).into() )
}
MaybeInitialized => match a {
Unprivileged(k) => Ok(vec![AccountMeta::new(*k, false)]),
Signer(pair) => Ok(vec![AccountMeta::new(pair.pubkey(), true)]),
_other => Err(format!("{} with IsInitialized = {:?} must be passed as Unprivileged or Signer (write access required in case of initialization)", std::any::type_name::<Self>(), a).into() )
}
}
} }
} }