Update request_airdrop utility function to pass along airdrop tx signature

This commit is contained in:
Tyera Eulberg 2018-08-26 22:14:20 -06:00 committed by Greg Fitzgerald
parent 77d820c842
commit 513a934ff6
1 changed files with 18 additions and 5 deletions

View File

@ -1,15 +1,18 @@
use bincode::serialize;
use bincode::{deserialize, serialize};
use drone::DroneRequest;
use signature::Pubkey;
use signature::{Pubkey, Signature};
use std::error;
use std::io::Write;
use std::io::prelude::*;
use std::io::{Error, ErrorKind, Write};
use std::mem::size_of;
use std::net::{SocketAddr, TcpStream};
pub fn request_airdrop(
drone_addr: &SocketAddr,
id: &Pubkey,
tokens: u64,
) -> Result<(), Box<error::Error>> {
) -> Result<Signature, Box<error::Error>> {
// TODO: make this async tokio client
let mut stream = TcpStream::connect(drone_addr)?;
let req = DroneRequest::GetAirdrop {
airdrop_request_amount: tokens,
@ -17,6 +20,16 @@ pub fn request_airdrop(
};
let tx = serialize(&req).expect("serialize drone request");
stream.write_all(&tx).unwrap();
let mut buffer = [0; size_of::<Signature>()];
stream
.read_exact(&mut buffer)
.or_else(|_| Err(Error::new(ErrorKind::Other, "Airdrop failed")))?;
let signature: Signature = deserialize(&buffer).or_else(|err| {
Err(Error::new(
ErrorKind::Other,
format!("deserialize signature in request_airdrop: {:?}", err),
))
})?;
// TODO: add timeout to this function, in case of unresponsive drone
Ok(())
Ok(signature)
}