Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

RLP encoder refactoring #252

Merged
merged 13 commits into from
Jan 29, 2016
6 changes: 3 additions & 3 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
pub fn close(self) -> ClosedBlock<'x, 'y> {
let mut s = self;
s.engine.on_close_block(&mut s.block);
s.block.base.header.transactions_root = ordered_trie_root(s.block.base.transactions.iter().map(|ref e| e.rlp_bytes()).collect());
s.block.base.header.transactions_root = ordered_trie_root(s.block.base.transactions.iter().map(|ref e| e.rlp_bytes().to_vec()).collect());
let uncle_bytes = s.block.base.uncles.iter().fold(RlpStream::new_list(s.block.base.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out();
s.block.base.header.uncles_hash = uncle_bytes.sha3();
s.block.base.header.state_root = s.block.state.root().clone();
s.block.base.header.receipts_root = ordered_trie_root(s.block.receipts.iter().map(|ref r| r.rlp_bytes()).collect());
s.block.base.header.receipts_root = ordered_trie_root(s.block.receipts.iter().map(|ref r| r.rlp_bytes().to_vec()).collect());
s.block.base.header.log_bloom = s.block.receipts.iter().fold(LogBloom::zero(), |mut b, r| {b |= &r.log_bloom; b});
s.block.base.header.gas_used = s.block.receipts.last().map_or(U256::zero(), |r| r.gas_used);
s.block.base.header.note_dirty();
Expand Down Expand Up @@ -301,7 +301,7 @@ impl SealedBlock {
pub fn rlp_bytes(&self) -> Bytes {
let mut block_rlp = RlpStream::new_list(3);
self.block.base.header.stream_rlp(&mut block_rlp, Seal::With);
block_rlp.append_list(self.block.receipts.len());
block_rlp.begin_list(self.block.receipts.len());
for t in &self.block.base.transactions { t.rlp_append(&mut block_rlp); }
block_rlp.append_raw(&self.uncle_bytes, 1);
block_rlp.out()
Expand Down
2 changes: 1 addition & 1 deletion src/ethereum/ethash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Engine for Ethash {
// Two fields - mix
fn seal_fields(&self) -> usize { 2 }
// Two empty data items in RLP.
fn seal_rlp(&self) -> Bytes { encode(&H64::new()) }
fn seal_rlp(&self) -> Bytes { encode(&H64::new()).to_vec() }

/// Additional engine-specific information for the user/developer concerning `header`.
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }
Expand Down
30 changes: 14 additions & 16 deletions src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,12 @@ impl Decodable for BlockDetails {
}

impl Encodable for BlockDetails {
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
encoder.emit_list(| e | {
self.number.encode(e);
self.total_difficulty.encode(e);
self.parent.encode(e);
self.children.encode(e);
})
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(4);
s.append(&self.number);
s.append(&self.total_difficulty);
s.append(&self.parent);
s.append_list(&self.children);
}
}

Expand Down Expand Up @@ -185,8 +184,8 @@ impl Decodable for BlockLogBlooms {
}

impl Encodable for BlockLogBlooms {
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
self.blooms.encode(encoder);
fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(&self.blooms);
}
}

Expand Down Expand Up @@ -231,9 +230,9 @@ impl Decodable for BlocksBlooms {
}

impl Encodable for BlocksBlooms {
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
fn rlp_append(&self, s: &mut RlpStream) {
let blooms_ref: &[H2048] = &self.blooms;
blooms_ref.encode(encoder);
s.append_list(&blooms_ref);
}
}

Expand Down Expand Up @@ -269,10 +268,9 @@ impl Decodable for TransactionAddress {
}

impl Encodable for TransactionAddress {
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
encoder.emit_list(| e | {
self.block_hash.encode(e);
self.index.encode(e);
})
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(2);
s.append(&self.block_hash);
s.append(&self.index);
}
}
24 changes: 3 additions & 21 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Header {
// TODO: make these functions traity
/// TODO [Gav Wood] Please document me
pub fn stream_rlp(&self, s: &mut RlpStream, with_seal: Seal) {
s.append_list(13 + match with_seal { Seal::With => self.seal.len(), _ => 0 });
s.begin_list(13 + match with_seal { Seal::With => self.seal.len(), _ => 0 });
s.append(&self.parent_hash);
s.append(&self.uncles_hash);
s.append(&self.author);
Expand Down Expand Up @@ -221,26 +221,8 @@ impl Decodable for Header {
}

impl Encodable for Header {
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
encoder.emit_list(| e | {
self.parent_hash.encode(e);
self.uncles_hash.encode(e);
self.author.encode(e);
self.state_root.encode(e);
self.transactions_root.encode(e);
self.receipts_root.encode(e);
self.log_bloom.encode(e);
self.difficulty.encode(e);
self.number.encode(e);
self.gas_limit.encode(e);
self.gas_used.encode(e);
self.timestamp.encode(e);
self.extra_data.encode(e);

for b in &self.seal {
e.emit_raw(&b);
}
})
fn rlp_append(&self, s: &mut RlpStream) {
self.stream_rlp(s, Seal::With);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/log_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub struct LogEntry {
pub data: Bytes,
}

impl RlpStandard for LogEntry {
impl Encodable for LogEntry {
fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(3);
s.begin_list(3);
s.append(&self.address);
s.append(&self.topics);
s.append_list(&self.topics);
s.append(&self.data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pod_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl PodAccount {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(v.as_slice())))).collect()));
stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(v.as_slice())).to_vec())).collect()));
stream.append(&self.code.sha3());
stream.out()
}
Expand Down
11 changes: 3 additions & 8 deletions src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ impl Receipt {
}
}

