openethereum/ethcore/src/error.rs

241 lines
6.9 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// 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 <http://www.gnu.org/licenses/>.
2016-01-10 14:05:39 +01:00
//! General error types for use in ethcore.
use util::*;
use header::BlockNumber;
2016-01-14 19:03:48 +01:00
use basic_types::LogBloom;
2016-01-10 14:05:39 +01:00
2016-01-11 17:37:22 +01:00
/// Result of executing the transaction.
#[derive(PartialEq, Debug)]
pub enum ExecutionError {
2016-01-15 13:07:44 +01:00
/// Returned when there gas paid for transaction execution is
/// lower than base gas required.
2016-02-15 16:01:52 +01:00
NotEnoughBaseGas {
2016-02-03 12:18:12 +01:00
/// Absolute minimum gas required.
2016-02-15 16:01:52 +01:00
required: U256,
2016-02-03 12:18:12 +01:00
/// Gas provided.
2016-01-19 17:02:01 +01:00
got: U256
},
2016-01-11 17:37:22 +01:00
/// Returned when block (gas_used + gas) > gas_limit.
2016-02-15 16:01:52 +01:00
///
2016-01-11 17:37:22 +01:00
/// If gas =< gas_limit, upstream may try to execute the transaction
/// in next block.
2016-02-15 16:01:52 +01:00
BlockGasLimitReached {
2016-02-03 12:18:12 +01:00
/// Gas limit of block for transaction.
2016-01-19 17:02:01 +01:00
gas_limit: U256,
2016-02-03 12:18:12 +01:00
/// Gas used in block prior to transaction.
2016-01-19 17:02:01 +01:00
gas_used: U256,
2016-02-03 12:18:12 +01:00
/// Amount of gas in block.
2016-02-15 16:01:52 +01:00
gas: U256
2016-01-19 17:02:01 +01:00
},
2016-01-11 17:37:22 +01:00
/// Returned when transaction nonce does not match state nonce.
2016-02-15 16:01:52 +01:00
InvalidNonce {
2016-02-03 12:18:12 +01:00
/// Nonce expected.
2016-01-19 17:02:01 +01:00
expected: U256,
2016-02-03 12:18:12 +01:00
/// Nonce found.
2016-01-19 17:02:01 +01:00
got: U256
},
2016-02-15 16:01:52 +01:00
/// Returned when cost of transaction (value + gas_price * gas) exceeds
2016-01-11 17:37:22 +01:00
/// current sender balance.
2016-02-15 16:01:52 +01:00
NotEnoughCash {
2016-02-03 12:18:12 +01:00
/// Minimum required balance.
2016-01-19 17:02:01 +01:00
required: U512,
2016-02-03 12:18:12 +01:00
/// Actual balance.
2016-01-19 17:02:01 +01:00
got: U512
},
2016-01-11 17:37:22 +01:00
/// Returned when internal evm error occurs.
Internal
}
#[derive(Debug)]
/// Errors concerning transaction processing.
pub enum TransactionError {
/// Transaction's gas price is below threshold.
InsufficientGasPrice {
/// Minimal expected gas price
minimal: U256,
/// Transaction gas price
got: U256,
},
/// Sender doesn't have enough funds to pay for this transaction
InsufficientBalance {
/// Senders balance
balance: U256,
/// Transaction cost
cost: U256,
},
2016-02-03 12:18:12 +01:00
/// Transaction's gas limit (aka gas) is invalid.
InvalidGasLimit(OutOfBounds<U256>),
}
2016-01-12 13:14:01 +01:00
#[derive(Debug, PartialEq, Eq)]
2016-02-03 12:18:12 +01:00
/// Errors concerning block processing.
2016-01-10 14:05:39 +01:00
pub enum BlockError {
2016-02-03 12:18:12 +01:00
/// Block has too many uncles.
TooManyUncles(OutOfBounds<usize>),
2016-02-03 12:18:12 +01:00
/// Extra data is of an invalid length.
2016-01-10 14:05:39 +01:00
ExtraDataOutOfBounds(OutOfBounds<usize>),
2016-02-03 12:18:12 +01:00
/// Seal is incorrect format.
2016-01-10 14:05:39 +01:00
InvalidSealArity(Mismatch<usize>),
2016-02-03 12:18:12 +01:00
/// Block has too much gas used.
TooMuchGasUsed(OutOfBounds<U256>),
2016-02-03 12:18:12 +01:00
/// Uncles hash in header is invalid.
InvalidUnclesHash(Mismatch<H256>),
2016-02-03 12:18:12 +01:00
/// An uncle is from a generation too old.
UncleTooOld(OutOfBounds<BlockNumber>),
2016-02-03 12:18:12 +01:00
/// An uncle is from the same generation as the block.
UncleIsBrother(OutOfBounds<BlockNumber>),
2016-02-03 12:18:12 +01:00
/// An uncle is already in the chain.
UncleInChain(H256),
2016-02-03 12:18:12 +01:00
/// An uncle has a parent not in the chain.
UncleParentNotInChain(H256),
2016-02-03 12:18:12 +01:00
/// State root header field is invalid.
2016-01-14 19:03:48 +01:00
InvalidStateRoot(Mismatch<H256>),
2016-02-03 12:18:12 +01:00
/// Gas used header field is invalid.
2016-01-14 19:03:48 +01:00
InvalidGasUsed(Mismatch<U256>),
2016-02-03 12:18:12 +01:00
/// Transactions root header field is invalid.
InvalidTransactionsRoot(Mismatch<H256>),
2016-02-03 12:18:12 +01:00
/// Difficulty is out of range; this can be used as an looser error prior to getting a definitive
/// value for difficulty. This error needs only provide bounds of which it is out.
DifficultyOutOfBounds(OutOfBounds<U256>),
/// Difficulty header field is invalid; this is a strong error used after getting a definitive
/// value for difficulty (which is provided).
InvalidDifficulty(Mismatch<U256>),
2016-02-03 12:18:12 +01:00
/// Seal element of type H256 (max_hash for Ethash, but could be something else for
/// other seal engines) is out of bounds.
MismatchedH256SealElement(Mismatch<H256>),
/// Proof-of-work aspect of seal, which we assume is a 256-bit value, is invalid.
InvalidProofOfWork(OutOfBounds<U256>),
/// Gas limit header field is invalid.
InvalidGasLimit(OutOfBounds<U256>),
2016-02-03 12:18:12 +01:00
/// Receipts trie root header field is invalid.
InvalidReceiptsRoot(Mismatch<H256>),
/// Timestamp header field is invalid.
InvalidTimestamp(OutOfBounds<u64>),
2016-02-03 12:18:12 +01:00
/// Log bloom header field is invalid.
2016-01-14 19:03:48 +01:00
InvalidLogBloom(Mismatch<LogBloom>),
2016-02-03 12:18:12 +01:00
/// Parent hash field of header is invalid; this is an invalid error indicating a logic flaw in the codebase.
/// TODO: remove and favour an assert!/panic!.
InvalidParentHash(Mismatch<H256>),
2016-02-03 12:18:12 +01:00
/// Number field of header is invalid.
InvalidNumber(Mismatch<BlockNumber>),
/// Block number isn't sensible.
RidiculousNumber(OutOfBounds<BlockNumber>),
2016-02-03 12:18:12 +01:00
/// Parent given is unknown.
UnknownParent(H256),
2016-02-03 12:18:12 +01:00
/// Uncle parent given is unknown.
UnknownUncleParent(H256),
2016-01-10 14:05:39 +01:00
}
2016-01-10 23:37:09 +01:00
#[derive(Debug)]
2016-02-02 12:12:32 +01:00
/// Import to the block queue result
2016-01-10 23:37:09 +01:00
pub enum ImportError {
/// Already in the block chain.
2016-01-10 23:37:09 +01:00
AlreadyInChain,
/// Already in the block queue.
2016-01-10 23:37:09 +01:00
AlreadyQueued,
/// Already marked as bad from a previous import (could mean parent is bad).
KnownBad,
2016-01-10 23:37:09 +01:00
}
2016-01-10 14:05:39 +01:00
#[derive(Debug)]
/// General error type which should be capable of representing all errors in ethcore.
pub enum Error {
2016-02-03 12:18:12 +01:00
/// Error concerning a utility.
2016-01-10 14:05:39 +01:00
Util(UtilError),
2016-02-03 12:18:12 +01:00
/// Error concerning block processing.
2016-01-10 14:05:39 +01:00
Block(BlockError),
2016-02-03 12:18:12 +01:00
/// Unknown engine given.
2016-01-10 14:05:39 +01:00
UnknownEngineName(String),
2016-02-03 12:18:12 +01:00
/// Error concerning EVM code execution.
2016-01-11 17:37:22 +01:00
Execution(ExecutionError),
2016-02-03 12:18:12 +01:00
/// Error concerning transaction processing.
Transaction(TransactionError),
/// Error concerning block import.
Import(ImportError),
/// PoW hash is invalid or out of date.
PowHashInvalid,
/// The value of the nonce or mishash is invalid.
PowInvalid,
}
/// Result of import block operation.
pub type ImportResult = Result<H256, Error>;
impl From<TransactionError> for Error {
fn from(err: TransactionError) -> Error {
Error::Transaction(err)
}
2016-01-10 14:05:39 +01:00
}
impl From<ImportError> for Error {
fn from(err: ImportError) -> Error {
Error::Import(err)
}
}
2016-01-10 14:05:39 +01:00
impl From<BlockError> for Error {
fn from(err: BlockError) -> Error {
Error::Block(err)
}
}
2016-01-11 17:37:22 +01:00
impl From<ExecutionError> for Error {
fn from(err: ExecutionError) -> Error {
Error::Execution(err)
}
}
2016-01-11 20:47:19 +01:00
impl From<CryptoError> for Error {
fn from(err: CryptoError) -> Error {
Error::Util(UtilError::Crypto(err))
}
}
impl From<DecoderError> for Error {
fn from(err: DecoderError) -> Error {
Error::Util(UtilError::Decoder(err))
}
}
2016-01-13 23:15:44 +01:00
impl From<UtilError> for Error {
fn from(err: UtilError) -> Error {
Error::Util(err)
}
}
impl From<IoError> for Error {
fn from(err: IoError) -> Error {
Error::Util(From::from(err))
}
}
2016-01-10 14:05:39 +01:00
// TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted.
/*#![feature(concat_idents)]
macro_rules! assimilate {
($name:ident) => (
impl From<concat_idents!($name, Error)> for Error {
fn from(err: concat_idents!($name, Error)) -> Error {
Error:: $name (err)
}
}
)
}
assimilate!(FromHex);
assimilate!(BaseData);*/