2016-01-09 12:30:41 +01:00
|
|
|
use util::*;
|
2016-01-09 22:13:13 +01:00
|
|
|
use basic_types::*;
|
2016-01-10 22:55:07 +01:00
|
|
|
use time::now_utc;
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2016-01-11 01:07:58 +01:00
|
|
|
pub type BlockNumber = u64;
|
2015-12-13 16:44:14 +01:00
|
|
|
|
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 {
|
2016-01-10 14:05:39 +01:00
|
|
|
// TODO: make all private.
|
2015-12-17 19:26:21 +01:00
|
|
|
pub parent_hash: H256,
|
2016-01-10 22:55:07 +01:00
|
|
|
pub timestamp: u64,
|
2016-01-11 01:07:58 +01:00
|
|
|
pub number: BlockNumber,
|
2015-12-17 19:26:21 +01:00
|
|
|
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>,
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2016-01-09 23:47:15 +01:00
|
|
|
pub hash: RefCell<Option<H256>>,
|
2015-12-13 16:44:14 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 22:13:13 +01:00
|
|
|
pub enum Seal {
|
|
|
|
With,
|
|
|
|
Without,
|
2016-01-09 18:58:04 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-01-04 13:25:32 +01:00
|
|
|
parent_hash: ZERO_H256.clone(),
|
2016-01-10 21:55:03 +01:00
|
|
|
timestamp: 0,
|
|
|
|
number: 0,
|
2016-01-04 13:25:32 +01:00
|
|
|
author: ZERO_ADDRESS.clone(),
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2015-12-27 00:48:03 +01:00
|
|
|
transactions_root: SHA3_NULL_RLP,
|
|
|
|
uncles_hash: SHA3_EMPTY_LIST_RLP,
|
2015-12-13 16:44:14 +01:00
|
|
|
extra_data: vec![],
|
|
|
|
|
2015-12-27 00:48:03 +01:00
|
|
|
state_root: SHA3_NULL_RLP,
|
|
|
|
receipts_root: SHA3_NULL_RLP,
|
2016-01-04 13:25:32 +01:00
|
|
|
log_bloom: ZERO_LOGBLOOM.clone(),
|
2015-12-27 00:48:03 +01:00
|
|
|
gas_used: ZERO_U256,
|
|
|
|
gas_limit: ZERO_U256,
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2015-12-27 00:48:03 +01:00
|
|
|
difficulty: ZERO_U256,
|
2015-12-13 16:44:14 +01:00
|
|
|
seal: vec![],
|
2016-01-04 13:25:32 +01:00
|
|
|
hash: RefCell::new(None),
|
2015-12-13 16:44:14 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 17:18:47 +01:00
|
|
|
|
2016-01-11 01:07:58 +01:00
|
|
|
pub fn number(&self) -> BlockNumber { self.number }
|
2016-01-10 22:55:07 +01:00
|
|
|
pub fn timestamp(&self) -> u64 { self.timestamp }
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn author(&self) -> &Address { &self.author }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn extra_data(&self) -> &Bytes { &self.extra_data }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-01-10 23:42:59 +01:00
|
|
|
pub fn state_root(&self) -> &H256 { &self.state_root }
|
|
|
|
pub fn receipts_root(&self) -> &H256 { &self.receipts_root }
|
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn seal(&self) -> &Vec<Bytes> { &self.seal }
|
|
|
|
|
|
|
|
// TODO: seal_at, set_seal_at &c.
|
|
|
|
|
2016-01-11 01:07:58 +01:00
|
|
|
pub fn set_number(&mut self, a: BlockNumber) { self.number = a; self.note_dirty(); }
|
2016-01-10 22:55:07 +01:00
|
|
|
pub fn set_timestamp(&mut self, a: u64) { self.timestamp = a; self.note_dirty(); }
|
|
|
|
pub fn set_timestamp_now(&mut self) { self.timestamp = now_utc().to_timespec().sec as u64; self.note_dirty(); }
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn set_author(&mut self, a: Address) { if a != self.author { self.author = a; self.note_dirty(); } }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn set_extra_data(&mut self, a: Bytes) { if a != self.extra_data { self.extra_data = a; self.note_dirty(); } }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn set_seal(&mut self, a: Vec<Bytes>) { self.seal = a; self.note_dirty(); }
|
|
|
|
|
2016-01-09 18:58:04 +01:00
|
|
|
/// Get the hash of this header (sha3 of the RLP).
|
2015-12-24 17:18:47 +01:00
|
|
|
pub fn hash(&self) -> H256 {
|
2016-01-09 12:30:41 +01:00
|
|
|
let mut hash = self.hash.borrow_mut();
|
|
|
|
match &mut *hash {
|
|
|
|
&mut Some(ref h) => h.clone(),
|
|
|
|
hash @ &mut None => {
|
2016-01-09 22:13:13 +01:00
|
|
|
*hash = Some(self.rlp_sha3(Seal::With));
|
|
|
|
hash.as_ref().unwrap().clone()
|
2016-01-09 12:30:41 +01:00
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
2015-12-24 17:18:47 +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
|
2016-01-09 22:13:13 +01:00
|
|
|
pub fn stream_rlp(&self, s: &mut RlpStream, with_seal: Seal) {
|
|
|
|
s.append_list(13 + match with_seal { Seal::With => self.seal.len(), _ => 0 });
|
2016-01-09 18:58:04 +01:00
|
|
|
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);
|
2016-01-09 22:13:13 +01:00
|
|
|
match with_seal {
|
|
|
|
Seal::With => for b in self.seal.iter() { s.append_raw(&b, 1); },
|
|
|
|
_ => {}
|
2016-01-09 18:58:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:13:13 +01:00
|
|
|
pub fn rlp(&self, with_seal: Seal) -> Bytes {
|
|
|
|
let mut s = RlpStream::new();
|
2016-01-09 18:58:04 +01:00
|
|
|
self.stream_rlp(&mut s, with_seal);
|
|
|
|
s.out()
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:13:13 +01:00
|
|
|
pub fn rlp_sha3(&self, with_seal: Seal) -> H256 { self.rlp(with_seal).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-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![],
|
2016-01-08 16:00:32 +01:00
|
|
|
hash: RefCell::new(Some(r.as_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() {
|
2016-01-08 16:00:32 +01:00
|
|
|
blockheader.seal.push(try!(r.at(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-24 17:18:47 +01:00
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:45:33 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
}
|