Skip to content

Commit

Permalink
update: draft #1 tests for settlement client
Browse files Browse the repository at this point in the history
  • Loading branch information
heemankv committed Jul 30, 2024
1 parent 385974d commit 12afa40
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/settlement-clients/ethereum/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use alloy::primitives::U256;

/// Converts a `&[Vec<u8>]` to `Vec<U256>`. 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<U256> {
pub fn slice_slice_u8_to_vec_u256(slices: &[[u8; 32]]) -> Vec<U256> {
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")
}
36 changes: 2 additions & 34 deletions crates/settlement-clients/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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| {
Expand All @@ -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);

Expand All @@ -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");
}
35 changes: 35 additions & 0 deletions crates/settlement-clients/ethereum/tests/conversion.rs
Original file line number Diff line number Diff line change
@@ -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::<U256>::new())]
fn test_slice_slice_u8_to_vec_u256(#[case] input: &[[u8; 32]], #[case] expected: Vec<U256>) {

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());
}
35 changes: 35 additions & 0 deletions crates/settlement-clients/ethereum/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 12afa40

Please sign in to comment.