Add poll_for_signature

This commit is contained in:
Greg Fitzgerald 2018-07-20 19:05:39 -04:00
parent c068ca4cb7
commit 0e305bd7dd
3 changed files with 23 additions and 9 deletions

0
src/bin/client-demo.rs Executable file → Normal file
View File

View File

@ -96,7 +96,6 @@ impl Drone {
}
pub fn send_airdrop(&mut self, req: DroneRequest) -> Result<Signature, io::Error> {
let tx: Transaction;
let request_amount: u64;
let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
@ -109,7 +108,7 @@ impl Drone {
);
let last_id = client.get_last_id();
match req {
let tx = match req {
DroneRequest::GetAirdrop {
airdrop_request_amount,
client_public_key,
@ -119,14 +118,14 @@ impl Drone {
airdrop_request_amount, client_public_key
);
request_amount = airdrop_request_amount;
tx = Transaction::new(
Transaction::new(
&self.mint_keypair,
client_public_key,
airdrop_request_amount as i64,
last_id,
);
)
}
}
};
if self.check_request_limit(request_amount) {
self.request_current += request_amount;
metrics::submit(

View File

@ -217,6 +217,19 @@ impl ThinClient {
balance
}
/// Poll the server to confirm a transaction.
pub fn poll_for_signature(&mut self, sig: &Signature) -> io::Result<()> {
let now = Instant::now();
while !self.check_signature(sig) {
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(())
}
/// Check a signature in the bank. This method blocks
/// until the server sends a response.
pub fn check_signature(&mut self, sig: &Signature) -> bool {
@ -304,10 +317,11 @@ mod tests {
transactions_socket,
);
let last_id = client.get_last_id();
let _sig = client
let sig = client
.transfer(500, &alice.keypair(), bob_pubkey, &last_id)
.unwrap();
let balance = client.poll_get_balance(&bob_pubkey);
client.poll_for_signature(&sig).unwrap();
let balance = client.get_balance(&bob_pubkey);
assert_eq!(balance.unwrap(), 500);
exit.store(true, Ordering::Relaxed);
server.join().unwrap();
@ -361,9 +375,10 @@ mod tests {
contract.tokens = 502;
contract.plan = Plan::Budget(Budget::new_payment(502, bob_pubkey));
}
let _sig = client.transfer_signed(&tr2).unwrap();
let sig = client.transfer_signed(&tr2).unwrap();
client.poll_for_signature(&sig).unwrap();
let balance = client.poll_get_balance(&bob_pubkey);
let balance = client.get_balance(&bob_pubkey);
assert_eq!(balance.unwrap(), 500);
exit.store(true, Ordering::Relaxed);
server.join().unwrap();