mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
add unit tests for BlobTransactionSidecar (#6431)
This commit is contained in:
19
crates/primitives/src/transaction/blob_data/README.md
Normal file
19
crates/primitives/src/transaction/blob_data/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Blob Data for Unit Tests
|
||||
|
||||
This file contains information about blobs used in unit tests. Each blob is associated with a transaction for reference.
|
||||
|
||||
- **Blob 1:**
|
||||
- **Transaction:** https://sepolia.etherscan.io/tx/0xe8ca35027a4f202b15ddabf0692a8e77e6a31f9f062af3e61cb0f83f1489c657
|
||||
- **Blob Link:** https://sepolia.blobscan.com/blob/0x0139247d353a186cc80e9b2e5656980bdd275df16074152acef304bc7ab374dd
|
||||
|
||||
- **Blob 2:**
|
||||
- **Transaction:** https://sepolia.etherscan.io/tx/0xe91a937bbf1b78d3e499ab480a7c4a4f22e264d590098563aa1a2f72b98fe081
|
||||
- **Blob Link:** https://sepolia.blobscan.com/blob/0x01aa4d37265e64c36ab524a4d494e2f0ad447e2389d66452023bb3152213ce14
|
||||
|
||||
- **Blob 3:**
|
||||
- **Transaction:** https://sepolia.etherscan.io/tx/0x12c24d2b4bf807b22bca90b7ca594b369f4ab97bc4fc30508c65c6fc7b2eec21
|
||||
- **Blob Link:** https://sepolia.blobscan.com/blob/0x016316f61a259aa607096440fc3eeb90356e079be01975d2fb18347bd50df33c
|
||||
|
||||
- **Blob 4:**
|
||||
- **Transaction:** https://sepolia.etherscan.io/tx/0xb9554c0b9d8717412c94fe286274aaeb52e5479d314a50221639ed8d158cda2d
|
||||
- **Blob Link:** https://sepolia.blobscan.com/blob/0x01de665211bb47c959cf3d31a6fdee4fac0c5b3563d52e02b231e311cae95d78
|
||||
5
crates/primitives/src/transaction/blob_data/blob1.json
Normal file
5
crates/primitives/src/transaction/blob_data/blob1.json
Normal file
File diff suppressed because one or more lines are too long
4
crates/primitives/src/transaction/blob_data/blob2.json
Normal file
4
crates/primitives/src/transaction/blob_data/blob2.json
Normal file
File diff suppressed because one or more lines are too long
4
crates/primitives/src/transaction/blob_data/blob3.json
Normal file
4
crates/primitives/src/transaction/blob_data/blob3.json
Normal file
File diff suppressed because one or more lines are too long
4
crates/primitives/src/transaction/blob_data/blob4.json
Normal file
4
crates/primitives/src/transaction/blob_data/blob4.json
Normal file
File diff suppressed because one or more lines are too long
@ -503,3 +503,154 @@ fn generate_blob_sidecar(blobs: Vec<Blob>) -> BlobTransactionSidecar {
|
||||
|
||||
BlobTransactionSidecar { blobs, commitments, proofs }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
hex,
|
||||
kzg::{Blob, Bytes48},
|
||||
transaction::sidecar::generate_blob_sidecar,
|
||||
BlobTransactionSidecar,
|
||||
};
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
#[test]
|
||||
fn test_blob_transaction_sidecar_generation() {
|
||||
// Read the contents of the JSON file into a string.
|
||||
let json_content = fs::read_to_string(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/transaction/blob_data/blob1.json"),
|
||||
)
|
||||
.expect("Failed to read the blob data file");
|
||||
|
||||
// Parse the JSON contents into a serde_json::Value
|
||||
let json_value: serde_json::Value =
|
||||
serde_json::from_str(&json_content).expect("Failed to deserialize JSON");
|
||||
|
||||
// Extract blob data from JSON and convert it to Blob
|
||||
let blobs: Vec<Blob> = vec![Blob::from_hex(
|
||||
json_value.get("data").unwrap().as_str().expect("Data is not a valid string"),
|
||||
)
|
||||
.unwrap()];
|
||||
|
||||
// Generate a BlobTransactionSidecar from the blobs
|
||||
let sidecar = generate_blob_sidecar(blobs.clone());
|
||||
|
||||
// Assert commitment equality
|
||||
assert_eq!(
|
||||
sidecar.commitments,
|
||||
vec![
|
||||
Bytes48::from_hex(json_value.get("commitment").unwrap().as_str().unwrap()).unwrap()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blob_transaction_sidecar_size() {
|
||||
// Vector to store blob data from each file
|
||||
let mut blobs: Vec<Blob> = Vec::new();
|
||||
|
||||
// Iterate over each file in the folder
|
||||
for entry in fs::read_dir(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/transaction/blob_data/"),
|
||||
)
|
||||
.expect("Failed to read blob_data folder")
|
||||
{
|
||||
let entry = entry.expect("Failed to read directory entry");
|
||||
let file_path = entry.path();
|
||||
|
||||
// Ensure the entry is a file and not a directory
|
||||
if !file_path.is_file() || file_path.extension().unwrap_or_default() != "json" {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read the contents of the JSON file into a string.
|
||||
let json_content =
|
||||
fs::read_to_string(file_path).expect("Failed to read the blob data file");
|
||||
|
||||
// Parse the JSON contents into a serde_json::Value
|
||||
let json_value: serde_json::Value =
|
||||
serde_json::from_str(&json_content).expect("Failed to deserialize JSON");
|
||||
|
||||
// Extract blob data from JSON and convert it to Blob
|
||||
if let Some(data) = json_value.get("data") {
|
||||
if let Some(data_str) = data.as_str() {
|
||||
if let Ok(blob) = Blob::from_hex(data_str) {
|
||||
blobs.push(blob);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a BlobTransactionSidecar from the blobs
|
||||
let sidecar = generate_blob_sidecar(blobs.clone());
|
||||
|
||||
// Assert sidecar size
|
||||
assert_eq!(sidecar.size(), 524672);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blob_transaction_sidecar_rlp_encode() {
|
||||
// Read the contents of the JSON file into a string.
|
||||
let json_content = fs::read_to_string(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/transaction/blob_data/blob1.json"),
|
||||
)
|
||||
.expect("Failed to read the blob data file");
|
||||
|
||||
// Parse the JSON contents into a serde_json::Value
|
||||
let json_value: serde_json::Value =
|
||||
serde_json::from_str(&json_content).expect("Failed to deserialize JSON");
|
||||
|
||||
// Extract blob data from JSON and convert it to Blob
|
||||
let blobs: Vec<Blob> = vec![Blob::from_hex(
|
||||
json_value.get("data").unwrap().as_str().expect("Data is not a valid string"),
|
||||
)
|
||||
.unwrap()];
|
||||
|
||||
// Generate a BlobTransactionSidecar from the blobs
|
||||
let sidecar = generate_blob_sidecar(blobs.clone());
|
||||
|
||||
// Create a vector to store the encoded RLP
|
||||
let mut encoded_rlp = Vec::new();
|
||||
|
||||
// Encode the inner data of the BlobTransactionSidecar into RLP
|
||||
sidecar.encode_inner(&mut encoded_rlp);
|
||||
|
||||
// Assert the equality between the expected RLP from the JSON and the encoded RLP
|
||||
assert_eq!(json_value.get("rlp").unwrap().as_str().unwrap(), hex::encode(&encoded_rlp));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blob_transaction_sidecar_rlp_decode() {
|
||||
// Read the contents of the JSON file into a string.
|
||||
let json_content = fs::read_to_string(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/transaction/blob_data/blob1.json"),
|
||||
)
|
||||
.expect("Failed to read the blob data file");
|
||||
|
||||
// Parse the JSON contents into a serde_json::Value
|
||||
let json_value: serde_json::Value =
|
||||
serde_json::from_str(&json_content).expect("Failed to deserialize JSON");
|
||||
|
||||
// Extract blob data from JSON and convert it to Blob
|
||||
let blobs: Vec<Blob> = vec![Blob::from_hex(
|
||||
json_value.get("data").unwrap().as_str().expect("Data is not a valid string"),
|
||||
)
|
||||
.unwrap()];
|
||||
|
||||
// Generate a BlobTransactionSidecar from the blobs
|
||||
let sidecar = generate_blob_sidecar(blobs.clone());
|
||||
|
||||
// Create a vector to store the encoded RLP
|
||||
let mut encoded_rlp = Vec::new();
|
||||
|
||||
// Encode the inner data of the BlobTransactionSidecar into RLP
|
||||
sidecar.encode_inner(&mut encoded_rlp);
|
||||
|
||||
// Decode the RLP-encoded data back into a BlobTransactionSidecar
|
||||
let decoded_sidecar =
|
||||
BlobTransactionSidecar::decode_inner(&mut encoded_rlp.as_slice()).unwrap();
|
||||
|
||||
// Assert the equality between the original BlobTransactionSidecar and the decoded one
|
||||
assert_eq!(sidecar, decoded_sidecar);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user