thin_client grooming: remove dead code, improve var names and error reporting
This commit is contained in:
parent
0e8540417f
commit
e7df3cfe22
|
@ -3,7 +3,6 @@
|
||||||
//! messages to the network directly. The binary encoding of its messages are
|
//! messages to the network directly. The binary encoding of its messages are
|
||||||
//! unstable and may change in future releases.
|
//! unstable and may change in future releases.
|
||||||
|
|
||||||
use crate::bank::Bank;
|
|
||||||
use crate::cluster_info::{ClusterInfo, ClusterInfoError, NodeInfo};
|
use crate::cluster_info::{ClusterInfo, ClusterInfoError, NodeInfo};
|
||||||
use crate::fullnode::{Fullnode, FullnodeConfig};
|
use crate::fullnode::{Fullnode, FullnodeConfig};
|
||||||
use crate::gossip_service::GossipService;
|
use crate::gossip_service::GossipService;
|
||||||
|
@ -12,7 +11,6 @@ use crate::result::{Error, Result};
|
||||||
use crate::rpc_request::{RpcClient, RpcRequest, RpcRequestHandler};
|
use crate::rpc_request::{RpcClient, RpcRequest, RpcRequestHandler};
|
||||||
use bincode::serialize_into;
|
use bincode::serialize_into;
|
||||||
use bs58;
|
use bs58;
|
||||||
use hashbrown::HashMap;
|
|
||||||
use log::Level;
|
use log::Level;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use solana_metrics;
|
use solana_metrics;
|
||||||
|
@ -38,10 +36,6 @@ pub struct ThinClient {
|
||||||
rpc_addr: SocketAddr,
|
rpc_addr: SocketAddr,
|
||||||
transactions_addr: SocketAddr,
|
transactions_addr: SocketAddr,
|
||||||
transactions_socket: UdpSocket,
|
transactions_socket: UdpSocket,
|
||||||
transaction_count: u64,
|
|
||||||
balances: HashMap<Pubkey, Account>,
|
|
||||||
signature_status: bool,
|
|
||||||
confirmation: Option<usize>,
|
|
||||||
rpc_client: RpcClient,
|
rpc_client: RpcClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,41 +76,39 @@ impl ThinClient {
|
||||||
rpc_addr,
|
rpc_addr,
|
||||||
transactions_addr,
|
transactions_addr,
|
||||||
transactions_socket,
|
transactions_socket,
|
||||||
transaction_count: 0,
|
|
||||||
balances: HashMap::new(),
|
|
||||||
signature_status: false,
|
|
||||||
confirmation: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a signed Transaction to the server for processing. This method
|
/// Send a signed Transaction to the server for processing. This method
|
||||||
/// does not wait for a response.
|
/// does not wait for a response.
|
||||||
pub fn transfer_signed(&self, tx: &Transaction) -> io::Result<Signature> {
|
pub fn transfer_signed(&self, transaction: &Transaction) -> io::Result<Signature> {
|
||||||
let mut buf = vec![0; tx.serialized_size().unwrap() as usize];
|
let mut buf = vec![0; transaction.serialized_size().unwrap() as usize];
|
||||||
let mut wr = std::io::Cursor::new(&mut buf[..]);
|
let mut wr = std::io::Cursor::new(&mut buf[..]);
|
||||||
serialize_into(&mut wr, &tx).expect("serialize Transaction in pub fn transfer_signed");
|
serialize_into(&mut wr, &transaction)
|
||||||
|
.expect("serialize Transaction in pub fn transfer_signed");
|
||||||
assert!(buf.len() < PACKET_DATA_SIZE);
|
assert!(buf.len() < PACKET_DATA_SIZE);
|
||||||
self.transactions_socket
|
self.transactions_socket
|
||||||
.send_to(&buf[..], &self.transactions_addr)?;
|
.send_to(&buf[..], &self.transactions_addr)?;
|
||||||
Ok(tx.signatures[0])
|
Ok(transaction.signatures[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retry a sending a signed Transaction to the server for processing.
|
/// Retry a sending a signed Transaction to the server for processing.
|
||||||
pub fn retry_transfer(
|
pub fn retry_transfer(
|
||||||
&mut self,
|
&mut self,
|
||||||
keypair: &Keypair,
|
keypair: &Keypair,
|
||||||
tx: &mut Transaction,
|
transaction: &mut Transaction,
|
||||||
tries: usize,
|
tries: usize,
|
||||||
) -> io::Result<Signature> {
|
) -> io::Result<Signature> {
|
||||||
for x in 0..tries {
|
for x in 0..tries {
|
||||||
tx.sign(&[keypair], self.get_last_id());
|
transaction.sign(&[keypair], self.get_last_id());
|
||||||
let mut buf = vec![0; tx.serialized_size().unwrap() as usize];
|
let mut buf = vec![0; transaction.serialized_size().unwrap() as usize];
|
||||||
let mut wr = std::io::Cursor::new(&mut buf[..]);
|
let mut wr = std::io::Cursor::new(&mut buf[..]);
|
||||||
serialize_into(&mut wr, &tx).expect("serialize Transaction in pub fn transfer_signed");
|
serialize_into(&mut wr, &transaction)
|
||||||
|
.expect("serialize Transaction in pub fn transfer_signed");
|
||||||
self.transactions_socket
|
self.transactions_socket
|
||||||
.send_to(&buf[..], &self.transactions_addr)?;
|
.send_to(&buf[..], &self.transactions_addr)?;
|
||||||
if self.poll_for_signature(&tx.signatures[0]).is_ok() {
|
if self.poll_for_signature(&transaction.signatures[0]).is_ok() {
|
||||||
return Ok(tx.signatures[0]);
|
return Ok(transaction.signatures[0]);
|
||||||
}
|
}
|
||||||
info!("{} tries failed transfer to {}", x, self.transactions_addr);
|
info!("{} tries failed transfer to {}", x, self.transactions_addr);
|
||||||
}
|
}
|
||||||
|
@ -129,21 +121,21 @@ impl ThinClient {
|
||||||
/// Creates, signs, and processes a Transaction. Useful for writing unit-tests.
|
/// Creates, signs, and processes a Transaction. Useful for writing unit-tests.
|
||||||
pub fn transfer(
|
pub fn transfer(
|
||||||
&self,
|
&self,
|
||||||
n: u64,
|
tokens: u64,
|
||||||
keypair: &Keypair,
|
keypair: &Keypair,
|
||||||
to: Pubkey,
|
to: Pubkey,
|
||||||
last_id: &Hash,
|
last_id: &Hash,
|
||||||
) -> io::Result<Signature> {
|
) -> io::Result<Signature> {
|
||||||
debug!(
|
debug!(
|
||||||
"transfer: n={} from={:?} to={:?} last_id={:?}",
|
"transfer: tokens={} from={:?} to={:?} last_id={:?}",
|
||||||
n,
|
tokens,
|
||||||
keypair.pubkey(),
|
keypair.pubkey(),
|
||||||
to,
|
to,
|
||||||
last_id
|
last_id
|
||||||
);
|
);
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let tx = SystemTransaction::new_account(keypair, to, n, *last_id, 0);
|
let transaction = SystemTransaction::new_account(keypair, to, tokens, *last_id, 0);
|
||||||
let result = self.transfer_signed(&tx);
|
let result = self.transfer_signed(&transaction);
|
||||||
solana_metrics::submit(
|
solana_metrics::submit(
|
||||||
influxdb::Point::new("thinclient")
|
influxdb::Point::new("thinclient")
|
||||||
.add_tag("op", influxdb::Value::String("transfer".to_string()))
|
.add_tag("op", influxdb::Value::String("transfer".to_string()))
|
||||||
|
@ -158,19 +150,24 @@ impl ThinClient {
|
||||||
|
|
||||||
pub fn get_account_userdata(&mut self, pubkey: &Pubkey) -> io::Result<Option<Vec<u8>>> {
|
pub fn get_account_userdata(&mut self, pubkey: &Pubkey) -> io::Result<Option<Vec<u8>>> {
|
||||||
let params = json!([format!("{}", pubkey)]);
|
let params = json!([format!("{}", pubkey)]);
|
||||||
let resp = self
|
let response =
|
||||||
.rpc_client
|
self.rpc_client
|
||||||
.make_rpc_request(1, RpcRequest::GetAccountInfo, Some(params));
|
.make_rpc_request(1, RpcRequest::GetAccountInfo, Some(params));
|
||||||
if let Ok(account_json) = resp {
|
match response {
|
||||||
|
Ok(account_json) => {
|
||||||
let account: Account =
|
let account: Account =
|
||||||
serde_json::from_value(account_json).expect("deserialize account");
|
serde_json::from_value(account_json).expect("deserialize account");
|
||||||
return Ok(Some(account.userdata));
|
Ok(Some(account.userdata))
|
||||||
}
|
}
|
||||||
|
Err(error) => {
|
||||||
|
debug!("get_account_userdata failed: {:?}", error);
|
||||||
Err(io::Error::new(
|
Err(io::Error::new(
|
||||||
io::ErrorKind::Other,
|
io::ErrorKind::Other,
|
||||||
"get_account_userdata failed",
|
"get_account_userdata failed",
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Request the balance of the user holding `pubkey`. This method blocks
|
/// Request the balance of the user holding `pubkey`. This method blocks
|
||||||
/// until the server sends a response. If the response packet is dropped
|
/// until the server sends a response. If the response packet is dropped
|
||||||
|
@ -178,69 +175,67 @@ impl ThinClient {
|
||||||
pub fn get_balance(&mut self, pubkey: &Pubkey) -> io::Result<u64> {
|
pub fn get_balance(&mut self, pubkey: &Pubkey) -> io::Result<u64> {
|
||||||
trace!("get_balance sending request to {}", self.rpc_addr);
|
trace!("get_balance sending request to {}", self.rpc_addr);
|
||||||
let params = json!([format!("{}", pubkey)]);
|
let params = json!([format!("{}", pubkey)]);
|
||||||
let resp = self
|
let response =
|
||||||
.rpc_client
|
self.rpc_client
|
||||||
.make_rpc_request(1, RpcRequest::GetAccountInfo, Some(params));
|
.make_rpc_request(1, RpcRequest::GetAccountInfo, Some(params));
|
||||||
if let Ok(account_json) = resp {
|
|
||||||
|
response
|
||||||
|
.and_then(|account_json| {
|
||||||
let account: Account =
|
let account: Account =
|
||||||
serde_json::from_value(account_json).expect("deserialize account");
|
serde_json::from_value(account_json).expect("deserialize account");
|
||||||
trace!("Response account {:?} {:?}", pubkey, account);
|
trace!("Response account {:?} {:?}", pubkey, account);
|
||||||
self.balances.insert(*pubkey, account.clone());
|
trace!("get_balance {:?}", account.tokens);
|
||||||
} else {
|
Ok(account.tokens)
|
||||||
debug!("Response account {}: None ", pubkey);
|
})
|
||||||
self.balances.remove(&pubkey);
|
.map_err(|error| {
|
||||||
}
|
debug!("Response account {}: None (error: {:?})", pubkey, error);
|
||||||
trace!("get_balance {:?}", self.balances.get(pubkey));
|
io::Error::new(io::ErrorKind::Other, "AccountNotFound")
|
||||||
|
})
|
||||||
// TODO: This is a hard coded call to introspect the balance of a budget_dsl contract
|
|
||||||
// In the future custom contracts would need their own introspection
|
|
||||||
self.balances
|
|
||||||
.get(pubkey)
|
|
||||||
.map(Bank::read_balance)
|
|
||||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "AccountNotFound"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request the confirmation time from the leader node
|
/// Request the confirmation time from the leader node
|
||||||
pub fn get_confirmation_time(&mut self) -> usize {
|
pub fn get_confirmation_time(&mut self) -> usize {
|
||||||
trace!("get_confirmation_time");
|
trace!("get_confirmation_time");
|
||||||
let mut done = false;
|
loop {
|
||||||
while !done {
|
|
||||||
debug!("get_confirmation_time send_to {}", &self.rpc_addr);
|
debug!("get_confirmation_time send_to {}", &self.rpc_addr);
|
||||||
let resp = self
|
|
||||||
.rpc_client
|
let response =
|
||||||
|
self.rpc_client
|
||||||
.make_rpc_request(1, RpcRequest::GetConfirmationTime, None);
|
.make_rpc_request(1, RpcRequest::GetConfirmationTime, None);
|
||||||
|
|
||||||
if let Ok(value) = resp {
|
match response {
|
||||||
done = true;
|
Ok(value) => {
|
||||||
let confirmation = value.as_u64().unwrap() as usize;
|
let confirmation = value.as_u64().unwrap() as usize;
|
||||||
self.confirmation = Some(confirmation);
|
return confirmation;
|
||||||
} else {
|
|
||||||
debug!("thin_client get_confirmation_time error: {:?}", resp);
|
|
||||||
}
|
}
|
||||||
|
Err(error) => {
|
||||||
|
debug!("thin_client get_confirmation_time error: {:?}", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
self.confirmation.expect("some confirmation")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request the transaction count. If the response packet is dropped by the network,
|
/// Request the transaction count. If the response packet is dropped by the network,
|
||||||
/// this method will try again 5 times.
|
/// this method will try again 5 times.
|
||||||
pub fn transaction_count(&mut self) -> u64 {
|
pub fn transaction_count(&mut self) -> u64 {
|
||||||
debug!("transaction_count");
|
debug!("transaction_count");
|
||||||
let mut tries_left = 5;
|
for _tries in 0..5 {
|
||||||
while tries_left > 0 {
|
let response =
|
||||||
let resp = self
|
self.rpc_client
|
||||||
.rpc_client
|
|
||||||
.make_rpc_request(1, RpcRequest::GetTransactionCount, None);
|
.make_rpc_request(1, RpcRequest::GetTransactionCount, None);
|
||||||
|
|
||||||
if let Ok(value) = resp {
|
match response {
|
||||||
debug!("transaction_count recv_response: {:?}", value);
|
Ok(value) => {
|
||||||
tries_left = 0;
|
debug!("transaction_count response: {:?}", value);
|
||||||
let transaction_count = value.as_u64().unwrap();
|
let transaction_count = value.as_u64().unwrap();
|
||||||
self.transaction_count = transaction_count;
|
return transaction_count;
|
||||||
} else {
|
|
||||||
tries_left -= 1;
|
|
||||||
}
|
}
|
||||||
|
Err(error) => {
|
||||||
|
debug!("transaction_count failed: {:?}", error);
|
||||||
}
|
}
|
||||||
self.transaction_count
|
};
|
||||||
|
}
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request the last Entry ID from the server. This method blocks
|
/// Request the last Entry ID from the server. This method blocks
|
||||||
|
@ -248,17 +243,20 @@ impl ThinClient {
|
||||||
pub fn get_last_id(&mut self) -> Hash {
|
pub fn get_last_id(&mut self) -> Hash {
|
||||||
loop {
|
loop {
|
||||||
trace!("get_last_id send_to {}", &self.rpc_addr);
|
trace!("get_last_id send_to {}", &self.rpc_addr);
|
||||||
let resp = self
|
let response = self
|
||||||
.rpc_client
|
.rpc_client
|
||||||
.make_rpc_request(1, RpcRequest::GetLastId, None);
|
.make_rpc_request(1, RpcRequest::GetLastId, None);
|
||||||
|
|
||||||
if let Ok(value) = resp {
|
match response {
|
||||||
|
Ok(value) => {
|
||||||
let last_id_str = value.as_str().unwrap();
|
let last_id_str = value.as_str().unwrap();
|
||||||
let last_id_vec = bs58::decode(last_id_str).into_vec().unwrap();
|
let last_id_vec = bs58::decode(last_id_str).into_vec().unwrap();
|
||||||
return Hash::new(&last_id_vec);
|
return Hash::new(&last_id_vec);
|
||||||
} else {
|
|
||||||
debug!("thin_client get_last_id error: {:?}", resp);
|
|
||||||
}
|
}
|
||||||
|
Err(error) => {
|
||||||
|
debug!("thin_client get_last_id error: {:?}", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,37 +329,43 @@ impl ThinClient {
|
||||||
/// Check a signature in the bank. This method blocks
|
/// Check a signature in the bank. This method blocks
|
||||||
/// until the server sends a response.
|
/// until the server sends a response.
|
||||||
pub fn check_signature(&mut self, signature: &Signature) -> bool {
|
pub fn check_signature(&mut self, signature: &Signature) -> bool {
|
||||||
trace!("check_signature");
|
trace!("check_signature: {:?}", signature);
|
||||||
let params = json!([format!("{}", signature)]);
|
let params = json!([format!("{}", signature)]);
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let mut done = false;
|
|
||||||
while !done {
|
loop {
|
||||||
let resp = self.rpc_client.make_rpc_request(
|
let response = self.rpc_client.make_rpc_request(
|
||||||
1,
|
1,
|
||||||
RpcRequest::ConfirmTransaction,
|
RpcRequest::ConfirmTransaction,
|
||||||
Some(params.clone()),
|
Some(params.clone()),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Ok(confirmation) = resp {
|
match response {
|
||||||
done = true;
|
Ok(confirmation) => {
|
||||||
self.signature_status = confirmation.as_bool().unwrap();
|
let signature_status = confirmation.as_bool().unwrap();
|
||||||
if self.signature_status {
|
if signature_status {
|
||||||
trace!("Response found signature");
|
trace!("Response found signature");
|
||||||
} else {
|
} else {
|
||||||
trace!("Response signature not found");
|
trace!("Response signature not found");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
solana_metrics::submit(
|
solana_metrics::submit(
|
||||||
influxdb::Point::new("thinclient")
|
influxdb::Point::new("thinclient")
|
||||||
.add_tag("op", influxdb::Value::String("check_signature".to_string()))
|
.add_tag("op", influxdb::Value::String("check_signature".to_string()))
|
||||||
.add_field(
|
.add_field(
|
||||||
"duration_ms",
|
"duration_ms",
|
||||||
influxdb::Value::Integer(timing::duration_as_ms(&now.elapsed()) as i64),
|
influxdb::Value::Integer(
|
||||||
|
timing::duration_as_ms(&now.elapsed()) as i64
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
);
|
);
|
||||||
self.signature_status
|
return signature_status;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
debug!("check_signature request failed: {:?}", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,7 +486,6 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::client::mk_client;
|
use crate::client::mk_client;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
|
||||||
use solana_sdk::system_instruction::SystemInstruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
use solana_sdk::vote_program::VoteState;
|
use solana_sdk::vote_program::VoteState;
|
||||||
use solana_sdk::vote_transaction::VoteTransaction;
|
use solana_sdk::vote_transaction::VoteTransaction;
|
||||||
|
|
Loading…
Reference in New Issue