Skip to content

Commit

Permalink
WIP: process timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed Mar 8, 2018
1 parent dd2bd67 commit 923162a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
42 changes: 30 additions & 12 deletions src/accountant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use signature::{KeyPair, PublicKey, Signature};
use mint::Mint;
use historian::{reserve_signature, Historian};
use std::sync::mpsc::SendError;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::result;
use chrono::prelude::*;

Expand All @@ -29,7 +29,9 @@ pub struct Accountant {
pub balances: HashMap<PublicKey, i64>,
pub first_id: Hash,
pub last_id: Hash,
pub pending: HashMap<Signature, Transaction<i64>>,
pending: HashMap<Signature, Transaction<i64>>,
time_sources: HashSet<PublicKey>,
last_time: DateTime<Utc>,
}

impl Accountant {
Expand All @@ -51,6 +53,8 @@ impl Accountant {
first_id: start_hash,
last_id: start_hash,
pending: HashMap::new(),
time_sources: HashSet::new(),
last_time: Utc.timestamp(0, 0),
};

// The second item in the log is a special transaction where the to and from
Expand Down Expand Up @@ -97,6 +101,16 @@ impl Accountant {
Ok(())
}

fn complete_transaction(self: &mut Self, tr: &Transaction<i64>) {
if self.balances.contains_key(&tr.to) {
if let Some(x) = self.balances.get_mut(&tr.to) {
*x += tr.asset;
}
} else {
self.balances.insert(tr.to, tr.asset);
}
}

fn process_verified_transaction(
self: &mut Self,
tr: &Transaction<i64>,
Expand All @@ -121,14 +135,7 @@ impl Accountant {
return Ok(());
}

if self.balances.contains_key(&tr.to) {
if let Some(x) = self.balances.get_mut(&tr.to) {
*x += tr.asset;
}
} else {
self.balances.insert(tr.to, tr.asset);
}

self.complete_transaction(tr);
Ok(())
}

Expand All @@ -146,15 +153,26 @@ impl Accountant {
Ok(())
}

fn process_verified_timestamp(&mut self, _from: PublicKey, _dt: DateTime<Utc>) -> Result<()> {
fn process_verified_timestamp(&mut self, from: PublicKey, dt: DateTime<Utc>) -> Result<()> {
// If this is the first timestamp we've seen, it probably came from the genesis block,
// so we'll trust it.
if self.last_time == Utc.timestamp(0, 0) {
self.time_sources.insert(from);
}

if self.time_sources.contains(&from) {
if dt > self.last_time {
self.last_time = dt;
}
}
// TODO: Lookup pending Transaction waiting on time, signed by a whitelisted PublicKey.

// Expire:
// if a Timestamp after this DateTime is in unless_any, return funds to tx.from,
// and remove the tx from this map.

// Process postponed:
// otherwise, if "Signature(from) is in if_all, remove it. If that causes that list
// otherwise, if "Timestamp(dt) >= self.last_time" is in if_all, remove it. If that causes that list
// to be empty, add the asset to to, and remove the tx from this map.
Ok(())
}
Expand Down
12 changes: 11 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The `event` crate provides the data structures for log events.
use signature::{PublicKey, Signature, SignatureUtil};
use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil};
use transaction::Transaction;
use chrono::prelude::*;
use bincode::serialize;
Expand All @@ -27,6 +27,16 @@ pub enum Event {
}

impl Event {
pub fn new_timestamp(from: &KeyPair, dt: DateTime<Utc>) -> Self {
let sign_data = serialize(&dt).unwrap();
let sig = Signature::clone_from_slice(from.sign(&sign_data).as_ref());
Event::Timestamp {
from: from.pubkey(),
dt,
sig,
}
}

// TODO: Rename this to transaction_signature().
pub fn get_signature(&self) -> Option<Signature> {
match *self {
Expand Down
3 changes: 2 additions & 1 deletion src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl Mint {
}

pub fn create_events(&self) -> Vec<Event> {
let tr = Transaction::new(&self.keypair(), self.pubkey(), self.tokens, self.seed());
let keypair = self.keypair();
let tr = Transaction::new(&keypair, self.pubkey(), self.tokens, self.seed());
vec![Event::Tick, Event::Transaction(tr)]
}

Expand Down

0 comments on commit 923162a

Please sign in to comment.