Store only spending plans, not full transactions
This commit is contained in:
parent
8c40d1bd72
commit
9d77fd7eec
|
@ -5,7 +5,7 @@
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::{Condition, Transaction};
|
use transaction::{Action, Condition, Plan, Transaction};
|
||||||
use signature::{KeyPair, PublicKey, Signature};
|
use signature::{KeyPair, PublicKey, Signature};
|
||||||
use mint::Mint;
|
use mint::Mint;
|
||||||
use historian::{reserve_signature, Historian};
|
use historian::{reserve_signature, Historian};
|
||||||
|
@ -30,7 +30,7 @@ pub struct Accountant {
|
||||||
pub balances: HashMap<PublicKey, i64>,
|
pub balances: HashMap<PublicKey, i64>,
|
||||||
pub first_id: Hash,
|
pub first_id: Hash,
|
||||||
pub last_id: Hash,
|
pub last_id: Hash,
|
||||||
pending: HashMap<Signature, Transaction<i64>>,
|
pending: HashMap<Signature, Plan<i64>>,
|
||||||
time_sources: HashSet<PublicKey>,
|
time_sources: HashSet<PublicKey>,
|
||||||
last_time: DateTime<Utc>,
|
last_time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
@ -108,21 +108,23 @@ impl Accountant {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commit funds to the 'to' party.
|
/// Commit funds to the 'to' party.
|
||||||
fn complete_transaction(self: &mut Self, tr: &Transaction<i64>) {
|
fn complete_transaction(self: &mut Self, plan: &Plan<i64>) {
|
||||||
let to = tr.plan.to();
|
let Action::Pay(ref payment) = plan.if_all.1;
|
||||||
|
let to = payment.to;
|
||||||
if self.balances.contains_key(&to) {
|
if self.balances.contains_key(&to) {
|
||||||
if let Some(x) = self.balances.get_mut(&to) {
|
if let Some(x) = self.balances.get_mut(&to) {
|
||||||
*x += tr.asset;
|
*x += payment.asset;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.balances.insert(to, tr.asset);
|
self.balances.insert(to, payment.asset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return funds to the 'from' party.
|
/// Return funds to the 'from' party.
|
||||||
fn cancel_transaction(self: &mut Self, tr: &Transaction<i64>) {
|
fn cancel_transaction(self: &mut Self, plan: &Plan<i64>) {
|
||||||
if let Some(x) = self.balances.get_mut(&tr.from) {
|
let Action::Pay(ref payment) = plan.unless_any.1;
|
||||||
*x += tr.asset;
|
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) {
|
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(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.complete_transaction(tr);
|
self.complete_transaction(&tr.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 mut cancel = false;
|
let mut cancel = false;
|
||||||
if let Some(tr) = self.pending.get(&tx_sig) {
|
if let Some(plan) = self.pending.get(&tx_sig) {
|
||||||
// Cancel:
|
// Cancel:
|
||||||
// if Signature(from) is in unless_any, return funds to tx.from, and remove the tx from this map.
|
// if Signature(from) is in unless_any, return funds to tx.from, and remove the tx from this map.
|
||||||
|
|
||||||
// TODO: Use find().
|
// 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 let Condition::Signature(pubkey) = *cond {
|
||||||
if from == pubkey {
|
if from == pubkey {
|
||||||
cancel = true;
|
cancel = true;
|
||||||
|
@ -187,8 +189,8 @@ impl Accountant {
|
||||||
}
|
}
|
||||||
|
|
||||||
if cancel {
|
if cancel {
|
||||||
if let Some(tr) = self.pending.remove(&tx_sig) {
|
if let Some(plan) = self.pending.remove(&tx_sig) {
|
||||||
self.cancel_transaction(&tr);
|
self.cancel_transaction(&plan);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,11 +222,11 @@ 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, tr) in &self.pending {
|
for (key, plan) in &self.pending {
|
||||||
for cond in &tr.plan.if_all.0 {
|
for cond in &plan.if_all.0 {
|
||||||
if let Condition::Timestamp(dt) = *cond {
|
if let Condition::Timestamp(dt) = *cond {
|
||||||
if self.last_time >= dt {
|
if self.last_time >= dt {
|
||||||
if tr.plan.if_all.0.len() == 1 {
|
if plan.if_all.0.len() == 1 {
|
||||||
completed.push(*key);
|
completed.push(*key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,13 +235,13 @@ impl Accountant {
|
||||||
// TODO: Add this in once we start removing constraints
|
// TODO: Add this in once we start removing constraints
|
||||||
//if tr.plan.if_all.0.is_empty() {
|
//if tr.plan.if_all.0.is_empty() {
|
||||||
// // TODO: Remove tr from pending
|
// // TODO: Remove tr from pending
|
||||||
// self.complete_transaction(tr);
|
// self.complete_transaction(plan);
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key in completed {
|
for key in completed {
|
||||||
if let Some(tr) = self.pending.remove(&key) {
|
if let Some(plan) = self.pending.remove(&key) {
|
||||||
self.complete_transaction(&tr);
|
self.complete_transaction(&plan);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,12 +24,12 @@ pub struct Payment<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct SpendingPlan<T> {
|
pub struct Plan<T> {
|
||||||
pub if_all: (Vec<Condition>, Action<T>),
|
pub if_all: (Vec<Condition>, Action<T>),
|
||||||
pub unless_any: (Vec<Condition>, Action<T>),
|
pub unless_any: (Vec<Condition>, Action<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SpendingPlan<T> {
|
impl<T> Plan<T> {
|
||||||
pub fn to(&self) -> PublicKey {
|
pub fn to(&self) -> PublicKey {
|
||||||
let Action::Pay(ref payment) = self.if_all.1;
|
let Action::Pay(ref payment) = self.if_all.1;
|
||||||
payment.to
|
payment.to
|
||||||
|
@ -39,7 +39,7 @@ impl<T> SpendingPlan<T> {
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Transaction<T> {
|
pub struct Transaction<T> {
|
||||||
pub from: PublicKey,
|
pub from: PublicKey,
|
||||||
pub plan: SpendingPlan<T>,
|
pub plan: Plan<T>,
|
||||||
pub asset: T,
|
pub asset: T,
|
||||||
pub last_id: Hash,
|
pub last_id: Hash,
|
||||||
pub sig: Signature,
|
pub sig: Signature,
|
||||||
|
@ -48,7 +48,7 @@ pub struct Transaction<T> {
|
||||||
impl<T: Serialize + Clone> Transaction<T> {
|
impl<T: Serialize + Clone> Transaction<T> {
|
||||||
pub fn new(from_keypair: &KeyPair, to: PublicKey, asset: T, last_id: Hash) -> Self {
|
pub fn new(from_keypair: &KeyPair, to: PublicKey, asset: T, last_id: Hash) -> Self {
|
||||||
let from = from_keypair.pubkey();
|
let from = from_keypair.pubkey();
|
||||||
let plan = SpendingPlan {
|
let plan = Plan {
|
||||||
if_all: (
|
if_all: (
|
||||||
vec![],
|
vec![],
|
||||||
Action::Pay(Payment {
|
Action::Pay(Payment {
|
||||||
|
@ -83,7 +83,7 @@ impl<T: Serialize + Clone> Transaction<T> {
|
||||||
last_id: Hash,
|
last_id: Hash,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let from = from_keypair.pubkey();
|
let from = from_keypair.pubkey();
|
||||||
let plan = SpendingPlan {
|
let plan = Plan {
|
||||||
if_all: (
|
if_all: (
|
||||||
vec![Condition::Timestamp(dt)],
|
vec![Condition::Timestamp(dt)],
|
||||||
Action::Pay(Payment {
|
Action::Pay(Payment {
|
||||||
|
@ -159,7 +159,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_claim() {
|
fn test_serialize_claim() {
|
||||||
let plan = SpendingPlan {
|
let plan = Plan {
|
||||||
if_all: (
|
if_all: (
|
||||||
Default::default(),
|
Default::default(),
|
||||||
Action::Pay(Payment {
|
Action::Pay(Payment {
|
||||||
|
|
Loading…
Reference in New Issue