New RLP decoding interface used

This commit is contained in:
arkpar 2016-01-27 17:22:01 +01:00
parent 60e2b53a1d
commit 6e33c2494b
10 changed files with 39 additions and 67 deletions

View File

@ -200,11 +200,11 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
pub fn close(self) -> ClosedBlock<'x, 'y> { pub fn close(self) -> ClosedBlock<'x, 'y> {
let mut s = self; let mut s = self;
s.engine.on_close_block(&mut s.block); s.engine.on_close_block(&mut s.block);
s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect()); s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes().to_vec()).collect());
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out(); let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out();
s.block.header.uncles_hash = uncle_bytes.sha3(); s.block.header.uncles_hash = uncle_bytes.sha3();
s.block.header.state_root = s.block.state.root().clone(); s.block.header.state_root = s.block.state.root().clone();
s.block.header.receipts_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.receipt.rlp_bytes()).collect()); s.block.header.receipts_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.receipt.rlp_bytes().to_vec()).collect());
s.block.header.log_bloom = s.block.archive.iter().fold(LogBloom::zero(), |mut b, e| {b |= &e.receipt.log_bloom; b}); s.block.header.log_bloom = s.block.archive.iter().fold(LogBloom::zero(), |mut b, e| {b |= &e.receipt.log_bloom; b});
s.block.header.gas_used = s.block.archive.last().map_or(U256::zero(), |t| t.receipt.gas_used); s.block.header.gas_used = s.block.archive.last().map_or(U256::zero(), |t| t.receipt.gas_used);
s.block.header.note_dirty(); s.block.header.note_dirty();
@ -256,7 +256,7 @@ impl SealedBlock {
pub fn rlp_bytes(&self) -> Bytes { pub fn rlp_bytes(&self) -> Bytes {
let mut block_rlp = RlpStream::new_list(3); let mut block_rlp = RlpStream::new_list(3);
self.block.header.stream_rlp(&mut block_rlp, Seal::With); self.block.header.stream_rlp(&mut block_rlp, Seal::With);
block_rlp.append_list(self.block.archive.len()); block_rlp.begin_list(self.block.archive.len());
for e in &self.block.archive { e.transaction.rlp_append(&mut block_rlp); } for e in &self.block.archive { e.transaction.rlp_append(&mut block_rlp); }
block_rlp.append_raw(&self.uncle_bytes, 1); block_rlp.append_raw(&self.uncle_bytes, 1);
block_rlp.out() block_rlp.out()

View File

@ -48,7 +48,7 @@ impl Engine for Ethash {
// Two fields - mix // Two fields - mix
fn seal_fields(&self) -> usize { 2 } fn seal_fields(&self) -> usize { 2 }
// Two empty data items in RLP. // 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`. /// Additional engine-specific information for the user/developer concerning `header`.
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() } fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }

View File

@ -145,13 +145,12 @@ impl Decodable for BlockDetails {
} }
impl Encodable for BlockDetails { impl Encodable for BlockDetails {
fn encode<E>(&self, encoder: &mut E) where E: Encoder { fn rlp_append(&self, s: &mut RlpStream) {
encoder.emit_list(| e | { s.begin_list(4);
self.number.encode(e); s.append(&self.number);
self.total_difficulty.encode(e); s.append(&self.total_difficulty);
self.parent.encode(e); s.append(&self.parent);
self.children.encode(e); s.append_list(&self.children);
})
} }
} }
@ -185,8 +184,8 @@ impl Decodable for BlockLogBlooms {
} }
impl Encodable for BlockLogBlooms { impl Encodable for BlockLogBlooms {
fn encode<E>(&self, encoder: &mut E) where E: Encoder { fn rlp_append(&self, s: &mut RlpStream) {
self.blooms.encode(encoder); s.append_list(&self.blooms);
} }
} }
@ -231,9 +230,9 @@ impl Decodable for BlocksBlooms {
} }
impl Encodable 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; let blooms_ref: &[H2048] = &self.blooms;
blooms_ref.encode(encoder); s.append_list(&blooms_ref);
} }
} }
@ -269,10 +268,9 @@ impl Decodable for TransactionAddress {
} }
impl Encodable for TransactionAddress { impl Encodable for TransactionAddress {
fn encode<E>(&self, encoder: &mut E) where E: Encoder { fn rlp_append(&self, s: &mut RlpStream) {
encoder.emit_list(| e | { s.begin_list(2);
self.block_hash.encode(e); s.append(&self.block_hash);
self.index.encode(e); s.append(&self.index);
})
} }
} }

View File

