Process timestamps as they are added

This commit is contained in:
Greg Fitzgerald 2018-03-08 15:39:03 -07:00
parent 7ef8d5ddde
commit 8d17aed785
3 changed files with 35 additions and 3 deletions

2
Cargo.lock generated
View File

@ -355,7 +355,7 @@ dependencies = [
[[package]] [[package]]
name = "silk" name = "silk"
version = "0.3.1" version = "0.3.2"
dependencies = [ dependencies = [
"bincode 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "silk" name = "silk"
description = "A silky smooth implementation of the Loom architecture" description = "A silky smooth implementation of the Loom architecture"
version = "0.3.1" version = "0.3.2"
documentation = "https://docs.rs/silk" documentation = "https://docs.rs/silk"
homepage = "http://loomprotocol.com/" homepage = "http://loomprotocol.com/"
repository = "https://github.com/loomprotocol/silk" repository = "https://github.com/loomprotocol/silk"

View File

@ -119,6 +119,21 @@ impl Accountant {
} }
} }
// TODO: Move this to transaction.rs
fn all_satisfied(&self, conds: &[Condition]) -> bool {
let mut satisfied = true;
for cond in conds {
if let &Condition::Timestamp(dt) = cond {
if dt > self.last_time {
satisfied = false;
}
} else {
satisfied = false;
}
}
satisfied
}
fn process_verified_transaction( fn process_verified_transaction(
self: &mut Self, self: &mut Self,
tr: &Transaction<i64>, tr: &Transaction<i64>,
@ -138,7 +153,7 @@ impl Accountant {
} }
} }
if !tr.if_all.is_empty() { if !self.all_satisfied(&tr.if_all) {
self.pending.insert(tr.sig, tr.clone()); self.pending.insert(tr.sig, tr.clone());
return Ok(()); return Ok(());
} }
@ -349,6 +364,23 @@ mod tests {
assert_ne!(acc.get_balance(&bob_pubkey), Some(2)); assert_ne!(acc.get_balance(&bob_pubkey), Some(2));
} }
#[test]
fn test_transfer_after_date() {
let alice = Mint::new(1);
let mut acc = Accountant::new(&alice, Some(2));
let alice_keypair = alice.keypair();
let bob_pubkey = KeyPair::new().pubkey();
let dt = Utc::now();
acc.process_verified_timestamp(alice.pubkey(), dt).unwrap();
// It's now past now, so this transfer should be processed immediately.
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt)
.unwrap();
assert_eq!(acc.get_balance(&alice.pubkey()), Some(0));
assert_eq!(acc.get_balance(&bob_pubkey), Some(1));
}
#[test] #[test]
fn test_cancel_transfer() { fn test_cancel_transfer() {
let alice = Mint::new(1); let alice = Mint::new(1);