openethereum/src/header.rs

133 lines
2.8 KiB
Rust
Raw Normal View History

2015-12-26 15:47:07 +01:00
use std::cell::Cell;
2015-12-08 16:31:36 +01:00
use util::hash::*;
2015-12-26 15:47:07 +01:00
use util::sha3::*;
use util::bytes::*;
2015-12-08 16:31:36 +01:00
use util::uint::*;
use util::rlp::*;
pub static ZERO_ADDRESS: Address = Address([0x00; 20]);
pub static ZERO_H256: H256 = H256([0x00; 32]);
pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]);
pub type LogBloom = H2048;
#[derive(Debug)]
pub struct Header {
2015-12-17 19:26:21 +01:00
pub parent_hash: H256,
pub timestamp: U256,
pub number: U256,
pub author: Address,
2015-12-17 19:26:21 +01:00
pub transactions_root: H256,
pub uncles_hash: H256,
pub extra_data: Bytes,
2015-12-17 19:26:21 +01:00
pub state_root: H256,
pub receipts_root: H256,
pub log_bloom: LogBloom,
pub gas_used: U256,
pub gas_limit: U256,
2015-12-17 19:26:21 +01:00
pub difficulty: U256,
pub seal: Vec<Bytes>,
2015-12-26 15:47:07 +01:00
pub hash: Cell<Option<H256>>, //TODO: make this private
}
impl Header {
pub fn new() -> Header {
Header {
2015-12-27 00:48:03 +01:00
parent_hash: ZERO_H256,
timestamp: BAD_U256,
number: ZERO_U256,
author: ZERO_ADDRESS,
2015-12-27 00:48:03 +01:00
transactions_root: SHA3_NULL_RLP,
uncles_hash: SHA3_EMPTY_LIST_RLP,
extra_data: vec![],
2015-12-27 00:48:03 +01:00
state_root: SHA3_NULL_RLP,
receipts_root: SHA3_NULL_RLP,
log_bloom: ZERO_LOGBLOOM,
gas_used: ZERO_U256,
gas_limit: ZERO_U256,
2015-12-27 00:48:03 +01:00
difficulty: ZERO_U256,
seal: vec![],
2015-12-26 15:47:07 +01:00
hash: Cell::new(None),
}
}
2015-12-24 17:18:47 +01:00
pub fn hash(&self) -> H256 {
2015-12-26 15:47:07 +01:00
let hash = self.hash.get();
match hash {
Some(h) => h,
None => {
let mut stream = RlpStream::new();
stream.append(self);
let h = stream.raw().sha3();
self.hash.set(Some(h.clone()));
h
}
}
2015-12-24 17:18:47 +01:00
}
2015-12-08 16:31:36 +01:00
}
impl Decodable for Header {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
2015-12-26 15:47:07 +01:00
let r = decoder.as_rlp();
2015-12-14 12:09:32 +01:00
2015-12-14 15:22:41 +01:00
let mut blockheader = Header {
2015-12-26 15:47:07 +01:00
parent_hash: try!(r.val_at(0)),
uncles_hash: try!(r.val_at(1)),
author: try!(r.val_at(2)),
state_root: try!(r.val_at(3)),
transactions_root: try!(r.val_at(4)),
receipts_root: try!(r.val_at(5)),
log_bloom: try!(r.val_at(6)),
difficulty: try!(r.val_at(7)),
number: try!(r.val_at(8)),
gas_limit: try!(r.val_at(9)),
gas_used: try!(r.val_at(10)),
timestamp: try!(r.val_at(11)),
extra_data: try!(r.val_at(12)),
2015-12-14 12:09:32 +01:00
seal: vec![],
2015-12-26 15:47:07 +01:00
hash: Cell::new(Some(r.raw().sha3()))
2015-12-14 12:09:32 +01:00
};
2015-12-14 15:22:41 +01:00
2015-12-26 15:47:07 +01:00
for i in 13..r.item_count() {
blockheader.seal.push(try!(r.val_at(i)))
2015-12-14 15:22:41 +01:00
}
2015-12-14 12:09:32 +01:00
Ok(blockheader)
2015-12-08 16:31:36 +01:00
}
}
impl Encodable for Header {
2015-12-08 16:31:36 +01:00
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
encoder.emit_list(| e | {
self.parent_hash.encode(e);
2015-12-09 19:03:25 +01:00
self.uncles_hash.encode(e);
self.author.encode(e);
2015-12-08 16:31:36 +01:00
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);
2015-12-24 17:18:47 +01:00
2015-12-14 15:22:41 +01:00
for b in self.seal.iter() {
b.encode(e);
}
2015-12-08 16:31:36 +01:00
})
}
}
2015-12-09 00:45:33 +01:00
#[cfg(test)]
mod tests {
}