Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eip7685: add unit tests and reduce paths #1531

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions crates/eips/src/eip7685.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,67 @@ impl Requests {
/// sha256(sha256(requests_0) ++ sha256(requests_1) ++ ...)
/// ```
#[cfg(feature = "sha2")]
pub fn requests_hash(&self) -> alloy_primitives::B256 {
pub fn requests_hash(&self) -> B256 {
use sha2::{Digest, Sha256};
let mut hash = sha2::Sha256::new();
let mut hash = Sha256::new();
for (ty, req) in self.0.iter().enumerate() {
let mut req_hash = Sha256::new();
req_hash.update([ty as u8]);
req_hash.update(req);
hash.update(req_hash.finalize());
}
alloy_primitives::B256::from(hash.finalize().as_ref())
B256::from(hash.finalize().as_ref())
}

/// Extend this container with requests from another container.
pub fn extend(&mut self, other: Self) {
self.0.extend(other.take());
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_extend() {
// Test extending a Requests container with another Requests container
let mut reqs1 = Requests::new(vec![Bytes::from(vec![0x01, 0x02])]);
let reqs2 =
Requests::new(vec![Bytes::from(vec![0x03, 0x04]), Bytes::from(vec![0x05, 0x06])]);

// Extend reqs1 with reqs2
reqs1.extend(reqs2);

// Ensure the requests are correctly combined
assert_eq!(reqs1.0.len(), 3);
assert_eq!(
reqs1.0,
vec![
Bytes::from(vec![0x01, 0x02]),
Bytes::from(vec![0x03, 0x04]),
Bytes::from(vec![0x05, 0x06])
]
);
}

#[test]
fn test_consistent_requests_hash() {
// We test that the empty requests hash is consistent with the EIP-7685 definition.
assert_eq!(
Requests(vec![Bytes::from(vec![]), Bytes::from(vec![]), Bytes::from(vec![])])
.requests_hash(),
EMPTY_REQUESTS_HASH,
);

// Test to hash a non-empty vector of requests.
assert_eq!(
Requests(vec![
Bytes::from(vec![0x0a, 0x0b, 0x0c]),
Bytes::from(vec![0x0d, 0x0e, 0x0f])
])
.requests_hash(),
b256!("be3a57667b9bb9e0275019c0faf0f415fdc8385a408fd03e13a5c50615e3530c"),
);
}
}