updated parity-local-store to edition 2018 and removed redundant Error type (#10800)
This commit is contained in:
parent
4489ca0a38
commit
413442d7be
@ -3,6 +3,7 @@ name = "parity-local-store"
|
|||||||
description = "Manages persistent local node data."
|
description = "Manages persistent local node data."
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Parity Technologies <admin@parity.io>"]
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
common-types = { path = "../../ethcore/types" }
|
common-types = { path = "../../ethcore/types" }
|
||||||
|
@ -16,62 +16,32 @@
|
|||||||
|
|
||||||
//! Manages local node data: pending local transactions, sync security level
|
//! Manages local node data: pending local transactions, sync security level
|
||||||
|
|
||||||
|
use std::io;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::fmt;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use types::transaction::{
|
use common_types::{
|
||||||
|
BlockNumber,
|
||||||
|
transaction::{
|
||||||
SignedTransaction, PendingTransaction, UnverifiedTransaction,
|
SignedTransaction, PendingTransaction, UnverifiedTransaction,
|
||||||
Condition as TransactionCondition
|
Condition as TransactionCondition
|
||||||
|
}
|
||||||
};
|
};
|
||||||
use io::IoHandler;
|
use ethcore_io::{IoHandler, TimerToken, IoContext};
|
||||||
use rlp::Rlp;
|
|
||||||
use kvdb::KeyValueDB;
|
use kvdb::KeyValueDB;
|
||||||
|
use log::{debug, trace, warn};
|
||||||
extern crate common_types as types;
|
use rlp::Rlp;
|
||||||
extern crate ethcore_io as io;
|
use serde_derive::{Serialize, Deserialize};
|
||||||
extern crate rlp;
|
use serde_json;
|
||||||
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;
|
|
||||||
|
|
||||||
const LOCAL_TRANSACTIONS_KEY: &'static [u8] = &*b"LOCAL_TXS";
|
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.
|
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)]
|
#[derive(Serialize, Deserialize)]
|
||||||
enum Condition {
|
enum Condition {
|
||||||
Number(types::BlockNumber),
|
Number(BlockNumber),
|
||||||
Timestamp(u64),
|
Timestamp(u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,10 +127,9 @@ pub struct LocalDataStore<T: NodeInfo> {
|
|||||||
|
|
||||||
impl<T: NodeInfo> LocalDataStore<T> {
|
impl<T: NodeInfo> LocalDataStore<T> {
|
||||||
/// Attempt to read pending transactions out of the local store.
|
/// Attempt to read pending transactions out of the local store.
|
||||||
pub fn pending_transactions(&self) -> Result<Vec<PendingTransaction>, Error> {
|
pub fn pending_transactions(&self) -> io::Result<Vec<PendingTransaction>> {
|
||||||
if let Some(val) = self.db.get(self.col, LOCAL_TRANSACTIONS_KEY).map_err(Error::Io)? {
|
if let Some(val) = self.db.get(self.col, LOCAL_TRANSACTIONS_KEY)? {
|
||||||
let local_txs: Vec<_> = ::serde_json::from_slice::<Vec<TransactionEntry>>(&val)
|
let local_txs: Vec<_> = serde_json::from_slice::<Vec<TransactionEntry>>(&val)?
|
||||||
.map_err(Error::Json)?
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(TransactionEntry::into_pending)
|
.filter_map(TransactionEntry::into_pending)
|
||||||
.collect();
|
.collect();
|
||||||
@ -172,7 +141,7 @@ impl<T: NodeInfo> LocalDataStore<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update the entries in the database.
|
/// 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.");
|
trace!(target: "local_store", "Updating local store entries.");
|
||||||
|
|
||||||
let local_entries: Vec<TransactionEntry> = self.node.pending_transactions()
|
let local_entries: Vec<TransactionEntry> = self.node.pending_transactions()
|
||||||
@ -184,32 +153,32 @@ impl<T: NodeInfo> LocalDataStore<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Clear data in this column.
|
/// 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.");
|
trace!(target: "local_store", "Clearing local store entries.");
|
||||||
|
|
||||||
self.write_txs(&[])
|
self.write_txs(&[])
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper for writing a vector of transaction entries to disk.
|
// 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 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);
|
let json_str = format!("{}", local_json);
|
||||||
|
|
||||||
batch.put_vec(self.col, LOCAL_TRANSACTIONS_KEY, json_str.into_bytes());
|
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<T: NodeInfo, M: Send + Sync + 'static> IoHandler<M> for LocalDataStore<T> {
|
impl<T: NodeInfo, M: Send + Sync + 'static> IoHandler<M> for LocalDataStore<T> {
|
||||||
fn initialize(&self, io: &::io::IoContext<M>) {
|
fn initialize(&self, io: &IoContext<M>) {
|
||||||
if let Err(e) = io.register_timer(UPDATE_TIMER, UPDATE_TIMEOUT) {
|
if let Err(e) = io.register_timer(UPDATE_TIMER, UPDATE_TIMEOUT) {
|
||||||
warn!(target: "local_store", "Error registering local store update timer: {}", e);
|
warn!(target: "local_store", "Error registering local store update timer: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn timeout(&self, _io: &::io::IoContext<M>, timer: ::io::TimerToken) {
|
fn timeout(&self, _io: &IoContext<M>, timer: TimerToken) {
|
||||||
if let UPDATE_TIMER = timer {
|
if let UPDATE_TIMER = timer {
|
||||||
if let Err(e) = self.update() {
|
if let Err(e) = self.update() {
|
||||||
debug!(target: "local_store", "Error updating local store: {}", e);
|
debug!(target: "local_store", "Error updating local store: {}", e);
|
||||||
@ -231,7 +200,7 @@ mod tests {
|
|||||||
use super::NodeInfo;
|
use super::NodeInfo;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use types::transaction::{Transaction, Condition, PendingTransaction};
|
use common_types::transaction::{Transaction, Condition, PendingTransaction};
|
||||||
use ethkey::{Brain, Generator};
|
use ethkey::{Brain, Generator};
|
||||||
|
|
||||||
// we want to test: round-trip of good transactions.
|
// we want to test: round-trip of good transactions.
|
||||||
|
Loading…
Reference in New Issue
Block a user