From c412f3935dbedd047905aedb986bbd118165155e Mon Sep 17 00:00:00 2001 From: PatStiles <33334338+PatStiles@users.noreply.github.com> Date: Thu, 10 Aug 2023 12:56:03 -0500 Subject: [PATCH] feat: add kzg_to_versioned_hash (#4085) Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com> --- Cargo.lock | 64 +++++++++---------- crates/consensus/common/src/validation.rs | 2 +- crates/primitives/Cargo.toml | 1 + crates/primitives/src/constants/eip4844.rs | 3 + .../primitives/src/{blobfee.rs => eip4844.rs} | 15 ++++- crates/primitives/src/header.rs | 2 +- crates/primitives/src/lib.rs | 2 +- 7 files changed, 53 insertions(+), 36 deletions(-) rename crates/primitives/src/{blobfee.rs => eip4844.rs} (50%) diff --git a/Cargo.lock b/Cargo.lock index cf825eaba2ec..52e30d02a76b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,9 +119,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "86b8f9420f797f2d9e935edf629310eb938a0d839f984e25327f3c7eed22300c" dependencies = [ "memchr", ] @@ -220,9 +220,9 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +checksum = "c677ab05e09154296dd37acecd46420c17b9713e8366facafa8fc0885167cf4c" dependencies = [ "anstyle", "windows-sys 0.48.0", @@ -420,9 +420,9 @@ dependencies = [ [[package]] name = "async-lock" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" dependencies = [ "event-listener", ] @@ -723,7 +723,7 @@ dependencies = [ [[package]] name = "boa_ast" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "bitflags 2.3.3", "boa_interner", @@ -736,7 +736,7 @@ dependencies = [ [[package]] name = "boa_engine" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "bitflags 2.3.3", "boa_ast", @@ -774,7 +774,7 @@ dependencies = [ [[package]] name = "boa_gc" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "boa_macros", "boa_profiler", @@ -785,7 +785,7 @@ dependencies = [ [[package]] name = "boa_icu_provider" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "icu_collections", "icu_normalizer", @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "boa_interner" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "boa_gc", "boa_macros", @@ -813,7 +813,7 @@ dependencies = [ [[package]] name = "boa_macros" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "proc-macro2 1.0.66", "quote 1.0.32", @@ -824,7 +824,7 @@ dependencies = [ [[package]] name = "boa_parser" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" dependencies = [ "bitflags 2.3.3", "boa_ast", @@ -844,7 +844,7 @@ dependencies = [ [[package]] name = "boa_profiler" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9665f8be3be60f475d816ca10430631f43d6c962" +source = "git+https://github.com/boa-dev/boa#22b014d5d5ab9fba5dc467734227558e75c66d20" [[package]] name = "brotli" @@ -933,7 +933,7 @@ dependencies = [ [[package]] name = "c-kzg" version = "0.1.0" -source = "git+https://github.com/ethereum/c-kzg-4844#6353f689e5d2802bbaf221253b3acafe4228331c" +source = "git+https://github.com/ethereum/c-kzg-4844#9d85ed8b194eee2a70380e58b6c45909f75933ea" dependencies = [ "bindgen 0.64.0 (git+https://github.com/rust-lang/rust-bindgen?rev=0de11f0a521611ac8738b7b01d19dddaf3899e66)", "cc", @@ -1241,9 +1241,9 @@ dependencies = [ [[package]] name = "const-oid" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "795bc6e66a8e340f075fcf6227e417a2dc976b92b91f3cdc778bb858778b6747" +checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" [[package]] name = "const-str" @@ -1363,9 +1363,9 @@ dependencies = [ [[package]] name = "critical-section" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52" +checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" [[package]] name = "crossbeam-channel" @@ -2725,7 +2725,7 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick 1.0.3", "bstr 1.6.0", "fnv", "log", @@ -4585,9 +4585,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c516611246607d0c04186886dbb3a754368ef82c79e9827a802c6d836dd111c" +checksum = "12cc1b0bf1727a77a54b6654e7b5f1af8604923edc8b81885f8ec92f9e3f0a05" [[package]] name = "pin-utils" @@ -5124,7 +5124,7 @@ version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick 1.0.3", "memchr", "regex-automata 0.3.6", "regex-syntax 0.7.4", @@ -5145,7 +5145,7 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick 1.0.3", "memchr", "regex-syntax 0.7.4", ] @@ -5829,6 +5829,7 @@ dependencies = [ "serde", "serde_json", "serde_with", + "sha2 0.10.7", "strum 0.25.0", "sucds", "tempfile", @@ -6470,9 +6471,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.101.2" +version = "0.101.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "513722fd73ad80a71f72b61009ea1b584bcfa1483ca93949c8f290298837fa59" +checksum = "261e9e0888cba427c3316e6322805653c9425240b6fd96cee7cb671ab70ab8d0" dependencies = [ "ring", "untrusted", @@ -7510,11 +7511,10 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.29.1" +version = "1.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "532826ff75199d5833b9d2c5fe410f29235e25704ee5f0ef599fb51c21f4a4da" +checksum = "2d3ce25f50619af8b0aec2eb23deebe84249e19e2ddd393a6e16e3300a6dadfd" dependencies = [ - "autocfg", "backtrace", "bytes", "libc", @@ -7523,7 +7523,7 @@ dependencies = [ "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", - "socket2 0.4.9", + "socket2 0.5.3", "tokio-macros", "windows-sys 0.48.0", ] @@ -8463,9 +8463,9 @@ checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" [[package]] name = "winnow" -version = "0.5.4" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acaaa1190073b2b101e15083c38ee8ec891b5e05cbee516521e94ec008f61e64" +checksum = "48f9aab5bf4474679c9908b82c245a17ee48b55e07350d439ef522020cce22ff" dependencies = [ "memchr", ] diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index ee055d1ffadd..2c2bde80fa40 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -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, }; diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 69cf6d3e0f6e..8716a1e228b4 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -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" diff --git a/crates/primitives/src/constants/eip4844.rs b/crates/primitives/src/constants/eip4844.rs index db86b18432a9..602229d2cc85 100644 --- a/crates/primitives/src/constants/eip4844.rs +++ b/crates/primitives/src/constants/eip4844.rs @@ -37,3 +37,6 @@ pub static KZG_TRUSTED_SETUP: Lazy> = 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; diff --git a/crates/primitives/src/blobfee.rs b/crates/primitives/src/eip4844.rs similarity index 50% rename from crates/primitives/src/blobfee.rs rename to crates/primitives/src/eip4844.rs index e82b5d2f8c65..01d954373066 100644 --- a/crates/primitives/src/blobfee.rs +++ b/crates/primitives/src/eip4844.rs @@ -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. diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index 8aeab18b84f8..bc6d75194a2b 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -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, diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 96941dfadf99..300a215f4af2 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -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;