From 9d77fd7eec5e6f85dfb5b6a5b01c594b571be8ea Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Sat, 10 Mar 2018 17:57:16 -0700 Subject: [PATCH] Store only spending plans, not full transactions --- src/accountant.rs | 44 +++++++++++++++++++++++--------------------- src/transaction.rs | 12 ++++++------ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/accountant.rs b/src/accountant.rs index d9f5203536..0b2aab422d 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -5,7 +5,7 @@ use hash::Hash; use entry::Entry; use event::Event; -use transaction::{Condition, Transaction}; +use transaction::{Action, Condition, Plan, Transaction}; use signature::{KeyPair, PublicKey, Signature}; use mint::Mint; use historian::{reserve_signature, Historian}; @@ -30,7 +30,7 @@ pub struct Accountant { pub balances: HashMap, pub first_id: Hash, pub last_id: Hash, - pending: HashMap>, + pending: HashMap>, time_sources: HashSet, last_time: DateTime, } @@ -108,21 +108,23 @@ impl Accountant { } /// Commit funds to the 'to' party. - fn complete_transaction(self: &mut Self, tr: &Transaction) { - let to = tr.plan.to(); + fn complete_transaction(self: &mut Self, plan: &Plan) { + let Action::Pay(ref payment) = plan.if_all.1; + let to = payment.to; if self.balances.contains_key(&to) { if let Some(x) = self.balances.get_mut(&to) { - *x += tr.asset; + *x += payment.asset; } } else { - self.balances.insert(to, tr.asset); + self.balances.insert(to, payment.asset); } } /// Return funds to the 'from' party. - fn cancel_transaction(self: &mut Self, tr: &Transaction) { - if let Some(x) = self.balances.get_mut(&tr.from) { - *x += tr.asset; + fn cancel_transaction(self: &mut Self, plan: &Plan) { + let Action::Pay(ref payment) = plan.unless_any.1; + if let Some(x) = self.balances.get_mut(&payment.to) { + *x += payment.asset; } } @@ -161,22 +163,22 @@ impl Accountant { } if !self.all_satisfied(&tr.plan.if_all.0) { - self.pending.insert(tr.sig, tr.clone()); + self.pending.insert(tr.sig, tr.plan.clone()); return Ok(()); } - self.complete_transaction(tr); + self.complete_transaction(&tr.plan); Ok(()) } fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> { let mut cancel = false; - if let Some(tr) = self.pending.get(&tx_sig) { + if let Some(plan) = self.pending.get(&tx_sig) { // Cancel: // if Signature(from) is in unless_any, return funds to tx.from, and remove the tx from this map. // TODO: Use find(). - for cond in &tr.plan.unless_any.0 { + for cond in &plan.unless_any.0 { if let Condition::Signature(pubkey) = *cond { if from == pubkey { cancel = true; @@ -187,8 +189,8 @@ impl Accountant { } if cancel { - if let Some(tr) = self.pending.remove(&tx_sig) { - self.cancel_transaction(&tr); + if let Some(plan) = self.pending.remove(&tx_sig) { + self.cancel_transaction(&plan); } } @@ -220,11 +222,11 @@ impl Accountant { // Check to see if any timelocked transactions can be completed. let mut completed = vec![]; - for (key, tr) in &self.pending { - for cond in &tr.plan.if_all.0 { + for (key, plan) in &self.pending { + for cond in &plan.if_all.0 { if let Condition::Timestamp(dt) = *cond { if self.last_time >= dt { - if tr.plan.if_all.0.len() == 1 { + if plan.if_all.0.len() == 1 { completed.push(*key); } } @@ -233,13 +235,13 @@ impl Accountant { // TODO: Add this in once we start removing constraints //if tr.plan.if_all.0.is_empty() { // // TODO: Remove tr from pending - // self.complete_transaction(tr); + // self.complete_transaction(plan); //} } for key in completed { - if let Some(tr) = self.pending.remove(&key) { - self.complete_transaction(&tr); + if let Some(plan) = self.pending.remove(&key) { + self.complete_transaction(&plan); } } diff --git a/src/transaction.rs b/src/transaction.rs index 47570e226a..33c5021716 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -24,12 +24,12 @@ pub struct Payment { } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct SpendingPlan { +pub struct Plan { pub if_all: (Vec, Action), pub unless_any: (Vec, Action), } -impl SpendingPlan { +impl Plan { pub fn to(&self) -> PublicKey { let Action::Pay(ref payment) = self.if_all.1; payment.to @@ -39,7 +39,7 @@ impl SpendingPlan { #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Transaction { pub from: PublicKey, - pub plan: SpendingPlan, + pub plan: Plan, pub asset: T, pub last_id: Hash, pub sig: Signature, @@ -48,7 +48,7 @@ pub struct Transaction { impl Transaction { pub fn new(from_keypair: &KeyPair, to: PublicKey, asset: T, last_id: Hash) -> Self { let from = from_keypair.pubkey(); - let plan = SpendingPlan { + let plan = Plan { if_all: ( vec![], Action::Pay(Payment { @@ -83,7 +83,7 @@ impl Transaction { last_id: Hash, ) -> Self { let from = from_keypair.pubkey(); - let plan = SpendingPlan { + let plan = Plan { if_all: ( vec![Condition::Timestamp(dt)], Action::Pay(Payment { @@ -159,7 +159,7 @@ mod tests { #[test] fn test_serialize_claim() { - let plan = SpendingPlan { + let plan = Plan { if_all: ( Default::default(), Action::Pay(Payment {