Move replaying ledger out of accountant
This commit is contained in:
parent
2b788d06b7
commit
daadae7987
|
@ -4,7 +4,6 @@
|
|||
//! already been signed and verified.
|
||||
|
||||
use chrono::prelude::*;
|
||||
use entry::Entry;
|
||||
use event::Event;
|
||||
use hash::Hash;
|
||||
use mint::Mint;
|
||||
|
@ -60,39 +59,6 @@ impl Accountant {
|
|||
Self::new_from_deposit(&deposit)
|
||||
}
|
||||
|
||||
/// Create an Accountant using an existing ledger.
|
||||
pub fn new_from_entries<I>(entries: I) -> (Self, Hash)
|
||||
where
|
||||
I: IntoIterator<Item = Entry>,
|
||||
{
|
||||
let mut entries = entries.into_iter();
|
||||
|
||||
// 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 = entries.next().unwrap();
|
||||
let deposit = if let Event::Transaction(ref tr) = entry1.events[0] {
|
||||
tr.plan.final_payment()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut acc = Self::new_from_deposit(&deposit.unwrap());
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
(acc, last_id)
|
||||
}
|
||||
|
||||
/// Verify and process the given Transaction.
|
||||
pub fn process_transaction(&mut self, tr: Transaction) -> Result<()> {
|
||||
if !tr.verify() {
|
||||
|
@ -183,7 +149,7 @@ impl Accountant {
|
|||
}
|
||||
|
||||
/// Process an Transaction or Witness that has already been verified.
|
||||
fn process_verified_event(&mut self, event: &Event) -> Result<()> {
|
||||
pub fn process_verified_event(&mut self, event: &Event) -> Result<()> {
|
||||
match *event {
|
||||
Event::Transaction(ref tr) => self.process_verified_transaction(tr),
|
||||
Event::Signature { from, tx_sig, .. } => self.process_verified_sig(from, tx_sig),
|
||||
|
|
|
@ -2,6 +2,8 @@ extern crate serde_json;
|
|||
extern crate solana;
|
||||
|
||||
use solana::accountant::Accountant;
|
||||
use solana::event::Event;
|
||||
use solana::entry::Entry;
|
||||
use solana::historian::Historian;
|
||||
use solana::accountant_skel::AccountantSkel;
|
||||
use std::io::{self, stdout, BufRead};
|
||||
|
@ -11,11 +13,35 @@ use std::sync::{Arc, Mutex};
|
|||
fn main() {
|
||||
let addr = "127.0.0.1:8000";
|
||||
let stdin = io::stdin();
|
||||
let entries = stdin
|
||||
let mut entries = stdin
|
||||
.lock()
|
||||
.lines()
|
||||
.map(|line| serde_json::from_str(&line.unwrap()).unwrap());
|
||||
let (acc, last_id) = Accountant::new_from_entries(entries);
|
||||
|
||||
// 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] {
|
||||
tr.plan.final_payment()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut acc = Accountant::new_from_deposit(&deposit.unwrap());
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
let historian = Historian::new(&last_id, Some(1000));
|
||||
let exit = Arc::new(AtomicBool::new(false));
|
||||
let skel = Arc::new(Mutex::new(AccountantSkel::new(
|
||||
|
|
Loading…
Reference in New Issue