2018-05-08 17:32:50 -06:00
|
|
|
//! The `thin_client` module is a client-side object that interfaces with
|
|
|
|
//! a server-side TPU. Client code should use this object instead of writing
|
|
|
|
//! messages to the network directly. The binary encoding of its messages are
|
|
|
|
//! unstable and may change in future releases.
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::bank::Bank;
|
|
|
|
use crate::cluster_info::{ClusterInfo, ClusterInfoError, NodeInfo};
|
|
|
|
use crate::gossip_service::GossipService;
|
|
|
|
use crate::packet::PACKET_DATA_SIZE;
|
|
|
|
use crate::result::{Error, Result};
|
|
|
|
use crate::rpc_request::{RpcClient, RpcRequest};
|
2018-11-05 10:50:58 -07:00
|
|
|
use bincode::serialize;
|
|
|
|
use bs58;
|
2018-09-09 04:50:43 +09:00
|
|
|
use log::Level;
|
2018-11-05 10:50:58 -07:00
|
|
|
use serde_json;
|
2018-11-16 08:45:59 -08:00
|
|
|
use solana_metrics;
|
|
|
|
use solana_metrics::influxdb;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::account::Account;
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, Signature};
|
2018-12-04 20:19:48 -08:00
|
|
|
use solana_sdk::system_transaction::SystemTransaction;
|
2018-11-16 08:45:59 -08:00
|
|
|
use solana_sdk::timing;
|
2018-11-29 16:18:47 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2018-09-09 04:50:43 +09:00
|
|
|
use std;
|
2018-04-17 18:14:53 -04:00
|
|
|
use std::collections::HashMap;
|
2018-03-28 20:13:10 -06:00
|
|
|
use std::io;
|
2018-04-28 00:31:20 -07:00
|
|
|
use std::net::{SocketAddr, UdpSocket};
|
2018-08-20 14:03:36 -06:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use std::sync::{Arc, RwLock};
|
2018-07-20 18:13:47 -04:00
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2018-07-05 21:40:09 -07:00
|
|
|
use std::time::Instant;
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-06-07 14:51:29 -06:00
|
|
|
/// An object for querying and sending transactions to the network.
|
2018-05-08 17:32:50 -06:00
|
|
|
pub struct ThinClient {
|
2018-11-05 10:50:58 -07:00
|
|
|
rpc_addr: SocketAddr,
|
2018-05-25 15:51:41 -06:00
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
2018-04-17 18:14:53 -04:00
|
|
|
last_id: Option<Hash>,
|
2018-05-14 07:16:39 -06:00
|
|
|
transaction_count: u64,
|
2018-08-10 21:13:34 -07:00
|
|
|
balances: HashMap<Pubkey, Account>,
|
2018-06-28 18:59:02 -06:00
|
|
|
signature_status: bool,
|
2018-08-13 08:55:13 -07:00
|
|
|
finality: Option<usize>,
|
2018-11-27 21:08:14 -08:00
|
|
|
|
|
|
|
rpc_client: RpcClient,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
2018-05-08 17:32:50 -06:00
|
|
|
impl ThinClient {
|
2018-11-05 10:50:58 -07:00
|
|
|
/// Create a new ThinClient that will interface with the Rpc at `rpc_addr` using TCP
|
|
|
|
/// and the Tpu at `transactions_addr` over `transactions_socket` using UDP.
|
2018-05-22 09:46:52 -07:00
|
|
|
pub fn new(
|
2018-11-05 10:50:58 -07:00
|
|
|
rpc_addr: SocketAddr,
|
2018-05-25 15:51:41 -06:00
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
2018-11-29 13:28:52 -08:00
|
|
|
) -> Self {
|
|
|
|
Self::new_from_client(
|
|
|
|
rpc_addr,
|
|
|
|
transactions_addr,
|
|
|
|
transactions_socket,
|
2018-12-03 15:24:54 -08:00
|
|
|
RpcClient::new_from_socket(rpc_addr),
|
2018-11-29 13:28:52 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_timeout(
|
|
|
|
rpc_addr: SocketAddr,
|
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Self {
|
2018-12-03 15:24:54 -08:00
|
|
|
let rpc_client = RpcClient::new_with_timeout(rpc_addr, timeout);
|
2018-11-29 13:28:52 -08:00
|
|
|
Self::new_from_client(rpc_addr, transactions_addr, transactions_socket, rpc_client)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_from_client(
|
|
|
|
rpc_addr: SocketAddr,
|
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
|
|
|
rpc_client: RpcClient,
|
2018-05-22 09:46:52 -07:00
|
|
|
) -> Self {
|
2018-07-11 14:40:46 -06:00
|
|
|
ThinClient {
|
2018-11-29 13:28:52 -08:00
|
|
|
rpc_client,
|
2018-11-05 10:50:58 -07:00
|
|
|
rpc_addr,
|
2018-05-25 15:51:41 -06:00
|
|
|
transactions_addr,
|
|
|
|
transactions_socket,
|
2018-04-17 18:14:53 -04:00
|
|
|
last_id: None,
|
2018-05-14 07:16:39 -06:00
|
|
|
transaction_count: 0,
|
2018-04-17 18:14:53 -04:00
|
|
|
balances: HashMap::new(),
|
2018-06-28 18:59:02 -06:00
|
|
|
signature_status: false,
|
2018-08-13 08:55:13 -07:00
|
|
|
finality: None,
|
2018-07-11 14:40:46 -06:00
|
|
|
}
|
2018-04-17 18:14:53 -04:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Send a signed Transaction to the server for processing. This method
|
|
|
|
/// does not wait for a response.
|
2018-07-20 18:58:13 -04:00
|
|
|
pub fn transfer_signed(&self, tx: &Transaction) -> io::Result<Signature> {
|
2018-05-25 16:05:37 -06:00
|
|
|
let data = serialize(&tx).expect("serialize Transaction in pub fn transfer_signed");
|
2018-11-15 19:29:54 -08:00
|
|
|
assert!(data.len() < PACKET_DATA_SIZE);
|
2018-05-25 15:51:41 -06:00
|
|
|
self.transactions_socket
|
2018-07-20 18:58:13 -04:00
|
|
|
.send_to(&data, &self.transactions_addr)?;
|
2018-10-26 14:43:34 -07:00
|
|
|
Ok(tx.signatures[0])
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:58:22 -07:00
|
|
|
/// Retry a sending a signed Transaction to the server for processing.
|
2018-11-06 06:07:43 -07:00
|
|
|
pub fn retry_transfer(
|
2018-08-26 15:58:22 -07:00
|
|
|
&mut self,
|
2018-11-06 06:07:43 -07:00
|
|
|
keypair: &Keypair,
|
|
|
|
tx: &mut Transaction,
|
2018-08-26 15:58:22 -07:00
|
|
|
tries: usize,
|
|
|
|
) -> io::Result<Signature> {
|
|
|
|
for x in 0..tries {
|
2018-10-26 14:43:34 -07:00
|
|
|
tx.sign(&[&keypair], self.get_last_id());
|
2018-11-06 06:07:43 -07:00
|
|
|
let data = serialize(&tx).expect("serialize Transaction in pub fn transfer_signed");
|
2018-08-26 15:58:22 -07:00
|
|
|
self.transactions_socket
|
|
|
|
.send_to(&data, &self.transactions_addr)?;
|
2018-10-26 14:43:34 -07:00
|
|
|
if self.poll_for_signature(&tx.signatures[0]).is_ok() {
|
|
|
|
return Ok(tx.signatures[0]);
|
2018-08-26 15:58:22 -07:00
|
|
|
}
|
|
|
|
info!("{} tries failed transfer to {}", x, self.transactions_addr);
|
|
|
|
}
|
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2018-11-06 06:07:43 -07:00
|
|
|
"retry_transfer failed",
|
2018-08-26 15:58:22 -07:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Creates, signs, and processes a Transaction. Useful for writing unit-tests.
|
2018-02-28 14:16:50 -07:00
|
|
|
pub fn transfer(
|
2018-03-05 11:11:00 -07:00
|
|
|
&self,
|
2018-11-05 09:36:22 -07:00
|
|
|
n: u64,
|
2018-08-09 08:56:04 -06:00
|
|
|
keypair: &Keypair,
|
2018-08-09 09:13:57 -06:00
|
|
|
to: Pubkey,
|
2018-03-06 17:36:45 -07:00
|
|
|
last_id: &Hash,
|
2018-03-01 12:23:27 -07:00
|
|
|
) -> io::Result<Signature> {
|
2018-07-05 21:40:09 -07:00
|
|
|
let now = Instant::now();
|
2018-09-26 09:51:51 -06:00
|
|
|
let tx = Transaction::system_new(keypair, to, n, *last_id);
|
2018-07-20 18:58:13 -04:00
|
|
|
let result = self.transfer_signed(&tx);
|
2018-11-16 08:45:59 -08:00
|
|
|
solana_metrics::submit(
|
2018-07-05 21:40:09 -07:00
|
|
|
influxdb::Point::new("thinclient")
|
|
|
|
.add_tag("op", influxdb::Value::String("transfer".to_string()))
|
|
|
|
.add_field(
|
|
|
|
"duration_ms",
|
|
|
|
influxdb::Value::Integer(timing::duration_as_ms(&now.elapsed()) as i64),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.to_owned(),
|
2018-07-05 21:40:09 -07:00
|
|
|
);
|
|
|
|
result
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
pub fn get_account_userdata(&mut self, pubkey: &Pubkey) -> io::Result<Option<Vec<u8>>> {
|
2018-11-14 18:57:34 -08:00
|
|
|
let params = json!([format!("{}", pubkey)]);
|
2018-12-03 15:24:54 -08:00
|
|
|
let resp = RpcRequest::GetAccountInfo.make_rpc_request(&self.rpc_client, 1, Some(params));
|
2018-11-05 10:50:58 -07:00
|
|
|
if let Ok(account_json) = resp {
|
|
|
|
let account: Account =
|
|
|
|
serde_json::from_value(account_json).expect("deserialize account");
|
|
|
|
return Ok(Some(account.userdata));
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
2018-11-05 10:50:58 -07:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"get_account_userdata failed",
|
|
|
|
))
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Request the balance of the user holding `pubkey`. This method blocks
|
|
|
|
/// until the server sends a response. If the response packet is dropped
|
|
|
|
/// by the network, this method will hang indefinitely.
|
2018-11-05 09:36:22 -07:00
|
|
|
pub fn get_balance(&mut self, pubkey: &Pubkey) -> io::Result<u64> {
|
2018-11-05 10:50:58 -07:00
|
|
|
trace!("get_balance sending request to {}", self.rpc_addr);
|
2018-11-14 18:57:34 -08:00
|
|
|
let params = json!([format!("{}", pubkey)]);
|
2018-12-03 15:24:54 -08:00
|
|
|
let resp = RpcRequest::GetAccountInfo.make_rpc_request(&self.rpc_client, 1, Some(params));
|
2018-11-05 10:50:58 -07:00
|
|
|
if let Ok(account_json) = resp {
|
|
|
|
let account: Account =
|
|
|
|
serde_json::from_value(account_json).expect("deserialize account");
|
|
|
|
trace!("Response account {:?} {:?}", pubkey, account);
|
|
|
|
self.balances.insert(*pubkey, account.clone());
|
|
|
|
} else {
|
|
|
|
debug!("Response account {}: None ", pubkey);
|
|
|
|
self.balances.remove(&pubkey);
|
2018-04-17 18:41:58 -04:00
|
|
|
}
|
2018-09-07 20:18:36 -07:00
|
|
|
trace!("get_balance {:?}", self.balances.get(pubkey));
|
2018-11-27 21:08:14 -08:00
|
|
|
|
2018-09-09 07:07:38 -07:00
|
|
|
// 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
|
2018-07-02 17:31:10 -06:00
|
|
|
self.balances
|
|
|
|
.get(pubkey)
|
2018-09-17 13:36:31 -07:00
|
|
|
.map(Bank::read_balance)
|
2018-09-21 21:01:13 -07:00
|
|
|
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "AccountNotFound"))
|
2018-03-01 12:23:27 -07:00
|
|
|
}
|
|
|
|
|
2018-08-13 08:55:13 -07:00
|
|
|
/// Request the finality from the leader node
|
|
|
|
pub fn get_finality(&mut self) -> usize {
|
|
|
|
trace!("get_finality");
|
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
2018-11-05 10:50:58 -07:00
|
|
|
debug!("get_finality send_to {}", &self.rpc_addr);
|
2018-12-03 15:24:54 -08:00
|
|
|
let resp = RpcRequest::GetFinality.make_rpc_request(&self.rpc_client, 1, None);
|
2018-11-05 10:50:58 -07:00
|
|
|
|
|
|
|
if let Ok(value) = resp {
|
|
|
|
done = true;
|
|
|
|
let finality = value.as_u64().unwrap() as usize;
|
|
|
|
self.finality = Some(finality);
|
|
|
|
} else {
|
|
|
|
debug!("thin_client get_finality error: {:?}", resp);
|
2018-08-13 08:55:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.finality.expect("some finality")
|
|
|
|
}
|
|
|
|
|
2018-05-14 07:16:39 -06:00
|
|
|
/// Request the transaction count. If the response packet is dropped by the network,
|
2018-08-23 20:57:13 -07:00
|
|
|
/// this method will try again 5 times.
|
2018-05-14 09:45:09 -06:00
|
|
|
pub fn transaction_count(&mut self) -> u64 {
|
2018-07-22 16:20:07 -07:00
|
|
|
debug!("transaction_count");
|
2018-08-23 20:57:13 -07:00
|
|
|
let mut tries_left = 5;
|
|
|
|
while tries_left > 0 {
|
2018-12-03 15:24:54 -08:00
|
|
|
let resp = RpcRequest::GetTransactionCount.make_rpc_request(&self.rpc_client, 1, None);
|
2018-11-05 10:50:58 -07:00
|
|
|
|
|
|
|
if let Ok(value) = resp {
|
|
|
|
debug!("transaction_count recv_response: {:?}", value);
|
|
|
|
tries_left = 0;
|
|
|
|
let transaction_count = value.as_u64().unwrap();
|
|
|
|
self.transaction_count = transaction_count;
|
2018-08-23 20:57:13 -07:00
|
|
|
} else {
|
|
|
|
tries_left -= 1;
|
2018-05-14 07:16:39 -06:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 09:43:40 -06:00
|
|
|
self.transaction_count
|
2018-05-14 07:16:39 -06:00
|
|
|
}
|
|
|
|
|
2018-04-02 09:30:10 -06:00
|
|
|
/// Request the last Entry ID from the server. This method blocks
|
2018-05-03 13:26:45 -06:00
|
|
|
/// until the server sends a response.
|
2018-05-31 13:41:42 -06:00
|
|
|
pub fn get_last_id(&mut self) -> Hash {
|
2018-06-28 12:58:33 -06:00
|
|
|
trace!("get_last_id");
|
2018-05-14 09:40:29 -06:00
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
2018-11-05 10:50:58 -07:00
|
|
|
debug!("get_last_id send_to {}", &self.rpc_addr);
|
2018-12-03 15:24:54 -08:00
|
|
|
let resp = RpcRequest::GetLastId.make_rpc_request(&self.rpc_client, 1, None);
|
2018-11-05 10:50:58 -07:00
|
|
|
|
|
|
|
if let Ok(value) = resp {
|
|
|
|
done = true;
|
|
|
|
let last_id_str = value.as_str().unwrap();
|
|
|
|
let last_id_vec = bs58::decode(last_id_str).into_vec().unwrap();
|
|
|
|
let last_id = Hash::new(&last_id_vec);
|
|
|
|
self.last_id = Some(last_id);
|
|
|
|
} else {
|
|
|
|
debug!("thin_client get_last_id error: {:?}", resp);
|
2018-05-14 09:40:29 -06:00
|
|
|
}
|
|
|
|
}
|
2018-05-31 13:41:42 -06:00
|
|
|
self.last_id.expect("some last_id")
|
2018-03-05 12:48:09 -07:00
|
|
|
}
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-08-25 18:24:25 -07:00
|
|
|
pub fn submit_poll_balance_metrics(elapsed: &Duration) {
|
2018-11-16 08:45:59 -08:00
|
|
|
solana_metrics::submit(
|
2018-07-05 21:40:09 -07:00
|
|
|
influxdb::Point::new("thinclient")
|
|
|
|
.add_tag("op", influxdb::Value::String("get_balance".to_string()))
|
|
|
|
.add_field(
|
|
|
|
"duration_ms",
|
2018-08-25 18:24:25 -07:00
|
|
|
influxdb::Value::Integer(timing::duration_as_ms(elapsed) as i64),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.to_owned(),
|
2018-07-05 21:40:09 -07:00
|
|
|
);
|
2018-08-25 18:24:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_balance_with_timeout(
|
|
|
|
&mut self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
polling_frequency: &Duration,
|
|
|
|
timeout: &Duration,
|
2018-11-05 09:36:22 -07:00
|
|
|
) -> io::Result<u64> {
|
2018-08-25 18:24:25 -07:00
|
|
|
let now = Instant::now();
|
|
|
|
loop {
|
2018-09-04 21:33:19 -07:00
|
|
|
match self.get_balance(&pubkey) {
|
|
|
|
Ok(bal) => {
|
|
|
|
ThinClient::submit_poll_balance_metrics(&now.elapsed());
|
|
|
|
return Ok(bal);
|
|
|
|
}
|
2018-08-25 18:24:25 -07:00
|
|
|
Err(e) => {
|
|
|
|
sleep(*polling_frequency);
|
|
|
|
if now.elapsed() > *timeout {
|
|
|
|
ThinClient::submit_poll_balance_metrics(&now.elapsed());
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-07-26 21:27:44 -07:00
|
|
|
}
|
2018-05-25 21:54:20 -06:00
|
|
|
}
|
2018-06-28 12:58:33 -06:00
|
|
|
|
2018-11-05 09:36:22 -07:00
|
|
|
pub fn poll_get_balance(&mut self, pubkey: &Pubkey) -> io::Result<u64> {
|
2018-08-25 18:24:25 -07:00
|
|
|
self.poll_balance_with_timeout(pubkey, &Duration::from_millis(100), &Duration::from_secs(1))
|
|
|
|
}
|
|
|
|
|
2018-07-20 19:05:39 -04:00
|
|
|
/// Poll the server to confirm a transaction.
|
2018-08-09 09:26:21 -06:00
|
|
|
pub fn poll_for_signature(&mut self, signature: &Signature) -> io::Result<()> {
|
2018-07-20 19:05:39 -04:00
|
|
|
let now = Instant::now();
|
2018-08-09 09:26:21 -06:00
|
|
|
while !self.check_signature(signature) {
|
2018-07-20 19:05:39 -04:00
|
|
|
if now.elapsed().as_secs() > 1 {
|
|
|
|
// TODO: Return a better error.
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "signature not found"));
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(100));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-06-28 12:58:33 -06:00
|
|
|
/// Check a signature in the bank. This method blocks
|
|
|
|
/// until the server sends a response.
|
2018-08-09 09:26:21 -06:00
|
|
|
pub fn check_signature(&mut self, signature: &Signature) -> bool {
|
2018-06-28 12:58:33 -06:00
|
|
|
trace!("check_signature");
|
2018-11-14 18:57:34 -08:00
|
|
|
let params = json!([format!("{}", signature)]);
|
2018-07-05 21:40:09 -07:00
|
|
|
let now = Instant::now();
|
2018-06-28 12:58:33 -06:00
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
2018-11-05 10:50:58 -07:00
|
|
|
let resp = RpcRequest::ConfirmTransaction.make_rpc_request(
|
2018-11-27 21:08:14 -08:00
|
|
|
&self.rpc_client,
|
2018-11-05 10:50:58 -07:00
|
|
|
1,
|
|
|
|
Some(params.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Ok(confirmation) = resp {
|
|
|
|
done = true;
|
|
|
|
self.signature_status = confirmation.as_bool().unwrap();
|
|
|
|
if self.signature_status {
|
|
|
|
trace!("Response found signature");
|
|
|
|
} else {
|
|
|
|
trace!("Response signature not found");
|
2018-06-28 12:58:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 08:45:59 -08:00
|
|
|
solana_metrics::submit(
|
2018-07-05 21:40:09 -07:00
|
|
|
influxdb::Point::new("thinclient")
|
|
|
|
.add_tag("op", influxdb::Value::String("check_signature".to_string()))
|
|
|
|
.add_field(
|
|
|
|
"duration_ms",
|
|
|
|
influxdb::Value::Integer(timing::duration_as_ms(&now.elapsed()) as i64),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.to_owned(),
|
2018-07-05 21:40:09 -07:00
|
|
|
);
|
2018-06-28 12:58:33 -06:00
|
|
|
self.signature_status
|
|
|
|
}
|
2018-05-22 09:46:52 -07:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:40:09 -07:00
|
|
|
impl Drop for ThinClient {
|
|
|
|
fn drop(&mut self) {
|
2018-11-16 08:45:59 -08:00
|
|
|
solana_metrics::flush();
|
2018-07-05 21:40:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 16:32:40 -07:00
|
|
|
pub fn poll_gossip_for_leader(leader_ncp: SocketAddr, timeout: Option<u64>) -> Result<NodeInfo> {
|
2018-08-20 14:03:36 -06:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-10-08 20:55:54 -06:00
|
|
|
let (node, gossip_socket) = ClusterInfo::spy_node();
|
2018-09-09 04:50:43 +09:00
|
|
|
let my_addr = gossip_socket.local_addr().unwrap();
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info = Arc::new(RwLock::new(ClusterInfo::new(node)));
|
2018-12-10 01:24:41 -08:00
|
|
|
let gossip_service =
|
|
|
|
GossipService::new(&cluster_info.clone(), None, gossip_socket, exit.clone());
|
2018-09-09 04:50:43 +09:00
|
|
|
|
2018-08-31 00:10:39 -07:00
|
|
|
let leader_entry_point = NodeInfo::new_entry_point(&leader_ncp);
|
2018-11-15 13:23:26 -08:00
|
|
|
cluster_info
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.insert_info(leader_entry_point);
|
2018-08-20 14:03:36 -06:00
|
|
|
|
|
|
|
sleep(Duration::from_millis(100));
|
|
|
|
|
2018-09-09 04:50:43 +09:00
|
|
|
let deadline = match timeout {
|
|
|
|
Some(timeout) => Duration::new(timeout, 0),
|
|
|
|
None => Duration::new(std::u64::MAX, 0),
|
|
|
|
};
|
2018-08-20 14:03:36 -06:00
|
|
|
let now = Instant::now();
|
|
|
|
// Block until leader's correct contact info is received
|
2018-09-09 04:50:43 +09:00
|
|
|
let leader;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
trace!("polling {:?} for leader from {:?}", leader_ncp, my_addr);
|
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
if let Some(l) = cluster_info.read().unwrap().get_gossip_top_leader() {
|
2018-09-09 04:50:43 +09:00
|
|
|
leader = Some(l.clone());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if log_enabled!(Level::Trace) {
|
2018-10-08 20:55:54 -06:00
|
|
|
trace!("{}", cluster_info.read().unwrap().node_info_trace());
|
2018-09-09 04:50:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if now.elapsed() > deadline {
|
2018-10-08 20:55:54 -06:00
|
|
|
return Err(Error::ClusterInfoError(ClusterInfoError::NoLeader));
|
2018-08-20 14:03:36 -06:00
|
|
|
}
|
2018-09-09 04:50:43 +09:00
|
|
|
|
|
|
|
sleep(Duration::from_millis(100));
|
2018-08-20 14:03:36 -06:00
|
|
|
}
|
|
|
|
|
2018-12-06 13:52:47 -07:00
|
|
|
gossip_service.close()?;
|
2018-09-09 04:50:43 +09:00
|
|
|
|
|
|
|
if log_enabled!(Level::Trace) {
|
2018-10-08 20:55:54 -06:00
|
|
|
trace!("{}", cluster_info.read().unwrap().node_info_trace());
|
2018-09-09 04:50:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(leader.unwrap().clone())
|
2018-08-20 14:03:36 -06:00
|
|
|
}
|
|
|
|
|
2018-10-31 17:47:50 -07:00
|
|
|
pub fn retry_get_balance(
|
|
|
|
client: &mut ThinClient,
|
|
|
|
bob_pubkey: &Pubkey,
|
2018-11-05 09:36:22 -07:00
|
|
|
expected_balance: Option<u64>,
|
|
|
|
) -> Option<u64> {
|
2018-10-31 17:47:50 -07:00
|
|
|
const LAST: usize = 30;
|
|
|
|
for run in 0..LAST {
|
|
|
|
let balance_result = client.poll_get_balance(bob_pubkey);
|
|
|
|
if expected_balance.is_none() {
|
|
|
|
return balance_result.ok();
|
|
|
|
}
|
|
|
|
trace!(
|
|
|
|
"retry_get_balance[{}] {:?} {:?}",
|
|
|
|
run,
|
|
|
|
balance_result,
|
|
|
|
expected_balance
|
|
|
|
);
|
|
|
|
if let (Some(expected_balance), Ok(balance_result)) = (expected_balance, balance_result) {
|
|
|
|
if expected_balance == balance_result {
|
|
|
|
return Some(balance_result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-02-28 14:16:50 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::bank::Bank;
|
|
|
|
use crate::cluster_info::Node;
|
|
|
|
use crate::fullnode::Fullnode;
|
|
|
|
use crate::leader_scheduler::LeaderScheduler;
|
|
|
|
use crate::ledger::create_tmp_ledger_with_mint;
|
2018-12-14 12:36:50 -08:00
|
|
|
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::mint::Mint;
|
2018-11-05 10:50:58 -07:00
|
|
|
use bincode::deserialize;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::system_instruction::SystemInstruction;
|
2018-12-04 07:45:32 -08:00
|
|
|
use solana_sdk::vote_program::VoteProgram;
|
2018-12-04 15:05:41 -08:00
|
|
|
use solana_sdk::vote_transaction::VoteTransaction;
|
2018-12-04 20:19:48 -08:00
|
|
|
use std::fs::remove_dir_all;
|
2018-02-28 14:16:50 -07:00
|
|
|
|
|
|
|
#[test]
|
2018-05-08 17:32:50 -06:00
|
|
|
fn test_thin_client() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_keypair = Arc::new(Keypair::new());
|
2018-08-28 16:32:40 -07:00
|
|
|
let leader = Node::new_localhost_with_pubkey(leader_keypair.pubkey());
|
2018-08-31 00:10:39 -07:00
|
|
|
let leader_data = leader.info.clone();
|
2018-04-28 00:31:20 -07:00
|
|
|
|
2018-03-07 17:08:12 -07:00
|
|
|
let alice = Mint::new(10_000);
|
2018-10-25 16:58:40 -07:00
|
|
|
let mut bank = Bank::new(&alice);
|
2018-08-09 08:56:04 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2018-10-17 13:42:54 -07:00
|
|
|
let ledger_path = create_tmp_ledger_with_mint("thin_client", &alice);
|
2018-05-15 10:05:20 -06:00
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::from_bootstrap_leader(
|
|
|
|
leader_data.id,
|
|
|
|
)));
|
|
|
|
bank.leader_scheduler = leader_scheduler;
|
|
|
|
let vote_account_keypair = Arc::new(Keypair::new());
|
2018-10-30 10:05:18 -07:00
|
|
|
let last_id = bank.last_id();
|
2018-08-22 17:37:57 -06:00
|
|
|
let server = Fullnode::new_with_bank(
|
2018-07-31 22:07:53 -07:00
|
|
|
leader_keypair,
|
2018-10-25 16:58:40 -07:00
|
|
|
vote_account_keypair,
|
2018-05-15 10:27:18 -06:00
|
|
|
bank,
|
2018-06-27 12:35:58 -07:00
|
|
|
0,
|
2018-10-30 10:05:18 -07:00
|
|
|
&last_id,
|
2018-06-28 14:51:53 -07:00
|
|
|
leader,
|
2018-08-22 17:37:57 -06:00
|
|
|
None,
|
2018-09-14 01:53:18 -07:00
|
|
|
&ledger_path,
|
2018-07-31 16:54:24 -07:00
|
|
|
false,
|
2018-11-05 10:50:58 -07:00
|
|
|
None,
|
2018-05-15 10:09:54 -06:00
|
|
|
);
|
2018-05-13 20:33:41 -07:00
|
|
|
sleep(Duration::from_millis(900));
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-05-25 15:51:41 -06:00
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-03-27 14:45:04 -06:00
|
|
|
|
2018-11-15 13:23:26 -08:00
|
|
|
let mut client = ThinClient::new(leader_data.rpc, leader_data.tpu, transactions_socket);
|
2018-11-05 10:50:58 -07:00
|
|
|
let transaction_count = client.transaction_count();
|
|
|
|
assert_eq!(transaction_count, 0);
|
|
|
|
let finality = client.get_finality();
|
|
|
|
assert_eq!(finality, 18446744073709551615);
|
2018-05-31 13:41:42 -06:00
|
|
|
let last_id = client.get_last_id();
|
2018-08-09 09:26:21 -06:00
|
|
|
let signature = client
|
2018-05-09 16:19:36 -06:00
|
|
|
.transfer(500, &alice.keypair(), bob_pubkey, &last_id)
|
2018-03-26 22:02:05 -06:00
|
|
|
.unwrap();
|
2018-08-09 09:26:21 -06:00
|
|
|
client.poll_for_signature(&signature).unwrap();
|
2018-07-20 19:05:39 -04:00
|
|
|
let balance = client.get_balance(&bob_pubkey);
|
2018-05-07 16:49:15 -07:00
|
|
|
assert_eq!(balance.unwrap(), 500);
|
2018-11-05 10:50:58 -07:00
|
|
|
let transaction_count = client.transaction_count();
|
|
|
|
assert_eq!(transaction_count, 1);
|
2018-09-10 23:38:40 -07:00
|
|
|
server.close().unwrap();
|
2018-08-06 12:35:38 -07:00
|
|
|
remove_dir_all(ledger_path).unwrap();
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
2018-05-04 11:11:39 -07:00
|
|
|
|
2018-07-05 12:01:40 -07:00
|
|
|
// sleep(Duration::from_millis(300)); is unstable
|
2018-05-09 13:33:33 -06:00
|
|
|
#[test]
|
2018-07-05 12:01:40 -07:00
|
|
|
#[ignore]
|
2018-05-09 13:33:33 -06:00
|
|
|
fn test_bad_sig() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_keypair = Arc::new(Keypair::new());
|
2018-08-28 16:32:40 -07:00
|
|
|
let leader = Node::new_localhost_with_pubkey(leader_keypair.pubkey());
|
2018-05-09 13:33:33 -06:00
|
|
|
let alice = Mint::new(10_000);
|
2018-10-25 16:58:40 -07:00
|
|
|
let mut bank = Bank::new(&alice);
|
2018-08-09 08:56:04 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2018-08-31 00:10:39 -07:00
|
|
|
let leader_data = leader.info.clone();
|
2018-10-17 13:42:54 -07:00
|
|
|
let ledger_path = create_tmp_ledger_with_mint("bad_sig", &alice);
|
2018-05-15 10:05:20 -06:00
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::from_bootstrap_leader(
|
|
|
|
leader_data.id,
|
|
|
|
)));
|
|
|
|
bank.leader_scheduler = leader_scheduler;
|
|
|
|
let vote_account_keypair = Arc::new(Keypair::new());
|
2018-10-30 10:05:18 -07:00
|
|
|
let last_id = bank.last_id();
|
2018-08-22 17:37:57 -06:00
|
|
|
let server = Fullnode::new_with_bank(
|
2018-07-31 22:07:53 -07:00
|
|
|
leader_keypair,
|
2018-10-25 16:58:40 -07:00
|
|
|
vote_account_keypair,
|
2018-05-15 10:27:18 -06:00
|
|
|
bank,
|
2018-06-27 12:35:58 -07:00
|
|
|
0,
|
2018-10-30 10:05:18 -07:00
|
|
|
&last_id,
|
2018-06-28 14:51:53 -07:00
|
|
|
leader,
|
2018-08-22 17:37:57 -06:00
|
|
|
None,
|
2018-09-14 01:53:18 -07:00
|
|
|
&ledger_path,
|
2018-07-31 16:54:24 -07:00
|
|
|
false,
|
2018-11-05 10:50:58 -07:00
|
|
|
None,
|
2018-05-15 10:09:54 -06:00
|
|
|
);
|
2018-07-05 12:01:40 -07:00
|
|
|
//TODO: remove this sleep, or add a retry so CI is stable
|
2018-05-09 13:33:33 -06:00
|
|
|
sleep(Duration::from_millis(300));
|
|
|
|
|
2018-05-25 15:51:41 -06:00
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-11-15 13:23:26 -08:00
|
|
|
let mut client = ThinClient::new(leader_data.rpc, leader_data.tpu, transactions_socket);
|
2018-05-31 13:41:42 -06:00
|
|
|
let last_id = client.get_last_id();
|
2018-05-09 13:33:33 -06:00
|
|
|
|
2018-09-26 09:51:51 -06:00
|
|
|
let tx = Transaction::system_new(&alice.keypair(), bob_pubkey, 500, last_id);
|
2018-05-09 13:33:33 -06:00
|
|
|
|
2018-07-11 14:40:46 -06:00
|
|
|
let _sig = client.transfer_signed(&tx).unwrap();
|
2018-05-09 13:33:33 -06:00
|
|
|
|
2018-05-31 13:41:42 -06:00
|
|
|
let last_id = client.get_last_id();
|
2018-05-09 13:33:33 -06:00
|
|
|
|
2018-09-26 09:51:51 -06:00
|
|
|
let mut tr2 = Transaction::system_new(&alice.keypair(), bob_pubkey, 501, last_id);
|
2018-09-28 16:16:35 -07:00
|
|
|
let mut instruction2 = deserialize(tr2.userdata(0)).unwrap();
|
2018-11-16 08:04:46 -08:00
|
|
|
if let SystemInstruction::Move { ref mut tokens } = instruction2 {
|
2018-09-17 13:36:31 -07:00
|
|
|
*tokens = 502;
|
2018-05-22 21:42:04 -06:00
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
tr2.instructions[0].userdata = serialize(&instruction2).unwrap();
|
2018-08-09 09:26:21 -06:00
|
|
|
let signature = client.transfer_signed(&tr2).unwrap();
|
|
|
|
client.poll_for_signature(&signature).unwrap();
|
2018-05-09 13:33:33 -06:00
|
|
|
|
2018-07-20 19:05:39 -04:00
|
|
|
let balance = client.get_balance(&bob_pubkey);
|
2018-05-22 09:46:52 -07:00
|
|
|
assert_eq!(balance.unwrap(), 500);
|
2018-09-10 23:38:40 -07:00
|
|
|
server.close().unwrap();
|
2018-08-03 11:06:06 -07:00
|
|
|
remove_dir_all(ledger_path).unwrap();
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
2018-06-28 12:58:33 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_client_check_signature() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_keypair = Arc::new(Keypair::new());
|
2018-08-28 16:32:40 -07:00
|
|
|
let leader = Node::new_localhost_with_pubkey(leader_keypair.pubkey());
|
2018-06-28 12:58:33 -06:00
|
|
|
let alice = Mint::new(10_000);
|
2018-10-25 16:58:40 -07:00
|
|
|
let mut bank = Bank::new(&alice);
|
2018-08-09 08:56:04 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2018-08-31 00:10:39 -07:00
|
|
|
let leader_data = leader.info.clone();
|
2018-10-17 13:42:54 -07:00
|
|
|
let ledger_path = create_tmp_ledger_with_mint("client_check_signature", &alice);
|
2018-08-03 11:06:06 -07:00
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::from_bootstrap_leader(
|
|
|
|
leader_data.id,
|
|
|
|
)));
|
|
|
|
bank.leader_scheduler = leader_scheduler;
|
|
|
|
let vote_account_keypair = Arc::new(Keypair::new());
|
2018-10-30 10:05:18 -07:00
|
|
|
let entry_height = alice.create_entries().len() as u64;
|
|
|
|
let last_id = bank.last_id();
|
2018-08-22 17:37:57 -06:00
|
|
|
let server = Fullnode::new_with_bank(
|
2018-07-31 22:07:53 -07:00
|
|
|
leader_keypair,
|
2018-10-25 16:58:40 -07:00
|
|
|
vote_account_keypair,
|
2018-06-28 12:58:33 -06:00
|
|
|
bank,
|
2018-10-12 00:39:10 -07:00
|
|
|
entry_height,
|
2018-10-30 10:05:18 -07:00
|
|
|
&last_id,
|
2018-06-28 14:51:53 -07:00
|
|
|
leader,
|
2018-08-22 17:37:57 -06:00
|
|
|
None,
|
2018-09-14 01:53:18 -07:00
|
|
|
&ledger_path,
|
2018-07-31 16:54:24 -07:00
|
|
|
false,
|
2018-11-05 10:50:58 -07:00
|
|
|
None,
|
2018-06-28 12:58:33 -06:00
|
|
|
);
|
|
|
|
sleep(Duration::from_millis(300));
|
|
|
|
|
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-11-15 13:23:26 -08:00
|
|
|
let mut client = ThinClient::new(leader_data.rpc, leader_data.tpu, transactions_socket);
|
2018-06-28 12:58:33 -06:00
|
|
|
let last_id = client.get_last_id();
|
2018-08-09 09:26:21 -06:00
|
|
|
let signature = client
|
2018-06-28 12:58:33 -06:00
|
|
|
.transfer(500, &alice.keypair(), bob_pubkey, &last_id)
|
|
|
|
.unwrap();
|
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
assert!(client.poll_for_signature(&signature).is_ok());
|
2018-06-28 12:58:33 -06:00
|
|
|
|
2018-09-10 23:38:40 -07:00
|
|
|
server.close().unwrap();
|
2018-08-03 11:06:06 -07:00
|
|
|
remove_dir_all(ledger_path).unwrap();
|
2018-06-28 12:58:33 -06:00
|
|
|
}
|
2018-08-23 20:57:13 -07:00
|
|
|
|
2018-10-31 17:47:50 -07:00
|
|
|
#[test]
|
|
|
|
fn test_register_vote_account() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-31 17:47:50 -07:00
|
|
|
let leader_keypair = Arc::new(Keypair::new());
|
|
|
|
let leader = Node::new_localhost_with_pubkey(leader_keypair.pubkey());
|
|
|
|
let mint = Mint::new(10_000);
|
|
|
|
let mut bank = Bank::new(&mint);
|
|
|
|
let leader_data = leader.info.clone();
|
|
|
|
let ledger_path = create_tmp_ledger_with_mint("client_check_signature", &mint);
|
|
|
|
|
|
|
|
let genesis_entries = &mint.create_entries();
|
|
|
|
let entry_height = genesis_entries.len() as u64;
|
|
|
|
|
|
|
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::from_bootstrap_leader(
|
|
|
|
leader_data.id,
|
|
|
|
)));
|
|
|
|
bank.leader_scheduler = leader_scheduler;
|
|
|
|
let leader_vote_account_keypair = Arc::new(Keypair::new());
|
|
|
|
let server = Fullnode::new_with_bank(
|
|
|
|
leader_keypair,
|
|
|
|
leader_vote_account_keypair.clone(),
|
|
|
|
bank,
|
|
|
|
entry_height,
|
2018-10-31 19:55:58 -07:00
|
|
|
&genesis_entries.last().unwrap().id,
|
2018-10-31 17:47:50 -07:00
|
|
|
leader,
|
|
|
|
None,
|
|
|
|
&ledger_path,
|
|
|
|
false,
|
2018-11-05 10:50:58 -07:00
|
|
|
None,
|
2018-10-31 17:47:50 -07:00
|
|
|
);
|
|
|
|
sleep(Duration::from_millis(300));
|
|
|
|
|
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-11-15 13:23:26 -08:00
|
|
|
let mut client = ThinClient::new(leader_data.rpc, leader_data.tpu, transactions_socket);
|
2018-10-31 17:47:50 -07:00
|
|
|
|
|
|
|
// Create the validator account, transfer some tokens to that account
|
|
|
|
let validator_keypair = Keypair::new();
|
|
|
|
let last_id = client.get_last_id();
|
|
|
|
let signature = client
|
|
|
|
.transfer(500, &mint.keypair(), validator_keypair.pubkey(), &last_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(client.poll_for_signature(&signature).is_ok());
|
|
|
|
|
2018-11-28 20:10:30 -08:00
|
|
|
// Create and register the vote account
|
2018-10-31 17:47:50 -07:00
|
|
|
let validator_vote_account_keypair = Keypair::new();
|
|
|
|
let vote_account_id = validator_vote_account_keypair.pubkey();
|
|
|
|
let last_id = client.get_last_id();
|
|
|
|
|
2018-11-15 17:05:31 -08:00
|
|
|
let transaction =
|
2018-11-28 20:10:30 -08:00
|
|
|
VoteTransaction::vote_account_new(&validator_keypair, vote_account_id, last_id, 1, 1);
|
2018-11-15 17:05:31 -08:00
|
|
|
let signature = client.transfer_signed(&transaction).unwrap();
|
2018-10-31 17:47:50 -07:00
|
|
|
assert!(client.poll_for_signature(&signature).is_ok());
|
2018-11-15 17:05:31 -08:00
|
|
|
|
2018-10-31 17:47:50 -07:00
|
|
|
let balance = retry_get_balance(&mut client, &vote_account_id, Some(1))
|
|
|
|
.expect("Expected balance for new account to exist");
|
|
|
|
assert_eq!(balance, 1);
|
|
|
|
|
|
|
|
const LAST: usize = 30;
|
|
|
|
for run in 0..=LAST {
|
|
|
|
println!("Checking for account registered: {}", run);
|
|
|
|
let account_user_data = client
|
|
|
|
.get_account_userdata(&vote_account_id)
|
|
|
|
.expect("Expected valid response for account userdata")
|
|
|
|
.expect("Expected valid account userdata to exist after account creation");
|
|
|
|
|
|
|
|
let vote_state = VoteProgram::deserialize(&account_user_data);
|
|
|
|
|
|
|
|
if vote_state.map(|vote_state| vote_state.node_id) == Ok(validator_keypair.pubkey()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if run == LAST {
|
|
|
|
panic!("Expected successful vote account registration");
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(900));
|
|
|
|
}
|
|
|
|
|
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:57:13 -07:00
|
|
|
#[test]
|
|
|
|
fn test_transaction_count() {
|
|
|
|
// set a bogus address, see that we don't hang
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-08-23 20:57:13 -07:00
|
|
|
let addr = "0.0.0.0:1234".parse().unwrap();
|
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-11-29 13:28:52 -08:00
|
|
|
let mut client =
|
|
|
|
ThinClient::new_with_timeout(addr, addr, transactions_socket, Duration::from_secs(2));
|
2018-08-23 20:57:13 -07:00
|
|
|
assert_eq!(client.transaction_count(), 0);
|
|
|
|
}
|
2018-09-04 21:33:19 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zero_balance_after_nonzero() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_keypair = Arc::new(Keypair::new());
|
2018-09-04 21:33:19 -07:00
|
|
|
let leader = Node::new_localhost_with_pubkey(leader_keypair.pubkey());
|
|
|
|
let alice = Mint::new(10_000);
|
2018-10-25 16:58:40 -07:00
|
|
|
let mut bank = Bank::new(&alice);
|
2018-09-04 21:33:19 -07:00
|
|
|
let bob_keypair = Keypair::new();
|
|
|
|
let leader_data = leader.info.clone();
|
2018-10-17 13:42:54 -07:00
|
|
|
let ledger_path = create_tmp_ledger_with_mint("zero_balance_check", &alice);
|
2018-09-04 21:33:19 -07:00
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::from_bootstrap_leader(
|
|
|
|
leader_data.id,
|
|
|
|
)));
|
|
|
|
bank.leader_scheduler = leader_scheduler;
|
|
|
|
let vote_account_keypair = Arc::new(Keypair::new());
|
2018-10-30 10:05:18 -07:00
|
|
|
let last_id = bank.last_id();
|
|
|
|
let entry_height = alice.create_entries().len() as u64;
|
2018-09-04 21:33:19 -07:00
|
|
|
let server = Fullnode::new_with_bank(
|
|
|
|
leader_keypair,
|
2018-10-25 16:58:40 -07:00
|
|
|
vote_account_keypair,
|
2018-09-04 21:33:19 -07:00
|
|
|
bank,
|
2018-10-12 00:39:10 -07:00
|
|
|
entry_height,
|
2018-10-30 10:05:18 -07:00
|
|
|
&last_id,
|
2018-09-04 21:33:19 -07:00
|
|
|
leader,
|
|
|
|
None,
|
2018-09-14 01:53:18 -07:00
|
|
|
&ledger_path,
|
2018-09-04 21:33:19 -07:00
|
|
|
false,
|
2018-11-05 10:50:58 -07:00
|
|
|
None,
|
2018-09-04 21:33:19 -07:00
|
|
|
);
|
|
|
|
sleep(Duration::from_millis(900));
|
|
|
|
|
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-11-15 13:23:26 -08:00
|
|
|
let mut client = ThinClient::new(leader_data.rpc, leader_data.tpu, transactions_socket);
|
2018-09-04 21:33:19 -07:00
|
|
|
let last_id = client.get_last_id();
|
|
|
|
|
|
|
|
// give bob 500 tokens
|
|
|
|
let signature = client
|
|
|
|
.transfer(500, &alice.keypair(), bob_keypair.pubkey(), &last_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(client.poll_for_signature(&signature).is_ok());
|
|
|
|
|
|
|
|
let balance = client.poll_get_balance(&bob_keypair.pubkey());
|
|
|
|
assert!(balance.is_ok());
|
|
|
|
assert_eq!(balance.unwrap(), 500);
|
|
|
|
|
|
|
|
// take them away
|
|
|
|
let signature = client
|
|
|
|
.transfer(500, &bob_keypair, alice.keypair().pubkey(), &last_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(client.poll_for_signature(&signature).is_ok());
|
|
|
|
|
|
|
|
// should get an error when bob's account is purged
|
|
|
|
let balance = client.poll_get_balance(&bob_keypair.pubkey());
|
|
|
|
assert!(balance.is_err());
|
|
|
|
|
2018-09-21 21:01:13 -07:00
|
|
|
server
|
|
|
|
.close()
|
|
|
|
.unwrap_or_else(|e| panic!("close() failed! {:?}", e));
|
2018-09-04 21:33:19 -07:00
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
2018-05-04 11:11:39 -07:00
|
|
|
}
|