2018-05-25 20:54:20 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate bincode;
|
2018-07-23 09:27:06 -07:00
|
|
|
extern crate rayon;
|
2018-06-28 14:51:53 -07:00
|
|
|
extern crate serde_json;
|
2018-05-25 20:54:20 -07:00
|
|
|
extern crate solana;
|
|
|
|
|
2018-07-23 09:27:06 -07:00
|
|
|
use rayon::prelude::*;
|
2018-05-27 18:21:39 -07:00
|
|
|
use solana::crdt::TestNode;
|
2018-07-11 00:18:48 -07:00
|
|
|
use solana::crdt::{Crdt, NodeInfo};
|
2018-06-28 14:51:53 -07:00
|
|
|
use solana::entry_writer::EntryWriter;
|
2018-07-12 16:09:58 -07:00
|
|
|
use solana::fullnode::{FullNode, LedgerFile};
|
2018-05-25 20:54:20 -07:00
|
|
|
use solana::logger;
|
|
|
|
use solana::mint::Mint;
|
2018-06-07 15:06:32 -07:00
|
|
|
use solana::ncp::Ncp;
|
2018-07-17 11:45:52 -07:00
|
|
|
use solana::service::Service;
|
2018-05-25 20:54:20 -07:00
|
|
|
use solana::signature::{KeyPair, KeyPairUtil, PublicKey};
|
|
|
|
use solana::streamer::default_window;
|
|
|
|
use solana::thin_client::ThinClient;
|
2018-06-28 14:51:53 -07:00
|
|
|
use std::fs::File;
|
2018-05-25 20:54:20 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-07-09 13:53:18 -07:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2018-05-25 20:54:20 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2018-07-11 00:18:48 -07:00
|
|
|
fn converge(leader: &NodeInfo, num_nodes: usize) -> Vec<NodeInfo> {
|
2018-05-25 20:54:20 -07:00
|
|
|
//lets spy on the network
|
2018-06-28 14:51:53 -07:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-07-16 19:31:52 -07:00
|
|
|
let mut spy = TestNode::new_localhost();
|
2018-05-25 20:54:20 -07:00
|
|
|
let daddr = "0.0.0.0:0".parse().unwrap();
|
|
|
|
let me = spy.data.id.clone();
|
2018-07-09 17:55:11 -07:00
|
|
|
spy.data.contact_info.tvu = daddr;
|
|
|
|
spy.data.contact_info.rpu = daddr;
|
2018-07-16 19:31:52 -07:00
|
|
|
let mut spy_crdt = Crdt::new(spy.data).expect("Crdt::new");
|
2018-05-25 20:54:20 -07:00
|
|
|
spy_crdt.insert(&leader);
|
|
|
|
spy_crdt.set_leader(leader.id);
|
|
|
|
let spy_ref = Arc::new(RwLock::new(spy_crdt));
|
|
|
|
let spy_window = default_window();
|
2018-07-09 13:53:18 -07:00
|
|
|
let ncp = Ncp::new(
|
2018-07-13 12:32:20 -07:00
|
|
|
&spy_ref,
|
2018-05-25 20:54:20 -07:00
|
|
|
spy_window,
|
|
|
|
spy.sockets.gossip,
|
2018-05-27 18:21:39 -07:00
|
|
|
spy.sockets.gossip_send,
|
2018-06-28 14:51:53 -07:00
|
|
|
exit.clone(),
|
2018-05-27 18:21:39 -07:00
|
|
|
).unwrap();
|
2018-05-25 20:54:20 -07:00
|
|
|
//wait for the network to converge
|
|
|
|
let mut converged = false;
|
2018-06-28 14:51:53 -07:00
|
|
|
let mut rv = vec![];
|
2018-05-25 20:54:20 -07:00
|
|
|
for _ in 0..30 {
|
|
|
|
let num = spy_ref.read().unwrap().convergence();
|
2018-07-11 00:18:48 -07:00
|
|
|
let mut v: Vec<NodeInfo> = spy_ref
|
2018-06-28 14:51:53 -07:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.table
|
|
|
|
.values()
|
|
|
|
.into_iter()
|
|
|
|
.filter(|x| x.id != me)
|
2018-07-18 01:07:43 -07:00
|
|
|
.filter(|x| Crdt::is_valid_address(x.contact_info.rpu))
|
2018-06-28 14:51:53 -07:00
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
if num >= num_nodes as u64 && v.len() >= num_nodes {
|
|
|
|
rv.append(&mut v);
|
2018-05-25 20:54:20 -07:00
|
|
|
converged = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(Duration::new(1, 0));
|
|
|
|
}
|
|
|
|
assert!(converged);
|
2018-07-09 13:53:18 -07:00
|
|
|
ncp.close().unwrap();
|
2018-06-28 14:51:53 -07:00
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
|
|
|
fn genesis(num: i64) -> (Mint, String) {
|
|
|
|
let mint = Mint::new(num);
|
|
|
|
let id = {
|
|
|
|
let ids: Vec<_> = mint.pubkey().iter().map(|id| format!("{}", id)).collect();
|
|
|
|
ids.join("")
|
|
|
|
};
|
|
|
|
let path = format!("target/test_multi_node_dynamic_network-{}.log", id);
|
|
|
|
let mut writer = File::create(path.clone()).unwrap();
|
|
|
|
|
|
|
|
EntryWriter::write_entries(&mut writer, mint.create_entries()).unwrap();
|
|
|
|
(mint, path.to_string())
|
2018-05-25 20:54:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-06-25 15:24:10 -07:00
|
|
|
fn test_multi_node_validator_catchup_from_zero() {
|
2018-05-25 20:54:20 -07:00
|
|
|
logger::setup();
|
|
|
|
const N: usize = 5;
|
2018-06-25 15:24:10 -07:00
|
|
|
trace!("test_multi_node_validator_catchup_from_zero");
|
2018-07-16 19:31:52 -07:00
|
|
|
let leader = TestNode::new_localhost();
|
2018-06-28 14:51:53 -07:00
|
|
|
let leader_data = leader.data.clone();
|
2018-05-25 20:54:20 -07:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
|
2018-06-28 14:51:53 -07:00
|
|
|
let (alice, ledger_path) = genesis(10_000);
|
2018-07-12 16:09:58 -07:00
|
|
|
let server = FullNode::new(
|
|
|
|
leader,
|
|
|
|
true,
|
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
let mut nodes = vec![server];
|
2018-05-25 20:54:20 -07:00
|
|
|
for _ in 0..N {
|
2018-07-05 12:01:40 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-07-16 19:31:52 -07:00
|
|
|
let validator = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-06-28 14:51:53 -07:00
|
|
|
let mut val = FullNode::new(
|
|
|
|
validator,
|
|
|
|
false,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
2018-07-05 12:01:40 -07:00
|
|
|
Some(keypair),
|
2018-07-09 17:55:11 -07:00
|
|
|
Some(leader_data.contact_info.ncp),
|
2018-06-28 14:51:53 -07:00
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
nodes.push(val);
|
2018-05-25 20:54:20 -07:00
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
let servers = converge(&leader_data, N + 1);
|
2018-06-25 15:24:10 -07:00
|
|
|
//contains the leader addr as well
|
|
|
|
assert_eq!(servers.len(), N + 1);
|
|
|
|
//verify leader can do transfer
|
2018-06-28 14:51:53 -07:00
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, None).unwrap();
|
2018-06-25 15:24:10 -07:00
|
|
|
assert_eq!(leader_balance, 500);
|
|
|
|
//verify validator has the same balance
|
|
|
|
let mut success = 0usize;
|
|
|
|
for server in servers.iter() {
|
2018-07-03 15:09:35 -07:00
|
|
|
info!("0server: {:x}", server.debug_id());
|
2018-06-25 15:24:10 -07:00
|
|
|
let mut client = mk_client(server);
|
|
|
|
if let Ok(bal) = client.poll_get_balance(&bob_pubkey) {
|
|
|
|
info!("validator balance {}", bal);
|
|
|
|
if bal == leader_balance {
|
|
|
|
success += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(success, servers.len());
|
|
|
|
|
|
|
|
success = 0;
|
|
|
|
// start up another validator, converge and then check everyone's balances
|
2018-07-05 12:01:40 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-07-16 19:31:52 -07:00
|
|
|
let validator = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-07-03 22:21:53 -07:00
|
|
|
let val = FullNode::new(
|
2018-07-05 12:01:40 -07:00
|
|
|
validator,
|
2018-06-28 14:51:53 -07:00
|
|
|
false,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
2018-07-05 12:01:40 -07:00
|
|
|
Some(keypair),
|
2018-07-09 17:55:11 -07:00
|
|
|
Some(leader_data.contact_info.ncp),
|
2018-06-28 14:51:53 -07:00
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
nodes.push(val);
|
2018-06-28 14:51:53 -07:00
|
|
|
//contains the leader and new node
|
|
|
|
let servers = converge(&leader_data, N + 2);
|
2018-06-25 15:24:10 -07:00
|
|
|
|
2018-06-28 14:51:53 -07:00
|
|
|
let mut leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, None).unwrap();
|
2018-06-25 15:24:10 -07:00
|
|
|
info!("leader balance {}", leader_balance);
|
|
|
|
loop {
|
2018-06-28 14:51:53 -07:00
|
|
|
let mut client = mk_client(&leader_data);
|
2018-06-25 15:24:10 -07:00
|
|
|
leader_balance = client.poll_get_balance(&bob_pubkey).unwrap();
|
|
|
|
if leader_balance == 1000 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(300));
|
|
|
|
}
|
|
|
|
assert_eq!(leader_balance, 1000);
|
|
|
|
|
|
|
|
for server in servers.iter() {
|
2018-06-28 14:51:53 -07:00
|
|
|
let mut client = mk_client(server);
|
2018-07-03 15:09:35 -07:00
|
|
|
info!("1server: {:x}", server.debug_id());
|
2018-07-05 12:01:40 -07:00
|
|
|
for _ in 0..15 {
|
2018-06-28 14:51:53 -07:00
|
|
|
if let Ok(bal) = client.poll_get_balance(&bob_pubkey) {
|
|
|
|
info!("validator balance {}", bal);
|
|
|
|
if bal == leader_balance {
|
|
|
|
success += 1;
|
|
|
|
break;
|
2018-06-25 15:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
sleep(Duration::from_millis(500));
|
2018-06-25 15:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
assert_eq!(success, servers.len());
|
2018-06-25 15:24:10 -07:00
|
|
|
|
2018-07-09 13:53:18 -07:00
|
|
|
for node in nodes {
|
|
|
|
node.close().unwrap();
|
2018-06-25 15:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multi_node_basic() {
|
|
|
|
logger::setup();
|
|
|
|
const N: usize = 5;
|
|
|
|
trace!("test_multi_node_basic");
|
2018-07-16 19:31:52 -07:00
|
|
|
let leader = TestNode::new_localhost();
|
2018-06-28 14:51:53 -07:00
|
|
|
let leader_data = leader.data.clone();
|
2018-06-25 15:24:10 -07:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
2018-06-28 14:51:53 -07:00
|
|
|
let (alice, ledger_path) = genesis(10_000);
|
2018-07-12 16:09:58 -07:00
|
|
|
let server = FullNode::new(
|
|
|
|
leader,
|
|
|
|
true,
|
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
let mut nodes = vec![server];
|
2018-06-25 15:24:10 -07:00
|
|
|
for _ in 0..N {
|
2018-07-05 12:01:40 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-07-16 19:31:52 -07:00
|
|
|
let validator = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-07-10 11:38:06 -07:00
|
|
|
let val = FullNode::new(
|
2018-06-28 14:51:53 -07:00
|
|
|
validator,
|
|
|
|
false,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
2018-07-05 12:01:40 -07:00
|
|
|
Some(keypair),
|
2018-07-09 17:55:11 -07:00
|
|
|
Some(leader_data.contact_info.ncp),
|
2018-06-28 14:51:53 -07:00
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
nodes.push(val);
|
2018-06-25 15:24:10 -07:00
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
let servers = converge(&leader_data, N + 1);
|
2018-05-25 20:54:20 -07:00
|
|
|
//contains the leader addr as well
|
|
|
|
assert_eq!(servers.len(), N + 1);
|
|
|
|
//verify leader can do transfer
|
2018-06-28 14:51:53 -07:00
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, None).unwrap();
|
2018-05-25 20:54:20 -07:00
|
|
|
assert_eq!(leader_balance, 500);
|
|
|
|
//verify validator has the same balance
|
|
|
|
let mut success = 0usize;
|
|
|
|
for server in servers.iter() {
|
|
|
|
let mut client = mk_client(server);
|
|
|
|
if let Ok(bal) = client.poll_get_balance(&bob_pubkey) {
|
|
|
|
trace!("validator balance {}", bal);
|
|
|
|
if bal == leader_balance {
|
|
|
|
success += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(success, servers.len());
|
2018-06-25 15:24:10 -07:00
|
|
|
|
2018-07-09 13:53:18 -07:00
|
|
|
for node in nodes {
|
|
|
|
node.close().unwrap();
|
2018-05-25 20:54:20 -07:00
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
std::fs::remove_file(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-07-03 10:35:41 -07:00
|
|
|
#[test]
|
|
|
|
fn test_boot_validator_from_file() {
|
|
|
|
logger::setup();
|
2018-07-16 19:31:52 -07:00
|
|
|
let leader = TestNode::new_localhost();
|
2018-07-03 10:35:41 -07:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let (alice, ledger_path) = genesis(100_000);
|
|
|
|
let leader_data = leader.data.clone();
|
2018-07-12 16:09:58 -07:00
|
|
|
let leader_fullnode = FullNode::new(
|
|
|
|
leader,
|
|
|
|
true,
|
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
);
|
2018-07-03 10:35:41 -07:00
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(500)).unwrap();
|
|
|
|
assert_eq!(leader_balance, 500);
|
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(1000)).unwrap();
|
|
|
|
assert_eq!(leader_balance, 1000);
|
|
|
|
|
2018-07-05 12:01:40 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-07-16 19:31:52 -07:00
|
|
|
let validator = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-07-03 10:35:41 -07:00
|
|
|
let validator_data = validator.data.clone();
|
|
|
|
let val_fullnode = FullNode::new(
|
|
|
|
validator,
|
|
|
|
false,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
2018-07-05 12:01:40 -07:00
|
|
|
Some(keypair),
|
2018-07-09 17:55:11 -07:00
|
|
|
Some(leader_data.contact_info.ncp),
|
2018-07-03 10:35:41 -07:00
|
|
|
);
|
|
|
|
let mut client = mk_client(&validator_data);
|
|
|
|
let getbal = retry_get_balance(&mut client, &bob_pubkey, Some(leader_balance));
|
|
|
|
assert!(getbal == Some(leader_balance));
|
|
|
|
|
2018-07-09 13:53:18 -07:00
|
|
|
leader_fullnode.close().unwrap();
|
|
|
|
val_fullnode.close().unwrap();
|
2018-07-03 10:35:41 -07:00
|
|
|
std::fs::remove_file(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-07-11 00:18:48 -07:00
|
|
|
fn create_leader(ledger_path: &str) -> (NodeInfo, FullNode) {
|
2018-07-16 19:31:52 -07:00
|
|
|
let leader = TestNode::new_localhost();
|
2018-07-09 11:36:39 -07:00
|
|
|
let leader_data = leader.data.clone();
|
|
|
|
let leader_fullnode = FullNode::new(
|
|
|
|
leader,
|
|
|
|
true,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(ledger_path.to_string()),
|
2018-07-09 11:36:39 -07:00
|
|
|
None,
|
2018-07-05 12:01:40 -07:00
|
|
|
None,
|
2018-07-09 11:36:39 -07:00
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
(leader_data, leader_fullnode)
|
2018-07-09 11:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leader_restart_validator_start_from_old_ledger() {
|
2018-07-09 13:58:55 -07:00
|
|
|
// this test verifies that a freshly started leader makes his ledger available
|
|
|
|
// in the repair window to validators that are started with an older
|
|
|
|
// ledger (currently up to WINDOW_SIZE entries)
|
2018-07-09 11:36:39 -07:00
|
|
|
logger::setup();
|
|
|
|
|
|
|
|
let (alice, ledger_path) = genesis(100_000);
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
|
2018-07-09 15:24:34 -07:00
|
|
|
let (leader_data, leader_fullnode) = create_leader(&ledger_path);
|
2018-07-09 11:36:39 -07:00
|
|
|
|
|
|
|
// lengthen the ledger
|
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(500)).unwrap();
|
|
|
|
assert_eq!(leader_balance, 500);
|
|
|
|
|
|
|
|
// create a "stale" ledger by copying current ledger
|
|
|
|
let mut stale_ledger_path = ledger_path.clone();
|
|
|
|
stale_ledger_path.insert_str(ledger_path.rfind("/").unwrap() + 1, "stale_");
|
|
|
|
|
2018-07-09 13:58:55 -07:00
|
|
|
std::fs::copy(&ledger_path, &stale_ledger_path)
|
2018-07-09 11:36:39 -07:00
|
|
|
.expect(format!("copy {} to {}", &ledger_path, &stale_ledger_path,).as_str());
|
|
|
|
|
|
|
|
// restart the leader
|
2018-07-09 15:24:34 -07:00
|
|
|
leader_fullnode.close().unwrap();
|
|
|
|
let (leader_data, leader_fullnode) = create_leader(&ledger_path);
|
2018-07-09 11:36:39 -07:00
|
|
|
|
|
|
|
// lengthen the ledger
|
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(1000)).unwrap();
|
|
|
|
assert_eq!(leader_balance, 1000);
|
|
|
|
|
|
|
|
// restart the leader
|
2018-07-09 15:24:34 -07:00
|
|
|
leader_fullnode.close().unwrap();
|
|
|
|
let (leader_data, leader_fullnode) = create_leader(&ledger_path);
|
2018-07-09 11:36:39 -07:00
|
|
|
|
|
|
|
// start validator from old ledger
|
2018-07-05 12:01:40 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-07-16 19:31:52 -07:00
|
|
|
let validator = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-07-09 11:36:39 -07:00
|
|
|
let validator_data = validator.data.clone();
|
|
|
|
let val_fullnode = FullNode::new(
|
|
|
|
validator,
|
|
|
|
false,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(stale_ledger_path.clone()),
|
2018-07-05 12:01:40 -07:00
|
|
|
Some(keypair),
|
2018-07-09 17:55:11 -07:00
|
|
|
Some(leader_data.contact_info.ncp),
|
2018-07-09 11:36:39 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// trigger broadcast, validator should catch up from leader, whose window contains
|
|
|
|
// the entries missing from the stale ledger
|
2018-07-09 17:35:23 -07:00
|
|
|
// send requests so the validator eventually sees a gap and requests a repair
|
|
|
|
let mut expected = 1500;
|
2018-07-09 11:36:39 -07:00
|
|
|
let mut client = mk_client(&validator_data);
|
2018-07-09 17:35:23 -07:00
|
|
|
for _ in 0..10 {
|
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(expected))
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(leader_balance, expected);
|
|
|
|
let getbal = retry_get_balance(&mut client, &bob_pubkey, Some(leader_balance));
|
|
|
|
if getbal == Some(leader_balance) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expected += 500;
|
|
|
|
}
|
|
|
|
let getbal = retry_get_balance(&mut client, &bob_pubkey, Some(expected));
|
|
|
|
assert_eq!(getbal, Some(expected));
|
2018-07-09 11:36:39 -07:00
|
|
|
|
2018-07-09 13:53:18 -07:00
|
|
|
leader_fullnode.close().unwrap();
|
|
|
|
val_fullnode.close().unwrap();
|
2018-07-09 11:36:39 -07:00
|
|
|
std::fs::remove_file(ledger_path).unwrap();
|
|
|
|
std::fs::remove_file(stale_ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-07-09 13:53:18 -07:00
|
|
|
//TODO: this test will run a long time so it's disabled for CI
|
2018-06-28 14:51:53 -07:00
|
|
|
#[test]
|
2018-07-03 16:40:45 -07:00
|
|
|
#[ignore]
|
2018-06-28 14:51:53 -07:00
|
|
|
fn test_multi_node_dynamic_network() {
|
|
|
|
logger::setup();
|
2018-07-05 12:01:40 -07:00
|
|
|
const N: usize = 60;
|
2018-07-16 19:31:52 -07:00
|
|
|
let leader = TestNode::new_localhost();
|
2018-06-28 14:51:53 -07:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let (alice, ledger_path) = genesis(100_000);
|
|
|
|
let leader_data = leader.data.clone();
|
2018-07-12 16:09:58 -07:00
|
|
|
let server = FullNode::new(
|
|
|
|
leader,
|
|
|
|
true,
|
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
);
|
2018-07-02 10:07:32 -07:00
|
|
|
info!("{:x} LEADER", leader_data.debug_id());
|
2018-06-28 14:51:53 -07:00
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(500)).unwrap();
|
|
|
|
assert_eq!(leader_balance, 500);
|
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(1000)).unwrap();
|
|
|
|
assert_eq!(leader_balance, 1000);
|
|
|
|
|
2018-07-23 09:27:06 -07:00
|
|
|
let keypairs: Vec<(KeyPair)> = (0..N)
|
|
|
|
.into_par_iter()
|
2018-07-05 12:01:40 -07:00
|
|
|
.map(|n| {
|
|
|
|
let keypair = KeyPair::new();
|
|
|
|
//send some tokens to the new validator
|
|
|
|
let bal =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &keypair.pubkey(), Some(500));
|
|
|
|
assert_eq!(bal, Some(500));
|
2018-07-23 09:27:06 -07:00
|
|
|
info!("sent balance to[{}/{}] {:x}", n, N, keypair.pubkey());
|
|
|
|
keypair
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let validators: Vec<(NodeInfo, FullNode)> = keypairs
|
|
|
|
.into_par_iter()
|
|
|
|
.map(|keypair| {
|
|
|
|
let validator = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
|
|
|
let rd = validator.data.clone();
|
|
|
|
info!("starting {:8x} {:x}", keypair.pubkey(), rd.debug_id());
|
2018-06-28 14:51:53 -07:00
|
|
|
let val = FullNode::new(
|
|
|
|
validator,
|
|
|
|
false,
|
2018-07-12 16:09:58 -07:00
|
|
|
LedgerFile::Path(ledger_path.clone()),
|
2018-07-05 12:01:40 -07:00
|
|
|
Some(keypair),
|
2018-07-09 17:55:11 -07:00
|
|
|
Some(leader_data.contact_info.ncp),
|
2018-06-28 14:51:53 -07:00
|
|
|
);
|
2018-07-09 13:53:18 -07:00
|
|
|
(rd, val)
|
2018-06-28 14:51:53 -07:00
|
|
|
})
|
|
|
|
.collect();
|
2018-07-09 13:53:18 -07:00
|
|
|
|
2018-07-16 19:31:52 -07:00
|
|
|
let mut consecutive_success = 0;
|
2018-06-28 14:51:53 -07:00
|
|
|
for i in 0..N {
|
|
|
|
//verify leader can do transfer
|
|
|
|
let expected = ((i + 3) * 500) as i64;
|
|
|
|
let leader_balance =
|
|
|
|
send_tx_and_retry_get_balance(&leader_data, &alice, &bob_pubkey, Some(expected))
|
|
|
|
.unwrap();
|
2018-07-05 12:01:40 -07:00
|
|
|
if leader_balance != expected {
|
|
|
|
info!(
|
|
|
|
"leader dropped transaction {} {:?} {:?}",
|
|
|
|
i, leader_balance, expected
|
|
|
|
);
|
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
//verify all validators have the same balance
|
2018-07-05 12:01:40 -07:00
|
|
|
{
|
2018-07-03 15:09:35 -07:00
|
|
|
let mut success = 0usize;
|
|
|
|
let mut distance = 0i64;
|
|
|
|
for server in validators.iter() {
|
|
|
|
let mut client = mk_client(&server.0);
|
2018-07-05 12:01:40 -07:00
|
|
|
trace!("{:x} {} get_balance start", server.0.debug_id(), i);
|
2018-07-10 12:06:33 -07:00
|
|
|
let getbal = retry_get_balance(&mut client, &bob_pubkey, Some(leader_balance));
|
2018-07-03 15:09:35 -07:00
|
|
|
trace!(
|
2018-07-10 12:06:33 -07:00
|
|
|
"{:x} {} get_balance: {:?} leader_balance: {}",
|
2018-07-03 15:09:35 -07:00
|
|
|
server.0.debug_id(),
|
|
|
|
i,
|
|
|
|
getbal,
|
2018-07-10 12:06:33 -07:00
|
|
|
leader_balance
|
2018-07-03 15:09:35 -07:00
|
|
|
);
|
|
|
|
let bal = getbal.unwrap_or(0);
|
2018-07-05 12:01:40 -07:00
|
|
|
distance += (leader_balance - bal) / 500;
|
2018-07-03 15:09:35 -07:00
|
|
|
if let Some(bal) = getbal {
|
|
|
|
if bal == leader_balance {
|
|
|
|
success += 1;
|
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 15:09:35 -07:00
|
|
|
info!(
|
2018-07-05 12:01:40 -07:00
|
|
|
"SUCCESS[{}] {} out of {} distance: {}",
|
|
|
|
i,
|
2018-07-03 15:09:35 -07:00
|
|
|
success,
|
|
|
|
validators.len(),
|
|
|
|
distance
|
|
|
|
);
|
2018-07-16 19:31:52 -07:00
|
|
|
if success == validators.len() && distance == 0 {
|
|
|
|
consecutive_success += 1;
|
|
|
|
} else {
|
|
|
|
consecutive_success = 0;
|
|
|
|
}
|
|
|
|
if consecutive_success == 10 {
|
|
|
|
break;
|
|
|
|
}
|
2018-06-28 14:51:53 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-16 19:31:52 -07:00
|
|
|
assert_eq!(consecutive_success, 10);
|
2018-07-16 22:25:36 -07:00
|
|
|
for (_, node) in &validators {
|
2018-07-17 08:18:42 -07:00
|
|
|
node.exit();
|
2018-07-16 22:22:29 -07:00
|
|
|
}
|
2018-07-17 08:18:42 -07:00
|
|
|
server.exit();
|
2018-07-09 13:53:18 -07:00
|
|
|
for (_, node) in validators {
|
2018-07-17 11:45:52 -07:00
|
|
|
node.join().unwrap();
|
2018-06-28 14:51:53 -07:00
|
|
|
}
|
2018-07-17 11:45:52 -07:00
|
|
|
server.join().unwrap();
|
2018-07-09 13:53:18 -07:00
|
|
|
|
2018-06-28 14:51:53 -07:00
|
|
|
std::fs::remove_file(ledger_path).unwrap();
|
2018-05-25 20:54:20 -07:00
|
|
|
}
|
|
|
|
|
2018-07-11 00:18:48 -07:00
|
|
|
fn mk_client(leader: &NodeInfo) -> ThinClient {
|
2018-05-25 20:54:20 -07:00
|
|
|
let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
|
|
|
requests_socket
|
|
|
|
.set_read_timeout(Some(Duration::new(1, 0)))
|
|
|
|
.unwrap();
|
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2018-07-18 01:07:43 -07:00
|
|
|
assert!(Crdt::is_valid_address(leader.contact_info.rpu));
|
|
|
|
assert!(Crdt::is_valid_address(leader.contact_info.tpu));
|
2018-05-25 20:54:20 -07:00
|
|
|
ThinClient::new(
|
2018-07-09 17:55:11 -07:00
|
|
|
leader.contact_info.rpu,
|
2018-05-25 20:54:20 -07:00
|
|
|
requests_socket,
|
2018-07-09 17:55:11 -07:00
|
|
|
leader.contact_info.tpu,
|
2018-05-25 20:54:20 -07:00
|
|
|
transactions_socket,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-06-28 14:51:53 -07:00
|
|
|
fn retry_get_balance(
|
|
|
|
client: &mut ThinClient,
|
|
|
|
bob_pubkey: &PublicKey,
|
|
|
|
expected: Option<i64>,
|
|
|
|
) -> Option<i64> {
|
2018-07-09 17:35:23 -07:00
|
|
|
const LAST: usize = 20;
|
2018-07-03 15:09:35 -07:00
|
|
|
for run in 0..(LAST + 1) {
|
2018-06-28 14:51:53 -07:00
|
|
|
let out = client.poll_get_balance(bob_pubkey);
|
2018-07-03 15:09:35 -07:00
|
|
|
if expected.is_none() || run == LAST {
|
2018-06-28 14:51:53 -07:00
|
|
|
return out.ok().clone();
|
|
|
|
}
|
2018-07-05 12:01:40 -07:00
|
|
|
trace!("retry_get_balance[{}] {:?} {:?}", run, out, expected);
|
2018-06-28 14:51:53 -07:00
|
|
|
if let (Some(e), Ok(o)) = (expected, out) {
|
|
|
|
if o == e {
|
|
|
|
return Some(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_tx_and_retry_get_balance(
|
2018-07-11 00:18:48 -07:00
|
|
|
leader: &NodeInfo,
|
2018-05-25 20:54:20 -07:00
|
|
|
alice: &Mint,
|
|
|
|
bob_pubkey: &PublicKey,
|
2018-06-28 14:51:53 -07:00
|
|
|
expected: Option<i64>,
|
|
|
|
) -> Option<i64> {
|
2018-05-25 20:54:20 -07:00
|
|
|
let mut client = mk_client(leader);
|
|
|
|
trace!("getting leader last_id");
|
2018-05-31 12:41:42 -07:00
|
|
|
let last_id = client.get_last_id();
|
2018-06-25 15:24:10 -07:00
|
|
|
info!("executing leader transfer");
|
2018-05-25 20:54:20 -07:00
|
|
|
let _sig = client
|
|
|
|
.transfer(500, &alice.keypair(), *bob_pubkey, &last_id)
|
|
|
|
.unwrap();
|
2018-06-28 14:51:53 -07:00
|
|
|
retry_get_balance(&mut client, bob_pubkey, expected)
|
2018-05-25 20:54:20 -07:00
|
|
|
}
|