diff --git a/api-server/api-server-common/src/storage/impls/postgres/queries.rs b/api-server/api-server-common/src/storage/impls/postgres/queries.rs index a5bc06e4cf..71a7d64290 100644 --- a/api-server/api-server-common/src/storage/impls/postgres/queries.rs +++ b/api-server/api-server-common/src/storage/impls/postgres/queries.rs @@ -631,7 +631,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { r" SELECT COALESCE(MIN(block_height), 0), COALESCE(MAX(block_height), 0) FROM ml_blocks - WHERE block_timestamp BETWEEN $1 AND $2 AND block_height != NULL + WHERE block_timestamp BETWEEN $1 AND $2 AND block_height IS NOT NULL ;", &[&from, &to], ) diff --git a/api-server/storage-test-suite/src/basic.rs b/api-server/storage-test-suite/src/basic.rs index 9cfbdfc12e..c7f1466efc 100644 --- a/api-server/storage-test-suite/src/basic.rs +++ b/api-server/storage-test-suite/src/basic.rs @@ -120,7 +120,8 @@ where // Create a test framework and blocks let genesis_id = chain_config.genesis_block_id(); - test_framework.create_chain(&genesis_id, 10, &mut rng).unwrap(); + let num_blocks = rng.gen_range(10..20); + test_framework.create_chain(&genesis_id, num_blocks, &mut rng).unwrap(); let block_id1 = test_framework.block_id(1).classify(&chain_config).chain_block_id().unwrap(); @@ -155,6 +156,65 @@ where assert_eq!(block.unwrap(), block1); } + { + for block_height in 1..num_blocks { + let block_height = block_height as u64; + let block_id = test_framework + .block_id(block_height) + .classify(&chain_config) + .chain_block_id() + .unwrap(); + let block = test_framework.block(block_id); + db_tx + .set_mainchain_block(block_id, BlockHeight::new(block_height), &block) + .await + .unwrap(); + db_tx + .set_block_aux_data( + block_id, + &BlockAuxData::new( + block_id, + BlockHeight::new(block_height), + block.timestamp(), + ), + ) + .await + .unwrap(); + } + + let random_height = rng.gen_range(1..3); + let block_id = test_framework + .block_id(random_height) + .classify(&chain_config) + .chain_block_id() + .unwrap(); + let block1 = test_framework.block(block_id); + let random_height2 = rng.gen_range(3..10); + let block_id2 = test_framework + .block_id(random_height2) + .classify(&chain_config) + .chain_block_id() + .unwrap(); + let block2 = test_framework.block(block_id2); + + let block1_timestamp = block1.timestamp(); + let block2_timestamp = block2.timestamp(); + + let (h1, h2) = db_tx + .get_block_range_from_time_range((block1_timestamp, block2_timestamp)) + .await + .unwrap(); + + assert_eq!(h1, BlockHeight::new(random_height)); + assert_eq!(h2, BlockHeight::new(random_height2)); + + // delete the main chain block + db_tx + .del_main_chain_blocks_above_height(block_height.prev_height().unwrap()) + .await + .unwrap(); + } + db_tx.commit().await.unwrap(); { diff --git a/chainstate/test-framework/src/framework.rs b/chainstate/test-framework/src/framework.rs index b19d8b065f..10a3942743 100644 --- a/chainstate/test-framework/src/framework.rs +++ b/chainstate/test-framework/src/framework.rs @@ -160,7 +160,9 @@ impl TestFramework { let mut prev_block_id = *parent_block; let result = || -> Result>, ChainstateError> { let mut ids = Vec::new(); + let target_block_time = self.chain_config().target_block_spacing(); for _ in 0..blocks_count { + self.progress_time_seconds_since_epoch(target_block_time.as_secs()); let block = self .make_block_builder() .add_test_transaction_with_parent(prev_block_id, rng)