Skip to content

Commit

Permalink
Use ByteCode::new_raw_checked and propagate error (#10584)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
danielcdz and mattsse committed Sep 2, 2024
1 parent 449d63d commit 212a6a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 9 additions & 1 deletion crates/primitives-traits/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self, BytecodeDecodeError> {
Ok(Self(RevmBytecode::new_raw(bytecode)))
}
}

impl Compact for Bytecode {
Expand Down
15 changes: 11 additions & 4 deletions crates/storage/db-common/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down

0 comments on commit 212a6a0

Please sign in to comment.