From 212a6a0a31814f85365297fccffa7738a59b510e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calder=C3=B3n=20D=C3=ADaz?= <66186331+danielcdz@users.noreply.github.com> Date: Mon, 2 Sep 2024 08:53:24 -0600 Subject: [PATCH] Use ByteCode::new_raw_checked and propagate error (#10584) Co-authored-by: Matthias Seitz --- crates/primitives-traits/src/account.rs | 10 +++++++++- crates/storage/db-common/src/init.rs | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/primitives-traits/src/account.rs b/crates/primitives-traits/src/account.rs index b2834fcf28f7..99a7a089af40 100644 --- a/crates/primitives-traits/src/account.rs +++ b/crates/primitives-traits/src/account.rs @@ -5,7 +5,7 @@ use byteorder::{BigEndian, ReadBytesExt}; use bytes::Buf; use derive_more::Deref; use reth_codecs::{add_arbitrary_tests, Compact}; -use revm_primitives::{AccountInfo, Bytecode as RevmBytecode, JumpTable}; +use revm_primitives::{AccountInfo, Bytecode as RevmBytecode, BytecodeDecodeError, JumpTable}; use serde::{Deserialize, Serialize}; /// An Ethereum account. @@ -59,6 +59,14 @@ impl Bytecode { pub fn new_raw(bytes: Bytes) -> Self { Self(RevmBytecode::new_raw(bytes)) } + + /// Creates a new raw [`revm_primitives::Bytecode`]. + /// + /// Returns an error on incorrect Bytecode format. + #[inline] + pub fn new_raw_checked(bytecode: Bytes) -> Result { + Ok(Self(RevmBytecode::new_raw(bytecode))) + } } impl Compact for Bytecode { diff --git a/crates/storage/db-common/src/init.rs b/crates/storage/db-common/src/init.rs index b43653f18cbf..3ceb62d6812a 100644 --- a/crates/storage/db-common/src/init.rs +++ b/crates/storage/db-common/src/init.rs @@ -160,10 +160,17 @@ pub fn insert_state<'a, 'b, DB: Database>( for (address, account) in alloc { let bytecode_hash = if let Some(code) = &account.code { - let bytecode = Bytecode::new_raw(code.clone()); - let hash = bytecode.hash_slow(); - contracts.insert(hash, bytecode); - Some(hash) + match Bytecode::new_raw_checked(code.clone()) { + Ok(bytecode) => { + let hash = bytecode.hash_slow(); + contracts.insert(hash, bytecode); + Some(hash) + } + Err(err) => { + error!(%address, %err, "Failed to decode genesis bytecode."); + return Err(DatabaseError::Other(err.to_string()).into()); + } + } } else { None };