From daadae79879cc3a5bde5c03d48e6747881645f2c Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Mon, 2 Apr 2018 14:51:38 -0600 Subject: [PATCH] Move replaying ledger out of accountant --- src/accountant.rs | 36 +----------------------------------- src/bin/testnode.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/accountant.rs b/src/accountant.rs index 561b317c7..dc73c6278 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -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(entries: I) -> (Self, Hash) - where - I: IntoIterator, - { - 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), diff --git a/src/bin/testnode.rs b/src/bin/testnode.rs index 346da9f70..ef6700d30 100644 --- a/src/bin/testnode.rs +++ b/src/bin/testnode.rs @@ -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(