solana/src/bin/testnode.rs

77 lines
2.2 KiB
Rust
Raw Normal View History

2018-03-26 21:07:11 -07:00
extern crate env_logger;
extern crate getopts;
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-04-11 19:24:14 -07:00
use getopts::Options;
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;
use solana::entry::Entry;
2018-04-02 20:15:21 -07:00
use solana::event::Event;
use solana::historian::Historian;
2018-04-11 19:24:14 -07:00
use std::env;
2018-03-26 11:17:19 -07:00
use std::io::{self, stdout, BufRead};
2018-04-19 07:06:19 -07:00
use std::process::exit;
2018-03-22 13:05:23 -07:00
use std::sync::atomic::AtomicBool;
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();
let mut port = 8000u16;
let mut opts = Options::new();
opts.optopt("p", "", "port", "port");
let args: Vec<String> = env::args().collect();
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
2018-04-19 07:06:19 -07:00
Err(e) => {
eprintln!("{}", e);
exit(1);
}
};
if matches.opt_present("p") {
port = matches.opt_str("p").unwrap().parse().expect("port");
}
let addr = format!("0.0.0.0:{}", port);
let stdin = io::stdin();
let mut entries = stdin
.lock()
.lines()
.map(|line| serde_json::from_str(&line.unwrap()).unwrap());
// 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()
} else {
None
};
let acc = Accountant::new_from_deposit(&deposit.unwrap());
let mut last_id = entry1.id;
for entry in entries {
last_id = entry.id;
acc.process_verified_events(entry.events).unwrap();
}
let historian = Historian::new(&last_id, Some(1000));
2018-03-22 13:05:23 -07:00
let exit = Arc::new(AtomicBool::new(false));
let skel = Arc::new(Mutex::new(AccountantSkel::new(
acc,
last_id,
stdout(),
historian,
)));
eprintln!("Listening on {}", addr);
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
}