From 413442d7be3adfe35aaf8cafd1e4f9ebee863245 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 27 Jun 2019 15:37:15 +0800 Subject: [PATCH] updated parity-local-store to edition 2018 and removed redundant Error type (#10800) --- miner/local-store/Cargo.toml | 1 + miner/local-store/src/lib.rs | 81 +++++++++++------------------------- 2 files changed, 26 insertions(+), 56 deletions(-) diff --git a/miner/local-store/Cargo.toml b/miner/local-store/Cargo.toml index 3d8030851..06411f09f 100644 --- a/miner/local-store/Cargo.toml +++ b/miner/local-store/Cargo.toml @@ -3,6 +3,7 @@ name = "parity-local-store" description = "Manages persistent local node data." version = "0.1.0" authors = ["Parity Technologies "] +edition = "2018" [dependencies] common-types = { path = "../../ethcore/types" } diff --git a/miner/local-store/src/lib.rs b/miner/local-store/src/lib.rs index 56c573b06..1c0811b4d 100644 --- a/miner/local-store/src/lib.rs +++ b/miner/local-store/src/lib.rs @@ -16,62 +16,32 @@ //! Manages local node data: pending local transactions, sync security level +use std::io; use std::sync::Arc; -use std::fmt; use std::time::Duration; -use types::transaction::{ - SignedTransaction, PendingTransaction, UnverifiedTransaction, - Condition as TransactionCondition +use common_types::{ + BlockNumber, + transaction::{ + SignedTransaction, PendingTransaction, UnverifiedTransaction, + Condition as TransactionCondition + } }; -use io::IoHandler; -use rlp::Rlp; +use ethcore_io::{IoHandler, TimerToken, IoContext}; use kvdb::KeyValueDB; - -extern crate common_types as types; -extern crate ethcore_io as io; -extern crate rlp; -extern crate serde_json; -extern crate serde; -extern crate kvdb; - -#[macro_use] -extern crate serde_derive; - -#[macro_use] -extern crate log; - -#[cfg(test)] -extern crate ethkey; -#[cfg(test)] -extern crate kvdb_memorydb; +use log::{debug, trace, warn}; +use rlp::Rlp; +use serde_derive::{Serialize, Deserialize}; +use serde_json; const LOCAL_TRANSACTIONS_KEY: &'static [u8] = &*b"LOCAL_TXS"; -const UPDATE_TIMER: ::io::TimerToken = 0; +const UPDATE_TIMER: TimerToken = 0; const UPDATE_TIMEOUT: Duration = Duration::from_secs(15 * 60); // once every 15 minutes. -/// Errors which can occur while using the local data store. -#[derive(Debug)] -pub enum Error { - /// Io and database errors: these manifest as `String`s. - Io(::std::io::Error), - /// JSON errors. - Json(::serde_json::Error), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Error::Io(ref val) => write!(f, "{}", val), - Error::Json(ref err) => write!(f, "{}", err), - } - } -} - #[derive(Serialize, Deserialize)] enum Condition { - Number(types::BlockNumber), + Number(BlockNumber), Timestamp(u64), } @@ -157,10 +127,9 @@ pub struct LocalDataStore { impl LocalDataStore { /// Attempt to read pending transactions out of the local store. - pub fn pending_transactions(&self) -> Result, Error> { - if let Some(val) = self.db.get(self.col, LOCAL_TRANSACTIONS_KEY).map_err(Error::Io)? { - let local_txs: Vec<_> = ::serde_json::from_slice::>(&val) - .map_err(Error::Json)? + pub fn pending_transactions(&self) -> io::Result> { + if let Some(val) = self.db.get(self.col, LOCAL_TRANSACTIONS_KEY)? { + let local_txs: Vec<_> = serde_json::from_slice::>(&val)? .into_iter() .filter_map(TransactionEntry::into_pending) .collect(); @@ -172,7 +141,7 @@ impl LocalDataStore { } /// Update the entries in the database. - pub fn update(&self) -> Result<(), Error> { + pub fn update(&self) -> io::Result<()> { trace!(target: "local_store", "Updating local store entries."); let local_entries: Vec = self.node.pending_transactions() @@ -184,32 +153,32 @@ impl LocalDataStore { } /// Clear data in this column. - pub fn clear(&self) -> Result<(), Error> { + pub fn clear(&self) -> io::Result<()> { trace!(target: "local_store", "Clearing local store entries."); self.write_txs(&[]) } // helper for writing a vector of transaction entries to disk. - fn write_txs(&self, txs: &[TransactionEntry]) -> Result<(), Error> { + fn write_txs(&self, txs: &[TransactionEntry]) -> io::Result<()> { let mut batch = self.db.transaction(); - let local_json = ::serde_json::to_value(txs).map_err(Error::Json)?; + let local_json = serde_json::to_value(txs)?; let json_str = format!("{}", local_json); batch.put_vec(self.col, LOCAL_TRANSACTIONS_KEY, json_str.into_bytes()); - self.db.write(batch).map_err(Error::Io) + self.db.write(batch) } } impl IoHandler for LocalDataStore { - fn initialize(&self, io: &::io::IoContext) { + fn initialize(&self, io: &IoContext) { if let Err(e) = io.register_timer(UPDATE_TIMER, UPDATE_TIMEOUT) { warn!(target: "local_store", "Error registering local store update timer: {}", e); } } - fn timeout(&self, _io: &::io::IoContext, timer: ::io::TimerToken) { + fn timeout(&self, _io: &IoContext, timer: TimerToken) { if let UPDATE_TIMER = timer { if let Err(e) = self.update() { debug!(target: "local_store", "Error updating local store: {}", e); @@ -231,7 +200,7 @@ mod tests { use super::NodeInfo; use std::sync::Arc; - use types::transaction::{Transaction, Condition, PendingTransaction}; + use common_types::transaction::{Transaction, Condition, PendingTransaction}; use ethkey::{Brain, Generator}; // we want to test: round-trip of good transactions.