Cleanup: replace bool retval with is_complete() method
This commit is contained in:
@ -141,20 +141,21 @@ impl Accountant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut plan = tr.plan.clone();
|
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);
|
self.pending.insert(tr.sig, plan);
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.complete_transaction(&plan);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> {
|
fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> {
|
||||||
let actionable = if let Some(plan) = self.pending.get_mut(&tx_sig) {
|
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 {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
@ -186,7 +187,8 @@ impl Accountant {
|
|||||||
// Check to see if any timelocked transactions can be completed.
|
// Check to see if any timelocked transactions can be completed.
|
||||||
let mut completed = vec![];
|
let mut completed = vec![];
|
||||||
for (key, plan) in &mut self.pending {
|
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());
|
completed.push(key.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
src/plan.rs
22
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 {
|
pub fn verify(&self, spendable_tokens: i64) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Plan::Pay(ref payment) => payment.tokens == spendable_tokens,
|
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;
|
let mut new_payment = None;
|
||||||
match *self {
|
match *self {
|
||||||
Plan::Pay(_) => return true,
|
Plan::Pay(_) => (),
|
||||||
Plan::After(ref cond, ref payment) => {
|
Plan::After(ref cond, ref payment) => {
|
||||||
if cond.is_satisfied(&event) {
|
if cond.is_satisfied(&event) {
|
||||||
new_payment = Some(payment.clone());
|
new_payment = Some(payment.clone());
|
||||||
@ -96,9 +103,6 @@ impl Plan {
|
|||||||
|
|
||||||
if let Some(payment) = new_payment {
|
if let Some(payment) = new_payment {
|
||||||
mem::replace(self, Plan::Pay(payment));
|
mem::replace(self, Plan::Pay(payment));
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,7 +143,7 @@ mod tests {
|
|||||||
let to = PublicKey::default();
|
let to = PublicKey::default();
|
||||||
|
|
||||||
let mut plan = Plan::new_authorized_payment(from, 42, to);
|
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));
|
assert_eq!(plan, Plan::new_payment(42, to));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +153,7 @@ mod tests {
|
|||||||
let to = PublicKey::default();
|
let to = PublicKey::default();
|
||||||
|
|
||||||
let mut plan = Plan::new_future_payment(dt, 42, to);
|
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));
|
assert_eq!(plan, Plan::new_payment(42, to));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,11 +164,11 @@ mod tests {
|
|||||||
let to = PublicKey::default();
|
let to = PublicKey::default();
|
||||||
|
|
||||||
let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to);
|
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));
|
assert_eq!(plan, Plan::new_payment(42, to));
|
||||||
|
|
||||||
let mut plan = Plan::new_cancelable_future_payment(dt, from, 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));
|
assert_eq!(plan, Plan::new_payment(42, from));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user