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

Tests for DA job #61

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c508f32
update: DA job tests draft #1
heemankv Jul 26, 2024
427b1fb
update: da_job: reformatting unit tests + added integration tests
heemankv Jul 27, 2024
5a024f3
update: shifted unit tests from src/jobs/da_job to src/test/jobs/da_job
heemankv Jul 27, 2024
9aaa1c0
update: draft #1 all da-tests running
heemankv Jul 27, 2024
7b4d30f
update: removing usage of serial
heemankv Jul 27, 2024
4c17857
docs: documented test functions
heemankv Jul 29, 2024
f773b59
Update crates/orchestrator/src/tests/jobs/da_job/mod.rs
heemankv Jul 30, 2024
45526f7
Update crates/orchestrator/src/tests/jobs/da_job/mod.rs
heemankv Jul 30, 2024
aed78ee
update: PR reviews
heemankv Jul 30, 2024
fac7ed8
update: PR reviews #2
heemankv Jul 30, 2024
556687d
update: added more testcases for test_da_job_process_job_success
heemankv Jul 30, 2024
176bde5
Update crates/orchestrator/src/tests/jobs/da_job/mod.rs
heemankv Jul 31, 2024
7c1e3a1
update: removed Result from test cases
heemankv Jul 31, 2024
ac086d7
update: introducing assert_matches! in Da job tests
heemankv Jul 31, 2024
2bbb618
Merge branch 'feat/increasing-test-coverage' into tests/da-job
heemankv Jul 31, 2024
a9b29dd
update: moving unit tests back in da_job file
heemankv Jul 31, 2024
ba1d475
update: removed pub from da_word
heemankv Aug 2, 2024
519f1d0
Update/job error using this error (#66)
heemankv Aug 5, 2024
78a7254
Revert "Update/job error using this error (#66)" (#69)
apoorvsadana Aug 5, 2024
8129e32
update: PR reviews rework
heemankv Aug 8, 2024
a1163b0
chore: add drop_database to TestConfigBuilder.build()
heemankv Aug 8, 2024
feb49bb
docs: added comment for unwrap in settlement client ethereum
heemankv Aug 8, 2024
1630d0d
Merge branch 'feat/increasing-test-coverage' into tests/da-job
ocdbytes Aug 8, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- Tests for updating the state.
- Function to update the state and publish blob on ethereum in state update job.
- Fixtures for testing.
- Tests for DA job.
- Added basic rust-toolchain support.


## Changed

- GitHub's coverage CI yml file for localstack and db testing.
- Orchestrator :Moved TestConfigBuilder to `config.rs` in tests folder.
- Shifted Unit tests to test folder for DA job.

## Removed

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ with_mongodb = ["mongodb"]
with_sqs = ["omniqueue"]

[dev-dependencies]
assert_matches = "1.5.0"
hyper = { version = "0.14", features = ["full"] }
rstest = { workspace = true }
httpmock = { workspace = true, features = ["remote"] }
77 changes: 49 additions & 28 deletions crates/orchestrator/src/jobs/da_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Job for DaJob {

async fn process_job(&self, config: &Config, job: &mut JobItem) -> Result<String> {
let block_no = job.internal_id.parse::<u64>()?;

let state_update = config.starknet_client().get_state_update(BlockId::Number(block_no)).await?;

let state_update = match state_update {
Expand Down Expand Up @@ -127,7 +128,7 @@ impl Job for DaJob {
}
}

fn fft_transformation(elements: Vec<BigUint>) -> Vec<BigUint> {
pub fn fft_transformation(elements: Vec<BigUint>) -> Vec<BigUint> {
let xs: Vec<BigUint> = (0..*BLOB_LEN)
.map(|i| {
let bin = format!("{:012b}", i);
Expand All @@ -149,7 +150,7 @@ fn fft_transformation(elements: Vec<BigUint>) -> Vec<BigUint> {
transform
}

fn convert_to_biguint(elements: Vec<FieldElement>) -> Vec<BigUint> {
pub fn convert_to_biguint(elements: Vec<FieldElement>) -> Vec<BigUint> {
// Initialize the vector with 4096 BigUint zeros
let mut biguint_vec = vec![BigUint::zero(); 4096];

Expand Down Expand Up @@ -200,7 +201,7 @@ fn data_to_blobs(blob_size: u64, block_data: Vec<BigUint>) -> Result<Vec<Vec<u8>
Ok(blobs)
}

async fn state_update_to_blob_data(
pub async fn state_update_to_blob_data(
block_no: u64,
state_update: StateUpdate,
config: &Config,
Expand Down Expand Up @@ -342,31 +343,37 @@ fn da_word(class_flag: bool, nonce_change: Option<FieldElement>, num_changes: u6
}

#[cfg(test)]
mod tests {

pub mod test {
use crate::jobs::da_job::da_word;
use std::fs;
use std::fs::File;
use std::io::Read;

use crate::data_storage::MockDataStorage;
use ::serde::{Deserialize, Serialize};
use color_eyre::Result;
use da_client_interface::MockDaClient;
use httpmock::prelude::*;
use majin_blob_core::blob;
use majin_blob_types::serde;
use majin_blob_types::state_diffs::UnorderedEq;
// use majin_blob_types::serde;
use crate::data_storage::MockDataStorage;
use da_client_interface::MockDaClient;
use rstest::rstest;
use serde_json::json;
use starknet_core::types::{FieldElement, StateUpdate};

use super::*;
use crate::tests::common::init_config;

/// Tests `da_word` function with various inputs for class flag, new nonce, and number of changes.
/// Verifies that `da_word` produces the correct FieldElement based on the provided parameters.
/// Uses test cases with different combinations of inputs and expected output strings.
/// Asserts the function's correctness by comparing the computed and expected FieldElements.
#[rstest]
#[case(false, 1, 1, "18446744073709551617")]
#[case(false, 1, 0, "18446744073709551616")]
#[case(false, 0, 6, "6")]
#[case(true, 1, 0, "340282366920938463481821351505477763072")]
fn da_word_works(
fn test_da_word(
#[case] class_flag: bool,
#[case] new_nonce: u64,
#[case] num_changes: u64,
Expand All @@ -378,24 +385,28 @@ mod tests {
assert_eq!(da_word, expected);
}

/// Tests `state_update_to_blob_data` conversion with different state update files and block numbers.
/// Mocks DA client and storage client interactions for the test environment.
/// Compares the generated blob data against expected values to ensure correctness.
/// Verifies the data integrity by checking that the parsed state diffs match the expected diffs.
#[rstest]
#[case(
631861,
"src/jobs/da_job/test_data/state_update_from_block_631861.txt",
"src/jobs/da_job/test_data/test_blob_631861.txt",
"src/jobs/da_job/test_data/nonces_from_block_631861.txt"
"src/tests/jobs/da_job/test_data/state_update/631861.txt",
"src/tests/jobs/da_job/test_data/test_blob/631861.txt",
"src/tests/jobs/da_job/test_data/nonces/631861.txt"
)]
#[case(
638353,
"src/jobs/da_job/test_data/state_update_from_block_638353.txt",
"src/jobs/da_job/test_data/test_blob_638353.txt",
"src/jobs/da_job/test_data/nonces_from_block_638353.txt"
"src/tests/jobs/da_job/test_data/state_update/638353.txt",
"src/tests/jobs/da_job/test_data/test_blob/638353.txt",
"src/tests/jobs/da_job/test_data/nonces/638353.txt"
)]
#[case(
640641,
"src/jobs/da_job/test_data/state_update_from_block_640641.txt",
"src/jobs/da_job/test_data/test_blob_640641.txt",
"src/jobs/da_job/test_data/nonces_from_block_640641.txt"
"src/tests/jobs/da_job/test_data/state_update/640641.txt",
"src/tests/jobs/da_job/test_data/test_blob/640641.txt",
"src/tests/jobs/da_job/test_data/nonces/640641.txt"
)]
#[tokio::test]
async fn test_state_update_to_blob_data(
Expand All @@ -404,6 +415,8 @@ mod tests {
#[case] file_path: &str,
#[case] nonce_file_path: &str,
) {
use crate::jobs::da_job::{convert_to_biguint, state_update_to_blob_data};

let server = MockServer::start();
let mut da_client = MockDaClient::new();
let mut storage_client = MockDataStorage::new();
Expand Down Expand Up @@ -445,16 +458,22 @@ mod tests {
assert!(block_data_state_diffs.unordered_eq(&blob_data_state_diffs), "value of data json should be identical");
}

/// Tests the `fft_transformation` function with various test blob files.
/// Verifies the correctness of FFT and IFFT transformations by ensuring round-trip consistency.
/// Parses the original blob data, recovers it using IFFT, and re-applies FFT.
/// Asserts that the transformed data matches the original pre-IFFT data, ensuring integrity.
#[rstest]
#[case("src/jobs/da_job/test_data/test_blob_631861.txt")]
#[case("src/jobs/da_job/test_data/test_blob_638353.txt")]
#[case("src/jobs/da_job/test_data/test_blob_639404.txt")]
#[case("src/jobs/da_job/test_data/test_blob_640641.txt")]
#[case("src/jobs/da_job/test_data/test_blob_640644.txt")]
#[case("src/jobs/da_job/test_data/test_blob_640646.txt")]
#[case("src/jobs/da_job/test_data/test_blob_640647.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/638353.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/631861.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/639404.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/640641.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/640644.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/640646.txt")]
#[case("src/tests/jobs/da_job/test_data/test_blob/640647.txt")]
fn test_fft_transformation(#[case] file_to_check: &str) {
// parsing the blob hex to the bigUints

use crate::jobs::da_job::fft_transformation;
let original_blob_data = serde::parse_file_to_blob_data(file_to_check);
// converting the data to its original format
let ifft_blob_data = blob::recover(original_blob_data.clone());
Expand All @@ -465,6 +484,10 @@ mod tests {
assert_eq!(fft_blob_data, original_blob_data);
}

/// Tests the serialization and deserialization process using bincode.
/// Serializes a nested vector of integers and then deserializes it back.
/// Verifies that the original data matches the deserialized data.
/// Ensures the integrity and correctness of bincode's (de)serialization.
#[rstest]
fn test_bincode() {
let data = vec![vec![1, 2], vec![3, 4]];
Expand All @@ -475,7 +498,7 @@ mod tests {
assert_eq!(data, deserialize_data);
}

pub fn read_state_update_from_file(file_path: &str) -> Result<StateUpdate> {
pub(crate) fn read_state_update_from_file(file_path: &str) -> Result<StateUpdate> {
// let file_path = format!("state_update_block_no_{}.txt", block_no);
let mut file = File::open(file_path)?;
let mut json = String::new();
Expand Down Expand Up @@ -519,8 +542,6 @@ mod tests {

let mut new_hex_chars = hex_chars.join("");
new_hex_chars = new_hex_chars.trim_start_matches('0').to_string();

// Handle the case where the trimmed string is empty (e.g., data was all zeros)
if new_hex_chars.is_empty() {
"0x0".to_string()
} else {
Expand Down
Loading