Skip to content

Commit

Permalink
feat: add kzg_to_versioned_hash (#4085)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Cline <[email protected]>
  • Loading branch information
PatStiles and Rjected authored Aug 10, 2023
1 parent 2338720 commit c412f39
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
64 changes: 32 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Collection of methods for block validation.
use reth_interfaces::{consensus::ConsensusError, Result as RethResult};
use reth_primitives::{
blobfee::calculate_excess_blob_gas,
constants::{
self,
eip4844::{DATA_GAS_PER_BLOB, MAX_DATA_GAS_PER_BLOCK},
},
eip4844::calculate_excess_blob_gas,
BlockNumber, ChainSpec, Hardfork, Header, InvalidTransactionError, SealedBlock, SealedHeader,
Transaction, TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxLegacy,
};
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ once_cell = "1.17.0"
zstd = { version = "0.12", features = ["experimental"] }
paste = "1.0"
tempfile = "3.3"
sha2 = "0.10.7"

# proof related
triehash = "0.8"
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/constants/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ pub static KZG_TRUSTED_SETUP: Lazy<Arc<KzgSettings>> = Lazy::new(|| {
file.write_all(TRUSTED_SETUP_RAW.as_bytes()).unwrap();
Arc::new(KzgSettings::load_trusted_setup_file(file.path().into()).unwrap())
});

/// Commitment version of a KZG commitment
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
//! Helpers for working with EIP-4844 blob fee
use crate::{
constants::eip4844::{TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG},
kzg::KzgCommitment,
H256,
};
use sha2::{Digest, Sha256};

use crate::constants::eip4844::TARGET_DATA_GAS_PER_BLOCK;
/// Calculates the versioned hash for a KzgCommitment
///
/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension)
pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> H256 {
let mut res = Sha256::digest(commitment.as_slice());
res[0] = VERSIONED_HASH_VERSION_KZG;
H256::from_slice(&res)
}

/// Calculates the excess data gas for the next block, after applying the current set of blobs on
/// top of the excess data gas.
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
basefee::calculate_next_block_base_fee,
blobfee::calculate_excess_blob_gas,
eip4844::calculate_excess_blob_gas,
keccak256,
proofs::{EMPTY_LIST_HASH, EMPTY_ROOT},
BaseFeeParams, BlockBodyRoots, BlockHash, BlockNumHash, BlockNumber, Bloom, Bytes, H160, H256,
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub mod abi;
mod account;
pub mod basefee;
mod bits;
pub mod blobfee;
mod block;
pub mod bloom;
mod chain;
mod compression;
pub mod constants;
pub mod contract;
pub mod eip4844;
mod forkid;
pub mod fs;
mod genesis;
Expand Down

0 comments on commit c412f39

Please sign in to comment.