refactor: rename HeaderLocked to SealedHeader (#173)

This commit is contained in:
Matthias Seitz
2022-11-07 21:06:53 +01:00
committed by GitHub
parent 099d3cee9a
commit caad026c70
10 changed files with 78 additions and 77 deletions

View File

@@ -1,4 +1,4 @@
use crate::{Header, HeaderLocked, Receipt, Transaction, TransactionSigned, H256};
use crate::{Header, Receipt, SealedHeader, Transaction, TransactionSigned, H256};
use std::ops::Deref;
/// Ethereum full block.
@@ -11,7 +11,7 @@ pub struct Block {
/// Block receipts.
pub receipts: Vec<Receipt>,
/// Ommers/uncles header
pub ommers: Vec<HeaderLocked>,
pub ommers: Vec<SealedHeader>,
}
impl Deref for Block {
@@ -25,13 +25,13 @@ impl Deref for Block {
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BlockLocked {
/// Locked block header.
pub header: HeaderLocked,
pub header: SealedHeader,
/// Transactions with signatures.
pub body: Vec<TransactionSigned>,
/// Block receipts.
pub receipts: Vec<Receipt>,
/// Omners/uncles header
pub ommers: Vec<HeaderLocked>,
pub ommers: Vec<SealedHeader>,
}
impl BlockLocked {

View File

@@ -66,17 +66,17 @@ pub struct Header {
impl Header {
/// Heavy function that will calculate hash of data and will *not* save the change to metadata.
/// Use lock, HeaderLocked and unlock if you need hash to be persistent.
/// Use [`Header::seal`], [`SealedHeader`] and unlock if you need hash to be persistent.
pub fn hash_slow(&self) -> H256 {
let mut out = BytesMut::new();
self.encode(&mut out);
H256::from_slice(keccak256(&out).as_slice())
}
/// Calculate hash and lock the Header so that it can't be changed.
pub fn lock(self) -> HeaderLocked {
/// Calculate hash and seal the Header so that it can't be changed.
pub fn seal(self) -> SealedHeader {
let hash = self.hash_slow();
HeaderLocked { header: self, hash }
SealedHeader { header: self, hash }
}
fn header_payload_length(&self) -> usize {
@@ -174,22 +174,23 @@ impl Decodable for Header {
}
}
/// A [`Header`] that is sealed at a precalculated hash, use [`SealedHeader::unseal()`] if you want
/// to modify header.
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
/// HeaderLocked that has precalculated hash, use unlock if you want to modify header.
pub struct HeaderLocked {
pub struct SealedHeader {
/// Locked Header fields.
header: Header,
/// Locked Header hash.
hash: BlockHash,
}
impl AsRef<Header> for HeaderLocked {
impl AsRef<Header> for SealedHeader {
fn as_ref(&self) -> &Header {
&self.header
}
}
impl Deref for HeaderLocked {
impl Deref for SealedHeader {
type Target = Header;
fn deref(&self) -> &Self::Target {
@@ -197,16 +198,16 @@ impl Deref for HeaderLocked {
}
}
impl HeaderLocked {
/// Construct a new locked header.
/// Applicable when hash is known from
/// the database provided it's not corrupted.
impl SealedHeader {
/// Construct a new sealed header.
///
/// Applicable when hash is known from the database provided it's not corrupted.
pub fn new(header: Header, hash: H256) -> Self {
Self { header, hash }
}
/// Extract raw header that can be modified.
pub fn unlock(self) -> Header {
pub fn unseal(self) -> Header {
self.header
}

View File

@@ -23,7 +23,7 @@ mod transaction;
pub use account::Account;
pub use block::{Block, BlockLocked};
pub use chain::Chain;
pub use header::{Header, HeaderLocked};
pub use header::{Header, SealedHeader};
pub use hex_bytes::Bytes;
pub use integer_list::IntegerList;
pub use jsonu256::JsonU256;