Use ByteCode::new_raw_checked and propagate error (#10584)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Daniel Calderón Díaz
2024-09-02 08:53:24 -06:00
committed by GitHub
parent 449d63d980
commit 212a6a0a31
2 changed files with 20 additions and 5 deletions

View File

@@ -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<Self, BytecodeDecodeError> {
Ok(Self(RevmBytecode::new_raw(bytecode)))
}
}
impl Compact for Bytecode {

View File

@@ -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
};