Skip to content

Commit

Permalink
Add DateTime and Cancel conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed Mar 8, 2018
1 parent dba6d7a commit 2379792
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"] }
13 changes: 13 additions & 0 deletions src/accountant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Utc>,
) -> Result<Signature> {
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<i64> {
self.balances.get(pubkey).map(|x| *x)
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc>),
Cancel,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Transaction<T> {
pub from: PublicKey,
pub to: PublicKey,
pub if_all: Vec<Condition>,
pub unless_any: Vec<Condition>,
pub asset: T,
pub last_id: Hash,
pub sig: Signature,
Expand All @@ -19,6 +28,28 @@ impl<T: Serialize> Transaction<T> {
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<Utc>,
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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 2379792

Please sign in to comment.