Cleanup client traits and create super trait (#3728)

This commit is contained in:
Jack May 2019-04-11 00:25:14 -07:00 committed by GitHub
parent 4b6c0198ad
commit d0f46d6a8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 153 additions and 115 deletions

View File

@ -10,11 +10,10 @@ use solana_client::thin_client::create_client;
use solana_client::thin_client::ThinClient;
use solana_drone::drone::request_airdrop_transaction;
use solana_metrics::influxdb;
use solana_sdk::async_client::AsyncClient;
use solana_sdk::client::{AsyncClient, SyncClient};
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
use solana_sdk::system_transaction;
use solana_sdk::timing::timestamp;

View File

@ -266,7 +266,7 @@ impl RpcClient {
/// Request the transaction count. If the response packet is dropped by the network,
/// this method will try again 5 times.
pub fn get_transaction_count(&self) -> Result<u64, Box<dyn error::Error>> {
pub fn get_transaction_count(&self) -> io::Result<u64> {
debug!("get_transaction_count");
let mut num_retries = 5;

View File

@ -6,18 +6,16 @@
use crate::rpc_client::RpcClient;
use bincode::{serialize_into, serialized_size};
use log::*;
use solana_sdk::async_client::AsyncClient;
use solana_sdk::client::{AsyncClient, Client, SyncClient};
use solana_sdk::hash::Hash;
use solana_sdk::instruction::Instruction;
use solana_sdk::message::Message;
use solana_sdk::packet::PACKET_DATA_SIZE;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
use solana_sdk::transaction::{self, Transaction};
use solana_sdk::transport::Result as TransportResult;
use std::error;
use std::io;
use std::net::{SocketAddr, UdpSocket};
use std::time::Duration;
@ -109,7 +107,7 @@ impl ThinClient {
return Ok(transaction.signatures[0]);
}
info!("{} tries failed transfer to {}", x, self.transactions_addr);
transaction.sign(keypairs, self.get_recent_blockhash()?);
transaction.sign(keypairs, self.rpc_client.get_recent_blockhash()?);
}
Err(io::Error::new(
io::ErrorKind::Other,
@ -117,14 +115,6 @@ impl ThinClient {
))
}
pub fn get_transaction_count(&self) -> Result<u64, Box<dyn error::Error>> {
self.rpc_client.get_transaction_count()
}
pub fn get_recent_blockhash(&self) -> io::Result<Hash> {
self.rpc_client.get_recent_blockhash()
}
pub fn get_new_blockhash(&self, blockhash: &Hash) -> io::Result<Hash> {
self.rpc_client.get_new_blockhash(blockhash)
}
@ -178,6 +168,8 @@ impl ThinClient {
}
}
impl Client for ThinClient {}
impl SyncClient for ThinClient {
fn send_message(&self, keypairs: &[&Keypair], message: Message) -> TransportResult<Signature> {
let blockhash = self.get_recent_blockhash()?;
@ -230,6 +222,16 @@ impl SyncClient for ThinClient {
})?;
Ok(status)
}
fn get_recent_blockhash(&self) -> TransportResult<Hash> {
let recent_blockhash = self.rpc_client.get_recent_blockhash()?;
Ok(recent_blockhash)
}
fn get_transaction_count(&self) -> TransportResult<u64> {
let transaction_count = self.rpc_client.get_transaction_count()?;
Ok(transaction_count)
}
}
impl AsyncClient for ThinClient {
@ -243,28 +245,34 @@ impl AsyncClient for ThinClient {
.send_to(&buf[..], &self.transactions_addr)?;
Ok(transaction.signatures[0])
}
fn async_send_message(&self, keypairs: &[&Keypair], message: Message) -> io::Result<Signature> {
let blockhash = self.get_recent_blockhash()?;
let transaction = Transaction::new(&keypairs, message, blockhash);
fn async_send_message(
&self,
keypairs: &[&Keypair],
message: Message,
recent_blockhash: Hash,
) -> io::Result<Signature> {
let transaction = Transaction::new(&keypairs, message, recent_blockhash);
self.async_send_transaction(transaction)
}
fn async_send_instruction(
&self,
keypair: &Keypair,
instruction: Instruction,
recent_blockhash: Hash,
) -> io::Result<Signature> {
let message = Message::new(vec![instruction]);
self.async_send_message(&[keypair], message)
self.async_send_message(&[keypair], message, recent_blockhash)
}
fn async_transfer(
&self,
lamports: u64,
keypair: &Keypair,
pubkey: &Pubkey,
recent_blockhash: Hash,
) -> io::Result<Signature> {
let transfer_instruction =
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
self.async_send_instruction(keypair, transfer_instruction)
self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
}
}

View File

@ -10,6 +10,7 @@ use crate::gossip_service::discover_nodes;
use crate::locktower::VOTE_THRESHOLD_DEPTH;
use crate::poh_service::PohServiceConfig;
use solana_client::thin_client::create_client;
use solana_sdk::client::SyncClient;
use solana_sdk::hash::Hash;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::system_transaction;

View File

@ -8,10 +8,10 @@ use crate::replicator::Replicator;
use crate::service::Service;
use solana_client::thin_client::create_client;
use solana_client::thin_client::ThinClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_transaction;
use solana_sdk::timing::DEFAULT_SLOTS_PER_EPOCH;
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;

View File

@ -19,7 +19,8 @@ use rand::Rng;
use solana_client::rpc_client::RpcClient;
use solana_client::rpc_request::RpcRequest;
use solana_client::thin_client::{create_client, ThinClient};
use solana_sdk::async_client::AsyncClient;
use solana_sdk::client::{AsyncClient, SyncClient};
use solana_sdk::hash::{Hash, Hasher};
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::system_transaction;

View File

@ -13,11 +13,10 @@ use bincode::deserialize;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaChaRng;
use solana_client::thin_client::{create_client_with_timeout, ThinClient};
use solana_sdk::async_client::AsyncClient;
use solana_sdk::client::{AsyncClient, SyncClient};
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_transaction;
use solana_sdk::transaction::Transaction;
use solana_storage_api::storage_instruction::{self, StorageInstruction};

View File

@ -28,8 +28,8 @@ mod bpf {
mod bpf_c {
use super::*;
use solana_sdk::bpf_loader;
use solana_sdk::client::SyncClient;
use solana_sdk::signature::KeypairUtil;
use solana_sdk::sync_client::SyncClient;
use std::io::Read;
#[test]
@ -100,8 +100,8 @@ mod bpf {
#[cfg(feature = "bpf_rust")]
mod bpf_rust {
use super::*;
use solana_sdk::client::SyncClient;
use solana_sdk::signature::KeypairUtil;
use solana_sdk::sync_client::SyncClient;
use std::io::Read;
#[test]

View File

@ -147,11 +147,11 @@ mod tests {
use crate::id;
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::instruction::InstructionError;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::transaction::TransactionError;
fn create_bank(lamports: u64) -> (Bank, Keypair) {

View File

@ -33,10 +33,10 @@ mod tests {
use serde_derive::{Deserialize, Serialize};
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]

View File

@ -451,9 +451,9 @@ mod test {
use crate::exchange_instruction;
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
use std::mem;

View File

@ -1,11 +1,11 @@
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_runtime::loader_utils::{create_invoke_instruction, load_program};
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::instruction::InstructionError;
use solana_sdk::native_loader;
use solana_sdk::signature::KeypairUtil;
use solana_sdk::sync_client::SyncClient;
use solana_sdk::transaction::TransactionError;
#[test]

View File

@ -1,10 +1,10 @@
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_runtime::loader_utils::{create_invoke_instruction, load_program};
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::native_loader;
use solana_sdk::signature::KeypairUtil;
use solana_sdk::sync_client::SyncClient;
#[test]
fn test_program_native_noop() {

View File

@ -90,12 +90,12 @@ mod tests {
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::account::{create_keyed_accounts, Account};
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::hash::{hash, Hash};
use solana_sdk::instruction::Instruction;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
fn test_instruction(

View File

@ -103,12 +103,12 @@ mod tests {
use crate::vote_state::{Vote, VoteState};
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::instruction::InstructionError;
use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
use solana_sdk::transaction::{Result, TransactionError};

View File

@ -1,11 +1,11 @@
use crate::bank::Bank;
use solana_sdk::async_client::AsyncClient;
use solana_sdk::client::{AsyncClient, SyncClient};
use solana_sdk::hash::Hash;
use solana_sdk::instruction::Instruction;
use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signature;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
use solana_sdk::transaction::{self, Transaction};
use solana_sdk::transport::Result;
@ -23,9 +23,13 @@ impl<'a> AsyncClient for BankClient<'a> {
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);
fn async_send_message(
&self,
keypairs: &[&Keypair],
message: Message,
recent_blockhash: Hash,
) -> io::Result<Signature> {
let transaction = Transaction::new(&keypairs, message, recent_blockhash);
self.async_send_transaction(transaction)
}
@ -33,9 +37,10 @@ impl<'a> AsyncClient for BankClient<'a> {
&self,
keypair: &Keypair,
instruction: Instruction,
recent_blockhash: Hash,
) -> io::Result<Signature> {
let message = Message::new(vec![instruction]);
self.async_send_message(&[keypair], message)
self.async_send_message(&[keypair], message, recent_blockhash)
}
/// Transfer `lamports` from `keypair` to `pubkey`
@ -44,10 +49,11 @@ impl<'a> AsyncClient for BankClient<'a> {
lamports: u64,
keypair: &Keypair,
pubkey: &Pubkey,
recent_blockhash: Hash,
) -> io::Result<Signature> {
let transfer_instruction =
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
self.async_send_instruction(keypair, transfer_instruction)
self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
}
}
@ -86,6 +92,15 @@ impl<'a> SyncClient for BankClient<'a> {
) -> Result<Option<transaction::Result<()>>> {
Ok(self.bank.get_signature_status(signature))
}
fn get_recent_blockhash(&self) -> Result<Hash> {
let last_blockhash = self.bank.last_blockhash();
Ok(last_blockhash)
}
fn get_transaction_count(&self) -> Result<u64> {
Ok(self.bank.transaction_count())
}
}
impl<'a> BankClient<'a> {

View File

@ -1,10 +1,11 @@
use crate::bank_client::BankClient;
use serde::Serialize;
use solana_sdk::client::SyncClient;
use solana_sdk::instruction::{AccountMeta, Instruction};
use solana_sdk::loader_instruction;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_instruction;
pub fn load_program(

View File

@ -107,10 +107,10 @@ mod tests {
use crate::bank::Bank;
use crate::bank_client::BankClient;
use solana_sdk::account::Account;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::instruction::{AccountMeta, Instruction, InstructionError};
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::sync_client::SyncClient;
use solana_sdk::system_program;
use solana_sdk::transaction::TransactionError;

View File

@ -1,35 +0,0 @@
//! Defines a trait for non-blocking (asynchronous) communication with a Solana server.
//! Implementations are expected to create tranasctions, sign them, and send
//! them but without waiting to see if the server accepted it.
use crate::instruction::Instruction;
use crate::message::Message;
use crate::pubkey::Pubkey;
use crate::signature::{Keypair, Signature};
use crate::transaction::Transaction;
use std::io;
pub trait AsyncClient {
/// Send a signed transaction, but don't wait to see if the server accepted it.
fn async_send_transaction(&self, transaction: Transaction) -> io::Result<Signature>;
/// Create a transaction from the given message, and send it to the
/// server, but don't wait for to see if the server accepted it.
fn async_send_message(&self, keypairs: &[&Keypair], message: Message) -> io::Result<Signature>;
/// Create a transaction from a single instruction that only requires
/// a single signer. Then send it to the server, but don't wait for a reply.
fn async_send_instruction(
&self,
keypair: &Keypair,
instruction: Instruction,
) -> io::Result<Signature>;
/// Attempt to transfer lamports from `keypair` to `pubkey`, but don't wait to confirm.
fn async_transfer(
&self,
lamports: u64,
keypair: &Keypair,
pubkey: &Pubkey,
) -> io::Result<Signature>;
}

86
sdk/src/client.rs Normal file
View File

@ -0,0 +1,86 @@
//! Defines traits for blocking (synchronous) and non-blocking (asynchronous)
//! communication with a Solana server as well a a trait that encompasses both.
//!
//! //! Synchronous implementations are expected to create transactions, sign them, and send
//! them with multiple retries, updating blockhashes and resigning as-needed.
//!
//! Asynchronous implementations are expected to create transactions, sign them, and send
//! them but without waiting to see if the server accepted it.
use crate::hash::Hash;
use crate::instruction::Instruction;
use crate::message::Message;
use crate::pubkey::Pubkey;
use crate::signature::{Keypair, Signature};
use crate::transaction;
use crate::transport::Result;
use std::io;
pub trait Client: SyncClient + AsyncClient {}
pub trait SyncClient {
/// Create a transaction from the given message, and send it to the
/// server, retrying as-needed.
fn send_message(&self, keypairs: &[&Keypair], message: Message) -> Result<Signature>;
/// Create a transaction from a single instruction that only requires
/// a single signer. Then send it to the server, retrying as-needed.
fn send_instruction(&self, keypair: &Keypair, instruction: Instruction) -> Result<Signature>;
/// Transfer lamports from `keypair` to `pubkey`, retrying until the
/// transfer completes or produces and error.
fn transfer(&self, lamports: u64, keypair: &Keypair, pubkey: &Pubkey) -> Result<Signature>;
/// Get an account or None if not found.
fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>>;
/// Get account balance or 0 if not found.
fn get_balance(&self, pubkey: &Pubkey) -> Result<u64>;
/// Get signature status.
fn get_signature_status(
&self,
signature: &Signature,
) -> Result<Option<transaction::Result<()>>>;
/// Get recent blockhash
fn get_recent_blockhash(&self) -> Result<Hash>;
/// Get transaction count
fn get_transaction_count(&self) -> Result<u64>;
}
pub trait AsyncClient {
/// Send a signed transaction, but don't wait to see if the server accepted it.
fn async_send_transaction(
&self,
transaction: transaction::Transaction,
) -> io::Result<Signature>;
/// Create a transaction from the given message, and send it to the
/// server, but don't wait for to see if the server accepted it.
fn async_send_message(
&self,
keypairs: &[&Keypair],
message: Message,
recent_blockhash: Hash,
) -> io::Result<Signature>;
/// Create a transaction from a single instruction that only requires
/// a single signer. Then send it to the server, but don't wait for a reply.
fn async_send_instruction(
&self,
keypair: &Keypair,
instruction: Instruction,
recent_blockhash: Hash,
) -> io::Result<Signature>;
/// Attempt to transfer lamports from `keypair` to `pubkey`, but don't wait to confirm.
fn async_transfer(
&self,
lamports: u64,
keypair: &Keypair,
pubkey: &Pubkey,
recent_blockhash: Hash,
) -> io::Result<Signature>;
}

View File

@ -1,6 +1,6 @@
pub mod account;
pub mod async_client;
pub mod bpf_loader;
pub mod client;
pub mod fee_calculator;
pub mod genesis_block;
pub mod hash;
@ -14,7 +14,6 @@ pub mod pubkey;
pub mod rpc_port;
pub mod short_vec;
pub mod signature;
pub mod sync_client;
pub mod system_instruction;
pub mod system_program;
pub mod system_transaction;

View File

@ -1,36 +0,0 @@
//! Defines a trait for blocking (synchronous) communication with a Solana server.
//! Implementations are expected to create tranasctions, sign them, and send
//! them with multiple retries, updating blockhashes and resigning as-needed.
use crate::instruction::Instruction;
use crate::message::Message;
use crate::pubkey::Pubkey;
use crate::signature::{Keypair, Signature};
use crate::transaction;
use crate::transport::Result;
pub trait SyncClient {
/// Create a transaction from the given message, and send it to the
/// server, retrying as-needed.
fn send_message(&self, keypairs: &[&Keypair], message: Message) -> Result<Signature>;
/// Create a transaction from a single instruction that only requires
/// a single signer. Then send it to the server, retrying as-needed.
fn send_instruction(&self, keypair: &Keypair, instruction: Instruction) -> Result<Signature>;
/// Transfer lamports from `keypair` to `pubkey`, retrying until the
/// transfer completes or produces and error.
fn transfer(&self, lamports: u64, keypair: &Keypair, pubkey: &Pubkey) -> Result<Signature>;
/// Get an account or None if not found.
fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>>;
/// Get account balance or 0 if not found.
fn get_balance(&self, pubkey: &Pubkey) -> Result<u64>;
/// Get signature status.
fn get_signature_status(
&self,
signature: &Signature,
) -> Result<Option<transaction::Result<()>>>;
}