Skip to content

Commit

Permalink
Audit issue 9: Check if max nonce reached in outbound-queue pallet (#…
Browse files Browse the repository at this point in the history
…1052)

* Check if max nonce reached in outbound-queue pallet

* merge

* review feedback
  • Loading branch information
vgeddes authored Dec 16, 2023
1 parent f7f06d2 commit 6f14f70
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion parachain/pallets/outbound-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bridge-hub-common = { path = "../../../polkadot-sdk/cumulus/parachains/runtimes/

snowbridge-core = { path = "../../primitives/core", features = ["serde"], default-features = false }
snowbridge-outbound-queue-merkle-tree = { path = "merkle-tree", default-features = false }
ethabi = { git = "https://github.com/Snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false }
ethabi = { git = "https://github.com/snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false }

xcm = { package = "staging-xcm", path = "../../../polkadot-sdk/polkadot/xcm", default-features = false }

Expand Down
20 changes: 11 additions & 9 deletions parachain/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,16 @@ 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> {
*nonce = nonce.checked_add(1).ok_or(Unsupported)?;
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 +327,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 +345,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);
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
8 changes: 4 additions & 4 deletions smoketest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f14f70

Please sign in to comment.