cleanup timestamp processing
This commit is contained in:
parent
30449b6054
commit
0eb3669fbf
|
@ -113,18 +113,7 @@ impl Accountant {
|
||||||
|
|
||||||
/// Commit funds to the 'to' party.
|
/// Commit funds to the 'to' party.
|
||||||
fn complete_transaction(self: &mut Self, plan: &Plan<i64>) {
|
fn complete_transaction(self: &mut Self, plan: &Plan<i64>) {
|
||||||
let payment = match *plan {
|
if let Plan::Action(Action::Pay(ref payment)) = *plan {
|
||||||
Plan::Action(Action::Pay(ref payment)) => Some(payment),
|
|
||||||
Plan::After(_, Action::Pay(ref payment)) => Some(payment),
|
|
||||||
Plan::Race(ref plan_a, _) => {
|
|
||||||
if let Plan::After(_, Action::Pay(ref payment)) = **plan_a {
|
|
||||||
Some(payment)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Some(payment) = payment {
|
|
||||||
if self.balances.contains_key(&payment.to) {
|
if self.balances.contains_key(&payment.to) {
|
||||||
if let Some(x) = self.balances.get_mut(&payment.to) {
|
if let Some(x) = self.balances.get_mut(&payment.to) {
|
||||||
*x += payment.asset;
|
*x += payment.asset;
|
||||||
|
@ -135,18 +124,6 @@ impl Accountant {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move this to transaction.rs
|
|
||||||
fn all_satisfied(&self, plan: &Plan<i64>) -> bool {
|
|
||||||
match *plan {
|
|
||||||
Plan::Action(_) => true,
|
|
||||||
Plan::After(Condition::Timestamp(dt), _) => dt <= self.last_time,
|
|
||||||
Plan::After(Condition::Signature(_), _) => false,
|
|
||||||
Plan::Race(ref plan_a, ref plan_b) => {
|
|
||||||
self.all_satisfied(plan_a) || self.all_satisfied(plan_b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_verified_transaction(
|
fn process_verified_transaction(
|
||||||
self: &mut Self,
|
self: &mut Self,
|
||||||
tr: &Transaction<i64>,
|
tr: &Transaction<i64>,
|
||||||
|
@ -162,12 +139,15 @@ impl Accountant {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.all_satisfied(&tr.plan) {
|
let mut plan = tr.plan.clone();
|
||||||
self.pending.insert(tr.sig, tr.plan.clone());
|
let actionable = plan.process_verified_timestamp(self.last_time);
|
||||||
|
|
||||||
|
if !actionable {
|
||||||
|
self.pending.insert(tr.sig, plan);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.complete_transaction(&tr.plan);
|
self.complete_transaction(&plan);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,17 +184,9 @@ 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 &self.pending {
|
for (key, plan) in &mut self.pending {
|
||||||
if let Plan::After(Condition::Timestamp(dt), _) = *plan {
|
if plan.process_verified_timestamp(self.last_time) {
|
||||||
if self.last_time >= dt {
|
completed.push(key.clone());
|
||||||
completed.push(*key);
|
|
||||||
}
|
|
||||||
} else if let Plan::Race(ref plan_a, _) = *plan {
|
|
||||||
if let Plan::After(Condition::Timestamp(dt), _) = **plan_a {
|
|
||||||
if self.last_time >= dt {
|
|
||||||
completed.push(*key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,7 @@ impl<T: Clone> Plan<T> {
|
||||||
pub fn process_verified_sig(&mut self, from: PublicKey) -> bool {
|
pub fn process_verified_sig(&mut self, from: PublicKey) -> bool {
|
||||||
let mut new_plan = None;
|
let mut new_plan = None;
|
||||||
match *self {
|
match *self {
|
||||||
|
Plan::Action(_) => return true,
|
||||||
Plan::Race(ref mut plan_a, ref mut plan_b) => {
|
Plan::Race(ref mut plan_a, ref mut plan_b) => {
|
||||||
plan_a.process_verified_sig(from);
|
plan_a.process_verified_sig(from);
|
||||||
plan_b.process_verified_sig(from);
|
plan_b.process_verified_sig(from);
|
||||||
|
@ -78,6 +79,33 @@ impl<T: Clone> Plan<T> {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn process_verified_timestamp(&mut self, last_time: DateTime<Utc>) -> bool {
|
||||||
|
let mut new_plan = None;
|
||||||
|
match *self {
|
||||||
|
Plan::Action(_) => return true,
|
||||||
|
Plan::Race(ref mut plan_a, ref mut plan_b) => {
|
||||||
|
plan_a.process_verified_timestamp(last_time);
|
||||||
|
plan_b.process_verified_timestamp(last_time);
|
||||||
|
}
|
||||||
|
Plan::After(Condition::Timestamp(dt), ref action) => {
|
||||||
|
if dt <= last_time {
|
||||||
|
new_plan = Some(Plan::Action(action.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
if self.run_race() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(plan) = new_plan {
|
||||||
|
mem::replace(self, plan);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
|
|
Loading…
Reference in New Issue