2016-01-09 12:30:41 +01:00
|
|
|
use util::*;
|
2015-12-11 03:51:23 +01:00
|
|
|
|
2016-01-06 15:57:17 +01:00
|
|
|
/// Type for a 2048-bit log-bloom, as used by our blocks.
|
|
|
|
pub type LogBloom = H2048;
|
|
|
|
|
|
|
|
/// Constant address for point 0. Often used as a default.
|
2015-12-13 16:44:14 +01:00
|
|
|
pub static ZERO_ADDRESS: Address = Address([0x00; 20]);
|
2016-01-06 15:57:17 +01:00
|
|
|
/// Constant 256-bit datum for 0. Often used as a default.
|
2015-12-13 16:44:14 +01:00
|
|
|
pub static ZERO_H256: H256 = H256([0x00; 32]);
|
2016-01-06 15:57:17 +01:00
|
|
|
/// Constant 2048-bit datum for 0. Often used as a default.
|
2015-12-13 16:44:14 +01:00
|
|
|
pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]);
|
|
|
|
|
2016-01-06 15:57:17 +01:00
|
|
|
/// A block header.
|
|
|
|
///
|
|
|
|
/// Reflects the specific RLP fields of a block in the chain with additional room for the seal
|
|
|
|
/// which is non-specific.
|
|
|
|
///
|
|
|
|
/// Doesn't do all that much on its own.
|
2015-12-13 16:44:14 +01:00
|
|
|
#[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-13 16:44:14 +01:00
|
|
|
|
2015-12-17 19:26:21 +01:00
|
|
|
pub transactions_root: H256,
|
|
|
|
pub uncles_hash: H256,
|
|
|
|
pub extra_data: Bytes,
|
2015-12-13 16:44:14 +01:00
|
|
|
|
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-13 16:44:14 +01:00
|
|
|
|
2015-12-17 19:26:21 +01:00
|
|
|
pub difficulty: U256,
|
|
|
|
pub seal: Vec<Bytes>,
|
2016-01-09 12:30:41 +01:00
|
|
|
|
|
|
|
pub hash: RefCell<Option<H256>>, //TODO: make this private
|
2015-12-13 16:44:14 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 18:58:04 +01:00
|
|
|
enum SealInclusion {
|
|
|
|
WithSeal,
|
|
|
|
WithoutSeal,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub use SealInclusion::*;
|
|
|
|
|
2015-12-13 16:44:14 +01:00
|
|
|
impl Header {
|
2016-01-06 15:57:17 +01:00
|
|
|
/// Create a new, default-valued, header.
|
2015-12-13 16:44:14 +01:00
|
|
|
pub fn new() -> Header {
|
|
|
|
Header {
|
|
|
|
parent_hash: ZERO_H256.clone(),
|
|
|
|
timestamp: BAD_U256.clone(),
|
|
|
|
number: ZERO_U256.clone(),
|
|
|
|
author: ZERO_ADDRESS.clone(),
|
|
|
|
|
|
|
|
transactions_root: ZERO_H256.clone(),
|
|
|
|
uncles_hash: ZERO_H256.clone(),
|
|
|
|
extra_data: vec![],
|
|
|
|
|
|
|
|
state_root: ZERO_H256.clone(),
|
|
|
|
receipts_root: ZERO_H256.clone(),
|
|
|
|
log_bloom: ZERO_LOGBLOOM.clone(),
|
|
|
|
gas_used: ZERO_U256.clone(),
|
|
|
|
gas_limit: ZERO_U256.clone(),
|
|
|
|
|
|
|
|
difficulty: ZERO_U256.clone(),
|
|
|
|
seal: vec![],
|
2016-01-09 12:30:41 +01:00
|
|
|
hash: RefCell::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 18:58:04 +01:00
|
|
|
/// Get the hash of this header (sha3 of the RLP).
|
2016-01-09 12:30:41 +01:00
|
|
|
pub fn hash(&self) -> H256 {
|
|
|
|
let mut hash = self.hash.borrow_mut();
|
|
|
|
match &mut *hash {
|
|
|
|
&mut Some(ref h) => h.clone(),
|
|
|
|
hash @ &mut None => {
|
2016-01-09 18:58:04 +01:00
|
|
|
*hash = Some(self.hash(WithSeal));
|
|
|
|
hash.unwrap().clone()
|
2016-01-09 12:30:41 +01:00
|
|
|
}
|
2015-12-13 16:44:14 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 18:58:04 +01:00
|
|
|
|
|
|
|
/// Note that some fields have changed. Resets the memoised hash.
|
|
|
|
pub fn note_dirty(&self) {
|
|
|
|
*self.hash.borrow_mut() = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: get hash without seal.
|
|
|
|
|
|
|
|
// TODO: make these functions traity
|
|
|
|
pub fn stream_rlp(&self, s: &mut RlpStream, with_seal: SealInclusion) {
|
|
|
|
s.append_list(13 + if with_seal == WithSeal {self.seal.len()} else {0})
|
|
|
|
s.append(&self.parent_hash);
|
|
|
|
s.append(&self.uncles_hash);
|
|
|
|
s.append(&self.author);
|
|
|
|
s.append(&self.state_root);
|
|
|
|
s.append(&self.transactions_root);
|
|
|
|
s.append(&self.receipts_root);
|
|
|
|
s.append(&self.log_bloom);
|
|
|
|
s.append(&self.difficulty);
|
|
|
|
s.append(&self.number);
|
|
|
|
s.append(&self.gas_limit);
|
|
|
|
s.append(&self.gas_used);
|
|
|
|
s.append(&self.timestamp);
|
|
|
|
s.append(&self.extra_data);
|
|
|
|
if with_seal == WithSeal {
|
|
|
|
for b in self.seal.iter() {
|
|
|
|
e.append_raw(&b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub rlp(&self, with_seal: SealInclusion) -> Bytes {
|
|
|
|
let s = RlpStream::new();
|
|
|
|
self.stream_rlp(&mut s, with_seal);
|
|
|
|
s.out()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub hash(&self, with_seal: SealInclusion) -> H256 { self.rlp().sha3() }
|
2015-12-08 16:31:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 16:44:14 +01:00
|
|
|
impl Decodable for Header {
|
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
2015-12-14 12:09:32 +01:00
|
|
|
let d = try!(decoder.as_list());
|
|
|
|
|
2015-12-14 15:22:41 +01:00
|
|
|
let mut blockheader = Header {
|
2015-12-14 12:09:32 +01:00
|
|
|
parent_hash: try!(Decodable::decode(&d[0])),
|
|
|
|
uncles_hash: try!(Decodable::decode(&d[1])),
|
|
|
|
author: try!(Decodable::decode(&d[2])),
|
|
|
|
state_root: try!(Decodable::decode(&d[3])),
|
|
|
|
transactions_root: try!(Decodable::decode(&d[4])),
|
|
|
|
receipts_root: try!(Decodable::decode(&d[5])),
|
|
|
|
log_bloom: try!(Decodable::decode(&d[6])),
|
|
|
|
difficulty: try!(Decodable::decode(&d[7])),
|
|
|
|
number: try!(Decodable::decode(&d[8])),
|
|
|
|
gas_limit: try!(Decodable::decode(&d[9])),
|
|
|
|
gas_used: try!(Decodable::decode(&d[10])),
|
|
|
|
timestamp: try!(Decodable::decode(&d[11])),
|
|
|
|
extra_data: try!(Decodable::decode(&d[12])),
|
|
|
|
seal: vec![],
|
2016-01-09 12:30:41 +01:00
|
|
|
hash: RefCell::new(None),
|
2015-12-14 12:09:32 +01:00
|
|
|
};
|
2015-12-14 15:22:41 +01:00
|
|
|
|
|
|
|
for i in 13..d.len() {
|
2016-01-06 15:57:17 +01:00
|
|
|
blockheader.seal.push(d[i].as_raw().to_vec());
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 16:44:14 +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);
|
2015-12-13 16:44:14 +01:00
|
|
|
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);
|
2015-12-13 16:44:14 +01:00
|
|
|
self.extra_data.encode(e);
|
2015-12-14 15:22:41 +01:00
|
|
|
|
|
|
|
for b in self.seal.iter() {
|
2016-01-06 15:57:17 +01:00
|
|
|
e.emit_raw(&b);
|
2015-12-14 15:22:41 +01:00
|
|
|
}
|
2015-12-08 16:31:36 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 19:10:29 +01:00
|
|
|
/*
|
|
|
|
trait RlpStandard {
|
|
|
|
fn append(&self, s: &mut RlpStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RlpStandard for Header {
|
|
|
|
fn append(&self, s: &mut RlpStream) {
|
|
|
|
s.append_list(13);
|
|
|
|
s.append(self.parent_hash);
|
|
|
|
s.append_raw(self.seal[0]);
|
|
|
|
s.append_standard(self.x);
|
|
|
|
}
|
|
|
|
fn populate(&mut self, s: &Rlp) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RlpStream {
|
|
|
|
fn append_standard<O>(&mut self, o: &O) where O: RlpStandard {
|
|
|
|
o.append(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2015-12-08 16:31:36 +01:00
|
|
|
|
2015-12-09 00:45:33 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
}
|