solana/runtime/src/bank_client.rs

118 lines
4.1 KiB
Rust
Raw Normal View History

2019-03-14 19:42:01 -07:00
use crate::bank::Bank;
2019-04-05 09:26:48 -07:00
use solana_sdk::async_client::AsyncClient;
2019-03-23 20:12:27 -07:00
use solana_sdk::instruction::Instruction;
2019-03-27 04:36:01 -07:00
use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature;
use solana_sdk::signature::{Keypair, KeypairUtil};
2019-04-03 14:11:08 -07:00
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
use solana_sdk::transaction::Transaction;
use solana_sdk::transport::Result;
2019-04-05 09:26:48 -07:00
use std::io;
2019-03-14 19:42:01 -07:00
pub struct BankClient<'a> {
bank: &'a Bank,
}
2019-04-05 09:26:48 -07:00
impl<'a> AsyncClient for BankClient<'a> {
fn async_send_transaction(&self, transaction: Transaction) -> io::Result<Signature> {
// Ignore the result. Client must use get_signature_status() instead.
let _ = self.bank.process_transaction(&transaction);
Ok(transaction.signatures.get(0).cloned().unwrap_or_default())
}
fn async_send_message(&self, keypairs: &[&Keypair], message: Message) -> io::Result<Signature> {
let blockhash = self.bank.last_blockhash();
let transaction = Transaction::new(&keypairs, message, blockhash);
self.async_send_transaction(transaction)
}
fn async_send_instruction(
&self,
keypair: &Keypair,
instruction: Instruction,
) -> io::Result<Signature> {
let message = Message::new(vec![instruction]);
self.async_send_message(&[keypair], message)
}
/// Transfer `lamports` from `keypair` to `pubkey`
fn async_transfer(
&self,
lamports: u64,
keypair: &Keypair,
pubkey: &Pubkey,
) -> io::Result<Signature> {
let transfer_instruction =
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
self.async_send_instruction(keypair, transfer_instruction)
}
}
2019-04-03 14:11:08 -07:00
impl<'a> SyncClient for BankClient<'a> {
fn send_message(&self, keypairs: &[&Keypair], message: Message) -> Result<Signature> {
2019-03-27 04:36:01 -07:00
let blockhash = self.bank.last_blockhash();
let transaction = Transaction::new(&keypairs, message, blockhash);
self.bank.process_transaction(&transaction)?;
Ok(transaction.signatures.get(0).cloned().unwrap_or_default())
}
/// Create and process a transaction from a single instruction.
fn send_instruction(&self, keypair: &Keypair, instruction: Instruction) -> Result<Signature> {
let message = Message::new(vec![instruction]);
2019-04-03 14:11:08 -07:00
self.send_message(&[keypair], message)
}
/// Transfer `lamports` from `keypair` to `pubkey`
fn transfer(&self, lamports: u64, keypair: &Keypair, pubkey: &Pubkey) -> Result<Signature> {
2019-04-05 09:26:48 -07:00
let transfer_instruction =
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
self.send_instruction(keypair, transfer_instruction)
2019-04-03 14:11:08 -07:00
}
fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>> {
Ok(self.bank.get_account(pubkey).map(|account| account.data))
}
fn get_balance(&self, pubkey: &Pubkey) -> Result<u64> {
Ok(self.bank.get_balance(pubkey))
}
2019-04-03 14:11:08 -07:00
}
impl<'a> BankClient<'a> {
pub fn new(bank: &'a Bank) -> Self {
Self { bank }
2019-03-14 19:42:01 -07:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::genesis_block::GenesisBlock;
2019-03-23 20:12:27 -07:00
use solana_sdk::instruction::AccountMeta;
#[test]
fn test_bank_client_new_with_keypairs() {
let (genesis_block, john_doe_keypair) = GenesisBlock::new(10_000);
let john_pubkey = john_doe_keypair.pubkey();
let jane_doe_keypair = Keypair::new();
let jane_pubkey = jane_doe_keypair.pubkey();
let doe_keypairs = vec![&john_doe_keypair, &jane_doe_keypair];
let bank = Bank::new(&genesis_block);
let bank_client = BankClient::new(&bank);
// Create 2-2 Multisig Transfer instruction.
let bob_pubkey = Pubkey::new_rand();
let mut move_instruction = system_instruction::transfer(&john_pubkey, &bob_pubkey, 42);
2019-03-19 12:03:20 -07:00
move_instruction
.accounts
.push(AccountMeta::new(jane_pubkey, true));
let message = Message::new(vec![move_instruction]);
2019-04-03 14:11:08 -07:00
bank_client.send_message(&doe_keypairs, message).unwrap();
assert_eq!(bank_client.get_balance(&bob_pubkey).unwrap(), 42);
}
}