This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
141 add futures #148
Closed
Closed
141 add futures #148
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
129585f
added FutureResult to return a Future that immediately resolves
rlkelly d6062bf
added FutureResult to return a Future that immediately resolves
rlkelly 4083357
removed json and log
rlkelly 2cba206
Merge branch 'master' into 141__add_futures
rlkelly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,5 @@ matches = "^0.1.6" | |
byteorder = "^1.2.1" | ||
libc = "^0.2.1" | ||
getopts = "^0.2" | ||
futures = "0.1" | ||
isatty = "0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
use accountant_skel::{Request, Response}; | ||
use bincode::{deserialize, serialize}; | ||
use futures::future::{err, ok, FutureResult}; | ||
use hash::Hash; | ||
use signature::{KeyPair, PublicKey, Signature}; | ||
use std::io; | ||
|
@@ -51,35 +52,38 @@ impl AccountantStub { | |
/// Request the balance of the user holding `pubkey`. This method blocks | ||
/// until the server sends a response. If the response packet is dropped | ||
/// by the network, this method will hang indefinitely. | ||
pub fn get_balance(&self, pubkey: &PublicKey) -> io::Result<Option<i64>> { | ||
pub fn get_balance(&self, pubkey: &PublicKey) -> FutureResult<i64, i64> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about something like |
||
let req = Request::GetBalance { key: *pubkey }; | ||
let data = serialize(&req).expect("serialize GetBalance"); | ||
self.socket.send_to(&data, &self.addr)?; | ||
self.socket.send_to(&data, &self.addr).expect("buffer error"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't panic on this one. It's too likely to happen. |
||
let mut buf = vec![0u8; 1024]; | ||
self.socket.recv_from(&mut buf)?; | ||
self.socket.recv_from(&mut buf).expect("buffer error"); | ||
let resp = deserialize(&buf).expect("deserialize balance"); | ||
if let Response::Balance { key, val } = resp { | ||
assert_eq!(key, *pubkey); | ||
return Ok(val); | ||
return match val { | ||
Some(x) => ok(x), | ||
_ => err(0), | ||
}; | ||
} | ||
Ok(None) | ||
err(0) | ||
} | ||
|
||
/// Request the last Entry ID from the server. This method blocks | ||
/// until the server sends a response. At the time of this writing, | ||
/// it also has the side-effect of causing the server to log any | ||
/// entries that have been published by the Historian. | ||
pub fn get_last_id(&self) -> io::Result<Hash> { | ||
pub fn get_last_id(&self) -> FutureResult<Hash, ()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, |
||
let req = Request::GetLastId; | ||
let data = serialize(&req).expect("serialize GetId"); | ||
self.socket.send_to(&data, &self.addr)?; | ||
self.socket.send_to(&data, &self.addr).expect("buffer error"); | ||
let mut buf = vec![0u8; 1024]; | ||
self.socket.recv_from(&mut buf)?; | ||
self.socket.recv_from(&mut buf).expect("buffer error"); | ||
let resp = deserialize(&buf).expect("deserialize Id"); | ||
if let Response::LastId { id } = resp { | ||
return Ok(id); | ||
return ok(id); | ||
} | ||
Ok(Default::default()) | ||
ok(Default::default()) | ||
} | ||
} | ||
|
||
|
@@ -88,6 +92,7 @@ mod tests { | |
use super::*; | ||
use accountant::Accountant; | ||
use accountant_skel::AccountantSkel; | ||
use futures::Future; | ||
use historian::Historian; | ||
use mint::Mint; | ||
use signature::{KeyPair, KeyPairUtil}; | ||
|
@@ -120,10 +125,10 @@ mod tests { | |
socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap(); | ||
|
||
let acc = AccountantStub::new(addr, socket); | ||
let last_id = acc.get_last_id().unwrap(); | ||
let last_id = acc.get_last_id().wait().unwrap(); | ||
let _sig = acc.transfer(500, &alice.keypair(), bob_pubkey, &last_id) | ||
.unwrap(); | ||
assert_eq!(acc.get_balance(&bob_pubkey).unwrap().unwrap(), 500); | ||
assert_eq!(acc.get_balance(&bob_pubkey).wait().unwrap(), 500); | ||
exit.store(true, Ordering::Relaxed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need this. Even if not commented out, it's not right. It should remove
last_id
, not the last item.