2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Block header.
|
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::cmp;
|
2017-08-30 19:18:28 +02:00
|
|
|
use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP, keccak};
|
2017-08-30 16:04:47 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
2018-01-14 22:43:28 +01:00
|
|
|
use ethereum_types::{H256, U256, Address, Bloom};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2018-04-16 15:52:12 +02:00
|
|
|
use rlp::{Rlp, RlpStream, Encodable, DecoderError, Decodable};
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2017-07-12 13:09:17 +02:00
|
|
|
pub use types::BlockNumber;
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2018-01-17 10:35:43 +01:00
|
|
|
/// Semantic boolean for when a seal/signature is included.
|
2018-04-03 10:01:28 +02:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
enum Seal {
|
2018-01-17 10:35:43 +01:00
|
|
|
/// The seal/signature is included.
|
|
|
|
With,
|
|
|
|
/// The seal/signature is not included.
|
|
|
|
Without,
|
|
|
|
}
|
|
|
|
|
2018-05-16 08:58:01 +02:00
|
|
|
/// Extended block header, wrapping `Header` with finalized and total difficulty information.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct ExtendedHeader {
|
|
|
|
/// The actual header.
|
|
|
|
pub header: Header,
|
|
|
|
/// Whether the block underlying this header is considered finalized.
|
|
|
|
pub is_finalized: bool,
|
|
|
|
/// The parent block difficulty.
|
|
|
|
pub parent_total_difficulty: U256,
|
|
|
|
/// The block metadata information.
|
|
|
|
pub metadata: Option<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
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.
|
2016-03-02 23:41:15 +01:00
|
|
|
#[derive(Debug, Clone, Eq)]
|
2015-12-13 16:44:14 +01:00
|
|
|
pub struct Header {
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Parent hash.
|
2016-08-29 11:35:24 +02:00
|
|
|
parent_hash: H256,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block timestamp.
|
2016-08-29 11:35:24 +02:00
|
|
|
timestamp: u64,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block number.
|
2016-08-29 11:35:24 +02:00
|
|
|
number: BlockNumber,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block author.
|
2016-08-29 11:35:24 +02:00
|
|
|
author: Address,
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Transactions root.
|
2016-08-29 11:35:24 +02:00
|
|
|
transactions_root: H256,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block uncles hash.
|
2016-08-29 11:35:24 +02:00
|
|
|
uncles_hash: H256,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block extra data.
|
2016-08-29 11:35:24 +02:00
|
|
|
extra_data: Bytes,
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2016-02-02 18:21:31 +01:00
|
|
|
/// State root.
|
2016-08-29 11:35:24 +02:00
|
|
|
state_root: H256,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block receipts root.
|
2016-08-29 11:35:24 +02:00
|
|
|
receipts_root: H256,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block bloom.
|
2018-01-14 22:43:28 +01:00
|
|
|
log_bloom: Bloom,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Gas used for contracts execution.
|
2016-08-29 11:35:24 +02:00
|
|
|
gas_used: U256,
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block gas limit.
|
2016-08-29 11:35:24 +02:00
|
|
|
gas_limit: U256,
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2016-02-02 18:21:31 +01:00
|
|
|
/// Block difficulty.
|
2016-08-29 11:35:24 +02:00
|
|
|
difficulty: U256,
|
2016-05-17 11:07:51 +02:00
|
|
|
/// Vector of post-RLP-encoded fields.
|
2016-08-29 11:35:24 +02:00
|
|
|
seal: Vec<Bytes>,
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2018-04-03 10:01:28 +02:00
|
|
|
/// Memoized hash of that header and the seal.
|
|
|
|
hash: Option<H256>,
|
2015-12-13 16:44:14 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 23:41:15 +01:00
|
|
|
impl PartialEq for Header {
|
|
|
|
fn eq(&self, c: &Header) -> bool {
|
2018-04-03 10:01:28 +02:00
|
|
|
if let (&Some(ref h1), &Some(ref h2)) = (&self.hash, &c.hash) {
|
|
|
|
if h1 == h2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 23:41:15 +01:00
|
|
|
self.parent_hash == c.parent_hash &&
|
|
|
|
self.timestamp == c.timestamp &&
|
|
|
|
self.number == c.number &&
|
|
|
|
self.author == c.author &&
|
|
|
|
self.transactions_root == c.transactions_root &&
|
|
|
|
self.uncles_hash == c.uncles_hash &&
|
|
|
|
self.extra_data == c.extra_data &&
|
|
|
|
self.state_root == c.state_root &&
|
|
|
|
self.receipts_root == c.receipts_root &&
|
|
|
|
self.log_bloom == c.log_bloom &&
|
|
|
|
self.gas_used == c.gas_used &&
|
|
|
|
self.gas_limit == c.gas_limit &&
|
|
|
|
self.difficulty == c.difficulty &&
|
|
|
|
self.seal == c.seal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:21:31 +01:00
|
|
|
impl Default for Header {
|
|
|
|
fn default() -> Self {
|
2015-12-13 16:44:14 +01:00
|
|
|
Header {
|
2016-07-06 11:23:29 +02:00
|
|
|
parent_hash: H256::default(),
|
2016-01-10 21:55:03 +01:00
|
|
|
timestamp: 0,
|
|
|
|
number: 0,
|
2016-07-06 11:23:29 +02:00
|
|
|
author: Address::default(),
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2017-08-30 19:18:28 +02:00
|
|
|
transactions_root: KECCAK_NULL_RLP,
|
|
|
|
uncles_hash: KECCAK_EMPTY_LIST_RLP,
|
2015-12-13 16:44:14 +01:00
|
|
|
extra_data: vec![],
|
|
|
|
|
2017-08-30 19:18:28 +02:00
|
|
|
state_root: KECCAK_NULL_RLP,
|
|
|
|
receipts_root: KECCAK_NULL_RLP,
|
2018-01-17 10:35:43 +01:00
|
|
|
log_bloom: Bloom::default(),
|
2016-07-06 11:23:29 +02:00
|
|
|
gas_used: U256::default(),
|
|
|
|
gas_limit: U256::default(),
|
2015-12-13 16:44:14 +01:00
|
|
|
|
2016-07-06 11:23:29 +02:00
|
|
|
difficulty: U256::default(),
|
2015-12-13 16:44:14 +01:00
|
|
|
seal: vec![],
|
2018-04-03 10:01:28 +02:00
|
|
|
hash: None,
|
2015-12-13 16:44:14 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 18:21:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
|
|
|
/// Create a new, default-valued, header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn new() -> Self { Self::default() }
|
2015-12-24 17:18:47 +01:00
|
|
|
|
2016-03-01 16:58:14 +01:00
|
|
|
/// Get the parent_hash field of the header.
|
|
|
|
pub fn parent_hash(&self) -> &H256 { &self.parent_hash }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the timestamp field of the header.
|
2016-01-10 22:55:07 +01:00
|
|
|
pub fn timestamp(&self) -> u64 { self.timestamp }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-03-01 16:58:14 +01:00
|
|
|
/// Get the number field of the header.
|
|
|
|
pub fn number(&self) -> BlockNumber { self.number }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the author field of the header.
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn author(&self) -> &Address { &self.author }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the extra data field of the header.
|
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-02-03 13:20:32 +01:00
|
|
|
/// Get the state root field of the header.
|
2016-01-10 23:42:59 +01:00
|
|
|
pub fn state_root(&self) -> &H256 { &self.state_root }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the receipts root field of the header.
|
2016-01-10 23:42:59 +01:00
|
|
|
pub fn receipts_root(&self) -> &H256 { &self.receipts_root }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-08-29 11:35:24 +02:00
|
|
|
/// Get the log bloom field of the header.
|
2018-01-14 22:43:28 +01:00
|
|
|
pub fn log_bloom(&self) -> &Bloom { &self.log_bloom }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-07-19 09:23:53 +02:00
|
|
|
/// Get the transactions root field of the header.
|
|
|
|
pub fn transactions_root(&self) -> &H256 { &self.transactions_root }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-07-19 09:23:53 +02:00
|
|
|
/// Get the uncles hash field of the header.
|
|
|
|
pub fn uncles_hash(&self) -> &H256 { &self.uncles_hash }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-08-29 11:35:24 +02:00
|
|
|
/// Get the gas used field of the header.
|
|
|
|
pub fn gas_used(&self) -> &U256 { &self.gas_used }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the gas limit field of the header.
|
2016-01-15 23:32:17 +01:00
|
|
|
pub fn gas_limit(&self) -> &U256 { &self.gas_limit }
|
2016-01-10 23:42:59 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the difficulty field of the header.
|
2016-01-15 23:32:17 +01:00
|
|
|
pub fn difficulty(&self) -> &U256 { &self.difficulty }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the seal field of the header.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub fn seal(&self) -> &[Bytes] { &self.seal }
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2017-11-01 11:29:03 +01:00
|
|
|
/// Get the seal field with RLP-decoded values as bytes.
|
|
|
|
pub fn decode_seal<'a, T: ::std::iter::FromIterator<&'a [u8]>>(&'a self) -> Result<T, DecoderError> {
|
|
|
|
self.seal.iter().map(|rlp| {
|
2018-04-16 15:52:12 +02:00
|
|
|
Rlp::new(rlp).data()
|
2017-11-01 11:29:03 +01:00
|
|
|
}).collect()
|
|
|
|
}
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2018-04-03 10:01:28 +02:00
|
|
|
/// Get a mutable reference to extra_data
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn extra_data_mut(&mut self) -> &mut Bytes {
|
|
|
|
self.hash = None;
|
|
|
|
&mut self.extra_data
|
|
|
|
}
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the number field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_parent_hash(&mut self, a: H256) {
|
|
|
|
change_field(&mut self.hash, &mut self.parent_hash, a);
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:12:05 +02:00
|
|
|
/// Set the uncles hash field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_uncles_hash(&mut self, a: H256) {
|
|
|
|
change_field(&mut self.hash, &mut self.uncles_hash, a);
|
|
|
|
|
|
|
|
}
|
2016-06-14 18:12:05 +02:00
|
|
|
/// Set the state root field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_state_root(&mut self, a: H256) {
|
|
|
|
change_field(&mut self.hash, &mut self.state_root, a);
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:12:05 +02:00
|
|
|
/// Set the transactions root field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_transactions_root(&mut self, a: H256) {
|
|
|
|
change_field(&mut self.hash, &mut self.transactions_root, a);
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:12:05 +02:00
|
|
|
/// Set the receipts root field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_receipts_root(&mut self, a: H256) {
|
|
|
|
change_field(&mut self.hash, &mut self.receipts_root, a);
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:12:05 +02:00
|
|
|
/// Set the log bloom field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_log_bloom(&mut self, a: Bloom) {
|
|
|
|
change_field(&mut self.hash, &mut self.log_bloom, a);
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the timestamp field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_timestamp(&mut self, a: u64) {
|
|
|
|
change_field(&mut self.hash, &mut self.timestamp, a);
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:58:14 +01:00
|
|
|
/// Set the number field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_number(&mut self, a: BlockNumber) {
|
|
|
|
change_field(&mut self.hash, &mut self.number, a);
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the author field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_author(&mut self, a: Address) {
|
|
|
|
change_field(&mut self.hash, &mut self.author, a);
|
|
|
|
}
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the extra data field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_extra_data(&mut self, a: Bytes) {
|
|
|
|
change_field(&mut self.hash, &mut self.extra_data, a);
|
|
|
|
}
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the gas used field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_gas_used(&mut self, a: U256) {
|
|
|
|
change_field(&mut self.hash, &mut self.gas_used, a);
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the gas limit field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_gas_limit(&mut self, a: U256) {
|
|
|
|
change_field(&mut self.hash, &mut self.gas_limit, a);
|
|
|
|
}
|
2016-01-15 23:32:17 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the difficulty field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_difficulty(&mut self, a: U256) {
|
|
|
|
change_field(&mut self.hash, &mut self.difficulty, a);
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Set the seal field of the header.
|
2018-04-03 10:01:28 +02:00
|
|
|
pub fn set_seal(&mut self, a: Vec<Bytes>) {
|
|
|
|
change_field(&mut self.hash, &mut self.seal, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get & memoize the hash of this header (keccak of the RLP with seal).
|
|
|
|
pub fn compute_hash(&mut self) -> H256 {
|
|
|
|
let hash = self.hash();
|
|
|
|
self.hash = Some(hash);
|
|
|
|
hash
|
|
|
|
}
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2018-04-03 10:01:28 +02:00
|
|
|
/// Get the hash of this header (keccak of the RLP with seal).
|
2015-12-24 17:18:47 +01:00
|
|
|
pub fn hash(&self) -> H256 {
|
2018-04-03 10:01:28 +02:00
|
|
|
self.hash.unwrap_or_else(|| keccak(self.rlp(Seal::With)))
|
2015-12-24 17:18:47 +01:00
|
|
|
}
|
2016-01-09 18:58:04 +01:00
|
|
|
|
2016-01-17 12:00:34 +01:00
|
|
|
/// Get the hash of the header excluding the seal
|
|
|
|
pub fn bare_hash(&self) -> H256 {
|
2018-04-03 10:01:28 +02:00
|
|
|
keccak(self.rlp(Seal::Without))
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:01:28 +02:00
|
|
|
/// Encode the header, getting a type-safe wrapper around the RLP.
|
|
|
|
pub fn encoded(&self) -> ::encoded::Header {
|
|
|
|
::encoded::Header::new(self.rlp(Seal::With))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the RLP representation of this Header.
|
|
|
|
fn rlp(&self, with_seal: Seal) -> Bytes {
|
|
|
|
let mut s = RlpStream::new();
|
|
|
|
self.stream_rlp(&mut s, with_seal);
|
|
|
|
s.out()
|
2016-01-09 18:58:04 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Place this header into an RLP stream `s`, optionally `with_seal`.
|
2018-04-03 10:01:28 +02:00
|
|
|
fn stream_rlp(&self, s: &mut RlpStream, with_seal: Seal) {
|
|
|
|
if let Seal::With = with_seal {
|
|
|
|
s.begin_list(13 + self.seal.len());
|
|
|
|
} else {
|
|
|
|
s.begin_list(13);
|
|
|
|
}
|
|
|
|
|
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);
|
2018-04-03 10:01:28 +02:00
|
|
|
|
2016-01-17 15:56:09 +01:00
|
|
|
if let Seal::With = with_seal {
|
2016-05-17 10:50:11 +02:00
|
|
|
for b in &self.seal {
|
2016-07-26 20:31:25 +02:00
|
|
|
s.append_raw(b, 1);
|
2016-01-17 15:56:09 +01:00
|
|
|
}
|
2016-01-09 18:58:04 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-03 10:01:28 +02:00
|
|
|
}
|
2016-01-09 18:58:04 +01:00
|
|
|
|
2018-04-03 10:01:28 +02:00
|
|
|
/// Alter value of given field, reset memoised hash if changed.
|
|
|
|
fn change_field<T>(hash: &mut Option<H256>, field: &mut T, value: T) where T: PartialEq<T> {
|
|
|
|
if field != &value {
|
|
|
|
*field = value;
|
|
|
|
*hash = None;
|
2017-09-05 17:54:05 +02:00
|
|
|
}
|
2015-12-08 16:31:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 16:44:14 +01:00
|
|
|
impl Decodable for Header {
|
2018-04-16 15:52:12 +02:00
|
|
|
fn decode(r: &Rlp) -> Result<Self, DecoderError> {
|
2015-12-14 15:22:41 +01:00
|
|
|
let mut blockheader = Header {
|
2016-12-27 12:53:56 +01:00
|
|
|
parent_hash: r.val_at(0)?,
|
|
|
|
uncles_hash: r.val_at(1)?,
|
|
|
|
author: r.val_at(2)?,
|
|
|
|
state_root: r.val_at(3)?,
|
|
|
|
transactions_root: r.val_at(4)?,
|
|
|
|
receipts_root: r.val_at(5)?,
|
|
|
|
log_bloom: r.val_at(6)?,
|
|
|
|
difficulty: r.val_at(7)?,
|
|
|
|
number: r.val_at(8)?,
|
|
|
|
gas_limit: r.val_at(9)?,
|
|
|
|
gas_used: r.val_at(10)?,
|
2017-07-29 17:12:07 +02:00
|
|
|
timestamp: cmp::min(r.val_at::<U256>(11)?, u64::max_value().into()).as_u64(),
|
2016-12-27 12:53:56 +01:00
|
|
|
extra_data: r.val_at(12)?,
|
2015-12-14 12:09:32 +01:00
|
|
|
seal: vec![],
|
2018-04-03 10:01:28 +02:00
|
|
|
hash: keccak(r.as_raw()).into(),
|
2015-12-14 12:09:32 +01:00
|
|
|
};
|
2015-12-14 15:22:41 +01:00
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
for i in 13..r.item_count()? {
|
2016-12-27 12:53:56 +01:00
|
|
|
blockheader.seal.push(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 {
|
2016-01-27 17:22:01 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
self.stream_rlp(s, Seal::With);
|
2015-12-08 16:31:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:50:24 +02:00
|
|
|
impl HeapSizeOf for Header {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.extra_data.heap_size_of_children() + self.seal.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl ::parity_machine::Header for Header {
|
|
|
|
fn bare_hash(&self) -> H256 { Header::bare_hash(self) }
|
|
|
|
fn hash(&self) -> H256 { Header::hash(self) }
|
|
|
|
fn seal(&self) -> &[Vec<u8>] { Header::seal(self) }
|
|
|
|
fn author(&self) -> &Address { Header::author(self) }
|
|
|
|
fn number(&self) -> BlockNumber { Header::number(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::parity_machine::ScoredHeader for Header {
|
2018-05-16 08:58:01 +02:00
|
|
|
type Value = U256;
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn score(&self) -> &U256 { self.difficulty() }
|
|
|
|
fn set_score(&mut self, score: U256) { self.set_difficulty(score) }
|
|
|
|
}
|
|
|
|
|
2018-05-16 08:58:01 +02:00
|
|
|
impl ::parity_machine::Header for ExtendedHeader {
|
|
|
|
fn bare_hash(&self) -> H256 { self.header.bare_hash() }
|
|
|
|
fn hash(&self) -> H256 { self.header.hash() }
|
|
|
|
fn seal(&self) -> &[Vec<u8>] { self.header.seal() }
|
|
|
|
fn author(&self) -> &Address { self.header.author() }
|
|
|
|
fn number(&self) -> BlockNumber { self.header.number() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::parity_machine::ScoredHeader for ExtendedHeader {
|
|
|
|
type Value = U256;
|
|
|
|
|
|
|
|
fn score(&self) -> &U256 { self.header.difficulty() }
|
|
|
|
fn set_score(&mut self, score: U256) { self.header.set_difficulty(score) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::parity_machine::TotalScoredHeader for ExtendedHeader {
|
|
|
|
type Value = U256;
|
|
|
|
|
|
|
|
fn total_score(&self) -> U256 { self.parent_total_difficulty + *self.header.difficulty() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::parity_machine::FinalizableHeader for ExtendedHeader {
|
|
|
|
fn is_finalized(&self) -> bool { self.is_finalized }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::parity_machine::WithMetadataHeader for ExtendedHeader {
|
|
|
|
fn metadata(&self) -> Option<&[u8]> { self.metadata.as_ref().map(|v| v.as_ref()) }
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:45:33 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-07-06 11:26:14 +02:00
|
|
|
use rustc_hex::FromHex;
|
2016-09-01 14:29:59 +02:00
|
|
|
use rlp;
|
2016-05-17 10:50:11 +02:00
|
|
|
use super::Header;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_seal_fields() {
|
|
|
|
// that's rlp of block header created with ethash engine.
|
|
|
|
let header_rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap();
|
2016-05-17 11:07:51 +02:00
|
|
|
let mix_hash = "a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap();
|
2017-11-01 11:29:03 +01:00
|
|
|
let mix_hash_decoded = "a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap();
|
2016-05-17 11:07:51 +02:00
|
|
|
let nonce = "88ab4e252a7e8c2a23".from_hex().unwrap();
|
2017-11-01 11:29:03 +01:00
|
|
|
let nonce_decoded = "ab4e252a7e8c2a23".from_hex().unwrap();
|
2016-05-17 10:50:11 +02:00
|
|
|
|
2018-05-08 11:22:12 +02:00
|
|
|
let header: Header = rlp::decode(&header_rlp).expect("error decoding header");
|
2017-11-01 11:29:03 +01:00
|
|
|
let seal_fields = header.seal.clone();
|
2016-05-17 10:50:11 +02:00
|
|
|
assert_eq!(seal_fields.len(), 2);
|
|
|
|
assert_eq!(seal_fields[0], mix_hash);
|
|
|
|
assert_eq!(seal_fields[1], nonce);
|
2017-11-01 11:29:03 +01:00
|
|
|
|
|
|
|
let decoded_seal = header.decode_seal::<Vec<_>>().unwrap();
|
|
|
|
assert_eq!(decoded_seal.len(), 2);
|
|
|
|
assert_eq!(decoded_seal[0], &*mix_hash_decoded);
|
|
|
|
assert_eq!(decoded_seal[1], &*nonce_decoded);
|
2016-05-17 10:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_and_encode_header() {
|
|
|
|
// that's rlp of block header created with ethash engine.
|
|
|
|
let header_rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap();
|
|
|
|
|
2018-05-08 11:22:12 +02:00
|
|
|
let header: Header = rlp::decode(&header_rlp).expect("error decoding header");
|
2017-06-28 14:16:53 +02:00
|
|
|
let encoded_header = rlp::encode(&header).into_vec();
|
2016-05-17 10:50:11 +02:00
|
|
|
|
|
|
|
assert_eq!(header_rlp, encoded_header);
|
|
|
|
}
|
2015-12-09 00:45:33 +01:00
|
|
|
}
|