Skip to content

Commit

Permalink
Merge pull request #1524 from mintlayer/fix/api-server-block-stat
Browse files Browse the repository at this point in the history
fix NULL comparison is query
  • Loading branch information
OBorce authored Feb 1, 2024
2 parents dc12c93 + b3cc93e commit 199bd31
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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],
)
Expand Down
64 changes: 63 additions & 1 deletion api-server/storage-test-suite/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ 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_return_ids_with_advancing_time(&genesis_id, num_blocks, &mut rng)
.unwrap();

let block_id1 =
test_framework.block_id(1).classify(&chain_config).chain_block_id().unwrap();
Expand Down Expand Up @@ -155,6 +158,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();

{
Expand Down
33 changes: 33 additions & 0 deletions chainstate/test-framework/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,39 @@ impl TestFramework {
result
}

/// Create and process a given amount of blocks. Return the ids of the produced blocks.
///
/// Each block contains a single transaction that spends a random amount from the previous
/// block outputs. Each block has an incremented timestamp
pub fn create_chain_return_ids_with_advancing_time(
&mut self,
parent_block: &Id<GenBlock>,
blocks_count: usize,
rng: &mut impl Rng,
) -> Result<Vec<Id<GenBlock>>, ChainstateError> {
let mut prev_block_id = *parent_block;
let result = || -> Result<Vec<Id<GenBlock>>, 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)
.with_parent(prev_block_id)
.build();
prev_block_id = block.get_id().into();
ids.push(prev_block_id);
self.do_process_block(block, BlockSource::Local)?;
}

Ok(ids)
}();

self.refresh_block_indices()?;
result
}

/// Same as `create_chain_return_ids`, but only return the id of the last produced block.
pub fn create_chain(
&mut self,
Expand Down

0 comments on commit 199bd31

Please sign in to comment.