From 12afa40a5ba6c1cb41662d99a9b67a6ae7e9d200 Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Tue, 30 Jul 2024 18:55:28 +0530 Subject: [PATCH] update: draft #1 tests for settlement client --- .../ethereum/src/conversion.rs | 4 +-- crates/settlement-clients/ethereum/src/lib.rs | 36 ++----------------- .../ethereum/tests/conversion.rs | 35 ++++++++++++++++++ .../settlement-clients/ethereum/tests/lib.rs | 35 ++++++++++++++++++ 4 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 crates/settlement-clients/ethereum/tests/conversion.rs create mode 100644 crates/settlement-clients/ethereum/tests/lib.rs diff --git a/crates/settlement-clients/ethereum/src/conversion.rs b/crates/settlement-clients/ethereum/src/conversion.rs index c86eb89a..28b87c5c 100644 --- a/crates/settlement-clients/ethereum/src/conversion.rs +++ b/crates/settlement-clients/ethereum/src/conversion.rs @@ -2,11 +2,11 @@ use alloy::primitives::U256; /// Converts a `&[Vec]` to `Vec`. Each inner slice is expected to be exactly 32 bytes long. /// Pads with zeros if any inner slice is shorter than 32 bytes. -pub(crate) fn slice_slice_u8_to_vec_u256(slices: &[[u8; 32]]) -> Vec { +pub fn slice_slice_u8_to_vec_u256(slices: &[[u8; 32]]) -> Vec { slices.iter().map(|slice| slice_u8_to_u256(slice)).collect() } /// Converts a `&[u8]` to `U256`. -pub(crate) fn slice_u8_to_u256(slice: &[u8]) -> U256 { +pub fn slice_u8_to_u256(slice: &[u8]) -> U256 { U256::try_from_be_slice(slice).expect("could not convert u8 slice to U256") } diff --git a/crates/settlement-clients/ethereum/src/lib.rs b/crates/settlement-clients/ethereum/src/lib.rs index cdf2788b..e3185ae0 100644 --- a/crates/settlement-clients/ethereum/src/lib.rs +++ b/crates/settlement-clients/ethereum/src/lib.rs @@ -22,7 +22,6 @@ use c_kzg::{Blob, Bytes32, KzgCommitment, KzgProof, KzgSettings}; use color_eyre::eyre::eyre; use color_eyre::Result; use mockall::{automock, lazy_static, predicate::*}; -use rstest::rstest; use std::fmt::Write; use std::path::{Path, PathBuf}; use std::str::FromStr; @@ -252,7 +251,7 @@ fn get_txn_input_bytes(program_output: Vec<[u8; 32]>, kzg_proof: [u8; 48]) -> By Bytes::from(program_output_hex_string + &kzg_proof_hex_string + function_selector) } -fn vec_u8_32_to_hex_string(data: Vec<[u8; 32]>) -> String { +pub fn vec_u8_32_to_hex_string(data: Vec<[u8; 32]>) -> String { data.into_iter().fold(String::new(), |mut output, arr| { // Convert the array to a hex string let hex = arr.iter().fold(String::new(), |mut output, byte| { @@ -266,7 +265,7 @@ fn vec_u8_32_to_hex_string(data: Vec<[u8; 32]>) -> String { }) } -fn u8_48_to_hex_string(data: [u8; 48]) -> String { +pub fn u8_48_to_hex_string(data: [u8; 48]) -> String { // Split the array into two parts let (first_32, last_16) = data.split_at(32); @@ -286,34 +285,3 @@ fn to_padded_hex(slice: &[u8]) -> String { }); format!("{:0<64}", hex) } - -#[rstest] -fn test_data_conversion() { - let data: [u8; 48] = [ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - ]; - - let result = u8_48_to_hex_string(data); - - assert_eq!(result, "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3000000000000000000000000000000000"); - - let mut data_2: [u8; 32] = [ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, - ]; - let mut data_vec: Vec<[u8; 32]> = Vec::new(); - data_vec.push(data_2); - data_2.reverse(); - data_vec.push(data_2); - - let data_3: [u8; 32] = [ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 0, 0, - ]; - data_vec.push(data_3); - - let result_2 = vec_u8_32_to_hex_string(data_vec); - - assert_eq!(result_2, "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a0908070605040302010102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e0000"); -} diff --git a/crates/settlement-clients/ethereum/tests/conversion.rs b/crates/settlement-clients/ethereum/tests/conversion.rs new file mode 100644 index 00000000..cc063fa4 --- /dev/null +++ b/crates/settlement-clients/ethereum/tests/conversion.rs @@ -0,0 +1,35 @@ +use rstest::rstest; +use alloy::primitives::U256; +use ethereum_settlement_client::conversion::slice_u8_to_u256; +use ethereum_settlement_client::conversion::slice_slice_u8_to_vec_u256; + +// Import the functions we're testing + +#[rstest] +#[case(&[[0u8; 32]], vec![U256::ZERO])] +#[case(&[[255u8; 32]], vec![U256::MAX])] +#[case(&[[1u8; 32], [2u8; 32]], vec![U256::from_be_bytes([1u8; 32]), U256::from_be_bytes([2u8; 32])])] +#[case(&[], Vec::::new())] +fn test_slice_slice_u8_to_vec_u256(#[case] input: &[[u8; 32]], #[case] expected: Vec) { + + let result = slice_slice_u8_to_vec_u256(input); + assert_eq!(result, expected); +} + +#[rstest] +#[case(&[0u8; 32], U256::ZERO)] +#[case(&[255u8; 32], U256::MAX)] +#[case(&[1u8; 32], U256::from(1))] +// #[case(&[1u8, 0u8; 31], U256::from(256))] +fn test_slice_u8_to_u256_happy(#[case] input: &[u8], #[case] expected: U256) { + let result = slice_u8_to_u256(input); + assert_eq!(result, expected); +} + +#[rstest] +#[case(&[])] +#[case(&[1u8; 33])] +fn test_slice_u8_to_u256_sad(#[case] input: &[u8]) { + let result = std::panic::catch_unwind(|| slice_u8_to_u256(input)); + assert!(result.is_err()); +} \ No newline at end of file diff --git a/crates/settlement-clients/ethereum/tests/lib.rs b/crates/settlement-clients/ethereum/tests/lib.rs new file mode 100644 index 00000000..0b5e73eb --- /dev/null +++ b/crates/settlement-clients/ethereum/tests/lib.rs @@ -0,0 +1,35 @@ +use ethereum_settlement_client::{u8_48_to_hex_string, vec_u8_32_to_hex_string}; +use rstest::rstest; + + + +#[rstest] +fn test_data_conversion() { + let data: [u8; 48] = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + ]; + + let result = u8_48_to_hex_string(data); + + assert_eq!(result, "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3000000000000000000000000000000000"); + + let mut data_2: [u8; 32] = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, + ]; + let mut data_vec: Vec<[u8; 32]> = Vec::new(); + data_vec.push(data_2); + data_2.reverse(); + data_vec.push(data_2); + + let data_3: [u8; 32] = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 0, 0, + ]; + data_vec.push(data_3); + + let result_2 = vec_u8_32_to_hex_string(data_vec); + + assert_eq!(result_2, "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a0908070605040302010102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e0000"); +}