Moving all Client public API types to separate mod & binary serialization codegen for that mod (#1051)

* transaction moved

* trash remove

* ids move

* receipt

* tree-route

* blockchain info

* log_entry move

* trace filter moved

* executed & trace moved

* localized trace moved

* block status moved

* build scripts and codegen refs

* Cargo.lock update

* binary for blockstatus, blockchaininfo

* binary for trace

* trace filters binary ser

* binary for log entries & executed

* binary for receipt

* special case for u8 & transaction binary attribute

* resolved remaining issues & error binary serialization

* json-tests util import

* fix warnings

* ids attr

* add missing attributes

* Update build.rs
This commit is contained in:
Nikolay Volf 2016-05-16 19:33:32 +03:00 committed by Gav Wood
parent 9301963d98
commit 4e41cbca81
32 changed files with 503 additions and 192 deletions

3
Cargo.lock generated
View File

@ -240,6 +240,8 @@ dependencies = [
"env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"ethash 1.2.0", "ethash 1.2.0",
"ethcore-devtools 1.2.0", "ethcore-devtools 1.2.0",
"ethcore-ipc 1.2.0",
"ethcore-ipc-codegen 1.2.0",
"ethcore-util 1.2.0", "ethcore-util 1.2.0",
"ethjson 0.1.0", "ethjson 0.1.0",
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -248,6 +250,7 @@ dependencies = [
"num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.35 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.35 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View File

@ -5,6 +5,11 @@ license = "GPL-3.0"
name = "ethcore" name = "ethcore"
version = "1.2.0" version = "1.2.0"
authors = ["Ethcore <admin@ethcore.io>"] authors = ["Ethcore <admin@ethcore.io>"]
build = "build.rs"
[build-dependencies]
syntex = "*"
"ethcore-ipc-codegen" = { path = "../ipc/codegen" }
[dependencies] [dependencies]
log = "0.3" log = "0.3"
@ -23,6 +28,7 @@ lazy_static = "0.1"
ethcore-devtools = { path = "../devtools" } ethcore-devtools = { path = "../devtools" }
ethjson = { path = "../json" } ethjson = { path = "../json" }
bloomchain = "0.1" bloomchain = "0.1"
"ethcore-ipc" = { path = "../ipc/rpc" }
[features] [features]
jit = ["evmjit"] jit = ["evmjit"]

33
ethcore/build.rs Normal file
View File

@ -0,0 +1,33 @@
// 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/>.
extern crate syntex;
extern crate ethcore_ipc_codegen as codegen;
use std::env;
use std::path::Path;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
// serialization pass
{
let src = Path::new("src/types/mod.rs.in");
let dst = Path::new(&out_dir).join("types.rs");
let mut registry = syntex::Registry::new();
codegen::register(&mut registry);
registry.expand("", &src, &dst).unwrap();
}
}

View File

@ -27,7 +27,7 @@ use chainfilter::{ChainFilter, BloomIndex, FilterDataSource};
use blockchain::block_info::{BlockInfo, BlockLocation, BranchBecomingCanonChainData}; use blockchain::block_info::{BlockInfo, BlockLocation, BranchBecomingCanonChainData};
use blockchain::best_block::BestBlock; use blockchain::best_block::BestBlock;
use blockchain::bloom_indexer::BloomIndexer; use blockchain::bloom_indexer::BloomIndexer;
use blockchain::tree_route::TreeRoute; use types::tree_route::TreeRoute;
use blockchain::update::ExtrasUpdate; use blockchain::update::ExtrasUpdate;
use blockchain::{CacheSize, ImportRoute}; use blockchain::{CacheSize, ImportRoute};
use db::{Writable, Readable, Key, CacheUpdatePolicy}; use db::{Writable, Readable, Key, CacheUpdatePolicy};

View File

@ -21,7 +21,6 @@ mod best_block;
mod block_info; mod block_info;
mod bloom_indexer; mod bloom_indexer;
mod cache; mod cache;
mod tree_route;
mod update; mod update;
mod import_route; mod import_route;
#[cfg(test)] #[cfg(test)]
@ -29,5 +28,5 @@ mod generator;
pub use self::blockchain::{BlockProvider, BlockChain, BlockChainConfig}; pub use self::blockchain::{BlockProvider, BlockChain, BlockChainConfig};
pub use self::cache::CacheSize; pub use self::cache::CacheSize;
pub use self::tree_route::TreeRoute; pub use types::tree_route::TreeRoute;
pub use self::import_route::ImportRoute; pub use self::import_route::ImportRoute;

View File

@ -44,34 +44,8 @@ use receipt::LocalizedReceipt;
pub use blockchain::CacheSize as BlockChainCacheSize; pub use blockchain::CacheSize as BlockChainCacheSize;
use trace::{TraceDB, ImportRequest as TraceImportRequest, LocalizedTrace, Database as TraceDatabase}; use trace::{TraceDB, ImportRequest as TraceImportRequest, LocalizedTrace, Database as TraceDatabase};
use trace; use trace;
pub use types::blockchain_info::BlockChainInfo;
/// General block status pub use types::block_status::BlockStatus;
#[derive(Debug, Eq, PartialEq)]
pub enum BlockStatus {
/// Part of the blockchain.
InChain,
/// Queued for import.
Queued,
/// Known as bad.
Bad,
/// Unknown.
Unknown,
}
/// Information about the blockchain gathered together.
#[derive(Debug)]
pub struct BlockChainInfo {
/// Blockchain difficulty.
pub total_difficulty: U256,
/// Block queue difficulty.
pub pending_total_difficulty: U256,
/// Genesis block hash.
pub genesis_hash: H256,
/// Best blockchain block hash.
pub best_block_hash: H256,
/// Best blockchain block number.
pub best_block_number: BlockNumber
}
impl fmt::Display for BlockChainInfo { impl fmt::Display for BlockChainInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -18,13 +18,12 @@
mod client; mod client;
mod config; mod config;
mod ids;
mod test_client; mod test_client;
mod trace; mod trace;
pub use self::client::*; pub use self::client::*;
pub use self::config::{ClientConfig, BlockQueueConfig, BlockChainConfig, Switch}; pub use self::config::{ClientConfig, BlockQueueConfig, BlockChainConfig, Switch};
pub use self::ids::{BlockId, TransactionId, UncleId, TraceId}; pub use types::ids::*;
pub use self::test_client::{TestBlockChainClient, EachBlockWith}; pub use self::test_client::{TestBlockChainClient, EachBlockWith};
pub use self::trace::Filter as TraceFilter; pub use self::trace::Filter as TraceFilter;
pub use executive::{Executed, Executive, TransactOptions}; pub use executive::{Executed, Executive, TransactOptions};

View File

@ -20,49 +20,7 @@ use util::*;
use header::BlockNumber; use header::BlockNumber;
use basic_types::LogBloom; use basic_types::LogBloom;
/// Result of executing the transaction. pub use types::executed::ExecutionError;
#[derive(PartialEq, Debug)]
pub enum ExecutionError {
/// Returned when there gas paid for transaction execution is
/// lower than base gas required.
NotEnoughBaseGas {
/// Absolute minimum gas required.
required: U256,
/// Gas provided.
got: U256
},
/// Returned when block (gas_used + gas) > gas_limit.
///
/// If gas =< gas_limit, upstream may try to execute the transaction
/// in next block.
BlockGasLimitReached {
/// Gas limit of block for transaction.
gas_limit: U256,
/// Gas used in block prior to transaction.
gas_used: U256,
/// Amount of gas in block.
gas: U256
},
/// Returned when transaction nonce does not match state nonce.
InvalidNonce {
/// Nonce expected.
expected: U256,
/// Nonce found.
got: U256
},
/// Returned when cost of transaction (value + gas_price * gas) exceeds
/// current sender balance.
NotEnoughCash {
/// Minimum required balance.
required: U512,
/// Actual balance.
got: U512
},
/// Returned when internal evm error occurs.
Internal,
/// Returned when generic transaction occurs
TransactionMalformed(String),
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
/// Errors concerning transaction processing. /// Errors concerning transaction processing.

View File

@ -24,6 +24,8 @@ use substate::*;
use trace::{Trace, Tracer, NoopTracer, ExecutiveTracer}; use trace::{Trace, Tracer, NoopTracer, ExecutiveTracer};
use crossbeam; use crossbeam;
pub use types::executed::{Executed, ExecutionResult};
/// Max depth to avoid stack overflow (when it's reached we start a new thread with VM) /// Max depth to avoid stack overflow (when it's reached we start a new thread with VM)
/// TODO [todr] We probably need some more sophisticated calculations here (limit on my machine 132) /// TODO [todr] We probably need some more sophisticated calculations here (limit on my machine 132)
/// Maybe something like here: `https://github.com/ethereum/libethereum/blob/4db169b8504f2b87f7d5a481819cfb959fc65f6c/libethereum/ExtVM.cpp` /// Maybe something like here: `https://github.com/ethereum/libethereum/blob/4db169b8504f2b87f7d5a481819cfb959fc65f6c/libethereum/ExtVM.cpp`
@ -45,45 +47,6 @@ pub struct TransactOptions {
pub check_nonce: bool, pub check_nonce: bool,
} }
/// Transaction execution receipt.
#[derive(Debug, PartialEq, Clone)]
pub struct Executed {
/// Gas paid up front for execution of transaction.
pub gas: U256,
/// Gas used during execution of transaction.
pub gas_used: U256,
/// Gas refunded after the execution of transaction.
/// To get gas that was required up front, add `refunded` and `gas_used`.
pub refunded: U256,
/// Cumulative gas used in current block so far.
///
/// `cumulative_gas_used = gas_used(t0) + gas_used(t1) + ... gas_used(tn)`
///
/// where `tn` is current transaction.
pub cumulative_gas_used: U256,
/// Vector of logs generated by transaction.
pub logs: Vec<LogEntry>,
/// Addresses of contracts created during execution of transaction.
/// Ordered from earliest creation.
///
/// eg. sender creates contract A and A in constructor creates contract B
///
/// B creation ends first, and it will be the first element of the vector.
pub contracts_created: Vec<Address>,
/// Transaction output.
pub output: Bytes,
/// The trace of this transaction.
pub trace: Option<Trace>,
}
/// Transaction execution result.
pub type ExecutionResult = Result<Executed, ExecutionError>;
/// Transaction executor. /// Transaction executor.
pub struct Executive<'a> { pub struct Executive<'a> {
state: &'a mut State, state: &'a mut State,

View File

@ -85,6 +85,7 @@ extern crate num_cpus;
extern crate crossbeam; extern crate crossbeam;
extern crate ethjson; extern crate ethjson;
extern crate bloomchain; extern crate bloomchain;
#[macro_use] extern crate ethcore_ipc as ipc;
#[cfg(test)] extern crate ethcore_devtools as devtools; #[cfg(test)] extern crate ethcore_devtools as devtools;
#[cfg(feature = "jit" )] extern crate evmjit; #[cfg(feature = "jit" )] extern crate evmjit;
@ -98,12 +99,9 @@ pub mod ethereum;
pub mod filter; pub mod filter;
pub mod header; pub mod header;
pub mod service; pub mod service;
pub mod log_entry;
pub mod trace; pub mod trace;
pub mod spec; pub mod spec;
pub mod transaction;
pub mod views; pub mod views;
pub mod receipt;
pub mod pod_state; pub mod pod_state;
mod db; mod db;
@ -128,9 +126,12 @@ mod executive;
mod externalities; mod externalities;
mod verification; mod verification;
mod blockchain; mod blockchain;
mod types;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[cfg(test)] #[cfg(test)]
#[cfg(feature="json-tests")] #[cfg(feature="json-tests")]
mod json_tests; mod json_tests;
pub use types::*;

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Flat trace module
use trace::BlockTraces; use trace::BlockTraces;
use super::trace::{Trace, Action, Res}; use super::trace::{Trace, Action, Res};

View File

@ -21,20 +21,18 @@ mod bloom;
mod config; mod config;
mod db; mod db;
mod executive_tracer; mod executive_tracer;
mod filter; pub mod flat;
mod flat;
mod import; mod import;
mod localized;
mod noop_tracer; mod noop_tracer;
pub mod trace;
pub use types::trace_types::*;
pub use self::block::BlockTraces; pub use self::block::BlockTraces;
pub use self::config::{Config, Switch}; pub use self::config::{Config, Switch};
pub use self::db::TraceDB; pub use self::db::TraceDB;
pub use self::trace::Trace; pub use types::trace_types::trace::Trace;
pub use self::noop_tracer::NoopTracer; pub use self::noop_tracer::NoopTracer;
pub use self::executive_tracer::ExecutiveTracer; pub use self::executive_tracer::ExecutiveTracer;
pub use self::filter::{Filter, AddressesFilter}; pub use types::trace_types::filter::{Filter, AddressesFilter};
pub use self::import::ImportRequest; pub use self::import::ImportRequest;
pub use self::localized::LocalizedTrace; pub use self::localized::LocalizedTrace;
use util::{Bytes, Address, U256, H256}; use util::{Bytes, Address, U256, H256};

View File

@ -0,0 +1,33 @@
// 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/>.
//! Block status description module
use ipc::binary::BinaryConvertError;
use std::collections::VecDeque;
/// General block status
#[derive(Debug, Eq, PartialEq, Binary)]
pub enum BlockStatus {
/// Part of the blockchain.
InChain,
/// Queued for import.
Queued,
/// Known as bad.
Bad,
/// Unknown.
Unknown,
}

View File

@ -0,0 +1,38 @@
// 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/>.
//! Blockhain info type definition
use util::numbers::*;
use header::BlockNumber;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Information about the blockchain gathered together.
#[derive(Debug, Binary)]
pub struct BlockChainInfo {
/// Blockchain difficulty.
pub total_difficulty: U256,
/// Block queue difficulty.
pub pending_total_difficulty: U256,
/// Genesis block hash.
pub genesis_hash: H256,
/// Best blockchain block hash.
pub best_block_hash: H256,
/// Best blockchain block number.
pub best_block_number: BlockNumber
}

View File

@ -0,0 +1,109 @@
// 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/>.
//! Transaction execution format module.
use util::numbers::*;
use util::Bytes;
use trace::Trace;
use types::log_entry::LogEntry;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Transaction execution receipt.
#[derive(Debug, PartialEq, Clone, Binary)]
pub struct Executed {
/// Gas paid up front for execution of transaction.
pub gas: U256,
/// Gas used during execution of transaction.
pub gas_used: U256,
/// Gas refunded after the execution of transaction.
/// To get gas that was required up front, add `refunded` and `gas_used`.
pub refunded: U256,
/// Cumulative gas used in current block so far.
///
/// `cumulative_gas_used = gas_used(t0) + gas_used(t1) + ... gas_used(tn)`
///
/// where `tn` is current transaction.
pub cumulative_gas_used: U256,
/// Vector of logs generated by transaction.
pub logs: Vec<LogEntry>,
/// Addresses of contracts created during execution of transaction.
/// Ordered from earliest creation.
///
/// eg. sender creates contract A and A in constructor creates contract B
///
/// B creation ends first, and it will be the first element of the vector.
pub contracts_created: Vec<Address>,
/// Transaction output.
pub output: Bytes,
/// The trace of this transaction.
pub trace: Option<Trace>,
}
/// Result of executing the transaction.
#[derive(PartialEq, Debug, Binary)]
pub enum ExecutionError {
/// Returned when there gas paid for transaction execution is
/// lower than base gas required.
NotEnoughBaseGas {
/// Absolute minimum gas required.
required: U256,
/// Gas provided.
got: U256
},
/// Returned when block (gas_used + gas) > gas_limit.
///
/// If gas =< gas_limit, upstream may try to execute the transaction
/// in next block.
BlockGasLimitReached {
/// Gas limit of block for transaction.
gas_limit: U256,
/// Gas used in block prior to transaction.
gas_used: U256,
/// Amount of gas in block.
gas: U256
},
/// Returned when transaction nonce does not match state nonce.
InvalidNonce {
/// Nonce expected.
expected: U256,
/// Nonce found.
got: U256
},
/// Returned when cost of transaction (value + gas_price * gas) exceeds
/// current sender balance.
NotEnoughCash {
/// Minimum required balance.
required: U512,
/// Actual balance.
got: U512
},
/// Returned when internal evm error occurs.
Internal,
/// Returned when generic transaction occurs
TransactionMalformed(String),
}
/// Transaction execution result.
pub type ExecutionResult = Result<Executed, ExecutionError>;

View File

@ -18,10 +18,13 @@
use util::hash::H256; use util::hash::H256;
use header::BlockNumber; use header::BlockNumber;
use util::bytes::{FromRawBytes, FromBytesError, ToBytesWithMap, Populatable}; use ipc::binary::BinaryConvertError;
use ipc::binary::BinaryConvertable;
use std::mem;
use std::collections::VecDeque;
/// Uniquely identifies block. /// Uniquely identifies block.
#[derive(Debug, PartialEq, Clone, Hash, Eq)] #[derive(Debug, PartialEq, Clone, Hash, Eq, Binary)]
pub enum BlockId { pub enum BlockId {
/// Block's sha3. /// Block's sha3.
/// Querying by hash is always faster. /// Querying by hash is always faster.
@ -35,7 +38,7 @@ pub enum BlockId {
} }
/// Uniquely identifies transaction. /// Uniquely identifies transaction.
#[derive(Debug, PartialEq, Clone, Hash, Eq)] #[derive(Debug, PartialEq, Clone, Hash, Eq, Binary)]
pub enum TransactionId { pub enum TransactionId {
/// Transaction's sha3. /// Transaction's sha3.
Hash(H256), Hash(H256),
@ -60,7 +63,3 @@ pub struct UncleId (
/// Position in block. /// Position in block.
pub usize pub usize
); );
sized_binary_map!(TransactionId);
sized_binary_map!(UncleId);
sized_binary_map!(BlockId);

View File

@ -14,15 +14,23 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Block log. //! Log entry type definition.
use util::*; use util::numbers::*;
use std::ops::Deref;
use util::rlp::*;
use util::Bytes;
use util::HeapSizeOf;
use util::sha3::*;
use basic_types::LogBloom; use basic_types::LogBloom;
use header::BlockNumber; use header::BlockNumber;
use ethjson; use ethjson;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// A record of execution for a `LOG` operation. /// A record of execution for a `LOG` operation.
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq, Binary)]
pub struct LogEntry { pub struct LogEntry {
/// The address of the contract executing at the point of the `LOG` operation. /// The address of the contract executing at the point of the `LOG` operation.
pub address: Address, pub address: Address,
@ -77,7 +85,7 @@ impl From<ethjson::state::Log> for LogEntry {
} }
/// Log localized in a blockchain. /// Log localized in a blockchain.
#[derive(Default, Debug, PartialEq, Clone)] #[derive(Default, Debug, PartialEq, Clone, Binary)]
pub struct LocalizedLogEntry { pub struct LocalizedLogEntry {
/// Plain log entry. /// Plain log entry.
pub entry: LogEntry, pub entry: LogEntry,

20
ethcore/src/types/mod.rs Normal file
View File

@ -0,0 +1,20 @@
// 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/>.
//! Types used in the public api
#![allow(dead_code, unused_assignments, unused_variables)] // codegen issues
include!(concat!(env!("OUT_DIR"), "/types.rs"));

View File

@ -0,0 +1,25 @@
// 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/>.
pub mod transaction;
pub mod ids;
pub mod receipt;
pub mod tree_route;
pub mod blockchain_info;
pub mod log_entry;
pub mod trace_types;
pub mod executed;
pub mod block_status;

View File

@ -16,13 +16,18 @@
//! Receipt //! Receipt
use util::*; use util::numbers::*;
use util::rlp::*;
use util::HeapSizeOf;
use basic_types::LogBloom; use basic_types::LogBloom;
use header::BlockNumber; use header::BlockNumber;
use log_entry::{LogEntry, LocalizedLogEntry}; use log_entry::{LogEntry, LocalizedLogEntry};
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Information describing execution of a transaction. /// Information describing execution of a transaction.
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone, Binary)]
pub struct Receipt { pub struct Receipt {
/// The state root after executing the transaction. /// The state root after executing the transaction.
pub state_root: H256, pub state_root: H256,
@ -76,7 +81,7 @@ impl HeapSizeOf for Receipt {
} }
/// Receipt with additional info. /// Receipt with additional info.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
pub struct LocalizedReceipt { pub struct LocalizedReceipt {
/// Transaction hash. /// Transaction hash.
pub transaction_hash: H256, pub transaction_hash: H256,
@ -98,7 +103,7 @@ pub struct LocalizedReceipt {
#[test] #[test]
fn test_basic() { fn test_basic() {
let expected = FromHex::from_hex("f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); let expected = ::rustc_serialize::hex::FromHex::from_hex("f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let r = Receipt::new( let r = Receipt::new(
x!("2f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee"), x!("2f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee"),
x!(0x40cae), x!(0x40cae),

View File

@ -14,41 +14,49 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Trace filters type definitions
use std::ops::Range; use std::ops::Range;
use bloomchain::{Filter as BloomFilter, Bloom, Number}; use bloomchain::{Filter as BloomFilter, Bloom, Number};
use util::{Address, FixedHash}; use util::{Address, FixedHash};
use util::sha3::Hashable; use util::sha3::Hashable;
use basic_types::LogBloom; use basic_types::LogBloom;
use super::flat::FlatTrace; use trace::flat::FlatTrace;
use super::trace::Action; use types::trace_types::trace::Action;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Addresses filter. /// Addresses filter.
/// ///
/// Used to create bloom possibilities and match filters. /// Used to create bloom possibilities and match filters.
pub struct AddressesFilter(Vec<Address>); #[derive(Binary)]
pub struct AddressesFilter {
list: Vec<Address>
}
impl From<Vec<Address>> for AddressesFilter { impl From<Vec<Address>> for AddressesFilter {
fn from(addresses: Vec<Address>) -> Self { fn from(addresses: Vec<Address>) -> Self {
AddressesFilter(addresses) AddressesFilter { list: addresses }
} }
} }
impl AddressesFilter { impl AddressesFilter {
/// Returns true if address matches one of the searched addresses. /// Returns true if address matches one of the searched addresses.
pub fn matches(&self, address: &Address) -> bool { pub fn matches(&self, address: &Address) -> bool {
self.matches_all() || self.0.contains(address) self.matches_all() || self.list.contains(address)
} }
/// Returns true if this address filter matches everything. /// Returns true if this address filter matches everything.
pub fn matches_all(&self) -> bool { pub fn matches_all(&self) -> bool {
self.0.is_empty() self.list.is_empty()
} }
/// Returns blooms of this addresses filter. /// Returns blooms of this addresses filter.
pub fn blooms(&self) -> Vec<LogBloom> { pub fn blooms(&self) -> Vec<LogBloom> {
match self.0.is_empty() { match self.list.is_empty() {
true => vec![LogBloom::new()], true => vec![LogBloom::new()],
false => self.0.iter() false => self.list.iter()
.map(|address| LogBloom::from_bloomed(&address.sha3())) .map(|address| LogBloom::from_bloomed(&address.sha3()))
.collect() .collect()
} }
@ -56,11 +64,11 @@ impl AddressesFilter {
/// Returns vector of blooms zipped with blooms of this addresses filter. /// Returns vector of blooms zipped with blooms of this addresses filter.
pub fn with_blooms(&self, blooms: Vec<LogBloom>) -> Vec<LogBloom> { pub fn with_blooms(&self, blooms: Vec<LogBloom>) -> Vec<LogBloom> {
match self.0.is_empty() { match self.list.is_empty() {
true => blooms, true => blooms,
false => blooms false => blooms
.into_iter() .into_iter()
.flat_map(|bloom| self.0.iter() .flat_map(|bloom| self.list.iter()
.map(|address| bloom.with_bloomed(&address.sha3())) .map(|address| bloom.with_bloomed(&address.sha3()))
.collect::<Vec<_>>()) .collect::<Vec<_>>())
.collect() .collect()
@ -68,6 +76,7 @@ impl AddressesFilter {
} }
} }
#[derive(Binary)]
/// Traces filter. /// Traces filter.
pub struct Filter { pub struct Filter {
/// Block range. /// Block range.

View File

@ -14,12 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Localized traces type definitions
use util::H256; use util::H256;
use super::trace::{Action, Res}; use super::trace::{Action, Res};
use header::BlockNumber; use header::BlockNumber;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Localized trace. /// Localized trace.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Binary)]
pub struct LocalizedTrace { pub struct LocalizedTrace {
/// Type of action performed by a transaction. /// Type of action performed by a transaction.
pub action: Action, pub action: Action,

View File

@ -0,0 +1,21 @@
// 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/>.
//! Types used in the public api
pub mod filter;
pub mod trace;
pub mod localized;

View File

@ -15,14 +15,18 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Tracing datatypes. //! Tracing datatypes.
use util::{U256, Bytes, Address, FixedHash}; use util::{U256, Bytes, Address, FixedHash};
use util::rlp::*; use util::rlp::*;
use util::sha3::Hashable; use util::sha3::Hashable;
use action_params::ActionParams; use action_params::ActionParams;
use basic_types::LogBloom; use basic_types::LogBloom;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// `Call` result. /// `Call` result.
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default, Binary)]
pub struct CallResult { pub struct CallResult {
/// Gas used by call. /// Gas used by call.
pub gas_used: U256, pub gas_used: U256,
@ -51,7 +55,7 @@ impl Decodable for CallResult {
} }
/// `Create` result. /// `Create` result.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
pub struct CreateResult { pub struct CreateResult {
/// Gas used by create. /// Gas used by create.
pub gas_used: U256, pub gas_used: U256,
@ -84,7 +88,7 @@ impl Decodable for CreateResult {
} }
/// Description of a _call_ action, either a `CALL` operation or a message transction. /// Description of a _call_ action, either a `CALL` operation or a message transction.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
pub struct Call { pub struct Call {
/// The sending account. /// The sending account.
pub from: Address, pub from: Address,
@ -146,7 +150,7 @@ impl Call {
} }
/// Description of a _create_ action, either a `CREATE` operation or a create transction. /// Description of a _create_ action, either a `CREATE` operation or a create transction.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
pub struct Create { pub struct Create {
/// The address of the creator. /// The address of the creator.
pub from: Address, pub from: Address,
@ -202,7 +206,7 @@ impl Create {
} }
/// Description of an action that we trace; will be either a call or a create. /// Description of an action that we trace; will be either a call or a create.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
pub enum Action { pub enum Action {
/// It's a call action. /// It's a call action.
Call(Call), Call(Call),
@ -249,7 +253,7 @@ impl Action {
} }
/// The result of the performed action. /// The result of the performed action.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
pub enum Res { pub enum Res {
/// Successful call action result. /// Successful call action result.
Call(CallResult), Call(CallResult),
@ -300,7 +304,7 @@ impl Decodable for Res {
} }
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Binary)]
/// A trace; includes a description of the action being traced and sub traces of each interior action. /// A trace; includes a description of the action being traced and sub traces of each interior action.
pub struct Trace { pub struct Trace {
/// The number of EVM execution environments active when this action happened; 0 if it's /// The number of EVM execution environments active when this action happened; 0 if it's

View File

@ -16,13 +16,21 @@
//! Transaction data structure. //! Transaction data structure.
use util::*; use util::numbers::*;
use std::ops::Deref;
use util::rlp::*;
use util::sha3::*;
use util::{UtilError, CryptoError, Bytes, Signature, Secret, ec};
use std::cell::*;
use error::*; use error::*;
use evm::Schedule; use evm::Schedule;
use header::BlockNumber; use header::BlockNumber;
use ethjson; use ethjson;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, Binary)]
/// Transaction action type. /// Transaction action type.
pub enum Action { pub enum Action {
/// Create creates new contract. /// Create creates new contract.
@ -48,7 +56,7 @@ impl Decodable for Action {
/// A set of information describing an externally-originating message call /// A set of information describing an externally-originating message call
/// or contract creation operation. /// or contract creation operation.
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq, Binary)]
pub struct Transaction { pub struct Transaction {
/// Nonce. /// Nonce.
pub nonce: U256, pub nonce: U256,
@ -183,7 +191,7 @@ impl Transaction {
} }
/// Signed transaction information. /// Signed transaction information.
#[derive(Debug, Clone, Eq)] #[derive(Debug, Clone, Eq, Binary)]
pub struct SignedTransaction { pub struct SignedTransaction {
/// Plain Transaction. /// Plain Transaction.
unsigned: Transaction, unsigned: Transaction,
@ -310,7 +318,7 @@ impl SignedTransaction {
} }
try!(self.sender()); try!(self.sender());
if self.gas < U256::from(self.gas_required(&schedule)) { if self.gas < U256::from(self.gas_required(&schedule)) {
Err(From::from(TransactionError::InvalidGasLimit(OutOfBounds{min: Some(U256::from(self.gas_required(&schedule))), max: None, found: self.gas}))) Err(From::from(TransactionError::InvalidGasLimit(::util::OutOfBounds{min: Some(U256::from(self.gas_required(&schedule))), max: None, found: self.gas})))
} else { } else {
Ok(self) Ok(self)
} }
@ -318,7 +326,7 @@ impl SignedTransaction {
} }
/// Signed Transaction that is a part of canon blockchain. /// Signed Transaction that is a part of canon blockchain.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq, Binary)]
pub struct LocalizedTransaction { pub struct LocalizedTransaction {
/// Signed part. /// Signed part.
pub signed: SignedTransaction, pub signed: SignedTransaction,
@ -340,7 +348,7 @@ impl Deref for LocalizedTransaction {
#[test] #[test]
fn sender_test() { fn sender_test() {
let t: SignedTransaction = decode(&FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap()); let t: SignedTransaction = decode(&::rustc_serialize::hex::FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap());
assert_eq!(t.data, b""); assert_eq!(t.data, b"");
assert_eq!(t.gas, U256::from(0x5208u64)); assert_eq!(t.gas, U256::from(0x5208u64));
assert_eq!(t.gas_price, U256::from(0x01u64)); assert_eq!(t.gas_price, U256::from(0x01u64));
@ -354,7 +362,7 @@ fn sender_test() {
#[test] #[test]
fn signing() { fn signing() {
let key = KeyPair::create().unwrap(); let key = ::util::crypto::KeyPair::create().unwrap();
let t = Transaction { let t = Transaction {
action: Action::Create, action: Action::Create,
nonce: U256::from(42), nonce: U256::from(42),

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Tree route info type definition
use util::numbers::H256; use util::numbers::H256;
/// Represents a tree route between `from` block and `to` block: /// Represents a tree route between `from` block and `to` block:

View File

@ -175,6 +175,11 @@ fn binary_expr_struct(
) -> Result<BinaryExpressions, Error> { ) -> Result<BinaryExpressions, Error> {
let size_exprs: Vec<P<ast::Expr>> = fields.iter().enumerate().map(|(index, field)| { let size_exprs: Vec<P<ast::Expr>> = fields.iter().enumerate().map(|(index, field)| {
if ::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty)) == "u8" {
return quote_expr!(cx, 1);
}
let field_type_ident = builder.id( let field_type_ident = builder.id(
&::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty))); &::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty)));
@ -228,23 +233,34 @@ fn binary_expr_struct(
}, },
}; };
write_stmts.push(quote_stmt!(cx, let next_line = offset + match $field_type_ident_qualified::len_params() { if ::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty)) == "u8" {
0 => mem::size_of::<$field_type_ident>(), write_stmts.push(quote_stmt!(cx, let next_line = offset + 1;).unwrap());
_ => { let size = $member_expr .size(); length_stack.push_back(size); size }, write_stmts.push(quote_stmt!(cx, buffer[offset] = $member_expr; ).unwrap());
}).unwrap()); }
else {
write_stmts.push(quote_stmt!(cx, write_stmts.push(quote_stmt!(cx, let next_line = offset + match $field_type_ident_qualified::len_params() {
if let Err(e) = $member_expr .to_bytes(&mut buffer[offset..next_line], length_stack) { return Err(e) };).unwrap()); 0 => mem::size_of::<$field_type_ident>(),
_ => { let size = $member_expr .size(); length_stack.push_back(size); size },
}).unwrap());
write_stmts.push(quote_stmt!(cx,
if let Err(e) = $member_expr .to_bytes(&mut buffer[offset..next_line], length_stack) { return Err(e) };).unwrap());
}
write_stmts.push(quote_stmt!(cx, offset = next_line; ).unwrap()); write_stmts.push(quote_stmt!(cx, offset = next_line; ).unwrap());
let field_index = builder.id(&format!("{}", index)); let field_index = builder.id(&format!("{}", index));
map_stmts.push(quote_stmt!(cx, map[$field_index] = total;).unwrap()); map_stmts.push(quote_stmt!(cx, map[$field_index] = total;).unwrap());
map_stmts.push(quote_stmt!(cx, let size = match $field_type_ident_qualified::len_params() {
0 => mem::size_of::<$field_type_ident>(), if ::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty)) == "u8" {
_ => length_stack.pop_front().unwrap(), map_stmts.push(quote_stmt!(cx, total = total + 1;).unwrap());
}).unwrap()); }
map_stmts.push(quote_stmt!(cx, total = total + size;).unwrap()); else {
map_stmts.push(quote_stmt!(cx, let size = match $field_type_ident_qualified::len_params() {
0 => mem::size_of::<$field_type_ident>(),
_ => length_stack.pop_front().unwrap(),
}).unwrap());
map_stmts.push(quote_stmt!(cx, total = total + size;).unwrap());
}
}; };
let read_expr = match fields.iter().any(|f| codegen::has_ptr(&f.ty)) { let read_expr = match fields.iter().any(|f| codegen::has_ptr(&f.ty)) {
@ -366,6 +382,8 @@ fn fields_sequence(
use syntax::parse::token; use syntax::parse::token;
use syntax::ast::TokenTree::Token; use syntax::ast::TokenTree::Token;
let named_members = fields.iter().any(|f| f.ident.is_some());
::quasi::parse_expr_panic(&mut ::syntax::parse::new_parser_from_tts( ::quasi::parse_expr_panic(&mut ::syntax::parse::new_parser_from_tts(
ext_cx.parse_sess(), ext_cx.parse_sess(),
ext_cx.cfg(), ext_cx.cfg(),
@ -373,7 +391,12 @@ fn fields_sequence(
let _sp = ext_cx.call_site(); let _sp = ext_cx.call_site();
let mut tt = ::std::vec::Vec::new(); let mut tt = ::std::vec::Vec::new();
tt.push(Token(_sp, token::Ident(variant_ident.clone()))); tt.push(Token(_sp, token::Ident(variant_ident.clone())));
tt.push(Token(_sp, token::OpenDelim(token::Paren))); if named_members {
tt.push(Token(_sp, token::OpenDelim(token::Brace)));
}
else {
tt.push(Token(_sp, token::OpenDelim(token::Paren)));
}
for (idx, field) in fields.iter().enumerate() { for (idx, field) in fields.iter().enumerate() {
if field.ident.is_some() { if field.ident.is_some() {
@ -381,6 +404,21 @@ fn fields_sequence(
tt.push(Token(_sp, token::Colon)); tt.push(Token(_sp, token::Colon));
} }
// special case for u8, it just takes byte form sequence
if ::syntax::print::pprust::ty_to_string(&field.ty) == "u8" {
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("buffer"))));
tt.push(Token(_sp, token::OpenDelim(token::Bracket)));
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("map"))));
tt.push(Token(_sp, token::OpenDelim(token::Bracket)));
tt.push(Token(_sp, token::Ident(ext_cx.ident_of(&format!("{}", idx)))));
tt.push(Token(_sp, token::CloseDelim(token::Bracket)));
tt.push(Token(_sp, token::CloseDelim(token::Bracket)));
tt.push(Token(_sp, token::Comma));
continue;
}
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("try!")))); tt.push(Token(_sp, token::Ident(ext_cx.ident_of("try!"))));
tt.push(Token(_sp, token::OpenDelim(token::Paren))); tt.push(Token(_sp, token::OpenDelim(token::Paren)));
tt.push( tt.push(
@ -393,6 +431,7 @@ fn fields_sequence(
tt.push(Token(_sp, token::OpenDelim(token::Paren))); tt.push(Token(_sp, token::OpenDelim(token::Paren)));
tt.push(Token(_sp, token::BinOp(token::And))); tt.push(Token(_sp, token::BinOp(token::And)));
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("buffer")))); tt.push(Token(_sp, token::Ident(ext_cx.ident_of("buffer"))));
tt.push(Token(_sp, token::OpenDelim(token::Bracket))); tt.push(Token(_sp, token::OpenDelim(token::Bracket)));
@ -418,8 +457,12 @@ fn fields_sequence(
tt.push(Token(_sp, token::CloseDelim(token::Paren))); tt.push(Token(_sp, token::CloseDelim(token::Paren)));
tt.push(Token(_sp, token::Comma)); tt.push(Token(_sp, token::Comma));
} }
tt.push(Token(_sp, token::CloseDelim(token::Paren))); if named_members {
tt.push(Token(_sp, token::CloseDelim(token::Brace)));
}
else {
tt.push(Token(_sp, token::CloseDelim(token::Paren)));
}
tt tt
}) })
).unwrap() ).unwrap()
@ -455,6 +498,21 @@ fn named_fields_sequence(
tt.push(Token(_sp, token::Ident(field.ident.clone().unwrap()))); tt.push(Token(_sp, token::Ident(field.ident.clone().unwrap())));
tt.push(Token(_sp, token::Colon)); tt.push(Token(_sp, token::Colon));
// special case for u8, it just takes byte form sequence
if ::syntax::print::pprust::ty_to_string(&field.ty) == "u8" {
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("buffer"))));
tt.push(Token(_sp, token::OpenDelim(token::Bracket)));
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("map"))));
tt.push(Token(_sp, token::OpenDelim(token::Bracket)));
tt.push(Token(_sp, token::Ident(ext_cx.ident_of(&format!("{}", idx)))));
tt.push(Token(_sp, token::CloseDelim(token::Bracket)));
tt.push(Token(_sp, token::CloseDelim(token::Bracket)));
tt.push(Token(_sp, token::Comma));
continue;
}
tt.push(Token(_sp, token::Ident(ext_cx.ident_of("try!")))); tt.push(Token(_sp, token::Ident(ext_cx.ident_of("try!"))));
tt.push(Token(_sp, token::OpenDelim(token::Paren))); tt.push(Token(_sp, token::OpenDelim(token::Paren)));
tt.push(Token( tt.push(Token(
@ -573,7 +631,6 @@ fn binary_expr_variant(
.map(|(id, field)|(field.ident.unwrap(), builder.pat().ref_id(id)))) .map(|(id, field)|(field.ident.unwrap(), builder.pat().ref_id(id))))
.build(); .build();
let binary_expr = try!(binary_expr_struct( let binary_expr = try!(binary_expr_struct(
cx, cx,
&builder, &builder,
@ -593,7 +650,7 @@ fn binary_expr_variant(
let buffer = &mut buffer[1..]; let buffer = &mut buffer[1..];
$write_expr $write_expr
}), }),
read: quote_arm!(cx, $pat => { $read_expr } ), read: quote_arm!(cx, $variant_index_ident => { $read_expr } ),
}) })
}, },
} }

View File

@ -17,9 +17,10 @@
//! Binary representation of types //! Binary representation of types
use util::bytes::Populatable; use util::bytes::Populatable;
use util::numbers::{U256, H256, H2048, Address}; use util::numbers::{U256, U512, H256, H2048, Address};
use std::mem; use std::mem;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::ops::Range;
#[derive(Debug)] #[derive(Debug)]
pub struct BinaryConvertError; pub struct BinaryConvertError;
@ -232,6 +233,29 @@ impl<T> BinaryConvertable for ::std::cell::RefCell<T> where T: BinaryConvertable
} }
} }
impl<T> BinaryConvertable for ::std::cell::Cell<T> where T: BinaryConvertable + Copy {
fn size(&self) -> usize {
self.get().size()
}
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
Ok(::std::cell::Cell::new(try!(T::from_empty_bytes())))
}
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
Ok(::std::cell::Cell::new(try!(T::from_bytes(buffer, length_stack))))
}
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
try!(self.get().to_bytes(buffer, length_stack));
Ok(())
}
fn len_params() -> usize {
T::len_params()
}
}
impl BinaryConvertable for Vec<u8> { impl BinaryConvertable for Vec<u8> {
fn size(&self) -> usize { fn size(&self) -> usize {
self.len() self.len()
@ -365,8 +389,9 @@ pub fn serialize<T: BinaryConvertable>(t: &T) -> Result<Vec<u8>, BinaryConvertEr
Ok(buff.into_inner()) Ok(buff.into_inner())
} }
#[macro_export]
macro_rules! binary_fixed_size { macro_rules! binary_fixed_size {
($target_ty: ident) => { ($target_ty: ty) => {
impl BinaryConvertable for $target_ty { impl BinaryConvertable for $target_ty {
fn from_bytes(bytes: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> { fn from_bytes(bytes: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) { match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
@ -398,9 +423,12 @@ binary_fixed_size!(usize);
binary_fixed_size!(i32); binary_fixed_size!(i32);
binary_fixed_size!(bool); binary_fixed_size!(bool);
binary_fixed_size!(U256); binary_fixed_size!(U256);
binary_fixed_size!(U512);
binary_fixed_size!(H256); binary_fixed_size!(H256);
binary_fixed_size!(H2048); binary_fixed_size!(H2048);
binary_fixed_size!(Address); binary_fixed_size!(Address);
binary_fixed_size!(Range<usize>);
binary_fixed_size!(Range<u64>);
#[test] #[test]
fn vec_serialize() { fn vec_serialize() {

View File

@ -36,3 +36,9 @@ pub struct DoubleRoot {
pub struct ReferenceStruct<'a> { pub struct ReferenceStruct<'a> {
pub ref_data: &'a u64, pub ref_data: &'a u64,
} }
#[derive(Binary, PartialEq, Debug)]
pub enum EnumWithStruct {
Left,
Right { how_much: u64 },
}

View File

@ -16,9 +16,7 @@
#![allow(dead_code)] #![allow(dead_code)]
extern crate bincode;
extern crate ethcore_ipc as ipc; extern crate ethcore_ipc as ipc;
extern crate serde;
extern crate ethcore_devtools as devtools; extern crate ethcore_devtools as devtools;
extern crate semver; extern crate semver;
extern crate nanomsg; extern crate nanomsg;

View File

@ -517,7 +517,7 @@ pub trait Uint: Sized + Default + FromStr + From<u64> + fmt::Debug + fmt::Displa
/// Return single byte /// Return single byte
fn byte(&self, index: usize) -> u8; fn byte(&self, index: usize) -> u8;
/// Get this Uint as slice of bytes /// Get this Uint as slice of bytes
fn to_bytes(&self, bytes: &mut[u8]); fn to_raw_bytes(&self, bytes: &mut[u8]);
/// Create `Uint(10**n)` /// Create `Uint(10**n)`
fn exp10(n: usize) -> Self; fn exp10(n: usize) -> Self;
@ -621,7 +621,7 @@ macro_rules! construct_uint {
(arr[index / 8] >> (((index % 8)) * 8)) as u8 (arr[index / 8] >> (((index % 8)) * 8)) as u8
} }
fn to_bytes(&self, bytes: &mut[u8]) { fn to_raw_bytes(&self, bytes: &mut[u8]) {
assert!($n_words * 8 == bytes.len()); assert!($n_words * 8 == bytes.len());
let &$name(ref arr) = self; let &$name(ref arr) = self;
for i in 0..bytes.len() { for i in 0..bytes.len() {
@ -780,7 +780,7 @@ macro_rules! construct_uint {
where S: serde::Serializer { where S: serde::Serializer {
let mut hex = "0x".to_owned(); let mut hex = "0x".to_owned();
let mut bytes = [0u8; 8 * $n_words]; let mut bytes = [0u8; 8 * $n_words];
self.to_bytes(&mut bytes); self.to_raw_bytes(&mut bytes);
let len = cmp::max((self.bits() + 7) / 8, 1); let len = cmp::max((self.bits() + 7) / 8, 1);
hex.push_str(bytes[bytes.len() - len..].to_hex().as_ref()); hex.push_str(bytes[bytes.len() - len..].to_hex().as_ref());
serializer.serialize_str(hex.as_ref()) serializer.serialize_str(hex.as_ref())
@ -1482,7 +1482,7 @@ mod tests {
let hex = "8090a0b0c0d0e0f00910203040506077583a2cf8264910e1436bda32571012f0"; let hex = "8090a0b0c0d0e0f00910203040506077583a2cf8264910e1436bda32571012f0";
let uint = U256::from_str(hex).unwrap(); let uint = U256::from_str(hex).unwrap();
let mut bytes = [0u8; 32]; let mut bytes = [0u8; 32];
uint.to_bytes(&mut bytes); uint.to_raw_bytes(&mut bytes);
let uint2 = U256::from(&bytes[..]); let uint2 = U256::from(&bytes[..]);
assert_eq!(uint, uint2); assert_eq!(uint, uint2);
} }

View File

@ -517,7 +517,7 @@ impl From<U256> for H256 {
fn from(value: U256) -> H256 { fn from(value: U256) -> H256 {
unsafe { unsafe {
let mut ret: H256 = ::std::mem::uninitialized(); let mut ret: H256 = ::std::mem::uninitialized();
value.to_bytes(&mut ret); value.to_raw_bytes(&mut ret);
ret ret
} }
} }
@ -527,7 +527,7 @@ impl<'_> From<&'_ U256> for H256 {
fn from(value: &'_ U256) -> H256 { fn from(value: &'_ U256) -> H256 {
unsafe { unsafe {
let mut ret: H256 = ::std::mem::uninitialized(); let mut ret: H256 = ::std::mem::uninitialized();
value.to_bytes(&mut ret); value.to_raw_bytes(&mut ret);
ret ret
} }
} }