2019-03-14 20:42:01 -06:00
|
|
|
use crate::bank::Bank;
|
2019-11-06 14:15:00 -07:00
|
|
|
use solana_sdk::{
|
|
|
|
account::Account,
|
|
|
|
client::{AsyncClient, Client, SyncClient},
|
|
|
|
commitment_config::CommitmentConfig,
|
|
|
|
fee_calculator::FeeCalculator,
|
|
|
|
hash::Hash,
|
|
|
|
instruction::Instruction,
|
|
|
|
message::Message,
|
|
|
|
pubkey::Pubkey,
|
2020-02-20 14:28:55 -07:00
|
|
|
signature::{Keypair, Signature, Signer},
|
2020-02-20 13:13:23 -07:00
|
|
|
signers::Signers,
|
2019-11-06 14:15:00 -07:00
|
|
|
system_instruction,
|
|
|
|
transaction::{self, Transaction},
|
|
|
|
transport::{Result, TransportError},
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
sync::{
|
|
|
|
mpsc::{channel, Receiver, Sender},
|
|
|
|
Arc, Mutex,
|
|
|
|
},
|
|
|
|
thread::{sleep, Builder},
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2019-03-14 20:42:01 -06:00
|
|
|
|
2019-04-11 11:29:59 -07:00
|
|
|
pub struct BankClient {
|
2019-04-19 07:29:07 -06:00
|
|
|
bank: Arc<Bank>,
|
2019-04-19 13:18:20 -07:00
|
|
|
transaction_sender: Mutex<Sender<Transaction>>,
|
2019-03-14 20:42:01 -06:00
|
|
|
}
|
|
|
|
|
2019-04-19 15:04:36 -06:00
|
|
|
impl Client for BankClient {
|
2019-09-06 09:07:40 -07:00
|
|
|
fn tpu_addr(&self) -> String {
|
2019-04-19 15:04:36 -06:00
|
|
|
"Local BankClient".to_string()
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 07:29:07 -06:00
|
|
|
|
2019-04-11 11:29:59 -07:00
|
|
|
impl AsyncClient for BankClient {
|
2019-04-05 10:26:48 -06:00
|
|
|
fn async_send_transaction(&self, transaction: Transaction) -> io::Result<Signature> {
|
2019-04-19 07:29:07 -06:00
|
|
|
let signature = transaction.signatures.get(0).cloned().unwrap_or_default();
|
2019-04-19 13:18:20 -07:00
|
|
|
let transaction_sender = self.transaction_sender.lock().unwrap();
|
|
|
|
transaction_sender.send(transaction).unwrap();
|
2019-04-19 07:29:07 -06:00
|
|
|
Ok(signature)
|
2019-04-05 10:26:48 -06:00
|
|
|
}
|
|
|
|
|
2020-02-20 13:13:23 -07:00
|
|
|
fn async_send_message<T: Signers>(
|
2019-04-11 00:25:14 -07:00
|
|
|
&self,
|
2020-02-20 13:13:23 -07:00
|
|
|
keypairs: &T,
|
2019-04-11 00:25:14 -07:00
|
|
|
message: Message,
|
|
|
|
recent_blockhash: Hash,
|
|
|
|
) -> io::Result<Signature> {
|
2020-02-20 13:13:23 -07:00
|
|
|
let transaction = Transaction::new(keypairs, message, recent_blockhash);
|
2019-04-05 10:26:48 -06:00
|
|
|
self.async_send_transaction(transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn async_send_instruction(
|
|
|
|
&self,
|
|
|
|
keypair: &Keypair,
|
|
|
|
instruction: Instruction,
|
2019-04-11 00:25:14 -07:00
|
|
|
recent_blockhash: Hash,
|
2019-04-05 10:26:48 -06:00
|
|
|
) -> io::Result<Signature> {
|
|
|
|
let message = Message::new(vec![instruction]);
|
2019-04-11 00:25:14 -07:00
|
|
|
self.async_send_message(&[keypair], message, recent_blockhash)
|
2019-04-05 10:26:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Transfer `lamports` from `keypair` to `pubkey`
|
|
|
|
fn async_transfer(
|
|
|
|
&self,
|
|
|
|
lamports: u64,
|
|
|
|
keypair: &Keypair,
|
|
|
|
pubkey: &Pubkey,
|
2019-04-11 00:25:14 -07:00
|
|
|
recent_blockhash: Hash,
|
2019-04-05 10:26:48 -06:00
|
|
|
) -> io::Result<Signature> {
|
|
|
|
let transfer_instruction =
|
|
|
|
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
2019-04-11 00:25:14 -07:00
|
|
|
self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
|
2019-04-05 10:26:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:29:59 -07:00
|
|
|
impl SyncClient for BankClient {
|
2020-02-20 13:13:23 -07:00
|
|
|
fn send_message<T: Signers>(&self, keypairs: &T, message: Message) -> Result<Signature> {
|
2019-03-27 05:36:01 -06:00
|
|
|
let blockhash = self.bank.last_blockhash();
|
2020-02-20 13:13:23 -07:00
|
|
|
let transaction = Transaction::new(keypairs, message, blockhash);
|
2019-04-03 16:36:10 -06:00
|
|
|
self.bank.process_transaction(&transaction)?;
|
|
|
|
Ok(transaction.signatures.get(0).cloned().unwrap_or_default())
|
2019-03-16 14:30:10 -06:00
|
|
|
}
|
|
|
|
|
2019-03-17 09:55:42 -06:00
|
|
|
/// Create and process a transaction from a single instruction.
|
2019-04-03 21:40:29 -06:00
|
|
|
fn send_instruction(&self, keypair: &Keypair, instruction: Instruction) -> Result<Signature> {
|
2019-03-27 05:50:50 -06:00
|
|
|
let message = Message::new(vec![instruction]);
|
2019-04-03 15:11:08 -06:00
|
|
|
self.send_message(&[keypair], message)
|
2019-03-16 14:30:10 -06:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:34:01 -06:00
|
|
|
/// Transfer `lamports` from `keypair` to `pubkey`
|
2019-04-03 21:40:29 -06:00
|
|
|
fn transfer(&self, lamports: u64, keypair: &Keypair, pubkey: &Pubkey) -> Result<Signature> {
|
2019-04-05 10:26:48 -06:00
|
|
|
let transfer_instruction =
|
|
|
|
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
|
|
|
self.send_instruction(keypair, transfer_instruction)
|
2019-04-03 15:11:08 -06:00
|
|
|
}
|
2019-04-03 16:36:10 -06:00
|
|
|
|
2019-04-03 21:40:29 -06:00
|
|
|
fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>> {
|
|
|
|
Ok(self.bank.get_account(pubkey).map(|account| account.data))
|
2019-04-03 16:36:10 -06:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:30:51 -07:00
|
|
|
fn get_account(&self, pubkey: &Pubkey) -> Result<Option<Account>> {
|
|
|
|
Ok(self.bank.get_account(pubkey))
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:15:00 -07:00
|
|
|
fn get_account_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
_commitment_config: CommitmentConfig,
|
|
|
|
) -> Result<Option<Account>> {
|
|
|
|
Ok(self.bank.get_account(pubkey))
|
|
|
|
}
|
|
|
|
|
2019-04-03 21:40:29 -06:00
|
|
|
fn get_balance(&self, pubkey: &Pubkey) -> Result<u64> {
|
|
|
|
Ok(self.bank.get_balance(pubkey))
|
2019-04-03 16:36:10 -06:00
|
|
|
}
|
2019-04-05 10:42:54 -06:00
|
|
|
|
2019-11-06 14:15:00 -07:00
|
|
|
fn get_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
_commitment_config: CommitmentConfig,
|
|
|
|
) -> Result<u64> {
|
|
|
|
Ok(self.bank.get_balance(pubkey))
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
fn get_recent_blockhash(&self) -> Result<(Hash, FeeCalculator)> {
|
|
|
|
Ok(self.bank.last_blockhash_with_fee_calculator())
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:15:00 -07:00
|
|
|
fn get_recent_blockhash_with_commitment(
|
|
|
|
&self,
|
|
|
|
_commitment_config: CommitmentConfig,
|
|
|
|
) -> Result<(Hash, FeeCalculator)> {
|
|
|
|
Ok(self.bank.last_blockhash_with_fee_calculator())
|
|
|
|
}
|
|
|
|
|
2019-04-05 10:42:54 -06:00
|
|
|
fn get_signature_status(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
) -> Result<Option<transaction::Result<()>>> {
|
|
|
|
Ok(self.bank.get_signature_status(signature))
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
|
2019-11-06 14:15:00 -07:00
|
|
|
fn get_signature_status_with_commitment(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
_commitment_config: CommitmentConfig,
|
|
|
|
) -> Result<Option<transaction::Result<()>>> {
|
|
|
|
Ok(self.bank.get_signature_status(signature))
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
fn get_slot(&self) -> Result<u64> {
|
|
|
|
Ok(self.bank.slot())
|
2019-04-11 00:25:14 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 14:15:00 -07:00
|
|
|
fn get_slot_with_commitment(&self, _commitment_config: CommitmentConfig) -> Result<u64> {
|
|
|
|
Ok(self.bank.slot())
|
|
|
|
}
|
|
|
|
|
2019-04-11 00:25:14 -07:00
|
|
|
fn get_transaction_count(&self) -> Result<u64> {
|
|
|
|
Ok(self.bank.transaction_count())
|
|
|
|
}
|
2019-04-25 12:46:40 -07:00
|
|
|
|
2019-11-06 14:15:00 -07:00
|
|
|
fn get_transaction_count_with_commitment(
|
|
|
|
&self,
|
|
|
|
_commitment_config: CommitmentConfig,
|
|
|
|
) -> Result<u64> {
|
|
|
|
Ok(self.bank.transaction_count())
|
|
|
|
}
|
|
|
|
|
2019-04-25 12:46:40 -07:00
|
|
|
fn poll_for_signature_confirmation(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
min_confirmed_blocks: usize,
|
2019-07-02 20:56:10 -07:00
|
|
|
) -> Result<usize> {
|
2019-04-25 12:46:40 -07:00
|
|
|
let mut now = Instant::now();
|
|
|
|
let mut confirmed_blocks = 0;
|
|
|
|
loop {
|
|
|
|
let response = self.bank.get_signature_confirmation_status(signature);
|
|
|
|
if let Some((confirmations, res)) = response {
|
|
|
|
if res.is_ok() {
|
|
|
|
if confirmed_blocks != confirmations {
|
|
|
|
now = Instant::now();
|
|
|
|
confirmed_blocks = confirmations;
|
|
|
|
}
|
|
|
|
if confirmations >= min_confirmed_blocks {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if now.elapsed().as_secs() > 15 {
|
|
|
|
return Err(TransportError::IoError(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2019-11-13 09:43:15 -07:00
|
|
|
format!(
|
|
|
|
"signature not found after {} seconds",
|
|
|
|
now.elapsed().as_secs()
|
|
|
|
),
|
2019-04-25 12:46:40 -07:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(250));
|
|
|
|
}
|
2019-07-02 20:56:10 -07:00
|
|
|
Ok(confirmed_blocks)
|
2019-04-25 12:46:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_for_signature(&self, signature: &Signature) -> Result<()> {
|
|
|
|
let now = Instant::now();
|
|
|
|
loop {
|
|
|
|
let response = self.bank.get_signature_status(signature);
|
|
|
|
if let Some(res) = response {
|
|
|
|
if res.is_ok() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if now.elapsed().as_secs() > 15 {
|
|
|
|
return Err(TransportError::IoError(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2019-11-13 09:43:15 -07:00
|
|
|
format!(
|
|
|
|
"signature not found after {} seconds",
|
|
|
|
now.elapsed().as_secs()
|
|
|
|
),
|
2019-04-25 12:46:40 -07:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(250));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-27 08:39:29 -07:00
|
|
|
|
2019-05-13 12:49:37 -07:00
|
|
|
fn get_new_blockhash(&self, blockhash: &Hash) -> Result<(Hash, FeeCalculator)> {
|
|
|
|
let (last_blockhash, fee_calculator) = self.get_recent_blockhash()?;
|
2019-04-27 08:39:29 -07:00
|
|
|
if last_blockhash != *blockhash {
|
2019-05-13 12:49:37 -07:00
|
|
|
Ok((last_blockhash, fee_calculator))
|
2019-04-27 08:39:29 -07:00
|
|
|
} else {
|
|
|
|
Err(TransportError::IoError(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"Unable to get new blockhash",
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 15:11:08 -06:00
|
|
|
}
|
|
|
|
|
2019-04-11 11:29:59 -07:00
|
|
|
impl BankClient {
|
2019-04-19 07:29:07 -06:00
|
|
|
fn run(bank: &Bank, transaction_receiver: Receiver<Transaction>) {
|
|
|
|
while let Ok(tx) = transaction_receiver.recv() {
|
2019-11-12 15:26:21 -07:00
|
|
|
let mut transactions = vec![tx];
|
|
|
|
while let Ok(tx) = transaction_receiver.try_recv() {
|
|
|
|
transactions.push(tx);
|
|
|
|
}
|
|
|
|
let _ = bank.process_transactions(&transactions);
|
2019-04-19 07:29:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:09:11 -07:00
|
|
|
pub fn new_shared(bank: &Arc<Bank>) -> Self {
|
2019-04-19 07:29:07 -06:00
|
|
|
let (transaction_sender, transaction_receiver) = channel();
|
2019-04-19 13:18:20 -07:00
|
|
|
let transaction_sender = Mutex::new(transaction_sender);
|
2019-04-19 07:29:07 -06:00
|
|
|
let thread_bank = bank.clone();
|
|
|
|
let bank = bank.clone();
|
|
|
|
Builder::new()
|
|
|
|
.name("solana-bank-client".to_string())
|
|
|
|
.spawn(move || Self::run(&thread_bank, transaction_receiver))
|
|
|
|
.unwrap();
|
|
|
|
Self {
|
|
|
|
bank,
|
|
|
|
transaction_sender,
|
|
|
|
}
|
2019-03-14 20:42:01 -06:00
|
|
|
}
|
2019-04-29 13:09:11 -07:00
|
|
|
|
|
|
|
pub fn new(bank: Bank) -> Self {
|
|
|
|
Self::new_shared(&Arc::new(bank))
|
|
|
|
}
|
2019-03-14 20:42:01 -06:00
|
|
|
}
|
2019-03-18 15:35:47 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-01-28 17:03:20 -08:00
|
|
|
use solana_sdk::{genesis_config::create_genesis_config, instruction::AccountMeta};
|
2019-03-18 15:35:47 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bank_client_new_with_keypairs() {
|
2019-11-08 23:56:57 -05:00
|
|
|
let (genesis_config, john_doe_keypair) = create_genesis_config(10_000);
|
2019-03-27 07:34:01 -06:00
|
|
|
let john_pubkey = john_doe_keypair.pubkey();
|
2019-03-18 15:35:47 -06:00
|
|
|
let jane_doe_keypair = Keypair::new();
|
2019-03-27 07:34:01 -06:00
|
|
|
let jane_pubkey = jane_doe_keypair.pubkey();
|
|
|
|
let doe_keypairs = vec![&john_doe_keypair, &jane_doe_keypair];
|
2019-11-08 23:56:57 -05:00
|
|
|
let bank = Bank::new(&genesis_config);
|
2019-04-11 11:29:59 -07:00
|
|
|
let bank_client = BankClient::new(bank);
|
2019-03-18 15:35:47 -06:00
|
|
|
|
2019-04-02 21:52:07 -06:00
|
|
|
// Create 2-2 Multisig Transfer instruction.
|
2019-03-30 21:37:33 -06:00
|
|
|
let bob_pubkey = Pubkey::new_rand();
|
2019-04-23 13:30:42 -06:00
|
|
|
let mut transfer_instruction = system_instruction::transfer(&john_pubkey, &bob_pubkey, 42);
|
|
|
|
transfer_instruction
|
2019-03-19 13:03:20 -06:00
|
|
|
.accounts
|
2019-03-19 15:25:48 -06:00
|
|
|
.push(AccountMeta::new(jane_pubkey, true));
|
2019-03-18 15:35:47 -06:00
|
|
|
|
2019-04-23 13:30:42 -06:00
|
|
|
let message = Message::new(vec![transfer_instruction]);
|
2019-04-03 15:11:08 -06:00
|
|
|
bank_client.send_message(&doe_keypairs, message).unwrap();
|
2019-04-03 21:40:29 -06:00
|
|
|
assert_eq!(bank_client.get_balance(&bob_pubkey).unwrap(), 42);
|
2019-03-18 15:35:47 -06:00
|
|
|
}
|
|
|
|
}
|