Skip to content

Commit 36e7890

Browse files
Tests/data availability job (#74)
* update: DA job tests draft #1 * update: da_job: reformatting unit tests + added integration tests * update: shifted unit tests from src/jobs/da_job to src/test/jobs/da_job * update: draft #1 all da-tests running * update: removing usage of serial * docs: documented test functions * Update crates/orchestrator/src/tests/jobs/da_job/mod.rs Co-authored-by: Apoorv Sadana <95699312+apoorvsadana@users.noreply.github.com> * Update crates/orchestrator/src/tests/jobs/da_job/mod.rs Co-authored-by: Apoorv Sadana <95699312+apoorvsadana@users.noreply.github.com> * update: PR reviews * update: PR reviews #2 * update: added more testcases for test_da_job_process_job_success * Update crates/orchestrator/src/tests/jobs/da_job/mod.rs Co-authored-by: Apoorv Sadana <95699312+apoorvsadana@users.noreply.github.com> * update: removed Result from test cases * update: introducing assert_matches! in Da job tests * update: moving unit tests back in da_job file * update: removed pub from da_word --------- Co-authored-by: Apoorv Sadana <95699312+apoorvsadana@users.noreply.github.com>
1 parent b905890 commit 36e7890

File tree

17 files changed

+240
-109
lines changed

17 files changed

+240
-109
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1515
- Basic rust-toolchain support.
1616
- `AWS_DEFAULT_REGION="localhost"` var. in .env.test for omniqueue queue testing.
1717
- Added basic rust-toolchain support.
18+
- Tests for DA job.
1819

1920
## Changed
2021

2122
- GitHub's coverage CI yml file for localstack and db testing.
2223
- Orchestrator :Moved TestConfigBuilder to `config.rs` in tests folder.
2324
- `.env` file requires two more variables which are queue urls for processing
2425
and verification.
26+
- Shifted Unit tests to test folder for DA job.
2527

2628
## Removed
2729

crates/orchestrator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ with_mongodb = ["mongodb"]
6969
with_sqs = ["omniqueue"]
7070

7171
[dev-dependencies]
72+
assert_matches = "1.5.0"
7273
hyper = { version = "0.14", features = ["full"] }
7374
rstest = { workspace = true }
7475
httpmock = { workspace = true, features = ["remote"] }

crates/orchestrator/src/jobs/da_job/mod.rs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl Job for DaJob {
6363

6464
async fn process_job(&self, config: &Config, job: &mut JobItem) -> Result<String> {
6565
let block_no = job.internal_id.parse::<u64>()?;
66+
6667
let state_update = config.starknet_client().get_state_update(BlockId::Number(block_no)).await?;
6768

6869
let state_update = match state_update {
@@ -127,7 +128,7 @@ impl Job for DaJob {
127128
}
128129
}
129130

130-
fn fft_transformation(elements: Vec<BigUint>) -> Vec<BigUint> {
131+
pub fn fft_transformation(elements: Vec<BigUint>) -> Vec<BigUint> {
131132
let xs: Vec<BigUint> = (0..*BLOB_LEN)
132133
.map(|i| {
133134
let bin = format!("{:012b}", i);
@@ -149,7 +150,7 @@ fn fft_transformation(elements: Vec<BigUint>) -> Vec<BigUint> {
149150
transform
150151
}
151152

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

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

203-
async fn state_update_to_blob_data(
204+
pub async fn state_update_to_blob_data(
204205
block_no: u64,
205206
state_update: StateUpdate,
206207
config: &Config,
@@ -342,32 +343,37 @@ fn da_word(class_flag: bool, nonce_change: Option<FieldElement>, num_changes: u6
342343
}
343344

344345
#[cfg(test)]
345-
mod tests {
346+
347+
pub mod test {
348+
use crate::jobs::da_job::da_word;
346349
use std::fs;
347350
use std::fs::File;
348351
use std::io::Read;
349352

353+
use crate::data_storage::MockDataStorage;
350354
use ::serde::{Deserialize, Serialize};
355+
use color_eyre::Result;
356+
use da_client_interface::MockDaClient;
351357
use httpmock::prelude::*;
352358
use majin_blob_core::blob;
353359
use majin_blob_types::serde;
354360
use majin_blob_types::state_diffs::UnorderedEq;
355-
// use majin_blob_types::serde;
356-
use crate::data_storage::MockDataStorage;
357-
use da_client_interface::MockDaClient;
358361
use rstest::rstest;
359362
use serde_json::json;
363+
use starknet_core::types::{FieldElement, StateUpdate};
360364

361-
use super::*;
362-
// use majin_blob_types::serde;
363365
use crate::tests::common::init_config;
364366

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

388+
/// Tests `state_update_to_blob_data` conversion with different state update files and block numbers.
389+
/// Mocks DA client and storage client interactions for the test environment.
390+
/// Compares the generated blob data against expected values to ensure correctness.
391+
/// Verifies the data integrity by checking that the parsed state diffs match the expected diffs.
382392
#[rstest]
383393
#[case(
384394
631861,
385-
"src/jobs/da_job/test_data/state_update_from_block_631861.txt",
386-
"src/jobs/da_job/test_data/test_blob_631861.txt",
387-
"src/jobs/da_job/test_data/nonces_from_block_631861.txt"
395+
"src/tests/jobs/da_job/test_data/state_update/631861.txt",
396+
"src/tests/jobs/da_job/test_data/test_blob/631861.txt",
397+
"src/tests/jobs/da_job/test_data/nonces/631861.txt"
388398
)]
389399
#[case(
390400
638353,
391-
"src/jobs/da_job/test_data/state_update_from_block_638353.txt",
392-
"src/jobs/da_job/test_data/test_blob_638353.txt",
393-
"src/jobs/da_job/test_data/nonces_from_block_638353.txt"
401+
"src/tests/jobs/da_job/test_data/state_update/638353.txt",
402+
"src/tests/jobs/da_job/test_data/test_blob/638353.txt",
403+
"src/tests/jobs/da_job/test_data/nonces/638353.txt"
394404
)]
395405
#[case(
396406
640641,
397-
"src/jobs/da_job/test_data/state_update_from_block_640641.txt",
398-
"src/jobs/da_job/test_data/test_blob_640641.txt",
399-
"src/jobs/da_job/test_data/nonces_from_block_640641.txt"
407+
"src/tests/jobs/da_job/test_data/state_update/640641.txt",
408+
"src/tests/jobs/da_job/test_data/test_blob/640641.txt",
409+
"src/tests/jobs/da_job/test_data/nonces/640641.txt"
400410
)]
401411
#[tokio::test]
402412
async fn test_state_update_to_blob_data(
@@ -405,6 +415,8 @@ mod tests {
405415
#[case] file_path: &str,
406416
#[case] nonce_file_path: &str,
407417
) {
418+
use crate::jobs::da_job::{convert_to_biguint, state_update_to_blob_data};
419+
408420
let server = MockServer::start();
409421
let mut da_client = MockDaClient::new();
410422
let mut storage_client = MockDataStorage::new();
@@ -446,16 +458,22 @@ mod tests {
446458
assert!(block_data_state_diffs.unordered_eq(&blob_data_state_diffs), "value of data json should be identical");
447459
}
448460

461+
/// Tests the `fft_transformation` function with various test blob files.
462+
/// Verifies the correctness of FFT and IFFT transformations by ensuring round-trip consistency.
463+
/// Parses the original blob data, recovers it using IFFT, and re-applies FFT.
464+
/// Asserts that the transformed data matches the original pre-IFFT data, ensuring integrity.
449465
#[rstest]
450-
#[case("src/jobs/da_job/test_data/test_blob_631861.txt")]
451-
#[case("src/jobs/da_job/test_data/test_blob_638353.txt")]
452-
#[case("src/jobs/da_job/test_data/test_blob_639404.txt")]
453-
#[case("src/jobs/da_job/test_data/test_blob_640641.txt")]
454-
#[case("src/jobs/da_job/test_data/test_blob_640644.txt")]
455-
#[case("src/jobs/da_job/test_data/test_blob_640646.txt")]
456-
#[case("src/jobs/da_job/test_data/test_blob_640647.txt")]
466+
#[case("src/tests/jobs/da_job/test_data/test_blob/638353.txt")]
467+
#[case("src/tests/jobs/da_job/test_data/test_blob/631861.txt")]
468+
#[case("src/tests/jobs/da_job/test_data/test_blob/639404.txt")]
469+
#[case("src/tests/jobs/da_job/test_data/test_blob/640641.txt")]
470+
#[case("src/tests/jobs/da_job/test_data/test_blob/640644.txt")]
471+
#[case("src/tests/jobs/da_job/test_data/test_blob/640646.txt")]
472+
#[case("src/tests/jobs/da_job/test_data/test_blob/640647.txt")]
457473
fn test_fft_transformation(#[case] file_to_check: &str) {
458474
// parsing the blob hex to the bigUints
475+
476+
use crate::jobs::da_job::fft_transformation;
459477
let original_blob_data = serde::parse_file_to_blob_data(file_to_check);
460478
// converting the data to its original format
461479
let ifft_blob_data = blob::recover(original_blob_data.clone());
@@ -466,6 +484,10 @@ mod tests {
466484
assert_eq!(fft_blob_data, original_blob_data);
467485
}
468486

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

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

521543
let mut new_hex_chars = hex_chars.join("");
522544
new_hex_chars = new_hex_chars.trim_start_matches('0').to_string();
523-
524-
// Handle the case where the trimmed string is empty (e.g., data was all zeros)
525545
if new_hex_chars.is_empty() {
526546
"0x0".to_string()
527547
} else {

0 commit comments

Comments
 (0)