impl RlpStandard for Receipt {
impl Encodable for Receipt {
fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(4);
s.begin_list(4);
s.append(&self.state_root);
s.append(&self.gas_used);
s.append(&self.log_bloom);
// TODO: make work:
//s.append(&self.logs);
s.append_list(self.logs.len());
for l in &self.logs {
l.rlp_append(s);
}
s.append_list(&self.logs);
}
}
10 changes: 5 additions & 5 deletions src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
// TODO: handle container types.
fn json_to_rlp(json: &Json) -> Bytes {
match *json {
Json::Boolean(o) => encode(&(if o {1u64} else {0})),
Json::I64(o) => encode(&(o as u64)),
Json::U64(o) => encode(&o),
Json::Boolean(o) => encode(&(if o {1u64} else {0})).to_vec(),
Json::I64(o) => encode(&(o as u64)).to_vec(),
Json::U64(o) => encode(&o).to_vec(),
Json::String(ref s) if s.len() >= 2 && &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => {
encode(&U256::from_str(&s[2..]).unwrap())
encode(&U256::from_str(&s[2..]).unwrap()).to_vec()
},
Json::String(ref s) => {
encode(s)
encode(s).to_vec()
},
_ => panic!()
}
Expand Down
4 changes: 2 additions & 2 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Transaction {

/// Append object into RLP stream, optionally with or without the signature.
pub fn rlp_append_opt(&self, s: &mut RlpStream, with_seal: Seal) {
s.append_list(6 + match with_seal { Seal::With => 3, _ => 0 });
s.begin_list(6 + match with_seal { Seal::With => 3, _ => 0 });
s.append(&self.nonce);
s.append(&self.gas_price);
s.append(&self.gas);
Expand Down Expand Up @@ -162,7 +162,7 @@ impl FromJson for Transaction {
}
}

impl RlpStandard for Transaction {
impl Encodable for Transaction {
fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_opt(s, Seal::With) }
}

Expand Down
11 changes: 4 additions & 7 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,12 @@ mod tests {
fn create_test_block_with_data(header: &Header, transactions: &[&Transaction], uncles: &[Header]) -> Bytes {
let mut rlp = RlpStream::new_list(3);
rlp.append(header);
rlp.append_list(transactions.len());
rlp.begin_list(transactions.len());
for t in transactions {
rlp.append_raw(&t.rlp_bytes_opt(Seal::With), 1);
}
rlp.append_list(uncles.len());
for h in uncles {
rlp.append(h);
}
rlp.out()
rlp.append_list(&uncles);
rlp.out().to_vec()
}

fn check_ok(result: Result<(), Error>) {
Expand Down Expand Up @@ -365,7 +362,7 @@ mod tests {

let good_uncles = vec![ good_uncle1.clone(), good_uncle2.clone() ];
let mut uncles_rlp = RlpStream::new();
uncles_rlp.append(&good_uncles);
uncles_rlp.append_list(&good_uncles);
let good_uncles_hash = uncles_rlp.as_raw().sha3();
let good_transactions_root = ordered_trie_root(good_transactions.iter().map(|t| t.rlp_bytes_opt(Seal::With)).collect());

Expand Down
1 change: 1 addition & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ elastic-array = "0.4"
heapsize = "0.2"
itertools = "0.4"
crossbeam = "0.2"
smallvec = "0.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not used, can you remove it?

slab = { git = "https://github.com/arkpar/slab.git" }
sha3 = { path = "sha3" }
serde = "0.6.7"
Expand Down
Loading