diff --git a/src/accountant.rs b/src/accountant.rs index cd50d00ef..01b797538 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -141,20 +141,21 @@ impl Accountant { } let mut plan = tr.plan.clone(); - let actionable = plan.process_witness(Witness::Timestamp(self.last_time)); + plan.process_witness(Witness::Timestamp(self.last_time)); - if !actionable { + if plan.is_complete() { + self.complete_transaction(&plan); + } else { self.pending.insert(tr.sig, plan); - return Ok(()); } - self.complete_transaction(&plan); Ok(()) } fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> { let actionable = if let Some(plan) = self.pending.get_mut(&tx_sig) { - plan.process_witness(Witness::Signature(from)) + plan.process_witness(Witness::Signature(from)); + plan.is_complete() } else { false }; @@ -186,7 +187,8 @@ impl Accountant { // Check to see if any timelocked transactions can be completed. let mut completed = vec![]; for (key, plan) in &mut self.pending { - if plan.process_witness(Witness::Timestamp(self.last_time)) { + plan.process_witness(Witness::Timestamp(self.last_time)); + if plan.is_complete() { completed.push(key.clone()); } } diff --git a/src/plan.rs b/src/plan.rs index f07bf913b..cb8cc2701 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -66,6 +66,13 @@ impl Plan { ) } + pub fn is_complete(&self) -> bool { + match *self { + Plan::Pay(_) => true, + _ => false, + } + } + pub fn verify(&self, spendable_tokens: i64) -> bool { match *self { Plan::Pay(ref payment) => payment.tokens == spendable_tokens, @@ -76,10 +83,10 @@ impl Plan { } } - pub fn process_witness(&mut self, event: Witness) -> bool { + pub fn process_witness(&mut self, event: Witness) { let mut new_payment = None; match *self { - Plan::Pay(_) => return true, + Plan::Pay(_) => (), Plan::After(ref cond, ref payment) => { if cond.is_satisfied(&event) { new_payment = Some(payment.clone()); @@ -96,9 +103,6 @@ impl Plan { if let Some(payment) = new_payment { mem::replace(self, Plan::Pay(payment)); - true - } else { - false } } } @@ -139,7 +143,7 @@ mod tests { let to = PublicKey::default(); let mut plan = Plan::new_authorized_payment(from, 42, to); - assert!(plan.process_witness(Witness::Signature(from))); + plan.process_witness(Witness::Signature(from)); assert_eq!(plan, Plan::new_payment(42, to)); } @@ -149,7 +153,7 @@ mod tests { let to = PublicKey::default(); let mut plan = Plan::new_future_payment(dt, 42, to); - assert!(plan.process_witness(Witness::Timestamp(dt))); + plan.process_witness(Witness::Timestamp(dt)); assert_eq!(plan, Plan::new_payment(42, to)); } @@ -160,11 +164,11 @@ mod tests { let to = PublicKey::default(); let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to); - assert!(plan.process_witness(Witness::Timestamp(dt))); + plan.process_witness(Witness::Timestamp(dt)); assert_eq!(plan, Plan::new_payment(42, to)); let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to); - assert!(plan.process_witness(Witness::Signature(from))); + plan.process_witness(Witness::Signature(from)); assert_eq!(plan, Plan::new_payment(42, from)); } }