From 6a6a90cb0dc57afa38e102e7bf179c8be1060fad Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 16 Jan 2025 15:47:45 +0400 Subject: [PATCH] add the small crates to solana_program::example_mocks --- sdk/program/src/example_mocks.rs | 241 +++++++++++++++++-------------- 1 file changed, 132 insertions(+), 109 deletions(-) diff --git a/sdk/program/src/example_mocks.rs b/sdk/program/src/example_mocks.rs index eaa02ee02724de..e04caa91afcbc2 100644 --- a/sdk/program/src/example_mocks.rs +++ b/sdk/program/src/example_mocks.rs @@ -106,79 +106,53 @@ pub mod solana_rpc_client_nonce_utils { } } -/// Re-exports and mocks of solana-program modules that mirror those from -/// solana-program. -/// -/// This lets examples in solana-program appear to be written as client -/// programs. -pub mod solana_sdk { - pub use crate::{ - hash, instruction, keccak, message, nonce, - pubkey::{self, Pubkey}, - system_instruction, system_program, - sysvar::{ - self, - clock::{self, Clock}, - }, - }; - - pub mod account { - use {crate::pubkey::Pubkey, solana_clock::Epoch}; - #[derive(Clone)] - pub struct Account { - pub lamports: u64, - pub data: Vec, - pub owner: Pubkey, - pub executable: bool, - pub rent_epoch: Epoch, - } +pub mod solana_account { + use {crate::pubkey::Pubkey, solana_clock::Epoch}; + #[derive(Clone)] + pub struct Account { + pub lamports: u64, + pub data: Vec, + pub owner: Pubkey, + pub executable: bool, + pub rent_epoch: Epoch, + } - pub trait ReadableAccount: Sized { - fn data(&self) -> &[u8]; - } + pub trait ReadableAccount: Sized { + fn data(&self) -> &[u8]; + } - impl ReadableAccount for Account { - fn data(&self) -> &[u8] { - &self.data - } + impl ReadableAccount for Account { + fn data(&self) -> &[u8] { + &self.data } } - pub mod account_utils { - use super::account::Account; + pub mod state_traits { + use super::Account; pub trait StateMut {} impl StateMut for Account {} } +} - pub mod signature { - use crate::pubkey::Pubkey; - - #[derive(Default, Debug)] - pub struct Signature; - - pub struct Keypair; - - impl Keypair { - pub fn new() -> Keypair { - Keypair - } - } +pub mod solana_signature { + #[derive(Default, Debug)] + pub struct Signature; +} - impl Signer for Keypair { - fn pubkey(&self) -> Pubkey { - Pubkey::default() - } - } +pub mod solana_signer { + use {solana_pubkey::Pubkey, thiserror::Error}; - pub trait Signer { - fn pubkey(&self) -> Pubkey; - } + #[derive(Error, Debug)] + #[error("mock-error")] + pub struct SignerError; + pub trait Signer { + fn pubkey(&self) -> Pubkey; } pub mod signers { - use super::signature::Signer; + use super::Signer; pub trait Signers {} @@ -186,27 +160,43 @@ pub mod solana_sdk { impl Signers for [&T; 1] {} impl Signers for [&T; 2] {} } +} - pub mod signer { - use thiserror::Error; +pub mod solana_keypair { + use {crate::example_mocks::solana_signer::Signer, solana_pubkey::Pubkey}; + pub struct Keypair; - #[derive(Error, Debug)] - #[error("mock-error")] - pub struct SignerError; + impl Keypair { + pub fn new() -> Keypair { + Keypair + } } - pub mod transaction { + impl Signer for Keypair { + fn pubkey(&self) -> Pubkey { + Pubkey::default() + } + } +} + +pub mod solana_transaction { + use { + crate::example_mocks::solana_signer::{signers::Signers, SignerError}, + serde_derive::Serialize, + solana_hash::Hash, + solana_instruction::Instruction, + solana_message::Message, + solana_pubkey::Pubkey, + }; + + pub mod versioned { use { - super::{signature::Signature, signer::SignerError, signers::Signers}, - crate::{ - hash::Hash, - instruction::Instruction, - message::{Message, VersionedMessage}, - pubkey::Pubkey, + crate::example_mocks::{ + solana_signature::Signature, + solana_signer::{signers::Signers, SignerError}, }, - serde_derive::Serialize, + solana_message::VersionedMessage, }; - pub struct VersionedTransaction { pub signatures: Vec, pub message: VersionedMessage, @@ -223,56 +213,89 @@ pub mod solana_sdk { }) } } + } - #[derive(Serialize)] - pub struct Transaction { - pub message: Message, - } + #[derive(Serialize)] + pub struct Transaction { + pub message: Message, + } - impl Transaction { - pub fn new( - _from_keypairs: &T, - _message: Message, - _recent_blockhash: Hash, - ) -> Transaction { - Transaction { - message: Message::new(&[], None), - } + impl Transaction { + pub fn new( + _from_keypairs: &T, + _message: Message, + _recent_blockhash: Hash, + ) -> Transaction { + Transaction { + message: Message::new(&[], None), } + } - pub fn new_unsigned(_message: Message) -> Self { - Transaction { - message: Message::new(&[], None), - } + pub fn new_unsigned(_message: Message) -> Self { + Transaction { + message: Message::new(&[], None), } + } - pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self { - Transaction { - message: Message::new(&[], None), - } + pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self { + Transaction { + message: Message::new(&[], None), } + } - pub fn new_signed_with_payer( - instructions: &[Instruction], - payer: Option<&Pubkey>, - signing_keypairs: &T, - recent_blockhash: Hash, - ) -> Self { - let message = Message::new(instructions, payer); - Self::new(signing_keypairs, message, recent_blockhash) - } + pub fn new_signed_with_payer( + instructions: &[Instruction], + payer: Option<&Pubkey>, + signing_keypairs: &T, + recent_blockhash: Hash, + ) -> Self { + let message = Message::new(instructions, payer); + Self::new(signing_keypairs, message, recent_blockhash) + } - pub fn sign(&mut self, _keypairs: &T, _recent_blockhash: Hash) {} + pub fn sign(&mut self, _keypairs: &T, _recent_blockhash: Hash) {} - pub fn try_sign( - &mut self, - _keypairs: &T, - _recent_blockhash: Hash, - ) -> Result<(), SignerError> { - Ok(()) - } + pub fn try_sign( + &mut self, + _keypairs: &T, + _recent_blockhash: Hash, + ) -> Result<(), SignerError> { + Ok(()) } } +} + +/// Re-exports and mocks of solana-program modules that mirror those from +/// solana-program. +/// +/// This lets examples in solana-program appear to be written as client +/// programs. +pub mod solana_sdk { + pub use crate::{ + example_mocks::{ + solana_account::{self as account, state_traits as account_utils}, + solana_signer::{self as signer, signers}, + }, + hash, instruction, keccak, message, nonce, + pubkey::{self, Pubkey}, + system_instruction, system_program, + sysvar::{ + self, + clock::{self, Clock}, + }, + }; + + pub mod signature { + pub use crate::example_mocks::{ + solana_keypair::Keypair, solana_signature::Signature, solana_signer::Signer, + }; + } + + pub mod transaction { + pub use crate::example_mocks::solana_transaction::{ + versioned::VersionedTransaction, Transaction, + }; + } pub use crate::address_lookup_table; }