Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up client demo #293

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ impl Bank {
last_item.0
}

/// Return all recent entry IDs. The last item is the most recent.
pub fn last_ids(&self) -> Vec<Hash> {
let last_ids = self.last_ids.read().expect("'last_ids' read lock");
last_ids.iter().map(|x| x.0).collect()
}

fn reserve_signature(signatures: &RwLock<HashSet<Signature>>, sig: &Signature) -> Result<()> {
if signatures
.read()
Expand Down
12 changes: 8 additions & 4 deletions src/bin/client-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ fn main() {
});
let mut client = mk_client(&client_addr, &leader);

println!("Get last ID...");
let last_id = client.get_last_id();
println!("Got last ID {:?}", last_id);
println!("Get last IDs...");
let last_ids = client.get_last_ids();
let last_ids = &last_ids[1024..]; // Ignore the oldest ones, since they'd get rejected after new entries are added.
println!("Got last IDs {:?}", last_ids);

let rnd = GenKeys::new(demo.mint.keypair().public_key_bytes());

Expand All @@ -145,7 +146,10 @@ fn main() {
let now = Instant::now();
let transactions: Vec<_> = keypair_pairs
.into_par_iter()
.map(|chunk| Transaction::new(&chunk[0], chunk[1].pubkey(), 1, last_id))
.enumerate()
.map(|(i, pair)| {
Transaction::new(&pair[0], pair[1].pubkey(), 1, last_ids[i % last_ids.len()])
})
.collect();
let duration = now.elapsed();
let ns = duration.as_secs() * 1_000_000_000 + u64::from(duration.subsec_nanos());
Expand Down
2 changes: 2 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use signature::PublicKey;
pub enum Request {
GetBalance { key: PublicKey },
GetLastId,
GetLastIds,
GetTransactionCount,
}

Expand All @@ -22,5 +23,6 @@ impl Request {
pub enum Response {
Balance { key: PublicKey, val: Option<i64> },
LastId { id: Hash },
LastIds { ids: Vec<Hash> },
TransactionCount { transaction_count: u64 },
}
6 changes: 6 additions & 0 deletions src/request_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ impl RequestProcessor {
info!("Response::LastId {:?}", rsp);
Some(rsp)
}
Request::GetLastIds => {
let ids = self.bank.last_ids();
let rsp = (Response::LastIds { ids }, rsp_addr);
info!("Response::LastIds {:?}", rsp);
Some(rsp)
}
Request::GetTransactionCount => {
let transaction_count = self.bank.transaction_count() as u64;
let rsp = (Response::TransactionCount { transaction_count }, rsp_addr);
Expand Down
73 changes: 69 additions & 4 deletions src/thin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct ThinClient {
requests_socket: UdpSocket,
transactions_addr: SocketAddr,
transactions_socket: UdpSocket,
last_id: Option<Hash>,
last_ids: Vec<Hash>,
transaction_count: u64,
balances: HashMap<PublicKey, Option<i64>>,
}
Expand All @@ -37,7 +37,7 @@ impl ThinClient {
requests_socket,
transactions_addr,
transactions_socket,
last_id: None,
last_ids: vec![],
transaction_count: 0,
balances: HashMap::new(),
};
Expand All @@ -61,7 +61,11 @@ impl ThinClient {
}
Response::LastId { id } => {
info!("Response last_id {:?}", id);
self.last_id = Some(id);
self.last_ids = vec![id];
}
Response::LastIds { ids } => {
info!("Response last_ids {:?}", ids);
self.last_ids = ids;
}
Response::TransactionCount { transaction_count } => {
info!("Response transaction count {:?}", transaction_count);
Expand Down Expand Up @@ -152,7 +156,27 @@ impl ThinClient {
}
self.process_response(resp);
}
self.last_id.expect("some last_id")
*self.last_ids.last().expect("some last_id")
}

/// Request the last Entry ID from the server. This method blocks
/// until the server sends a response.
pub fn get_last_ids(&mut self) -> Vec<Hash> {
info!("get_last_id");
let req = Request::GetLastIds;
let data = serialize(&req).expect("serialize GetLastId in pub fn get_last_id");
self.requests_socket
.send_to(&data, &self.requests_addr)
.expect("buffer error in pub fn get_last_id");
let mut done = false;
while !done {
let resp = self.recv_response().expect("get_last_id response");
if let &Response::LastIds { .. } = &resp {
done = true;
}
self.process_response(resp);
}
self.last_ids.clone()
}

pub fn poll_get_balance(&mut self, pubkey: &PublicKey) -> io::Result<i64> {
Expand Down Expand Up @@ -233,6 +257,47 @@ mod tests {
}
}

#[test]
fn test_get_last_ids() {
logger::setup();
let leader = TestNode::new();

let mint = Mint::new(10_000);
let bank = Bank::new(&mint);
let exit = Arc::new(AtomicBool::new(false));

let server = Server::new_leader(
bank,
None,
leader.data.clone(),
leader.sockets.requests,
leader.sockets.transaction,
leader.sockets.broadcast,
leader.sockets.respond,
leader.sockets.gossip,
exit.clone(),
sink(),
);
sleep(Duration::from_millis(900));

let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();

let mut client = ThinClient::new(
leader.data.requests_addr,
requests_socket,
leader.data.transactions_addr,
transactions_socket,
);
let last_ids = client.get_last_ids();
assert_eq!(last_ids.len(), 1);

exit.store(true, Ordering::Relaxed);
for t in server.thread_hdls {
t.join().unwrap();
}
}

#[test]
fn test_bad_sig() {
logger::setup();
Expand Down