2018-03-26 21:07:11 -07:00
|
|
|
extern crate env_logger;
|
2018-03-03 23:13:40 -08:00
|
|
|
extern crate serde_json;
|
2018-03-27 15:24:05 -07:00
|
|
|
extern crate solana;
|
2018-02-28 17:04:35 -08:00
|
|
|
|
2018-03-27 15:24:05 -07:00
|
|
|
use solana::accountant::Accountant;
|
2018-04-02 20:15:21 -07:00
|
|
|
use solana::accountant_skel::AccountantSkel;
|
2018-04-02 13:51:38 -07:00
|
|
|
use solana::entry::Entry;
|
2018-04-02 20:15:21 -07:00
|
|
|
use solana::event::Event;
|
2018-04-02 13:41:07 -07:00
|
|
|
use solana::historian::Historian;
|
2018-03-26 11:17:19 -07:00
|
|
|
use std::io::{self, stdout, BufRead};
|
2018-03-22 13:05:23 -07:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2018-03-26 21:03:26 -07:00
|
|
|
use std::sync::{Arc, Mutex};
|
2018-02-28 17:04:35 -08:00
|
|
|
|
|
|
|
fn main() {
|
2018-03-26 21:07:11 -07:00
|
|
|
env_logger::init().unwrap();
|
2018-02-28 17:04:35 -08:00
|
|
|
let addr = "127.0.0.1:8000";
|
2018-03-05 14:34:15 -08:00
|
|
|
let stdin = io::stdin();
|
2018-04-02 13:51:38 -07:00
|
|
|
let mut entries = stdin
|
2018-03-05 14:34:15 -08:00
|
|
|
.lock()
|
|
|
|
.lines()
|
|
|
|
.map(|line| serde_json::from_str(&line.unwrap()).unwrap());
|
2018-04-02 13:51:38 -07:00
|
|
|
|
|
|
|
// The first item in the ledger is required to be an entry with zero num_hashes,
|
|
|
|
// which implies its id can be used as the ledger's seed.
|
|
|
|
entries.next().unwrap();
|
|
|
|
|
|
|
|
// The second item in the ledger is a special transaction where the to and from
|
|
|
|
// fields are the same. That entry should be treated as a deposit, not a
|
|
|
|
// transfer to oneself.
|
|
|
|
let entry1: Entry = entries.next().unwrap();
|
|
|
|
let deposit = if let Event::Transaction(ref tr) = entry1.events[0] {
|
2018-03-26 21:07:11 -07:00
|
|
|
tr.data.plan.final_payment()
|
2018-04-02 13:51:38 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new_from_deposit(&deposit.unwrap());
|
2018-04-02 13:51:38 -07:00
|
|
|
|
|
|
|
let mut last_id = entry1.id;
|
|
|
|
for entry in entries {
|
|
|
|
last_id = entry.id;
|
|
|
|
for event in entry.events {
|
|
|
|
acc.process_verified_event(&event).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 13:41:07 -07:00
|
|
|
let historian = Historian::new(&last_id, Some(1000));
|
2018-03-22 13:05:23 -07:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-04-02 13:41:07 -07:00
|
|
|
let skel = Arc::new(Mutex::new(AccountantSkel::new(
|
|
|
|
acc,
|
|
|
|
last_id,
|
|
|
|
stdout(),
|
|
|
|
historian,
|
|
|
|
)));
|
2018-03-05 14:34:15 -08:00
|
|
|
eprintln!("Listening on {}", addr);
|
2018-04-02 19:32:58 -07:00
|
|
|
let threads = AccountantSkel::serve(&skel, addr, exit.clone()).unwrap();
|
2018-03-23 20:49:28 -07:00
|
|
|
for t in threads {
|
|
|
|
t.join().expect("join");
|
|
|
|
}
|
2018-02-28 17:04:35 -08:00
|
|
|
}
|