From 7ad8599324ac0dc3ba76cbea65c45a39066b0494 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 13:26:44 +0300 Subject: [PATCH 1/8] uncle as rlp in the api --- ethcore/src/client/client.rs | 9 +++-- ethcore/src/client/mod.rs | 4 +- ethcore/src/client/test_client.rs | 4 +- ethcore/src/types/ids.rs | 10 ++--- rpc/src/v1/impls/eth.rs | 64 +++++++++++++++++-------------- 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index d5e509e62..e265fe4c0 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -22,7 +22,7 @@ use util::*; use util::panics::*; use views::BlockView; use error::{Error, ImportError, ExecutionError, BlockError, ImportResult}; -use header::{BlockNumber, Header}; +use header::{BlockNumber}; use state::State; use spec::Spec; use engine::Engine; @@ -50,6 +50,7 @@ pub use types::blockchain_info::BlockChainInfo; pub use types::block_status::BlockStatus; use evm::Factory as EvmFactory; use miner::{Miner, MinerService, TransactionImportResult, AccountDetails}; +use basic_types::*; const MAX_TX_QUEUE_SIZE: usize = 4096; @@ -579,9 +580,9 @@ impl BlockChainClient for Client { self.transaction_address(id).and_then(|address| self.chain.transaction(&address)) } - fn uncle(&self, id: UncleID) -> Option
{ - let index = id.1; - self.block(id.0).and_then(|block| BlockView::new(&block).uncle_at(index)) + fn uncle(&self, id: UncleID) -> Option { + let index = id.position; + self.block(id.block).and_then(|block| BlockView::new(&block).uncle_at(index).and_then(|u| Some(u.rlp(Seal::With)))) } fn transaction_receipt(&self, id: TransactionID) -> Option { diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 7f3c3bb3a..27a1d3265 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -38,7 +38,7 @@ use util::Itertools; use blockchain::TreeRoute; use block_queue::BlockQueueInfo; use block::{OpenBlock, SealedBlock}; -use header::{BlockNumber, Header}; +use header::{BlockNumber}; use transaction::{LocalizedTransaction, SignedTransaction}; use log_entry::LocalizedLogEntry; use filter::Filter; @@ -126,7 +126,7 @@ pub trait BlockChainClient : Sync + Send { fn transaction(&self, id: TransactionID) -> Option; /// Get uncle with given id. - fn uncle(&self, id: UncleID) -> Option
; + fn uncle(&self, id: UncleID) -> Option; /// Get transaction receipt with given hash. fn transaction_receipt(&self, id: TransactionID) -> Option; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index f51f978de..be90d9b67 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -244,7 +244,7 @@ impl MiningBlockChainClient for TestBlockChainClient { fn prepare_open_block(&self, _author: Address, _gas_range_target: (U256, U256), _extra_data: Bytes) -> OpenBlock { unimplemented!(); } - + fn vm_factory(&self) -> &EvmFactory { unimplemented!(); } @@ -298,7 +298,7 @@ impl BlockChainClient for TestBlockChainClient { unimplemented!(); } - fn uncle(&self, _id: UncleID) -> Option { + fn uncle(&self, _id: UncleID) -> Option { unimplemented!(); } diff --git a/ethcore/src/types/ids.rs b/ethcore/src/types/ids.rs index 0a492735c..99dadc4ea 100644 --- a/ethcore/src/types/ids.rs +++ b/ethcore/src/types/ids.rs @@ -55,10 +55,10 @@ pub struct TraceId { } /// Uniquely identifies Uncle. -#[derive(Debug)] -pub struct UncleID ( +#[derive(Debug, Binary)] +pub struct UncleID { /// Block id. - pub BlockID, + pub block: BlockID, /// Position in block. - pub usize -); + pub position: usize +} diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 8bfc661e3..f2325de93 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -41,6 +41,7 @@ use v1::traits::Eth; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, CallRequest, OptionalValue, Index, Filter, Log, Receipt}; use v1::impls::{default_gas_price, dispatch_transaction, error_codes}; use serde; +use ethcore::header::Header as BlockHeader; /// Eth rpc implementation. pub struct EthClient where @@ -126,33 +127,38 @@ impl EthClient where fn uncle(&self, id: UncleID) -> Result { let client = take_weak!(self.client); - match client.uncle(id).and_then(|u| client.block_total_difficulty(BlockID::Hash(u.parent_hash().clone())).map(|diff| (diff, u))) { - Some((parent_difficulty, uncle)) => { - let block = Block { - hash: OptionalValue::Value(uncle.hash()), - parent_hash: uncle.parent_hash, - uncles_hash: uncle.uncles_hash, - author: uncle.author, - miner: uncle.author, - state_root: uncle.state_root, - transactions_root: uncle.transactions_root, - number: OptionalValue::Value(U256::from(uncle.number)), - gas_used: uncle.gas_used, - gas_limit: uncle.gas_limit, - logs_bloom: uncle.log_bloom, - timestamp: U256::from(uncle.timestamp), - difficulty: uncle.difficulty, - total_difficulty: uncle.difficulty + parent_difficulty, - receipts_root: uncle.receipts_root, - extra_data: Bytes::new(uncle.extra_data), - seal_fields: uncle.seal.into_iter().map(|f| decode(&f)).map(Bytes::new).collect(), - uncles: vec![], - transactions: BlockTransactions::Hashes(vec![]), - }; - to_value(&block) - }, - None => Ok(Value::Null) - } + + let uncle: BlockHeader = match client.uncle(id) { + Some(rlp) => decode(&rlp), + None => { return Ok(Value::Null); } + }; + let parent_difficulty = match client.block_total_difficulty(BlockID::Hash(uncle.parent_hash().clone())) { + Some(difficulty) => difficulty, + None => { return Ok(Value::Null); } + }; + + let block = Block { + hash: OptionalValue::Value(uncle.hash()), + parent_hash: uncle.parent_hash, + uncles_hash: uncle.uncles_hash, + author: uncle.author, + miner: uncle.author, + state_root: uncle.state_root, + transactions_root: uncle.transactions_root, + number: OptionalValue::Value(U256::from(uncle.number)), + gas_used: uncle.gas_used, + gas_limit: uncle.gas_limit, + logs_bloom: uncle.log_bloom, + timestamp: U256::from(uncle.timestamp), + difficulty: uncle.difficulty, + total_difficulty: uncle.difficulty + parent_difficulty, + receipts_root: uncle.receipts_root, + extra_data: Bytes::new(uncle.extra_data), + seal_fields: uncle.seal.into_iter().map(|f| decode(&f)).map(Bytes::new).collect(), + uncles: vec![], + transactions: BlockTransactions::Hashes(vec![]), + }; + to_value(&block) } fn sign_call(&self, request: CallRequest) -> Result { @@ -435,12 +441,12 @@ impl Eth for EthClient where fn uncle_by_block_hash_and_index(&self, params: Params) -> Result { from_params::<(H256, Index)>(params) - .and_then(|(hash, index)| self.uncle(UncleID(BlockID::Hash(hash), index.value()))) + .and_then(|(hash, index)| self.uncle(UncleID { block: BlockID::Hash(hash), position: index.value() })) } fn uncle_by_block_number_and_index(&self, params: Params) -> Result { from_params::<(BlockNumber, Index)>(params) - .and_then(|(number, index)| self.uncle(UncleID(number.into(), index.value()))) + .and_then(|(number, index)| self.uncle(UncleID { block: number.into(), position: index.value() })) } fn compilers(&self, params: Params) -> Result { From d4ff3e51e9302d742189c4d81e6984d8b1aff72a Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 16:33:37 +0300 Subject: [PATCH 2/8] uncle rlp in block view --- ethcore/src/client/client.rs | 2 +- ethcore/src/views/block.rs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index e265fe4c0..87ad26647 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -582,7 +582,7 @@ impl BlockChainClient for Client { fn uncle(&self, id: UncleID) -> Option { let index = id.position; - self.block(id.block).and_then(|block| BlockView::new(&block).uncle_at(index).and_then(|u| Some(u.rlp(Seal::With)))) + self.block(id.block).and_then(|block| BlockView::new(&block).uncle_rlp_at(index)) } fn transaction_receipt(&self, id: TransactionID) -> Option { diff --git a/ethcore/src/views/block.rs b/ethcore/src/views/block.rs index 82b8fb805..42fd52a20 100644 --- a/ethcore/src/views/block.rs +++ b/ethcore/src/views/block.rs @@ -139,6 +139,11 @@ impl<'a> BlockView<'a> { pub fn uncle_at(&self, index: usize) -> Option
{ self.rlp.at(2).iter().nth(index).map(|rlp| rlp.as_val()) } + + /// Return nth uncle rlp. + pub fn uncle_rlp_at(&self, index: usize) -> Option { + self.rlp.at(2).iter().nth(index).map(|rlp| rlp.as_raw().to_vec()) + } } impl<'a> Hashable for BlockView<'a> { From 0907722fc87dc857226474bb9744a5d7280fc4df Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 16:34:15 +0300 Subject: [PATCH 3/8] fix warning --- ethcore/src/client/client.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 87ad26647..9bfe1758d 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -50,7 +50,6 @@ pub use types::blockchain_info::BlockChainInfo; pub use types::block_status::BlockStatus; use evm::Factory as EvmFactory; use miner::{Miner, MinerService, TransactionImportResult, AccountDetails}; -use basic_types::*; const MAX_TX_QUEUE_SIZE: usize = 4096; From 07521c17b4e9b876db9d3a110ce6cf5c5625240e Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 21:33:59 +0300 Subject: [PATCH 4/8] dedicated types --- ethcore/src/client/mod.rs | 4 ++- ethcore/src/error.rs | 17 ++++++++- ethcore/src/types/block_import_error.rs | 44 +++++++++++++++++++++++ ethcore/src/types/mod.rs.in | 2 ++ ethcore/src/types/transaction_import.rs | 46 +++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 ethcore/src/types/block_import_error.rs create mode 100644 ethcore/src/types/transaction_import.rs diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 27a1d3265..e54e8e382 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -49,6 +49,8 @@ use trace::LocalizedTrace; use evm::Factory as EvmFactory; use miner::{TransactionImportResult}; use error::Error as EthError; +pub use block_import_error::BlockImportError; +pub use transaction_import::{TransactionImportResult, TransactionImportError}; /// Options concerning what analytics we run on the call. #[derive(Eq, PartialEq, Default, Clone, Copy, Debug)] @@ -188,7 +190,7 @@ pub trait BlockChainClient : Sync + Send { fn last_hashes(&self) -> LastHashes; /// import transactions from network/other 3rd party - fn import_transactions(&self, transactions: Vec) -> Vec>; + fn import_transactions(&self, transactions: Vec) -> Vec>; /// Queue transactions for importing. fn queue_transactions(&self, transactions: Vec); diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index 92d3cbe6b..7460762c2 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -250,7 +250,7 @@ impl fmt::Display for Error { } /// Result of import block operation. -pub type ImportResult = Result; +pub type ImportResult = Result; impl From for Error { fn from(err: ClientError) -> Error { @@ -312,6 +312,21 @@ impl From for Error { } } +impl From for Error { + fn from(err: BlockImportError) -> Error { + match err { + BlockImportError::Block(e) => Error::Block(e), + BlockImportError::Import(e) => Error::Import(e), + BlockImportError::Other(s) => Error::Util(UtilError::SimpleString(s)), + } + } +} + +binary_fixed_size!(BlockError); +binary_fixed_size!(ImportError); +binary_fixed_size!(TransactionError); + + // TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted. /*#![feature(concat_idents)] macro_rules! assimilate { diff --git a/ethcore/src/types/block_import_error.rs b/ethcore/src/types/block_import_error.rs new file mode 100644 index 000000000..8b400613f --- /dev/null +++ b/ethcore/src/types/block_import_error.rs @@ -0,0 +1,44 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Block import error related types + +use std::mem; +use ipc::binary::BinaryConvertError; +use std::collections::VecDeque; +use error::{ImportError, BlockError, Error}; +use std::convert::From; + +/// Error dedicated to import block function +#[derive(Binary, Debug)] +pub enum BlockImportError { + /// Import error + Import(ImportError), + /// Block error + Block(BlockError), + /// Other error + Other(String), +} + +impl From for BlockImportError { + fn from(e: Error) -> Self { + match e { + Error::Block(block_error) => BlockImportError::Block(block_error), + Error::Import(import_error) => BlockImportError::Import(import_error), + _ => BlockImportError::Other(format!("other block import error: {:?}", e)), + } + } +} diff --git a/ethcore/src/types/mod.rs.in b/ethcore/src/types/mod.rs.in index b51e9e57b..97579da8a 100644 --- a/ethcore/src/types/mod.rs.in +++ b/ethcore/src/types/mod.rs.in @@ -25,3 +25,5 @@ pub mod executed; pub mod block_status; pub mod account_diff; pub mod state_diff; +pub mod transaction_import; +pub mod block_import_error; diff --git a/ethcore/src/types/transaction_import.rs b/ethcore/src/types/transaction_import.rs new file mode 100644 index 000000000..78078cfea --- /dev/null +++ b/ethcore/src/types/transaction_import.rs @@ -0,0 +1,46 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Transaction import result related types + +use ipc::binary::BinaryConvertError; +use std::collections::VecDeque; + +#[derive(Debug, Clone, PartialEq)] +/// Represents the result of importing transaction. +pub enum TransactionImportResult { + /// Transaction was imported to current queue. + Current, + /// Transaction was imported to future queue. + Future +} + +binary_fixed_size!(TransactionImportResult); + +#[derive(Debug, Clone, Binary)] +pub enum TransactionImportError { + Transaction(TransactionError), + Other(String), +} + +impl From for BlockImportError { + fn from(e: Error) -> Self { + match e { + Error::Transaction(transaction_error) => TransactionImportError::Transaction(block_error), + _ => TransactionImportError::Other(format!("other block import error: {:?}", e)), + } + } +} From 46b0af6121f017cef079811f3e168cc078f5c543 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 22:13:56 +0300 Subject: [PATCH 5/8] flush work --- ethcore/src/client/client.rs | 16 +++++++++------- ethcore/src/client/mod.rs | 3 +-- ethcore/src/client/test_client.rs | 8 +++++--- ethcore/src/error.rs | 6 ++++-- ethcore/src/miner/miner.rs | 4 +++- ethcore/src/miner/mod.rs | 3 ++- ethcore/src/miner/transaction_queue.rs | 10 +--------- ethcore/src/types/transaction_import.rs | 8 +++++--- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 9bfe1758d..655c66fde 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -38,7 +38,9 @@ use filter::Filter; use log_entry::LocalizedLogEntry; use block_queue::{BlockQueue, BlockQueueInfo}; use blockchain::{BlockChain, BlockProvider, TreeRoute, ImportRoute}; -use client::{BlockID, TransactionID, UncleID, TraceId, ClientConfig, DatabaseCompactionProfile, BlockChainClient, MiningBlockChainClient, TraceFilter, CallAnalytics}; +use client::{BlockID, TransactionID, UncleID, TraceId, ClientConfig, DatabaseCompactionProfile, + BlockChainClient, MiningBlockChainClient, TraceFilter, CallAnalytics, TransactionImportError, + BlockImportError, TransactionImportResult}; use client::Error as ClientError; use env_info::EnvInfo; use executive::{Executive, Executed, TransactOptions, contract_address}; @@ -49,7 +51,7 @@ use trace; pub use types::blockchain_info::BlockChainInfo; pub use types::block_status::BlockStatus; use evm::Factory as EvmFactory; -use miner::{Miner, MinerService, TransactionImportResult, AccountDetails}; +use miner::{Miner, MinerService, AccountDetails}; const MAX_TX_QUEUE_SIZE: usize = 4096; @@ -648,17 +650,17 @@ impl BlockChainClient for Client { self.chain.block_receipts(hash).map(|receipts| rlp::encode(&receipts).to_vec()) } - fn import_block(&self, bytes: Bytes) -> ImportResult { + fn import_block(&self, bytes: Bytes) -> Result { { let header = BlockView::new(&bytes).header_view(); if self.chain.is_known(&header.sha3()) { - return Err(ImportError::AlreadyInChain.into()); + return Err(BlockImportError::Import(ImportError::AlreadyInChain)); } if self.block_status(BlockID::Hash(header.parent_hash())) == BlockStatus::Unknown { - return Err(BlockError::UnknownParent(header.parent_hash()).into()); + return Err(BlockImportError::Block(BlockError::UnknownParent(header.parent_hash()))); } } - self.block_queue.import_block(bytes) + Ok(try!(self.block_queue.import_block(bytes))) } fn queue_info(&self) -> BlockQueueInfo { @@ -772,7 +774,7 @@ impl BlockChainClient for Client { self.build_last_hashes(self.chain.best_block_hash()) } - fn import_transactions(&self, transactions: Vec) -> Vec> { + fn import_transactions(&self, transactions: Vec) -> Vec> { let fetch_account = |a: &Address| AccountDetails { nonce: self.latest_nonce(a), balance: self.latest_balance(a), diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index e54e8e382..54d38b034 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -47,7 +47,6 @@ use error::{ImportResult, ExecutionError}; use receipt::LocalizedReceipt; use trace::LocalizedTrace; use evm::Factory as EvmFactory; -use miner::{TransactionImportResult}; use error::Error as EthError; pub use block_import_error::BlockImportError; pub use transaction_import::{TransactionImportResult, TransactionImportError}; @@ -147,7 +146,7 @@ pub trait BlockChainClient : Sync + Send { fn block_receipts(&self, hash: &H256) -> Option; /// Import a block into the blockchain. - fn import_block(&self, bytes: Bytes) -> ImportResult; + fn import_block(&self, bytes: Bytes) -> Result; /// Get block queue information. fn queue_info(&self) -> BlockQueueInfo; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index be90d9b67..0ce308a30 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -20,7 +20,9 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder}; use util::*; use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action}; use blockchain::TreeRoute; -use client::{BlockChainClient, MiningBlockChainClient, BlockChainInfo, BlockStatus, BlockID, TransactionID, UncleID, TraceId, TraceFilter, LastHashes, CallAnalytics}; +use client::{BlockChainClient, MiningBlockChainClient, BlockChainInfo, BlockStatus, BlockID, + TransactionID, UncleID, TraceId, TraceFilter, LastHashes, CallAnalytics, + TransactionImportError, BlockImportError}; use header::{Header as BlockHeader, BlockNumber}; use filter::Filter; use log_entry::LocalizedLogEntry; @@ -402,7 +404,7 @@ impl BlockChainClient for TestBlockChainClient { None } - fn import_block(&self, b: Bytes) -> ImportResult { + fn import_block(&self, b: Bytes) -> Result { let header = Rlp::new(&b).val_at::(0); let h = header.hash(); let number: usize = header.number as usize; @@ -487,7 +489,7 @@ impl BlockChainClient for TestBlockChainClient { unimplemented!(); } - fn import_transactions(&self, transactions: Vec) -> Vec> { + fn import_transactions(&self, transactions: Vec) -> Vec> { let nonces = self.nonces.read().unwrap(); let balances = self.balances.read().unwrap(); let fetch_account = |a: &Address| AccountDetails { diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index 7460762c2..cfe6b3679 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -20,8 +20,10 @@ use util::*; use header::BlockNumber; use basic_types::LogBloom; use client::Error as ClientError; - pub use types::executed::ExecutionError; +use ipc::binary::{BinaryConvertError, BinaryConvertable}; +use types::transaction_import::TransactionImportError; +use types::block_import_error::BlockImportError; #[derive(Debug, PartialEq)] /// Errors concerning transaction processing. @@ -250,7 +252,7 @@ impl fmt::Display for Error { } /// Result of import block operation. -pub type ImportResult = Result; +pub type ImportResult = Result; impl From for Error { fn from(err: ClientError) -> Error { diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index d3e32c797..fe1877116 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -29,8 +29,10 @@ use transaction::SignedTransaction; use receipt::{Receipt}; use spec::Spec; use engine::Engine; -use miner::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; +use miner::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionOrigin}; use miner::work_notify::WorkPoster; +use client::TransactionImportResult; + /// Different possible definitions for pending transaction set. #[derive(Debug)] diff --git a/ethcore/src/miner/mod.rs b/ethcore/src/miner/mod.rs index 152bd1a61..3d0185753 100644 --- a/ethcore/src/miner/mod.rs +++ b/ethcore/src/miner/mod.rs @@ -47,9 +47,10 @@ mod external; mod transaction_queue; mod work_notify; -pub use self::transaction_queue::{TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; +pub use self::transaction_queue::{TransactionQueue, AccountDetails, TransactionOrigin}; pub use self::miner::{Miner, MinerOptions, PendingSet}; pub use self::external::{ExternalMiner, ExternalMinerService}; +pub use client::TransactionImportResult; use std::collections::BTreeMap; use util::{H256, U256, Address, Bytes}; diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index 7f5b59c38..fde8b67d8 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -90,6 +90,7 @@ use util::hash::{Address, H256}; use util::table::*; use transaction::*; use error::{Error, TransactionError}; +use client::TransactionImportResult; /// Transaction origin #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -309,15 +310,6 @@ pub struct TransactionQueueStatus { pub future: usize, } -#[derive(Debug, PartialEq)] -/// Represents the result of importing transaction. -pub enum TransactionImportResult { - /// Transaction was imported to current queue. - Current, - /// Transaction was imported to future queue. - Future -} - /// Details of account pub struct AccountDetails { /// Most recent account nonce diff --git a/ethcore/src/types/transaction_import.rs b/ethcore/src/types/transaction_import.rs index 78078cfea..fa7455cfd 100644 --- a/ethcore/src/types/transaction_import.rs +++ b/ethcore/src/types/transaction_import.rs @@ -16,8 +16,10 @@ //! Transaction import result related types -use ipc::binary::BinaryConvertError; +use ipc::binary::{BinaryConvertError, BinaryConvertable}; use std::collections::VecDeque; +use error::{TransactionError, Error}; +use std::mem; #[derive(Debug, Clone, PartialEq)] /// Represents the result of importing transaction. @@ -36,10 +38,10 @@ pub enum TransactionImportError { Other(String), } -impl From for BlockImportError { +impl From for TransactionImportError { fn from(e: Error) -> Self { match e { - Error::Transaction(transaction_error) => TransactionImportError::Transaction(block_error), + Error::Transaction(transaction_error) => TransactionImportError::Transaction(transaction_error), _ => TransactionImportError::Other(format!("other block import error: {:?}", e)), } } From edb86631954a18a810ca4ddeee5ee415cb9aab72 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 22:34:50 +0300 Subject: [PATCH 6/8] ethcore finished --- ethcore/src/client/client.rs | 8 ++++++-- ethcore/src/client/mod.rs | 1 - ethcore/src/client/test_client.rs | 4 +++- ethcore/src/error.rs | 3 +-- ethcore/src/types/transaction_import.rs | 4 ++++ util/src/error.rs | 2 +- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 655c66fde..522f1c457 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -21,7 +21,7 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; use util::*; use util::panics::*; use views::BlockView; -use error::{Error, ImportError, ExecutionError, BlockError, ImportResult}; +use error::{ImportError, ExecutionError, BlockError, ImportResult}; use header::{BlockNumber}; use state::State; use spec::Spec; @@ -779,7 +779,11 @@ impl BlockChainClient for Client { nonce: self.latest_nonce(a), balance: self.latest_balance(a), }; - self.miner.import_transactions(self, transactions, fetch_account) + + self.miner.import_transactions(self, transactions, &fetch_account) + .into_iter() + .map(|res| res.map_err(|e| e.into())) + .collect() } fn queue_transactions(&self, transactions: Vec) { diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 54d38b034..c09da450d 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -47,7 +47,6 @@ use error::{ImportResult, ExecutionError}; use receipt::LocalizedReceipt; use trace::LocalizedTrace; use evm::Factory as EvmFactory; -use error::Error as EthError; pub use block_import_error::BlockImportError; pub use transaction_import::{TransactionImportResult, TransactionImportError}; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 0ce308a30..1e51719c0 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -40,7 +40,6 @@ use error::{ExecutionError}; use trace::LocalizedTrace; use miner::{TransactionImportResult, AccountDetails}; -use error::Error as EthError; /// Test client. pub struct TestBlockChainClient { @@ -498,6 +497,9 @@ impl BlockChainClient for TestBlockChainClient { }; self.miner.import_transactions(self, transactions, &fetch_account) + .into_iter() + .map(|res| res.map_err(|e| e.into())) + .collect() } fn queue_transactions(&self, transactions: Vec) { diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index cfe6b3679..8c37e98ef 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -22,10 +22,9 @@ use basic_types::LogBloom; use client::Error as ClientError; pub use types::executed::ExecutionError; use ipc::binary::{BinaryConvertError, BinaryConvertable}; -use types::transaction_import::TransactionImportError; use types::block_import_error::BlockImportError; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] /// Errors concerning transaction processing. pub enum TransactionError { /// Transaction is already imported to the queue diff --git a/ethcore/src/types/transaction_import.rs b/ethcore/src/types/transaction_import.rs index fa7455cfd..c52a9fb80 100644 --- a/ethcore/src/types/transaction_import.rs +++ b/ethcore/src/types/transaction_import.rs @@ -20,6 +20,7 @@ use ipc::binary::{BinaryConvertError, BinaryConvertable}; use std::collections::VecDeque; use error::{TransactionError, Error}; use std::mem; +use util::Populatable; #[derive(Debug, Clone, PartialEq)] /// Represents the result of importing transaction. @@ -32,9 +33,12 @@ pub enum TransactionImportResult { binary_fixed_size!(TransactionImportResult); +/// Api-level error for transaction import #[derive(Debug, Clone, Binary)] pub enum TransactionImportError { + /// Transaction error Transaction(TransactionError), + /// Other error Other(String), } diff --git a/util/src/error.rs b/util/src/error.rs index 5e9f8e7a8..c5f40d717 100644 --- a/util/src/error.rs +++ b/util/src/error.rs @@ -101,7 +101,7 @@ impl fmt::Display for Mismatch { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Clone)] /// Error indicating value found is outside of a valid range. pub struct OutOfBounds { /// Minimum allowed value. From 0f7b66f5573a2fbd99fb1ad33470a687ca5f0326 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 1 Jul 2016 22:40:54 +0300 Subject: [PATCH 7/8] fixed compilation & warnings --- parity/main.rs | 6 +++--- sync/src/chain.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/parity/main.rs b/parity/main.rs index d466987ef..8d46d28e5 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -82,8 +82,8 @@ use rustc_serialize::hex::FromHex; use ctrlc::CtrlC; use util::{H256, ToPretty, NetworkConfiguration, PayloadInfo, Bytes, UtilError, paint, Colour, version}; use util::panics::{MayPanic, ForwardPanic, PanicHandler}; -use ethcore::client::{BlockID, BlockChainClient, ClientConfig, get_db_path}; -use ethcore::error::{Error, ImportError}; +use ethcore::client::{BlockID, BlockChainClient, ClientConfig, get_db_path, BlockImportError}; +use ethcore::error::{ImportError}; use ethcore::service::ClientService; use ethcore::spec::Spec; use ethsync::EthSync; @@ -465,7 +465,7 @@ fn execute_import(conf: Configuration) { while client.queue_info().is_full() { sleep(Duration::from_secs(1)); } match client.import_block(bytes) { Ok(_) => {} - Err(Error::Import(ImportError::AlreadyInChain)) => { trace!("Skipping block already in chain."); } + Err(BlockImportError::Import(ImportError::AlreadyInChain)) => { trace!("Skipping block already in chain."); } Err(e) => die!("Cannot import block: {:?}", e) } informant.tick(client.deref(), None); diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 1897b02f4..6845c32e3 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -93,7 +93,7 @@ use util::*; use std::mem::{replace}; use ethcore::views::{HeaderView, BlockView}; use ethcore::header::{BlockNumber, Header as BlockHeader}; -use ethcore::client::{BlockChainClient, BlockStatus, BlockID, BlockChainInfo}; +use ethcore::client::{BlockChainClient, BlockStatus, BlockID, BlockChainInfo, BlockImportError}; use ethcore::error::*; use ethcore::block::Block; use io::SyncIo; @@ -544,10 +544,10 @@ impl ChainSync { peer.latest_number = Some(header.number()); } match io.chain().import_block(block_rlp.as_raw().to_vec()) { - Err(Error::Import(ImportError::AlreadyInChain)) => { + Err(BlockImportError::Import(ImportError::AlreadyInChain)) => { trace!(target: "sync", "New block already in chain {:?}", h); }, - Err(Error::Import(ImportError::AlreadyQueued)) => { + Err(BlockImportError::Import(ImportError::AlreadyQueued)) => { trace!(target: "sync", "New block already queued {:?}", h); }, Ok(_) => { @@ -557,7 +557,7 @@ impl ChainSync { } trace!(target: "sync", "New block queued {:?} ({})", h, header.number); }, - Err(Error::Block(BlockError::UnknownParent(p))) => { + Err(BlockImportError::Block(BlockError::UnknownParent(p))) => { unknown = true; trace!(target: "sync", "New block with unknown parent ({:?}) {:?}", p, h); }, @@ -841,11 +841,11 @@ impl ChainSync { } match io.chain().import_block(block) { - Err(Error::Import(ImportError::AlreadyInChain)) => { + Err(BlockImportError::Import(ImportError::AlreadyInChain)) => { trace!(target: "sync", "Block already in chain {:?}", h); self.block_imported(&h, number, &parent); }, - Err(Error::Import(ImportError::AlreadyQueued)) => { + Err(BlockImportError::Import(ImportError::AlreadyQueued)) => { trace!(target: "sync", "Block already queued {:?}", h); self.block_imported(&h, number, &parent); }, @@ -854,7 +854,7 @@ impl ChainSync { imported.insert(h.clone()); self.block_imported(&h, number, &parent); }, - Err(Error::Block(BlockError::UnknownParent(_))) if self.state == SyncState::NewBlocks => { + Err(BlockImportError::Block(BlockError::UnknownParent(_))) if self.state == SyncState::NewBlocks => { trace!(target: "sync", "Unknown new block parent, restarting sync"); break; }, From 8f6e48e4c3f1cf3e077dcebcab7eff06e1a90341 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 2 Jul 2016 20:08:52 +0300 Subject: [PATCH 8/8] fix tests --- ethcore/src/miner/transaction_queue.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index fde8b67d8..0e71b1d83 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -804,6 +804,7 @@ mod test { use error::{Error, TransactionError}; use super::*; use super::{TransactionSet, TransactionOrder, VerifiedTransaction}; + use client::TransactionImportResult; fn unwrap_tx_err(err: Result) -> TransactionError { match err.unwrap_err() {