|
| 1 | +/// Basic trait for sending transactions to validators |
| 2 | +pub trait SendTransaction { |
| 3 | + type Output; |
| 4 | +} |
| 5 | + |
| 6 | +/// Basic trait for simulating transactions in a validator |
| 7 | +pub trait SimulateTransaction { |
| 8 | + type SimulationOutput: SimulationResult; |
| 9 | +} |
| 10 | + |
| 11 | +/// Trait for the output of a simulation |
| 12 | +pub trait SimulationResult { |
| 13 | + fn get_compute_units_consumed(&self) -> ProgramClientResult<u64>; |
| 14 | +} |
| 15 | + |
| 16 | +/// Extend basic `SendTransaction` trait with function `send` where client is |
| 17 | +/// `&mut BankClient`. Required for `ProgramBanksClient`. |
| 18 | +pub trait SendTransactionBanksClient: SendTransaction { |
| 19 | + fn send<'a>( |
| 20 | + &self, |
| 21 | + client: &'a mut BanksClient, |
| 22 | + transaction: Transaction, |
| 23 | + ) -> BoxFuture<'a, ProgramClientResult<Self::Output>>; |
| 24 | +} |
| 25 | + |
| 26 | +/// Extends basic `SimulateTransaction` trait with function `simulation` where |
| 27 | +/// client is `&mut BanksClient`. Required for `ProgramBanksClient`. |
| 28 | +pub trait SimulateTransactionBanksClient: SimulateTransaction { |
| 29 | + fn simulate<'a>( |
| 30 | + &self, |
| 31 | + client: &'a mut BanksClient, |
| 32 | + transaction: Transaction, |
| 33 | + ) -> BoxFuture<'a, ProgramClientResult<Self::SimulationOutput>>; |
| 34 | +} |
| 35 | + |
| 36 | +/// Send transaction to validator using `BanksClient::process_transaction`. |
| 37 | +#[derive(Debug, Clone, Copy, Default)] |
| 38 | +pub struct ProgramBanksClientProcessTransaction; |
| 39 | + |
| 40 | +impl SendTransaction for ProgramBanksClientProcessTransaction { |
| 41 | + type Output = (); |
| 42 | +} |
| 43 | + |
| 44 | +impl SendTransactionBanksClient for ProgramBanksClientProcessTransaction { |
| 45 | + fn send<'a>( |
| 46 | + &self, |
| 47 | + client: &'a mut BanksClient, |
| 48 | + transaction: Transaction, |
| 49 | + ) -> BoxFuture<'a, ProgramClientResult<Self::Output>> { |
| 50 | + Box::pin(async move { |
| 51 | + client |
| 52 | + .process_transaction(transaction) |
| 53 | + .await |
| 54 | + .map_err(Into::into) |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub type ProgramClientError = Box<dyn std::error::Error + Send + Sync>; |
| 60 | +pub type ProgramClientResult<T> = Result<T, ProgramClientError>; |
| 61 | + |
| 62 | +/// Generic client interface for programs. |
| 63 | +#[async_trait] |
| 64 | +pub trait ProgramClient<ST> |
| 65 | +where |
| 66 | + ST: SendTransaction + SimulateTransaction, |
| 67 | +{ |
| 68 | + async fn get_minimum_balance_for_rent_exemption( |
| 69 | + &self, |
| 70 | + data_len: usize, |
| 71 | + ) -> ProgramClientResult<u64>; |
| 72 | + |
| 73 | + async fn get_latest_blockhash(&self) -> ProgramClientResult<Hash>; |
| 74 | + |
| 75 | + async fn send_transaction(&self, transaction: &Transaction) -> ProgramClientResult<ST::Output>; |
| 76 | + |
| 77 | + async fn get_account(&self, address: Pubkey) -> ProgramClientResult<Option<Account>>; |
| 78 | + |
| 79 | + async fn simulate_transaction( |
| 80 | + &self, |
| 81 | + transaction: &Transaction, |
| 82 | + ) -> ProgramClientResult<ST::SimulationOutput>; |
| 83 | +} |
| 84 | + |
| 85 | +enum ProgramBanksClientContext { |
| 86 | + Client(Arc<Mutex<BanksClient>>), |
| 87 | + Context(Arc<Mutex<ProgramTestContext>>), |
| 88 | +} |
| 89 | + |
| 90 | +/// Program client for `BanksClient` from crate `solana-program-test`. |
| 91 | +pub struct ProgramBanksClient<ST> { |
| 92 | + context: ProgramBanksClientContext, |
| 93 | + send: ST, |
| 94 | +} |
| 95 | + |
| 96 | +impl<ST> fmt::Debug for ProgramBanksClient<ST> { |
| 97 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 98 | + f.debug_struct("ProgramBanksClient").finish() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<ST> ProgramBanksClient<ST> { |
| 103 | + fn new(context: ProgramBanksClientContext, send: ST) -> Self { |
| 104 | + Self { context, send } |
| 105 | + } |
| 106 | + |
| 107 | + pub fn new_from_client(client: Arc<Mutex<BanksClient>>, send: ST) -> Self { |
| 108 | + Self::new(ProgramBanksClientContext::Client(client), send) |
| 109 | + } |
| 110 | + |
| 111 | + pub fn new_from_context(context: Arc<Mutex<ProgramTestContext>>, send: ST) -> Self { |
| 112 | + Self::new(ProgramBanksClientContext::Context(context), send) |
| 113 | + } |
| 114 | + |
| 115 | + async fn run_in_lock<F, O>(&self, f: F) -> O |
| 116 | + where |
| 117 | + for<'a> F: Fn(&'a mut BanksClient) -> BoxFuture<'a, O>, |
| 118 | + { |
| 119 | + match &self.context { |
| 120 | + ProgramBanksClientContext::Client(client) => { |
| 121 | + let mut lock = client.lock().await; |
| 122 | + f(&mut lock).await |
| 123 | + } |
| 124 | + ProgramBanksClientContext::Context(context) => { |
| 125 | + let mut lock = context.lock().await; |
| 126 | + f(&mut lock.banks_client).await |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments