Skip to content

Commit

Permalink
feat: Sort descending in FindTransactions query (#5190)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Murzin <diralik@yandex.ru>
  • Loading branch information
dima74 authored Oct 24, 2024
1 parent c99c418 commit fbe8817
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
41 changes: 41 additions & 0 deletions crates/iroha/tests/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,44 @@ fn too_big_fetch_size_is_not_allowed() {
))
));
}

#[test]
fn find_blocks_reversed() -> eyre::Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let client = network.client();

client.submit_blocking(Register::domain(Domain::new("domain1".parse()?)))?;

let blocks = client.query(FindBlocks).execute_all()?;
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[1].header().prev_block_hash, None);
assert_eq!(
blocks[0].header().prev_block_hash,
Some(blocks[1].header().hash())
);

Ok(())
}

#[test]
fn find_transactions_reversed() -> eyre::Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let client = network.client();

let register_domain = Register::domain(Domain::new("domain1".parse()?));
client.submit_blocking(register_domain.clone())?;

let txs = client.query(FindTransactions).execute_all()?;

// check that latest transaction is register domain
let Executable::Instructions(instructions) = txs[0].transaction.value.instructions() else {
panic!("Expected instructions");
};
assert_eq!(instructions.len(), 1);
assert_eq!(
instructions[0],
InstructionBox::Register(register_domain.into())
);

Ok(())
}
10 changes: 6 additions & 4 deletions crates/iroha/tests/tx_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ fn client_has_rejected_and_accepted_txs_should_return_tx_history() -> Result<()>
.execute_all()?;
assert_eq!(transactions.len(), 50);

let mut prev_creation_time = core::time::Duration::from_millis(0);
let mut prev_creation_time = None;
transactions
.iter()
.map(AsRef::as_ref)
.map(AsRef::as_ref)
.for_each(|tx| {
assert_eq!(tx.authority(), &account_id);
//check sorted
assert!(tx.creation_time() >= prev_creation_time);
prev_creation_time = tx.creation_time();
//check sorted descending
if let Some(prev_creation_time) = prev_creation_time {
assert!(tx.creation_time() <= prev_creation_time);
}
prev_creation_time = Some(tx.creation_time());
});
Ok(())
}
13 changes: 7 additions & 6 deletions crates/iroha_core/src/smartcontracts/isi/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ use nonzero_ext::nonzero;
use super::*;
use crate::smartcontracts::ValidQuery;

/// Iterates transactions of a block in reverse order
pub(crate) struct BlockTransactionIter(Arc<SignedBlock>, usize);
pub(crate) struct BlockTransactionRef(Arc<SignedBlock>, usize);

impl BlockTransactionIter {
fn new(block: Arc<SignedBlock>) -> Self {
Self(block, 0)
let n_transactions = block.transactions().len();
Self(block, n_transactions)
}
}

impl Iterator for BlockTransactionIter {
type Item = BlockTransactionRef;

fn next(&mut self) -> Option<Self::Item> {
if self.1 < self.0.transactions().len() {
let res = Some(BlockTransactionRef(Arc::clone(&self.0), self.1));

self.1 += 1;
return res;
if self.1 != 0 {
self.1 -= 1;
return Some(BlockTransactionRef(Arc::clone(&self.0), self.1));
}

None
Expand Down Expand Up @@ -69,6 +69,7 @@ impl ValidQuery for FindTransactions {
) -> Result<impl Iterator<Item = Self::Item>, QueryExecutionFail> {
Ok(state_ro
.all_blocks(nonzero!(1_usize))
.rev()
.flat_map(BlockTransactionIter::new)
.map(|tx| TransactionQueryOutput {
block_hash: tx.block_hash(),
Expand Down

0 comments on commit fbe8817

Please sign in to comment.