@ -157,7 +157,7 @@ impl Header {
// TODO: make these functions traity // TODO: make these functions traity
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub fn stream_rlp(&self, s: &mut RlpStream, with_seal: Seal) { 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.parent_hash);
s.append(&self.uncles_hash); s.append(&self.uncles_hash);
s.append(&self.author); s.append(&self.author);
@ -221,26 +221,8 @@ impl Decodable for Header {
} }
impl Encodable for Header { impl Encodable for Header {
fn encode<E>(&self, encoder: &mut E) where E: Encoder { fn rlp_append(&self, s: &mut RlpStream) {
encoder.emit_list(| e | { self.stream_rlp(s, Seal::With);
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);
}
})
} }
} }

View File

@ -12,11 +12,11 @@ pub struct LogEntry {
pub data: Bytes, pub data: Bytes,
} }
impl RlpStandard for LogEntry { impl Encodable for LogEntry {
fn rlp_append(&self, s: &mut RlpStream) { fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(3); s.begin_list(3);
s.append(&self.address); s.append(&self.address);
s.append(&self.topics); s.append_list(&self.topics);
s.append(&self.data); s.append(&self.data);
} }
} }

View File

@ -36,7 +36,7 @@ impl PodAccount {
let mut stream = RlpStream::new_list(4); let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce); stream.append(&self.nonce);
stream.append(&self.balance); 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.append(&self.code.sha3());
stream.out() stream.out()
} }

View File

@ -27,17 +27,12 @@ impl Receipt {
} }
} }
impl RlpStandard for Receipt { impl Encodable for Receipt {
fn rlp_append(&self, s: &mut RlpStream) { fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(4); s.begin_list(4);
s.append(&self.state_root); s.append(&self.state_root);
s.append(&self.gas_used); s.append(&self.gas_used);
s.append(&self.log_bloom); s.append(&self.log_bloom);
// TODO: make work: s.append_list(&self.logs);
//s.append(&self.logs);
s.append_list(self.logs.len());
for l in &self.logs {
l.rlp_append(s);
}
} }
} }

View File

@ -19,14 +19,14 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
// TODO: handle container types. // TODO: handle container types.
fn json_to_rlp(json: &Json) -> Bytes { fn json_to_rlp(json: &Json) -> Bytes {
match *json { match *json {
Json::Boolean(o) => encode(&(if o {1u64} else {0})), Json::Boolean(o) => encode(&(if o {1u64} else {0})).to_vec(),
Json::I64(o) => encode(&(o as u64)), Json::I64(o) => encode(&(o as u64)).to_vec(),
Json::U64(o) => encode(&o), 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() => { 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) => { Json::String(ref s) => {
encode(s) encode(s).to_vec()
}, },
_ => panic!() _ => panic!()
} }

View File

@ -107,7 +107,7 @@ impl Transaction {
/// Append object into RLP stream, optionally with or without the signature. /// Append object into RLP stream, optionally with or without the signature.
pub fn rlp_append_opt(&self, s: &mut RlpStream, with_seal: Seal) { 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.nonce);
s.append(&self.gas_price); s.append(&self.gas_price);
s.append(&self.gas); s.append(&self.gas);
@ -158,7 +158,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) } fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_opt(s, Seal::With) }
} }

View File

@ -233,15 +233,12 @@ mod tests {
fn create_test_block_with_data(header: &Header, transactions: &[&Transaction], uncles: &[Header]) -> Bytes { fn create_test_block_with_data(header: &Header, transactions: &[&Transaction], uncles: &[Header]) -> Bytes {
let mut rlp = RlpStream::new_list(3); let mut rlp = RlpStream::new_list(3);
rlp.append(header); rlp.append(header);
rlp.append_list(transactions.len()); rlp.begin_list(transactions.len());
for t in transactions { for t in transactions {
rlp.append_raw(&t.rlp_bytes_opt(Seal::With), 1); rlp.append_raw(&t.rlp_bytes_opt(Seal::With), 1);
} }
rlp.append_list(uncles.len()); rlp.append_list(&uncles);
for h in uncles { rlp.out().to_vec()
rlp.append(h);
}
rlp.out()
} }
fn check_ok(result: Result<(), Error>) { fn check_ok(result: Result<(), Error>) {
@ -365,7 +362,7 @@ mod tests {
let good_uncles = vec![ good_uncle1.clone(), good_uncle2.clone() ]; let good_uncles = vec![ good_uncle1.clone(), good_uncle2.clone() ];
let mut uncles_rlp = RlpStream::new(); 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_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()); let good_transactions_root = ordered_trie_root(good_transactions.iter().map(|t| t.rlp_bytes_opt(Seal::With)).collect());