Return bool on signature check

This commit is contained in:
Tyera Eulberg
2018-06-28 18:59:02 -06:00
committed by Grimes
parent e4e1f8ec1e
commit d354e85a9a
4 changed files with 17 additions and 21 deletions

View File

@ -434,16 +434,16 @@ impl Bank {
self.transaction_count.load(Ordering::Relaxed) self.transaction_count.load(Ordering::Relaxed)
} }
pub fn check_signature(&self, signature: &Signature) -> Option<(Hash, Signature)> { pub fn check_signature(&self, signature: &Signature) -> bool {
let last_ids_sigs = self.last_ids_sigs let last_ids_sigs = self.last_ids_sigs
.read() .read()
.expect("'last_ids_sigs' read lock"); .expect("'last_ids_sigs' read lock");
for (hash, signatures) in last_ids_sigs.iter() { for (_hash, signatures) in last_ids_sigs.iter() {
if let Some(sig) = signatures.get(signature) { if let Some(_sig) = signatures.get(signature) {
return Some((*hash, *sig)); return true;
} }
} }
return None; return false;
} }
} }
@ -640,7 +640,7 @@ mod tests {
let sig = Signature::default(); let sig = Signature::default();
bank.reserve_signature_with_last_id(&sig, &mint.last_id()) bank.reserve_signature_with_last_id(&sig, &mint.last_id())
.expect("reserve signature"); .expect("reserve signature");
assert_eq!(bank.check_signature(&sig), Some((mint.last_id(), sig))); assert!(bank.check_signature(&sig));
} }
#[test] #[test]

View File

@ -143,14 +143,10 @@ fn main() -> io::Result<()> {
// Confirm the last client transaction by signature // Confirm the last client transaction by signature
"confirm" => match last_transaction_sig { "confirm" => match last_transaction_sig {
Some(sig) => { Some(sig) => {
let check_signature = client.check_signature(&sig); if client.check_signature(&sig) {
match check_signature { println!("Signature found at bank id {:?}", id);
Some((id, _sig)) => { } else {
println!("Signature found at bank id {:?}", id);
}
None => {
println!("Uh oh... Signature not found!"); println!("Uh oh... Signature not found!");
}
} }
} }
None => { None => {

View File

@ -32,6 +32,6 @@ pub enum Response {
transaction_count: u64, transaction_count: u64,
}, },
SignatureStatus { SignatureStatus {
signature_status: Option<(Hash, Signature)>, signature_status: bool,
}, },
} }

View File

@ -21,7 +21,7 @@ pub struct ThinClient {
last_id: Option<Hash>, last_id: Option<Hash>,
transaction_count: u64, transaction_count: u64,
balances: HashMap<PublicKey, Option<i64>>, balances: HashMap<PublicKey, Option<i64>>,
signature_status: Option<(Hash, Signature)>, signature_status: bool,
} }
impl ThinClient { impl ThinClient {
@ -42,7 +42,7 @@ impl ThinClient {
last_id: None, last_id: None,
transaction_count: 0, transaction_count: 0,
balances: HashMap::new(), balances: HashMap::new(),
signature_status: None, signature_status: false,
}; };
client client
} }
@ -73,10 +73,10 @@ impl ThinClient {
Response::SignatureStatus { signature_status } => { Response::SignatureStatus { signature_status } => {
self.signature_status = signature_status; self.signature_status = signature_status;
match signature_status { match signature_status {
Some((_, signature)) => { true => {
trace!("Response found signature: {:?}", signature); trace!("Response found signature");
} }
None => { false => {
trace!("Response signature not found"); trace!("Response signature not found");
} }
} }
@ -195,7 +195,7 @@ impl ThinClient {
/// Check a signature in the bank. This method blocks /// Check a signature in the bank. This method blocks
/// until the server sends a response. /// until the server sends a response.
pub fn check_signature(&mut self, sig: &Signature) -> Option<(Hash, Signature)> { pub fn check_signature(&mut self, sig: &Signature) -> bool {
trace!("check_signature"); trace!("check_signature");
let req = Request::GetSignature { signature: *sig }; let req = Request::GetSignature { signature: *sig };
let data = serialize(&req).expect("serialize GetSignature in pub fn check_signature"); let data = serialize(&req).expect("serialize GetSignature in pub fn check_signature");
@ -378,7 +378,7 @@ mod tests {
.unwrap(); .unwrap();
sleep(Duration::from_millis(100)); sleep(Duration::from_millis(100));
assert_eq!(client.check_signature(&sig), Some((last_id, sig))); assert!(client.check_signature(&sig));
exit.store(true, Ordering::Relaxed); exit.store(true, Ordering::Relaxed);
for t in server.thread_hdls { for t in server.thread_hdls {