Skip to content
This repository has been archived by the owner on Feb 21, 2025. It is now read-only.

Commit

Permalink
chore(genesis): gas limit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Feb 6, 2025
1 parent c1356e3 commit 35bb793
Showing 1 changed file with 112 additions and 2 deletions.
114 changes: 112 additions & 2 deletions crates/genesis/src/updates/gas_limit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The gas limit update type.
use alloy_primitives::U64;
use alloy_primitives::{U256, U64};
use alloy_sol_types::{sol, SolType};

use crate::{GasLimitUpdateError, SystemConfig, SystemConfigLog};
Expand Down Expand Up @@ -46,6 +46,12 @@ impl TryFrom<&SystemConfigLog> for GasLimitUpdate {
return Err(GasLimitUpdateError::GasLimitDecodingError);
};

// Prevent overflows here.
let max = U256::from(u64::MAX as u128);
if gas_limit > max {
return Err(GasLimitUpdateError::GasLimitDecodingError);
}

Ok(Self { gas_limit: U64::from(gas_limit).saturating_to::<u64>() })
}
}
Expand All @@ -54,7 +60,7 @@ impl TryFrom<&SystemConfigLog> for GasLimitUpdate {
mod tests {
use super::*;
use crate::{CONFIG_UPDATE_EVENT_VERSION_0, CONFIG_UPDATE_TOPIC};
use alloy_primitives::{hex, Address, Log, LogData, B256};
use alloy_primitives::{hex, Address, Bytes, Log, LogData, B256};

#[test]
fn test_gas_limit_update_try_from() {
Expand All @@ -77,4 +83,108 @@ mod tests {

assert_eq!(update.gas_limit, 0xbeef_u64);
}

#[test]
fn test_gas_limit_update_invalid_data_len() {
let log =
Log { address: Address::ZERO, data: LogData::new_unchecked(vec![], Bytes::default()) };
let system_log = SystemConfigLog::new(log, false);
let err = GasLimitUpdate::try_from(&system_log).unwrap_err();
assert_eq!(err, GasLimitUpdateError::InvalidDataLen(0));
}

#[test]
fn test_gas_limit_update_pointer_decoding_error() {
let log = Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![
CONFIG_UPDATE_TOPIC,
CONFIG_UPDATE_EVENT_VERSION_0,
B256::ZERO,
],
hex!("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000babe0000beef").into()
)
};

let system_log = SystemConfigLog::new(log, false);
let err = GasLimitUpdate::try_from(&system_log).unwrap_err();
assert_eq!(err, GasLimitUpdateError::PointerDecodingError);
}

#[test]
fn test_gas_limit_update_invalid_pointer_length() {
let log = Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![
CONFIG_UPDATE_TOPIC,
CONFIG_UPDATE_EVENT_VERSION_0,
B256::ZERO,
],
hex!("000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000babe0000beef").into()
)
};

let system_log = SystemConfigLog::new(log, false);
let err = GasLimitUpdate::try_from(&system_log).unwrap_err();
assert_eq!(err, GasLimitUpdateError::InvalidDataPointer(33));
}

#[test]
fn test_gas_limit_update_length_decoding_error() {
let log = Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![
CONFIG_UPDATE_TOPIC,
CONFIG_UPDATE_EVENT_VERSION_0,
B256::ZERO,
],
hex!("0000000000000000000000000000000000000000000000000000000000000020FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000babe0000beef").into()
)
};

let system_log = SystemConfigLog::new(log, false);
let err = GasLimitUpdate::try_from(&system_log).unwrap_err();
assert_eq!(err, GasLimitUpdateError::LengthDecodingError);
}

#[test]
fn test_gas_limit_update_invalid_data_length() {
let log = Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![
CONFIG_UPDATE_TOPIC,
CONFIG_UPDATE_EVENT_VERSION_0,
B256::ZERO,
],
hex!("000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000210000000000000000000000000000000000000000000000000000babe0000beef").into()
)
};

let system_log = SystemConfigLog::new(log, false);
let err = GasLimitUpdate::try_from(&system_log).unwrap_err();
assert_eq!(err, GasLimitUpdateError::InvalidDataLength(33));
}

#[test]
fn test_gas_limit_update_gas_limit_decoding_error() {
let log = Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![
CONFIG_UPDATE_TOPIC,
CONFIG_UPDATE_EVENT_VERSION_0,
B256::ZERO,
],
hex!("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").into()
)
};

let system_log = SystemConfigLog::new(log, false);
let err = GasLimitUpdate::try_from(&system_log).unwrap_err();
assert_eq!(err, GasLimitUpdateError::GasLimitDecodingError);
}
}

0 comments on commit 35bb793

Please sign in to comment.