From 2379792e0aa948882706d3e25e137f268b8c4afc Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Wed, 7 Mar 2018 21:55:49 -0700 Subject: [PATCH] Add DateTime and Cancel conditions Fixes #32, #33 --- Cargo.toml | 3 ++- src/accountant.rs | 13 +++++++++++++ src/lib.rs | 1 + src/transaction.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a51cf84e4390ad..6c0911ab122c45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "silk" description = "A silky smooth implementation of the Loom architecture" -version = "0.3.0" +version = "0.3.1" documentation = "https://docs.rs/silk" homepage = "http://loomprotocol.com/" repository = "https://github.com/loomprotocol/silk" @@ -53,3 +53,4 @@ serde_json = "1.0.10" ring = "0.12.1" untrusted = "0.5.1" bincode = "1.0.0" +chrono = { version = "0.4.0", features = ["serde"] } diff --git a/src/accountant.rs b/src/accountant.rs index c84cb70125d815..3a54c7c46af23a 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -12,6 +12,7 @@ use historian::{reserve_signature, Historian}; use std::sync::mpsc::SendError; use std::collections::HashMap; use std::result; +use chrono::prelude::*; #[derive(Debug, PartialEq, Eq)] pub enum AccountingError { @@ -138,6 +139,18 @@ impl Accountant { self.process_transaction(tr).map(|_| sig) } + pub fn transfer_on_date( + self: &mut Self, + n: i64, + keypair: &KeyPair, + to: PublicKey, + dt: DateTime, + ) -> Result { + let tr = Transaction::new_on_date(keypair, to, dt, n, self.last_id); + let sig = tr.sig; + self.process_transaction(tr).map(|_| sig) + } + pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option { self.balances.get(pubkey).map(|x| *x) } diff --git a/src/lib.rs b/src/lib.rs index 01cbc93aea0503..f9f98c2e82063b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ pub mod accountant; pub mod accountant_skel; pub mod accountant_stub; extern crate bincode; +extern crate chrono; extern crate generic_array; extern crate rayon; extern crate ring; diff --git a/src/transaction.rs b/src/transaction.rs index dc3d400065a788..8256da28d3d003 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -4,11 +4,20 @@ use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil}; use serde::Serialize; use bincode::serialize; use hash::Hash; +use chrono::prelude::*; + +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] +pub enum Condition { + DateTime(DateTime), + Cancel, +} #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Transaction { pub from: PublicKey, pub to: PublicKey, + pub if_all: Vec, + pub unless_any: Vec, pub asset: T, pub last_id: Hash, pub sig: Signature, @@ -19,6 +28,28 @@ impl Transaction { let mut tr = Transaction { from: from_keypair.pubkey(), to, + if_all: vec![], + unless_any: vec![], + asset, + last_id, + sig: Signature::default(), + }; + tr.sign(from_keypair); + tr + } + + pub fn new_on_date( + from_keypair: &KeyPair, + to: PublicKey, + dt: DateTime, + asset: T, + last_id: Hash, + ) -> Self { + let mut tr = Transaction { + from: from_keypair.pubkey(), + to, + if_all: vec![Condition::DateTime(dt)], + unless_any: vec![Condition::Cancel], asset, last_id, sig: Signature::default(), @@ -72,6 +103,8 @@ mod tests { let claim0 = Transaction { from: Default::default(), to: Default::default(), + if_all: Default::default(), + unless_any: Default::default(), asset: 0u8, last_id: Default::default(), sig: Default::default(),