Skip to content

Commit

Permalink
rlp serialization refactor (openethereum#4873)
Browse files Browse the repository at this point in the history
* fixed naming of rlp modules

* RlpStream cleanup

* appending short rlp lists (0...55 bytes) is 25% faster

* RlpStream does not use bytes module, nor trait Stream

* removed unused code from rlp module

* compiling ethcore-util with new rlp serialization

* compiling parity with new rlp serialization

* fixed compiling ethcore-light with new rlp serialization

* fixed compiling ethsync with new rlp serialization

* removed redundant comment, print

* removed redundant double-space

* replace usage of WriteBytesExt with ByteOrder
  • Loading branch information
debris authored and gavofyork committed Mar 20, 2017
1 parent 16860c3 commit a555686
Show file tree
Hide file tree
Showing 59 changed files with 620 additions and 767 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ethcore/light/src/cht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use ethcore::ids::BlockId;
use util::{Bytes, H256, U256, HashDB, MemoryDB};
use util::trie::{self, TrieMut, TrieDBMut, Trie, TrieDB, Recorder};
use rlp::{Stream, RlpStream, UntrustedRlp, View};
use rlp::{RlpStream, UntrustedRlp, View};

// encode a key.
macro_rules! key {
Expand Down
2 changes: 1 addition & 1 deletion ethcore/light/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ethcore::receipt::Receipt;

use io::TimerToken;
use network::{NetworkProtocolHandler, NetworkContext, PeerId};
use rlp::{RlpStream, Stream, UntrustedRlp, View};
use rlp::{RlpStream, UntrustedRlp, View};
use util::hash::H256;
use util::{Bytes, DBValue, Mutex, RwLock, U256};
use time::{Duration, SteadyTime};
Expand Down
2 changes: 1 addition & 1 deletion ethcore/light/src/net/request_credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Default for CostTable {
}
}

impl RlpEncodable for CostTable {
impl Encodable for CostTable {
fn rlp_append(&self, s: &mut RlpStream) {
fn append_cost(s: &mut RlpStream, msg_id: u8, cost: &Cost) {
s.begin_list(3)
Expand Down
6 changes: 3 additions & 3 deletions ethcore/light/src/net/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Peer status and capabilities.
use rlp::{DecoderError, RlpDecodable, RlpEncodable, RlpStream, Stream, UntrustedRlp, View};
use rlp::{DecoderError, RlpDecodable, Encodable, RlpStream, UntrustedRlp, View};
use util::{H256, U256};

use super::request_credits::FlowParams;
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'a> Parser<'a> {
}

// Helper for encoding a key-value pair
fn encode_pair<T: RlpEncodable>(key: Key, val: &T) -> Vec<u8> {
fn encode_pair<T: Encodable>(key: Key, val: &T) -> Vec<u8> {
let mut s = RlpStream::new_list(2);
s.append(&key.as_str()).append(val);
s.out()
Expand Down Expand Up @@ -374,7 +374,7 @@ mod tests {
use super::*;
use super::super::request_credits::FlowParams;
use util::{U256, H256};
use rlp::{RlpStream, Stream ,UntrustedRlp, View};
use rlp::{RlpStream, UntrustedRlp, View};

#[test]
fn full_handshake() {
Expand Down
2 changes: 1 addition & 1 deletion ethcore/light/src/on_demand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use ethcore::executed::{Executed, ExecutionError};
use futures::{Async, Poll, Future};
use futures::sync::oneshot::{self, Sender, Receiver};
use network::PeerId;
use rlp::{RlpStream, Stream};
use rlp::RlpStream;
use util::{Bytes, DBValue, RwLock, Mutex, U256};
use util::sha3::{SHA3_NULL_RLP, SHA3_EMPTY_LIST_RLP};

Expand Down
6 changes: 3 additions & 3 deletions ethcore/light/src/on_demand/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use ethcore::receipt::Receipt;
use ethcore::state::{self, ProvedExecution};
use ethcore::transaction::SignedTransaction;

use rlp::{RlpStream, Stream, UntrustedRlp, View};
use rlp::{RlpStream, UntrustedRlp, View};
use util::{Address, Bytes, DBValue, HashDB, H256, U256};
use util::memorydb::MemoryDB;
use util::sha3::Hashable;
Expand Down Expand Up @@ -323,7 +323,7 @@ mod tests {

#[test]
fn check_body() {
use rlp::{RlpStream, Stream};
use rlp::RlpStream;

let header = Header::new();
let mut body_stream = RlpStream::new_list(2);
Expand Down Expand Up @@ -360,7 +360,7 @@ mod tests {

#[test]
fn check_state_proof() {
use rlp::{RlpStream, Stream};
use rlp::RlpStream;

let mut root = H256::default();
let mut db = MemoryDB::new();
Expand Down
4 changes: 2 additions & 2 deletions ethcore/light/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub trait Provider: Send + Sync {
///
/// Returns a vector of RLP-encoded lists satisfying the requests.
fn proofs(&self, req: request::StateProofs) -> Vec<Bytes> {
use rlp::{RlpStream, Stream};
use rlp::RlpStream;

let mut results = Vec::with_capacity(req.requests.len());

Expand Down Expand Up @@ -166,7 +166,7 @@ pub trait Provider: Send + Sync {
/// The first element is a block header and the second a merkle proof of
/// the header in a requested CHT.
fn header_proofs(&self, req: request::HeaderProofs) -> Vec<Bytes> {
use rlp::{self, RlpStream, Stream};
use rlp::{self, RlpStream};

req.requests.into_iter()
.map(|req| self.header_proof(req))
Expand Down
8 changes: 4 additions & 4 deletions ethcore/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::cmp;
use std::sync::Arc;
use std::collections::HashSet;

use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, Decoder, DecoderError, View, Stream};
use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, Decoder, DecoderError, View};
use util::{Bytes, Address, Uint, Hashable, U256, H256, ordered_trie_root, SHA3_NULL_RLP};
use util::error::{Mismatch, OutOfBounds};

Expand Down Expand Up @@ -59,8 +59,8 @@ impl Block {
pub fn rlp_bytes(&self, seal: Seal) -> Bytes {
let mut block_rlp = RlpStream::new_list(3);
self.header.stream_rlp(&mut block_rlp, seal);
block_rlp.append(&self.transactions);
block_rlp.append(&self.uncles);
block_rlp.append_list(&self.transactions);
block_rlp.append_list(&self.uncles);
block_rlp.out()
}
}
Expand Down Expand Up @@ -507,7 +507,7 @@ impl SealedBlock {
pub fn rlp_bytes(&self) -> Bytes {
let mut block_rlp = RlpStream::new_list(3);
self.block.header.stream_rlp(&mut block_rlp, Seal::With);
block_rlp.append(&self.block.transactions);
block_rlp.append_list(&self.block.transactions);
block_rlp.append_raw(&self.uncle_bytes, 1);
block_rlp.out()
}
Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/blockchain/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Encodable for BlockDetails {
s.append(&self.number);
s.append(&self.total_difficulty);
s.append(&self.parent);
s.append(&self.children);
s.append_list(&self.children);
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ impl Decodable for BlockReceipts {

impl Encodable for BlockReceipts {
fn rlp_append(&self, s: &mut RlpStream) {
Encodable::rlp_append(&self.receipts, s);
s.append_list(&self.receipts);
}
}

Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/blockchain/generator/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl Encodable for Block {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(3);
s.append(&self.header);
s.append(&self.transactions);
s.append(&self.uncles);
s.append_list(&self.transactions);
s.append_list(&self.uncles);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/blooms/bloom_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Decodable for BloomGroup {

impl Encodable for BloomGroup {
fn rlp_append(&self, s: &mut RlpStream) {
Encodable::rlp_append(&self.blooms, s)
s.append_list(&self.blooms);
}
}

Expand Down
5 changes: 3 additions & 2 deletions ethcore/src/engines/tendermint/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use util::*;
use super::{Height, View, BlockHash, Step};
use error::Error;
use header::Header;
use rlp::{Rlp, UntrustedRlp, RlpStream, Stream, RlpEncodable, Encodable, Decodable, Decoder, DecoderError, View as RlpView};
use rlp::{Rlp, UntrustedRlp, RlpStream, Encodable, Decodable, Decoder, DecoderError, View as RlpView};
use ethkey::{recover, public_to_address};
use super::super::vote_collector::Message;

Expand Down Expand Up @@ -162,7 +162,7 @@ impl Decodable for Step {

impl Encodable for Step {
fn rlp_append(&self, s: &mut RlpStream) {
RlpEncodable::rlp_append(&self.number(), s);
s.append_internal(&self.number());
}
}

Expand Down Expand Up @@ -278,6 +278,7 @@ mod tests {
::rlp::encode(&H520::default()).to_vec(),
Vec::new()
];

header.set_seal(seal);
let message = ConsensusMessage::new_proposal(&header).unwrap();
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions ethcore/src/engines/tendermint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl Tendermint {
let seal = vec![
::rlp::encode(&view).to_vec(),
::rlp::encode(&seal.proposal).to_vec(),
::rlp::encode(&seal.votes).to_vec()
::rlp::encode_list(&seal.votes).to_vec()
];
self.submit_seal(block_hash, seal);
self.to_next_height(height);
Expand Down Expand Up @@ -825,7 +825,7 @@ mod tests {
let vote_info = message_info_rlp(&VoteStep::new(2, 0, Step::Precommit), Some(header.bare_hash()));
let signature1 = tap.sign(proposer, None, vote_info.sha3()).unwrap();

seal[2] = ::rlp::encode(&vec![H520::from(signature1.clone())]).to_vec();
seal[2] = ::rlp::encode_list(&vec![H520::from(signature1.clone())]).to_vec();
header.set_seal(seal.clone());

// One good signature is not enough.
Expand All @@ -837,15 +837,15 @@ mod tests {
let voter = insert_and_unlock(&tap, "0");
let signature0 = tap.sign(voter, None, vote_info.sha3()).unwrap();

seal[2] = ::rlp::encode(&vec![H520::from(signature1.clone()), H520::from(signature0.clone())]).to_vec();
seal[2] = ::rlp::encode_list(&vec![H520::from(signature1.clone()), H520::from(signature0.clone())]).to_vec();
header.set_seal(seal.clone());

assert!(engine.verify_block_family(&header, &parent_header, None).is_ok());

let bad_voter = insert_and_unlock(&tap, "101");
let bad_signature = tap.sign(bad_voter, None, vote_info.sha3()).unwrap();

seal[2] = ::rlp::encode(&vec![H520::from(signature1), H520::from(bad_signature)]).to_vec();
seal[2] = ::rlp::encode_list(&vec![H520::from(signature1), H520::from(bad_signature)]).to_vec();
header.set_seal(seal);

// One good and one bad signature.
Expand Down
8 changes: 4 additions & 4 deletions ethcore/src/engines/vote_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ mod tests {

#[test]
fn seal_retrieval() {
let collector = VoteCollector::default();
let collector = VoteCollector::default();
let bh = Some("1".sha3());
let mut signatures = Vec::new();
for _ in 0..5 {
Expand Down Expand Up @@ -284,7 +284,7 @@ mod tests {

#[test]
fn count_votes() {
let collector = VoteCollector::default();
let collector = VoteCollector::default();
let round1 = 1;
let round3 = 3;
// good 1
Expand Down Expand Up @@ -318,7 +318,7 @@ mod tests {

#[test]
fn remove_old() {
let collector = VoteCollector::default();
let collector = VoteCollector::default();
let vote = |round, hash| {
random_vote(&collector, H520::random(), round, hash);
};
Expand All @@ -334,7 +334,7 @@ mod tests {

#[test]
fn malicious_authority() {
let collector = VoteCollector::default();
let collector = VoteCollector::default();
let round = 3;
// Vote is inserted fine.
assert!(full_vote(&collector, H520::random(), round, Some("0".sha3()), &Address::default()).is_none());
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const STACK_SIZE_PER_DEPTH: usize = 24*1024;

/// Returns new address created from address and given nonce.
pub fn contract_address(address: &Address, nonce: &U256) -> Address {
use rlp::{RlpStream, Stream};
use rlp::RlpStream;

let mut stream = RlpStream::new_list(2);
stream.append(address);
Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/migrations/state/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use util::migration::{Batch, Config, Error, Migration, SimpleMigration, Progress
use util::sha3::Hashable;
use std::sync::Arc;

use rlp::{decode, Rlp, RlpStream, Stream, View};
use rlp::{decode, Rlp, RlpStream, View};


// attempt to migrate a key, value pair. None if migration not possible.
Expand Down Expand Up @@ -199,7 +199,7 @@ impl OverlayRecentV7 {
stream.begin_list(2).append(&k).append(&v);
}

stream.append(&deleted_keys);
stream.append_list(&deleted_keys);

// and insert it into the new database.
batch.insert(entry_key, stream.out(), dest)?;
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/migrations/v9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! This migration consolidates all databases into single one using Column Families.
use rlp::{Rlp, RlpStream, View, Stream};
use rlp::{Rlp, RlpStream, View};
use util::kvdb::Database;
use util::migration::{Batch, Config, Error, Migration, Progress};
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/pod_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use state::Account;
use account_db::AccountDBMut;
use ethjson;
use types::account_diff::*;
use rlp::{self, RlpStream, Stream};
use rlp::{self, RlpStream};

#[derive(Debug, Clone, PartialEq, Eq)]
/// An account, expressed as Plain-Old-Data (hence the name).
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/snapshot/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use snapshot::Error;

use util::{U256, H256, Bytes, HashDB, SHA3_EMPTY, SHA3_NULL_RLP};
use util::trie::{TrieDB, Trie};
use rlp::{RlpStream, Stream, UntrustedRlp, View};
use rlp::{RlpStream, UntrustedRlp, View};

use std::collections::HashSet;

Expand Down
8 changes: 5 additions & 3 deletions ethcore/src/snapshot/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use block::Block;
use header::Header;

use views::BlockView;
use rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View};
use rlp::{DecoderError, RlpStream, UntrustedRlp, View};
use util::{Bytes, Hashable, H256};
use util::triehash::ordered_trie_root;

Expand Down Expand Up @@ -69,7 +69,9 @@ impl AbridgedBlock {
.append(&header.extra_data());

// write block values.
stream.append(&block_view.transactions()).append(&block_view.uncles());
stream
.append_list(&block_view.transactions())
.append_list(&block_view.uncles());

// write seal fields.
for field in seal_fields {
Expand Down Expand Up @@ -108,7 +110,7 @@ impl AbridgedBlock {
header.set_receipts_root(receipts_root);

let mut uncles_rlp = RlpStream::new();
uncles_rlp.append(&uncles);
uncles_rlp.append_list(&uncles);
header.set_uncles_hash(uncles_rlp.as_raw().sha3());

let mut seal_fields = Vec::new();
Expand Down
8 changes: 4 additions & 4 deletions ethcore/src/snapshot/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::path::{Path, PathBuf};

use util::Bytes;
use util::hash::H256;
use rlp::{self, Encodable, RlpStream, UntrustedRlp, Stream, View};
use rlp::{self, Encodable, RlpStream, UntrustedRlp, View};

use super::ManifestData;

Expand Down Expand Up @@ -122,8 +122,8 @@ impl SnapshotWriter for PackedWriter {
// they are consistent with ours.
let mut stream = RlpStream::new_list(5);
stream
.append(&self.state_hashes)
.append(&self.block_hashes)
.append_list(&self.state_hashes)
.append_list(&self.block_hashes)
.append(&manifest.state_root)
.append(&manifest.block_number)
.append(&manifest.block_hash);
Expand Down Expand Up @@ -428,4 +428,4 @@ mod tests {
reader.chunk(hash.clone()).unwrap();
}
}
}
}
2 changes: 1 addition & 1 deletion ethcore/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use util::journaldb::{self, Algorithm, JournalDB};
use util::kvdb::Database;
use util::trie::{TrieDB, TrieDBMut, Trie, TrieMut};
use util::sha3::SHA3_NULL_RLP;
use rlp::{RlpStream, Stream, UntrustedRlp, View};
use rlp::{RlpStream, UntrustedRlp, View};
use bloom_journal::Bloom;

use self::block::AbridgedBlock;
Expand Down
Loading

0 comments on commit a555686

Please sign in to comment.