Merge pull request #234 from sakridge/fix_events_addr

Send events to the right address
This commit is contained in:
Greg Fitzgerald 2018-05-22 16:59:28 -06:00 committed by GitHub
commit abfd7d6951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 43 deletions

View File

@ -34,7 +34,7 @@ fn print_usage(program: &str, opts: Options) {
fn main() { fn main() {
let mut threads = 4usize; let mut threads = 4usize;
let mut addr: String = "127.0.0.1:8000".to_string(); let mut server_addr: String = "127.0.0.1:8000".to_string();
let mut requests_addr: String = "127.0.0.1:8010".to_string(); let mut requests_addr: String = "127.0.0.1:8010".to_string();
let mut opts = Options::new(); let mut opts = Options::new();
@ -57,7 +57,7 @@ fn main() {
return; return;
} }
if matches.opt_present("s") { if matches.opt_present("s") {
addr = matches.opt_str("s").unwrap(); server_addr = matches.opt_str("s").unwrap();
} }
if matches.opt_present("c") { if matches.opt_present("c") {
requests_addr = matches.opt_str("c").unwrap(); requests_addr = matches.opt_str("c").unwrap();
@ -94,7 +94,16 @@ fn main() {
.set_read_timeout(Some(Duration::new(5, 0))) .set_read_timeout(Some(Duration::new(5, 0)))
.unwrap(); .unwrap();
let events_socket = UdpSocket::bind(&events_addr).unwrap(); let events_socket = UdpSocket::bind(&events_addr).unwrap();
let mut client = ThinClient::new(addr.parse().unwrap(), requests_socket, events_socket); let requests_addr: SocketAddr = server_addr.parse().unwrap();
let requests_port = requests_addr.port();
let mut events_server_addr = requests_addr.clone();
events_server_addr.set_port(requests_port + 3);
let mut client = ThinClient::new(
requests_addr,
requests_socket,
events_server_addr,
events_socket,
);
println!("Get last ID..."); println!("Get last ID...");
let last_id = client.get_last_id().wait().unwrap(); let last_id = client.get_last_id().wait().unwrap();
@ -138,17 +147,22 @@ fn main() {
let chunks: Vec<_> = transactions.chunks(sz).collect(); let chunks: Vec<_> = transactions.chunks(sz).collect();
chunks.into_par_iter().for_each(|trs| { chunks.into_par_iter().for_each(|trs| {
println!("Transferring 1 unit {} times... to", trs.len()); println!("Transferring 1 unit {} times... to", trs.len());
let mut requests_addr: SocketAddr = requests_addr.parse().unwrap(); let requests_addr: SocketAddr = server_addr.parse().unwrap();
requests_addr.set_port(0); let mut requests_cb_addr = requests_addr.clone();
let requests_socket = UdpSocket::bind(requests_addr).unwrap(); requests_cb_addr.set_port(0);
let requests_socket = UdpSocket::bind(requests_cb_addr).unwrap();
requests_socket requests_socket
.set_read_timeout(Some(Duration::new(5, 0))) .set_read_timeout(Some(Duration::new(5, 0)))
.unwrap(); .unwrap();
let mut events_addr: SocketAddr = requests_addr.clone(); let mut events_addr: SocketAddr = requests_addr.clone();
let requests_port = events_addr.port(); events_addr.set_port(0);
events_addr.set_port(requests_port + 1);
let events_socket = UdpSocket::bind(&events_addr).unwrap(); let events_socket = UdpSocket::bind(&events_addr).unwrap();
let client = ThinClient::new(addr.parse().unwrap(), requests_socket, events_socket); let client = ThinClient::new(
requests_addr,
requests_socket,
events_server_addr,
events_socket,
);
for tr in trs { for tr in trs {
client.transfer_signed(tr.clone()).unwrap(); client.transfer_signed(tr.clone()).unwrap();
} }

View File

@ -55,6 +55,7 @@ fn main() {
let gossip_addr = format!("0.0.0.0:{}", port + 1); let gossip_addr = format!("0.0.0.0:{}", port + 1);
let replicate_addr = format!("0.0.0.0:{}", port + 2); let replicate_addr = format!("0.0.0.0:{}", port + 2);
let events_addr = format!("0.0.0.0:{}", port + 3); let events_addr = format!("0.0.0.0:{}", port + 3);
eprintln!("events_addr: {:?}", events_addr);
if stdin_isatty() { if stdin_isatty() {
eprintln!("nothing found on stdin, expected a log file"); eprintln!("nothing found on stdin, expected a log file");

View File

@ -53,6 +53,10 @@ pub fn receiver(
recycler: PacketRecycler, recycler: PacketRecycler,
packet_sender: PacketSender, packet_sender: PacketSender,
) -> JoinHandle<()> { ) -> JoinHandle<()> {
let res = sock.set_read_timeout(Some(Duration::new(1, 0)));
if res.is_err() {
panic!("streamer::receiver set_read_timeout error");
}
spawn(move || { spawn(move || {
let _ = recv_loop(&sock, &exit, &recycler, &packet_sender); let _ = recv_loop(&sock, &exit, &recycler, &packet_sender);
() ()

View File

@ -15,9 +15,10 @@ use std::net::{SocketAddr, UdpSocket};
use transaction::Transaction; use transaction::Transaction;
pub struct ThinClient { pub struct ThinClient {
pub addr: SocketAddr, requests_addr: SocketAddr,
pub requests_socket: UdpSocket, requests_socket: UdpSocket,
pub events_socket: UdpSocket, events_addr: SocketAddr,
events_socket: UdpSocket,
last_id: Option<Hash>, last_id: Option<Hash>,
transaction_count: u64, transaction_count: u64,
balances: HashMap<PublicKey, Option<i64>>, balances: HashMap<PublicKey, Option<i64>>,
@ -27,10 +28,16 @@ impl ThinClient {
/// Create a new ThinClient that will interface with Rpu /// Create a new ThinClient that will interface with Rpu
/// over `requests_socket` and `events_socket`. To receive responses, the caller must bind `socket` /// over `requests_socket` and `events_socket`. To receive responses, the caller must bind `socket`
/// to a public address before invoking ThinClient methods. /// to a public address before invoking ThinClient methods.
pub fn new(addr: SocketAddr, requests_socket: UdpSocket, events_socket: UdpSocket) -> Self { pub fn new(
requests_addr: SocketAddr,
requests_socket: UdpSocket,
events_addr: SocketAddr,
events_socket: UdpSocket,
) -> Self {
let client = ThinClient { let client = ThinClient {
addr: addr, requests_addr,
requests_socket, requests_socket,
events_addr,
events_socket, events_socket,
last_id: None, last_id: None,
transaction_count: 0, transaction_count: 0,
@ -70,7 +77,7 @@ impl ThinClient {
pub fn transfer_signed(&self, tr: Transaction) -> io::Result<usize> { pub fn transfer_signed(&self, tr: Transaction) -> io::Result<usize> {
let event = Event::Transaction(tr); let event = Event::Transaction(tr);
let data = serialize(&event).expect("serialize Transaction in pub fn transfer_signed"); let data = serialize(&event).expect("serialize Transaction in pub fn transfer_signed");
self.events_socket.send_to(&data, &self.addr) self.events_socket.send_to(&data, &self.events_addr)
} }
/// Creates, signs, and processes a Transaction. Useful for writing unit-tests. /// Creates, signs, and processes a Transaction. Useful for writing unit-tests.
@ -94,7 +101,7 @@ impl ThinClient {
let req = Request::GetBalance { key: *pubkey }; let req = Request::GetBalance { key: *pubkey };
let data = serialize(&req).expect("serialize GetBalance in pub fn get_balance"); let data = serialize(&req).expect("serialize GetBalance in pub fn get_balance");
self.requests_socket self.requests_socket
.send_to(&data, &self.addr) .send_to(&data, &self.requests_addr)
.expect("buffer error in pub fn get_balance"); .expect("buffer error in pub fn get_balance");
let mut done = false; let mut done = false;
while !done { while !done {
@ -116,7 +123,7 @@ impl ThinClient {
let data = let data =
serialize(&req).expect("serialize GetTransactionCount in pub fn transaction_count"); serialize(&req).expect("serialize GetTransactionCount in pub fn transaction_count");
self.requests_socket self.requests_socket
.send_to(&data, &self.addr) .send_to(&data, &self.requests_addr)
.expect("buffer error in pub fn transaction_count"); .expect("buffer error in pub fn transaction_count");
let mut done = false; let mut done = false;
while !done { while !done {
@ -137,12 +144,11 @@ impl ThinClient {
let req = Request::GetLastId; let req = Request::GetLastId;
let data = serialize(&req).expect("serialize GetLastId in pub fn get_last_id"); let data = serialize(&req).expect("serialize GetLastId in pub fn get_last_id");
self.requests_socket self.requests_socket
.send_to(&data, &self.addr) .send_to(&data, &self.requests_addr)
.expect("buffer error in pub fn get_last_id"); .expect("buffer error in pub fn get_last_id");
let mut done = false; let mut done = false;
while !done { while !done {
let resp = self.recv_response().expect("get_last_id response"); let resp = self.recv_response().expect("get_last_id response");
info!("recv_response {:?}", resp);
if let &Response::LastId { .. } = &resp { if let &Response::LastId { .. } = &resp {
done = true; done = true;
} }
@ -152,6 +158,22 @@ impl ThinClient {
} }
} }
#[cfg(test)]
pub fn poll_get_balance(client: &mut ThinClient, pubkey: &PublicKey) -> io::Result<i64> {
use std::time::Instant;
let mut balance;
let now = Instant::now();
loop {
balance = client.get_balance(pubkey);
if balance.is_ok() || now.elapsed().as_secs() > 1 {
break;
}
}
balance
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -169,12 +191,10 @@ mod tests {
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use std::time::Instant;
use streamer::default_window; use streamer::default_window;
use tvu::{self, Tvu}; use tvu::{self, Tvu};
#[test] #[test]
#[ignore]
fn test_thin_client() { fn test_thin_client() {
logger::setup(); logger::setup();
let gossip = UdpSocket::bind("0.0.0.0:0").unwrap(); let gossip = UdpSocket::bind("0.0.0.0:0").unwrap();
@ -183,6 +203,7 @@ mod tests {
.set_read_timeout(Some(Duration::new(1, 0))) .set_read_timeout(Some(Duration::new(1, 0)))
.unwrap(); .unwrap();
let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let events_addr = events_socket.local_addr().unwrap();
let addr = requests_socket.local_addr().unwrap(); let addr = requests_socket.local_addr().unwrap();
let pubkey = KeyPair::new().pubkey(); let pubkey = KeyPair::new().pubkey();
let d = ReplicatedData::new( let d = ReplicatedData::new(
@ -220,22 +241,12 @@ mod tests {
let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let mut client = ThinClient::new(addr, requests_socket, events_socket); let mut client = ThinClient::new(addr, requests_socket, events_addr, events_socket);
let last_id = client.get_last_id().wait().unwrap(); let last_id = client.get_last_id().wait().unwrap();
let _sig = client let _sig = client
.transfer(500, &alice.keypair(), bob_pubkey, &last_id) .transfer(500, &alice.keypair(), bob_pubkey, &last_id)
.unwrap(); .unwrap();
let mut balance; let balance = poll_get_balance(&mut client, &bob_pubkey);
let now = Instant::now();
loop {
balance = client.get_balance(&bob_pubkey);
if balance.is_ok() {
break;
}
if now.elapsed().as_secs() > 0 {
break;
}
}
assert_eq!(balance.unwrap(), 500); assert_eq!(balance.unwrap(), 500);
exit.store(true, Ordering::Relaxed); exit.store(true, Ordering::Relaxed);
for t in server.thread_hdls { for t in server.thread_hdls {
@ -244,8 +255,8 @@ mod tests {
} }
#[test] #[test]
#[ignore]
fn test_bad_sig() { fn test_bad_sig() {
logger::setup();
let (leader_data, leader_gossip, _, leader_serve, _leader_events) = tvu::test_node(); let (leader_data, leader_gossip, _, leader_serve, _leader_events) = tvu::test_node();
let alice = Mint::new(10_000); let alice = Mint::new(10_000);
let bank = Bank::new(&alice); let bank = Bank::new(&alice);
@ -258,6 +269,7 @@ mod tests {
let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let broadcast_socket = UdpSocket::bind(local).unwrap(); let broadcast_socket = UdpSocket::bind(local).unwrap();
let respond_socket = UdpSocket::bind(local.clone()).unwrap(); let respond_socket = UdpSocket::bind(local.clone()).unwrap();
let events_addr = events_socket.local_addr().unwrap();
let server = Server::new( let server = Server::new(
bank, bank,
@ -279,11 +291,9 @@ mod tests {
.set_read_timeout(Some(Duration::new(5, 0))) .set_read_timeout(Some(Duration::new(5, 0)))
.unwrap(); .unwrap();
let events_socket = UdpSocket::bind("127.0.0.1:0").unwrap(); let events_socket = UdpSocket::bind("127.0.0.1:0").unwrap();
let mut client = ThinClient::new(serve_addr, requests_socket, events_socket); let mut client = ThinClient::new(serve_addr, requests_socket, events_addr, events_socket);
let last_id = client.get_last_id().wait().unwrap(); let last_id = client.get_last_id().wait().unwrap();
trace!("doing stuff");
let tr = Transaction::new(&alice.keypair(), bob_pubkey, 500, last_id); let tr = Transaction::new(&alice.keypair(), bob_pubkey, 500, last_id);
let _sig = client.transfer_signed(tr).unwrap(); let _sig = client.transfer_signed(tr).unwrap();
@ -295,10 +305,9 @@ mod tests {
tr2.data.plan = Plan::new_payment(502, bob_pubkey); tr2.data.plan = Plan::new_payment(502, bob_pubkey);
let _sig = client.transfer_signed(tr2).unwrap(); let _sig = client.transfer_signed(tr2).unwrap();
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 500); let balance = poll_get_balance(&mut client, &bob_pubkey);
trace!("exiting"); assert_eq!(balance.unwrap(), 500);
exit.store(true, Ordering::Relaxed); exit.store(true, Ordering::Relaxed);
trace!("joining threads");
for t in server.thread_hdls { for t in server.thread_hdls {
t.join().unwrap(); t.join().unwrap();
} }
@ -405,6 +414,7 @@ mod tests {
local.set_port(0); local.set_port(0);
let broadcast_socket = UdpSocket::bind(local).unwrap(); let broadcast_socket = UdpSocket::bind(local).unwrap();
let respond_socket = UdpSocket::bind(local.clone()).unwrap(); let respond_socket = UdpSocket::bind(local.clone()).unwrap();
let events_addr = leader.4.local_addr().unwrap();
let server = Server::new( let server = Server::new(
leader_bank, leader_bank,
@ -435,7 +445,12 @@ mod tests {
.unwrap(); .unwrap();
let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let mut client = ThinClient::new(leader.0.serve_addr, requests_socket, events_socket); let mut client = ThinClient::new(
leader.0.serve_addr,
requests_socket,
events_addr,
events_socket,
);
trace!("getting leader last_id"); trace!("getting leader last_id");
let last_id = client.get_last_id().wait().unwrap(); let last_id = client.get_last_id().wait().unwrap();
info!("executing leader transer"); info!("executing leader transer");
@ -455,7 +470,8 @@ mod tests {
.unwrap(); .unwrap();
let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let events_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let mut client = ThinClient::new(*serve_addr, requests_socket, events_socket); let mut client =
ThinClient::new(*serve_addr, requests_socket, events_addr, events_socket);
for i in 0..10 { for i in 0..10 {
trace!("getting replicant balance {} {}/10", *serve_addr, i); trace!("getting replicant balance {} {}/10", *serve_addr, i);
if let Ok(bal) = client.get_balance(&bob_pubkey) { if let Ok(bal) = client.get_balance(&bob_pubkey) {

View File

@ -212,7 +212,6 @@ mod tests {
/// Test that mesasge sent from leader to target1 and repliated to target2 /// Test that mesasge sent from leader to target1 and repliated to target2
#[test] #[test]
#[ignore]
fn test_replicate() { fn test_replicate() {
logger::setup(); logger::setup();
let (leader_data, leader_gossip, _, leader_serve, _) = test_node(); let (leader_data, leader_gossip, _, leader_serve, _) = test_node();