Skip to content

Commit

Permalink
Merge branch 'main' into serde-optional-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks authored Nov 18, 2024
2 parents 3564a9a + d657ac4 commit aae1318
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/consensus/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub struct Header {
/// block’s header, in its entirety; formally Hp.
pub parent_hash: B256,
/// The Keccak 256-bit hash of the ommers list portion of this block; formally Ho.
#[cfg_attr(feature = "serde", serde(rename = "sha3Uncles"))]
#[cfg_attr(feature = "serde", serde(rename = "sha3Uncles", alias = "ommersHash"))]
pub ommers_hash: B256,
/// The 160-bit address to which all fees collected from the successful mining of this block
/// be transferred; formally Hc.
#[cfg_attr(feature = "serde", serde(rename = "miner"))]
#[cfg_attr(feature = "serde", serde(rename = "miner", alias = "beneficiary"))]
pub beneficiary: Address,
/// The Keccak 256-bit hash of the root node of the state trie, after all transactions are
/// executed and finalisations applied; formally Hr.
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub struct TxEip1559 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
#[cfg_attr(
feature = "serde",
serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
)]
pub gas_limit: u64,
/// A scalar value equal to the maximum
/// amount of gas that should be used in executing
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub struct TxEip2930 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
#[cfg_attr(
feature = "serde",
serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
)]
pub gas_limit: u64,
/// The 160-bit address of the message call’s recipient or, for a contract creation
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ pub struct TxEip4844 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
#[cfg_attr(
feature = "serde",
serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
)]
pub gas_limit: u64,
/// A scalar value equal to the maximum
/// amount of gas that should be used in executing
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub struct TxEip7702 {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
#[cfg_attr(
feature = "serde",
serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
)]
pub gas_limit: u64,
/// A scalar value equal to the maximum
/// amount of gas that should be used in executing
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ pub struct TxLegacy {
/// this transaction. This is paid up-front, before any
/// computation is done and may not be increased
/// later; formally Tg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
#[cfg_attr(
feature = "serde",
serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
)]
pub gas_limit: u64,
/// The 160-bit address of the message call’s recipient or, for a contract creation
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
Expand Down
126 changes: 126 additions & 0 deletions crates/serde/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,130 @@ mod tests {

assert_eq!(num_key.as_b256(), hash_key.as_b256());
}

#[test]
fn test_json_storage_key_from_b256() {
let b256_value = B256::from([1u8; 32]);
let key = JsonStorageKey::from(b256_value);
assert_eq!(key, JsonStorageKey::Hash(b256_value));
assert_eq!(
key.to_string(),
"0x0101010101010101010101010101010101010101010101010101010101010101"
);
}

#[test]
fn test_json_storage_key_from_u256() {
let u256_value = U256::from(42);
let key = JsonStorageKey::from(u256_value);
assert_eq!(key, JsonStorageKey::Number(u256_value));
assert_eq!(key.to_string(), "0x2a");
}

#[test]
fn test_json_storage_key_from_u8_array() {
let bytes = [0u8; 32];
let key = JsonStorageKey::from(bytes);
assert_eq!(key, JsonStorageKey::Hash(B256::from(bytes)));
}

#[test]
fn test_from_str_parsing() {
let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101";
let key = JsonStorageKey::from_str(hex_str).unwrap();
assert_eq!(key, JsonStorageKey::Hash(B256::from_str(hex_str).unwrap()));

let num_str = "42";
let key = JsonStorageKey::from_str(num_str).unwrap();
assert_eq!(key, JsonStorageKey::Number(U256::from(42)));
}

#[test]
fn test_deserialize_storage_map_with_valid_data() {
let json_data = json!({
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x22",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x33"
});

// Specify the deserialization type explicitly
let deserialized: Option<BTreeMap<B256, B256>> = deserialize_storage_map(
&serde_json::from_value::<serde_json::Value>(json_data).unwrap(),
)
.unwrap();

assert_eq!(
deserialized.unwrap(),
BTreeMap::from([
(B256::from(U256::from(1u128)), B256::from(U256::from(0x22u128))),
(B256::from(U256::from(2u128)), B256::from(U256::from(0x33u128)))
])
);
}

#[test]
fn test_deserialize_storage_map_with_empty_data() {
let json_data = json!({});
let deserialized: Option<BTreeMap<B256, B256>> = deserialize_storage_map(
&serde_json::from_value::<serde_json::Value>(json_data).unwrap(),
)
.unwrap();
assert!(deserialized.unwrap().is_empty());
}

#[test]
fn test_deserialize_storage_map_with_none() {
let json_data = json!(null);
let deserialized: Option<BTreeMap<B256, B256>> = deserialize_storage_map(
&serde_json::from_value::<serde_json::Value>(json_data).unwrap(),
)
.unwrap();
assert!(deserialized.is_none());
}

#[test]
fn test_from_bytes_to_b256_with_valid_input() {
// Test case with input less than 32 bytes, should be left-padded with zeros
let bytes = Bytes::from(vec![0x1, 0x2, 0x3, 0x4]);
let result = from_bytes_to_b256::<serde_json::Value>(bytes).unwrap();
let expected = B256::from_slice(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2, 3, 4,
]);
assert_eq!(result, expected);
}

#[test]
fn test_from_bytes_to_b256_with_exact_32_bytes() {
// Test case with input exactly 32 bytes long
let bytes = Bytes::from(vec![
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20,
]);
let result = from_bytes_to_b256::<serde_json::Value>(bytes).unwrap();
let expected = B256::from_slice(&[
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20,
]);
assert_eq!(result, expected);
}

#[test]
fn test_from_bytes_to_b256_with_input_too_long() {
// Test case with input longer than 32 bytes, should return an error
let bytes = Bytes::from(vec![0x1; 33]); // 33 bytes long
let result = from_bytes_to_b256::<serde_json::Value>(bytes);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "input too long to be a B256");
}

#[test]
fn test_from_bytes_to_b256_with_empty_input() {
// Test case with empty input, should be all zeros
let bytes = Bytes::from(vec![]);
let result = from_bytes_to_b256::<serde_json::Value>(bytes).unwrap();
let expected = B256::from_slice(&[0; 32]); // All zeros
assert_eq!(result, expected);
}
}

0 comments on commit aae1318

Please sign in to comment.