Skip to content

Commit

Permalink
Check if max nonce reached in outbound-queue pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Dec 14, 2023
1 parent c03175b commit abb39da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
25 changes: 16 additions & 9 deletions parachain/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ pub mod pallet {
InvalidFeeConfig,
/// Invalid Channel
InvalidChannel,
/// Maximum Nonce reached
MaxNonceReached,
}

/// Messages to be committed in the current block. This storage value is killed in
Expand Down Expand Up @@ -308,10 +310,19 @@ pub mod pallet {
let queued_message: QueuedMessage =
versioned_queued_message.try_into().map_err(|_| Unsupported)?;

let pricing_params = T::PricingParameters::get();

let next_nonce = Nonce::<T>::get(queued_message.channel_id).saturating_add(1);
// Obtain next nonce
let nonce = <Nonce<T>>::try_mutate(
queued_message.channel_id,
|nonce| -> Result<u64, ProcessMessageError> {
if *nonce == u64::MAX {
return Err(Unsupported)
}
*nonce = nonce.saturating_add(1);
Ok(*nonce)
},
)?;

let pricing_params = T::PricingParameters::get();
let command = queued_message.command.index();
let params = queued_message.command.abi_encode();
let max_dispatch_gas =
Expand All @@ -321,7 +332,7 @@ pub mod pallet {
// Construct the final committed message
let message = CommittedMessage {
channel_id: queued_message.channel_id,
nonce: next_nonce,
nonce,
command,
params,
max_dispatch_gas,
Expand All @@ -339,12 +350,8 @@ pub mod pallet {

Messages::<T>::append(Box::new(message));
MessageLeaves::<T>::append(message_abi_encoded_hash);
Nonce::<T>::set(queued_message.channel_id, next_nonce);

Self::deposit_event(Event::MessageAccepted {
id: queued_message.id,
nonce: next_nonce,
});
Self::deposit_event(Event::MessageAccepted { id: queued_message.id, nonce });

Ok(true)
}
Expand Down
34 changes: 32 additions & 2 deletions parachain/pallets/outbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,46 @@ fn process_message_yields_on_max_messages_per_block() {
})
}

#[test]
fn process_message_fails_on_max_nonce_reached() {
new_tester().execute_with(|| {
let sibling_id = 1000;
let channel_id: ChannelId = ParaId::from(sibling_id).into();
let origin = AggregateMessageOrigin::Snowbridge(channel_id.into());
let message: QueuedMessage = QueuedMessage {
id: H256::zero(),
channel_id,
command: mock_message(sibling_id).command,
};
let versioned_queued_message: VersionedQueuedMessage = message.try_into().unwrap();
let encoded = versioned_queued_message.encode();
let mut meter = WeightMeter::with_limit(Weight::MAX);

Nonce::<Test>::set(channel_id, u64::MAX);

assert_noop!(
OutboundQueue::process_message(&encoded.as_slice(), origin, &mut meter, &mut [0u8; 32]),
ProcessMessageError::Unsupported
);
})
}

#[test]
fn process_message_fails_on_overweight_message() {
new_tester().execute_with(|| {
let sibling_id = 1000;
let channel_id: ChannelId = ParaId::from(sibling_id).into();
let origin = AggregateMessageOrigin::Snowbridge(channel_id.into());
let message = mock_message(sibling_id).encode();
let message: QueuedMessage = QueuedMessage {
id: H256::zero(),
channel_id,
command: mock_message(sibling_id).command,
};
let versioned_queued_message: VersionedQueuedMessage = message.try_into().unwrap();
let encoded = versioned_queued_message.encode();
let mut meter = WeightMeter::with_limit(Weight::from_parts(1, 1));
assert_noop!(
OutboundQueue::process_message(&message.as_slice(), origin, &mut meter, &mut [0u8; 32]),
OutboundQueue::process_message(&encoded.as_slice(), origin, &mut meter, &mut [0u8; 32]),
ProcessMessageError::Overweight(<Test as Config>::WeightInfo::do_process_message())
);
})
Expand Down

0 comments on commit abb39da

Please sign in to comment.