Skip to content

Commit

Permalink
Aviod iteration all the time to the beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Jul 11, 2024
1 parent 63d1074 commit 59003a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
16 changes: 7 additions & 9 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,18 +640,16 @@ pub mod pallet {

// collect message_nonces that were supposed to be pruned
let mut unpruned_message_nonces = Vec::new();
let mut nonce_to_check = expected_last_prunned_nonce;
loop {
const MAX_MESSAGES_ITERATION: u64 = 16;
let start_nonce =
expected_last_prunned_nonce.checked_sub(MAX_MESSAGES_ITERATION).unwrap_or(0);
for current_nonce in start_nonce..=expected_last_prunned_nonce {
// check a message for current_nonce
if OutboundMessages::<T, I>::contains_key(MessageKey {
lane_id,
nonce: nonce_to_check,
nonce: current_nonce,
}) {
unpruned_message_nonces.push(nonce_to_check);
}
if let Some(new_nonce_to_check) = nonce_to_check.checked_sub(One::one()) {
nonce_to_check = new_nonce_to_check;
} else {
break;
unpruned_message_nonces.push(current_nonce);
}
}

Expand Down
34 changes: 32 additions & 2 deletions bridges/modules/messages/src/tests/pallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ use bp_runtime::{BasicOperatingMode, PreComputedSize, RangeInclusiveExt, Size};
use bp_test_utils::generate_owned_bridge_module_tests;
use codec::Encode;
use frame_support::{
assert_noop, assert_ok,
assert_err, assert_noop, assert_ok,
dispatch::Pays,
storage::generator::{StorageMap, StorageValue},
weights::Weight,
};
use frame_system::{EventRecord, Pallet as System, Phase};
use sp_core::Get;
use sp_runtime::DispatchError;
use sp_runtime::{BoundedVec, DispatchError};

fn get_ready_for_events() {
System::<TestRuntime>::set_block_number(1);
Expand Down Expand Up @@ -983,3 +983,33 @@ fn maybe_outbound_lanes_count_returns_correct_value() {
Some(mock::ActiveOutboundLanes::get().len() as u32)
);
}

#[test]
fn do_try_state_for_outbound_lanes_works() {
run_test(|| {
let lane_id = TEST_LANE_ID;

// setup delivered nonce 1
OutboundLanes::<TestRuntime>::insert(
lane_id,
OutboundLaneData {
oldest_unpruned_nonce: 2,
latest_received_nonce: 1,
latest_generated_nonce: 0,
},
);
// store message for nonce 1
OutboundMessages::<TestRuntime>::insert(
MessageKey { lane_id, nonce: 1 },
BoundedVec::default(),
);
assert_err!(
Pallet::<TestRuntime>::do_try_state(),
sp_runtime::TryRuntimeError::Other("Found unpruned lanes!")
);

// remove message for nonce 1
OutboundMessages::<TestRuntime>::remove(MessageKey { lane_id, nonce: 1 });
assert_ok!(Pallet::<TestRuntime>::do_try_state());
})
}

0 comments on commit 59003a5

Please sign in to comment.