Remove some expect() statements (#2123)

* Return error on save_message()

Prevent unexpected failures.

* Remove RefCell

* Fix spellcheck

* CI fixes
This commit is contained in:
Serban Iorga
2023-05-09 11:48:24 +03:00
committed by Bastian Köcher
parent eec97df358
commit 06e0204694
4 changed files with 92 additions and 91 deletions
+5 -4
View File
@@ -17,8 +17,8 @@
//! Messages pallet benchmarking. //! Messages pallet benchmarking.
use crate::{ use crate::{
inbound_lane::InboundLaneStorage, inbound_lane_storage, outbound_lane, inbound_lane::InboundLaneStorage, outbound_lane, weights_ext::EXPECTED_DEFAULT_MESSAGE_LENGTH,
weights_ext::EXPECTED_DEFAULT_MESSAGE_LENGTH, Call, OutboundLanes, Call, OutboundLanes, RuntimeInboundLaneStorage,
}; };
use bp_messages::{ use bp_messages::{
@@ -443,11 +443,12 @@ benchmarks_instance_pallet! {
fn send_regular_message<T: Config<I>, I: 'static>() { fn send_regular_message<T: Config<I>, I: 'static>() {
let mut outbound_lane = outbound_lane::<T, I>(T::bench_lane_id()); let mut outbound_lane = outbound_lane::<T, I>(T::bench_lane_id());
outbound_lane.send_message(vec![]); outbound_lane.send_message(vec![]).expect("We craft valid messages");
} }
fn receive_messages<T: Config<I>, I: 'static>(nonce: MessageNonce) { fn receive_messages<T: Config<I>, I: 'static>(nonce: MessageNonce) {
let mut inbound_lane_storage = inbound_lane_storage::<T, I>(T::bench_lane_id()); let mut inbound_lane_storage =
RuntimeInboundLaneStorage::<T, I>::from_lane_id(T::bench_lane_id());
inbound_lane_storage.set_data(InboundLaneData { inbound_lane_storage.set_data(InboundLaneData {
relayers: vec![UnrewardedRelayer { relayers: vec![UnrewardedRelayer {
relayer: T::bridged_relayer_id(), relayer: T::bridged_relayer_id(),
+21 -21
View File
@@ -40,7 +40,7 @@ pub trait InboundLaneStorage {
/// Return maximal number of unconfirmed messages in inbound lane. /// Return maximal number of unconfirmed messages in inbound lane.
fn max_unconfirmed_messages(&self) -> MessageNonce; fn max_unconfirmed_messages(&self) -> MessageNonce;
/// Get lane data from the storage. /// Get lane data from the storage.
fn data(&self) -> InboundLaneData<Self::Relayer>; fn get_or_init_data(&mut self) -> InboundLaneData<Self::Relayer>;
/// Update lane data in the storage. /// Update lane data in the storage.
fn set_data(&mut self, data: InboundLaneData<Self::Relayer>); fn set_data(&mut self, data: InboundLaneData<Self::Relayer>);
} }
@@ -117,9 +117,9 @@ impl<S: InboundLaneStorage> InboundLane<S> {
InboundLane { storage } InboundLane { storage }
} }
/// Returns storage reference. /// Returns `mut` storage reference.
pub fn storage(&self) -> &S { pub fn storage_mut(&mut self) -> &mut S {
&self.storage &mut self.storage
} }
/// Receive state of the corresponding outbound lane. /// Receive state of the corresponding outbound lane.
@@ -127,7 +127,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
&mut self, &mut self,
outbound_lane_data: OutboundLaneData, outbound_lane_data: OutboundLaneData,
) -> Option<MessageNonce> { ) -> Option<MessageNonce> {
let mut data = self.storage.data(); let mut data = self.storage.get_or_init_data();
let last_delivered_nonce = data.last_delivered_nonce(); let last_delivered_nonce = data.last_delivered_nonce();
if outbound_lane_data.latest_received_nonce > last_delivered_nonce { if outbound_lane_data.latest_received_nonce > last_delivered_nonce {
@@ -170,7 +170,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
nonce: MessageNonce, nonce: MessageNonce,
message_data: DispatchMessageData<Dispatch::DispatchPayload>, message_data: DispatchMessageData<Dispatch::DispatchPayload>,
) -> ReceivalResult<Dispatch::DispatchLevelResult> { ) -> ReceivalResult<Dispatch::DispatchLevelResult> {
let mut data = self.storage.data(); let mut data = self.storage.get_or_init_data();
let is_correct_message = nonce == data.last_delivered_nonce() + 1; let is_correct_message = nonce == data.last_delivered_nonce() + 1;
if !is_correct_message { if !is_correct_message {
return ReceivalResult::InvalidNonce return ReceivalResult::InvalidNonce
@@ -252,7 +252,7 @@ mod tests {
None, None,
); );
assert_eq!(lane.storage.data().last_confirmed_nonce, 0); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 0);
}); });
} }
@@ -270,7 +270,7 @@ mod tests {
}), }),
Some(3), Some(3),
); );
assert_eq!(lane.storage.data().last_confirmed_nonce, 3); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 3);
assert_eq!( assert_eq!(
lane.receive_state_update(OutboundLaneData { lane.receive_state_update(OutboundLaneData {
@@ -279,7 +279,7 @@ mod tests {
}), }),
None, None,
); );
assert_eq!(lane.storage.data().last_confirmed_nonce, 3); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 3);
}); });
} }
@@ -290,9 +290,9 @@ mod tests {
receive_regular_message(&mut lane, 1); receive_regular_message(&mut lane, 1);
receive_regular_message(&mut lane, 2); receive_regular_message(&mut lane, 2);
receive_regular_message(&mut lane, 3); receive_regular_message(&mut lane, 3);
assert_eq!(lane.storage.data().last_confirmed_nonce, 0); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 0);
assert_eq!( assert_eq!(
lane.storage.data().relayers, lane.storage.get_or_init_data().relayers,
vec![unrewarded_relayer(1, 3, TEST_RELAYER_A)] vec![unrewarded_relayer(1, 3, TEST_RELAYER_A)]
); );
@@ -303,9 +303,9 @@ mod tests {
}), }),
Some(2), Some(2),
); );
assert_eq!(lane.storage.data().last_confirmed_nonce, 2); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 2);
assert_eq!( assert_eq!(
lane.storage.data().relayers, lane.storage.get_or_init_data().relayers,
vec![unrewarded_relayer(3, 3, TEST_RELAYER_A)] vec![unrewarded_relayer(3, 3, TEST_RELAYER_A)]
); );
@@ -316,8 +316,8 @@ mod tests {
}), }),
Some(3), Some(3),
); );
assert_eq!(lane.storage.data().last_confirmed_nonce, 3); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 3);
assert_eq!(lane.storage.data().relayers, vec![]); assert_eq!(lane.storage.get_or_init_data().relayers, vec![]);
}); });
} }
@@ -325,7 +325,7 @@ mod tests {
fn receive_status_update_works_with_batches_from_relayers() { fn receive_status_update_works_with_batches_from_relayers() {
run_test(|| { run_test(|| {
let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
let mut seed_storage_data = lane.storage.data(); let mut seed_storage_data = lane.storage.get_or_init_data();
// Prepare data // Prepare data
seed_storage_data.last_confirmed_nonce = 0; seed_storage_data.last_confirmed_nonce = 0;
seed_storage_data.relayers.push_back(unrewarded_relayer(1, 1, TEST_RELAYER_A)); seed_storage_data.relayers.push_back(unrewarded_relayer(1, 1, TEST_RELAYER_A));
@@ -341,9 +341,9 @@ mod tests {
}), }),
Some(3), Some(3),
); );
assert_eq!(lane.storage.data().last_confirmed_nonce, 3); assert_eq!(lane.storage.get_or_init_data().last_confirmed_nonce, 3);
assert_eq!( assert_eq!(
lane.storage.data().relayers, lane.storage.get_or_init_data().relayers,
vec![ vec![
unrewarded_relayer(4, 4, TEST_RELAYER_B), unrewarded_relayer(4, 4, TEST_RELAYER_B),
unrewarded_relayer(5, 5, TEST_RELAYER_C) unrewarded_relayer(5, 5, TEST_RELAYER_C)
@@ -364,7 +364,7 @@ mod tests {
), ),
ReceivalResult::InvalidNonce ReceivalResult::InvalidNonce
); );
assert_eq!(lane.storage.data().last_delivered_nonce(), 0); assert_eq!(lane.storage.get_or_init_data().last_delivered_nonce(), 0);
}); });
} }
@@ -470,7 +470,7 @@ mod tests {
ReceivalResult::Dispatched(dispatch_result(0)) ReceivalResult::Dispatched(dispatch_result(0))
); );
assert_eq!( assert_eq!(
lane.storage.data().relayers, lane.storage.get_or_init_data().relayers,
vec![ vec![
unrewarded_relayer(1, 1, TEST_RELAYER_A), unrewarded_relayer(1, 1, TEST_RELAYER_A),
unrewarded_relayer(2, 2, TEST_RELAYER_B), unrewarded_relayer(2, 2, TEST_RELAYER_B),
@@ -508,7 +508,7 @@ mod tests {
run_test(|| { run_test(|| {
let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
receive_regular_message(&mut lane, 1); receive_regular_message(&mut lane, 1);
assert_eq!(lane.storage.data().last_delivered_nonce(), 1); assert_eq!(lane.storage.get_or_init_data().last_delivered_nonce(), 1);
}); });
} }
+37 -45
View File
@@ -67,7 +67,7 @@ use bp_runtime::{BasicOperatingMode, ChainId, OwnedBridgeModule, PreComputedSize
use codec::{Decode, Encode, MaxEncodedLen}; use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::PostDispatchInfo, ensure, fail, traits::Get}; use frame_support::{dispatch::PostDispatchInfo, ensure, fail, traits::Get};
use sp_runtime::traits::UniqueSaturatedFrom; use sp_runtime::traits::UniqueSaturatedFrom;
use sp_std::{cell::RefCell, marker::PhantomData, prelude::*}; use sp_std::{marker::PhantomData, prelude::*};
mod inbound_lane; mod inbound_lane;
mod outbound_lane; mod outbound_lane;
@@ -319,7 +319,7 @@ pub mod pallet {
// subtract extra storage proof bytes from the actual PoV size - there may be // subtract extra storage proof bytes from the actual PoV size - there may be
// less unrewarded relayers than the maximal configured value // less unrewarded relayers than the maximal configured value
let lane_extra_proof_size_bytes = lane.storage().extra_proof_size_bytes(); let lane_extra_proof_size_bytes = lane.storage_mut().extra_proof_size_bytes();
actual_weight = actual_weight.set_proof_size( actual_weight = actual_weight.set_proof_size(
actual_weight.proof_size().saturating_sub(lane_extra_proof_size_bytes), actual_weight.proof_size().saturating_sub(lane_extra_proof_size_bytes),
); );
@@ -332,7 +332,7 @@ pub mod pallet {
"Received lane {:?} state update: latest_confirmed_nonce={}. Unrewarded relayers: {:?}", "Received lane {:?} state update: latest_confirmed_nonce={}. Unrewarded relayers: {:?}",
lane_id, lane_id,
updated_latest_confirmed_nonce, updated_latest_confirmed_nonce,
UnrewardedRelayersState::from(&lane.storage().data()), UnrewardedRelayersState::from(&lane.storage_mut().get_or_init_data()),
); );
} }
} }
@@ -531,12 +531,12 @@ pub mod pallet {
NotOperatingNormally, NotOperatingNormally,
/// The outbound lane is inactive. /// The outbound lane is inactive.
InactiveOutboundLane, InactiveOutboundLane,
/// The message is too large to be sent over the bridge.
MessageIsTooLarge,
/// Message has been treated as invalid by chain verifier. /// Message has been treated as invalid by chain verifier.
MessageRejectedByChainVerifier(VerificationError), MessageRejectedByChainVerifier(VerificationError),
/// Message has been treated as invalid by lane verifier. /// Message has been treated as invalid by lane verifier.
MessageRejectedByLaneVerifier(VerificationError), MessageRejectedByLaneVerifier(VerificationError),
/// Message has been treated as invalid by the pallet logic.
MessageRejectedByPallet(VerificationError),
/// Submitter has failed to pay fee for delivering and dispatching messages. /// Submitter has failed to pay fee for delivering and dispatching messages.
FailedToWithdrawMessageFee, FailedToWithdrawMessageFee,
/// The transaction brings too many messages. /// The transaction brings too many messages.
@@ -727,11 +727,9 @@ fn send_message<T: Config<I>, I: 'static>(
// finally, save message in outbound storage and emit event // finally, save message in outbound storage and emit event
let encoded_payload = payload.encode(); let encoded_payload = payload.encode();
let encoded_payload_len = encoded_payload.len(); let encoded_payload_len = encoded_payload.len();
ensure!( let nonce = lane
encoded_payload_len <= T::MaximalOutboundPayloadSize::get() as usize, .send_message(encoded_payload)
Error::<T, I>::MessageIsTooLarge .map_err(Error::<T, I>::MessageRejectedByPallet)?;
);
let nonce = lane.send_message(encoded_payload);
log::trace!( log::trace!(
target: LOG_TARGET, target: LOG_TARGET,
@@ -761,18 +759,7 @@ fn ensure_normal_operating_mode<T: Config<I>, I: 'static>() -> Result<(), Error<
fn inbound_lane<T: Config<I>, I: 'static>( fn inbound_lane<T: Config<I>, I: 'static>(
lane_id: LaneId, lane_id: LaneId,
) -> InboundLane<RuntimeInboundLaneStorage<T, I>> { ) -> InboundLane<RuntimeInboundLaneStorage<T, I>> {
InboundLane::new(inbound_lane_storage::<T, I>(lane_id)) InboundLane::new(RuntimeInboundLaneStorage::from_lane_id(lane_id))
}
/// Creates new runtime inbound lane storage.
fn inbound_lane_storage<T: Config<I>, I: 'static>(
lane_id: LaneId,
) -> RuntimeInboundLaneStorage<T, I> {
RuntimeInboundLaneStorage {
lane_id,
cached_data: RefCell::new(None),
_phantom: Default::default(),
}
} }
/// Creates new outbound lane object, backed by runtime storage. /// Creates new outbound lane object, backed by runtime storage.
@@ -785,10 +772,17 @@ fn outbound_lane<T: Config<I>, I: 'static>(
/// Runtime inbound lane storage. /// Runtime inbound lane storage.
struct RuntimeInboundLaneStorage<T: Config<I>, I: 'static = ()> { struct RuntimeInboundLaneStorage<T: Config<I>, I: 'static = ()> {
lane_id: LaneId, lane_id: LaneId,
cached_data: RefCell<Option<InboundLaneData<T::InboundRelayer>>>, cached_data: Option<InboundLaneData<T::InboundRelayer>>,
_phantom: PhantomData<I>, _phantom: PhantomData<I>,
} }
impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {
/// Creates new runtime inbound lane storage.
fn from_lane_id(lane_id: LaneId) -> RuntimeInboundLaneStorage<T, I> {
RuntimeInboundLaneStorage { lane_id, cached_data: None, _phantom: Default::default() }
}
}
impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> { impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {
/// Returns number of bytes that may be subtracted from the PoV component of /// Returns number of bytes that may be subtracted from the PoV component of
/// `receive_messages_proof` call, because the actual inbound lane state is smaller than the /// `receive_messages_proof` call, because the actual inbound lane state is smaller than the
@@ -798,9 +792,9 @@ impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {
/// `MaxUnrewardedRelayerEntriesAtInboundLane` constant from the pallet configuration. The PoV /// `MaxUnrewardedRelayerEntriesAtInboundLane` constant from the pallet configuration. The PoV
/// of the call includes the maximal size of inbound lane state. If the actual size is smaller, /// of the call includes the maximal size of inbound lane state. If the actual size is smaller,
/// we may subtract extra bytes from this component. /// we may subtract extra bytes from this component.
pub fn extra_proof_size_bytes(&self) -> u64 { pub fn extra_proof_size_bytes(&mut self) -> u64 {
let max_encoded_len = StoredInboundLaneData::<T, I>::max_encoded_len(); let max_encoded_len = StoredInboundLaneData::<T, I>::max_encoded_len();
let relayers_count = self.data().relayers.len(); let relayers_count = self.get_or_init_data().relayers.len();
let actual_encoded_len = let actual_encoded_len =
InboundLaneData::<T::InboundRelayer>::encoded_size_hint(relayers_count) InboundLaneData::<T::InboundRelayer>::encoded_size_hint(relayers_count)
.unwrap_or(usize::MAX); .unwrap_or(usize::MAX);
@@ -823,26 +817,20 @@ impl<T: Config<I>, I: 'static> InboundLaneStorage for RuntimeInboundLaneStorage<
T::MaxUnconfirmedMessagesAtInboundLane::get() T::MaxUnconfirmedMessagesAtInboundLane::get()
} }
fn data(&self) -> InboundLaneData<T::InboundRelayer> { fn get_or_init_data(&mut self) -> InboundLaneData<T::InboundRelayer> {
match self.cached_data.clone().into_inner() { match self.cached_data {
Some(data) => data, Some(ref data) => data.clone(),
None => { None => {
let data: InboundLaneData<T::InboundRelayer> = let data: InboundLaneData<T::InboundRelayer> =
InboundLanes::<T, I>::get(self.lane_id).into(); InboundLanes::<T, I>::get(self.lane_id).into();
*self.cached_data.try_borrow_mut().expect( self.cached_data = Some(data.clone());
"we're in the single-threaded environment;\
we have no recursive borrows; qed",
) = Some(data.clone());
data data
}, },
} }
} }
fn set_data(&mut self, data: InboundLaneData<T::InboundRelayer>) { fn set_data(&mut self, data: InboundLaneData<T::InboundRelayer>) {
*self.cached_data.try_borrow_mut().expect( self.cached_data = Some(data.clone());
"we're in the single-threaded environment;\
we have no recursive borrows; qed",
) = Some(data.clone());
InboundLanes::<T, I>::insert(self.lane_id, StoredInboundLaneData::<T, I>(data)) InboundLanes::<T, I>::insert(self.lane_id, StoredInboundLaneData::<T, I>(data))
} }
} }
@@ -872,15 +860,17 @@ impl<T: Config<I>, I: 'static> OutboundLaneStorage for RuntimeOutboundLaneStorag
.map(Into::into) .map(Into::into)
} }
fn save_message(&mut self, nonce: MessageNonce, message_payload: MessagePayload) { fn save_message(
&mut self,
nonce: MessageNonce,
message_payload: MessagePayload,
) -> Result<(), VerificationError> {
OutboundMessages::<T, I>::insert( OutboundMessages::<T, I>::insert(
MessageKey { lane_id: self.lane_id, nonce }, MessageKey { lane_id: self.lane_id, nonce },
StoredMessagePayload::<T, I>::try_from(message_payload).expect( StoredMessagePayload::<T, I>::try_from(message_payload)
"save_message is called after all checks in send_message; \ .map_err(|_| VerificationError::MessageTooLarge)?,
send_message checks message size; \
qed",
),
); );
Ok(())
} }
fn remove_message(&mut self, nonce: &MessageNonce) { fn remove_message(&mut self, nonce: &MessageNonce) {
@@ -1128,7 +1118,9 @@ mod tests {
TEST_LANE_ID, TEST_LANE_ID,
message_payload.clone(), message_payload.clone(),
), ),
Error::<TestRuntime, ()>::MessageIsTooLarge, Error::<TestRuntime, ()>::MessageRejectedByPallet(
VerificationError::MessageTooLarge
),
); );
// let's check that we're able to send `MAX_OUTBOUND_PAYLOAD_SIZE` messages // let's check that we're able to send `MAX_OUTBOUND_PAYLOAD_SIZE` messages
@@ -2097,10 +2089,10 @@ mod tests {
fn storage(relayer_entries: usize) -> RuntimeInboundLaneStorage<TestRuntime, ()> { fn storage(relayer_entries: usize) -> RuntimeInboundLaneStorage<TestRuntime, ()> {
RuntimeInboundLaneStorage { RuntimeInboundLaneStorage {
lane_id: Default::default(), lane_id: Default::default(),
cached_data: RefCell::new(Some(InboundLaneData { cached_data: Some(InboundLaneData {
relayers: vec![relayer_entry(); relayer_entries].into_iter().collect(), relayers: vec![relayer_entry(); relayer_entries].into_iter().collect(),
last_confirmed_nonce: 0, last_confirmed_nonce: 0,
})), }),
_phantom: Default::default(), _phantom: Default::default(),
} }
} }
+29 -21
View File
@@ -20,6 +20,7 @@ use crate::{Config, LOG_TARGET};
use bp_messages::{ use bp_messages::{
DeliveredMessages, LaneId, MessageNonce, MessagePayload, OutboundLaneData, UnrewardedRelayer, DeliveredMessages, LaneId, MessageNonce, MessagePayload, OutboundLaneData, UnrewardedRelayer,
VerificationError,
}; };
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use frame_support::{ use frame_support::{
@@ -42,7 +43,11 @@ pub trait OutboundLaneStorage {
#[cfg(test)] #[cfg(test)]
fn message(&self, nonce: &MessageNonce) -> Option<MessagePayload>; fn message(&self, nonce: &MessageNonce) -> Option<MessagePayload>;
/// Save outbound message in the storage. /// Save outbound message in the storage.
fn save_message(&mut self, nonce: MessageNonce, message_payload: MessagePayload); fn save_message(
&mut self,
nonce: MessageNonce,
message_payload: MessagePayload,
) -> Result<(), VerificationError>;
/// Remove outbound message from the storage. /// Remove outbound message from the storage.
fn remove_message(&mut self, nonce: &MessageNonce); fn remove_message(&mut self, nonce: &MessageNonce);
} }
@@ -85,15 +90,18 @@ impl<S: OutboundLaneStorage> OutboundLane<S> {
/// Send message over lane. /// Send message over lane.
/// ///
/// Returns new message nonce. /// Returns new message nonce.
pub fn send_message(&mut self, message_payload: MessagePayload) -> MessageNonce { pub fn send_message(
&mut self,
message_payload: MessagePayload,
) -> Result<MessageNonce, VerificationError> {
let mut data = self.storage.data(); let mut data = self.storage.data();
let nonce = data.latest_generated_nonce + 1; let nonce = data.latest_generated_nonce + 1;
data.latest_generated_nonce = nonce; data.latest_generated_nonce = nonce;
self.storage.save_message(nonce, message_payload); self.storage.save_message(nonce, message_payload)?;
self.storage.set_data(data); self.storage.set_data(data);
nonce Ok(nonce)
} }
/// Confirm messages delivery. /// Confirm messages delivery.
@@ -209,7 +217,7 @@ mod tests {
}, },
outbound_lane, outbound_lane,
}; };
use frame_support::weights::constants::RocksDbWeight; use frame_support::{assert_ok, weights::constants::RocksDbWeight};
use sp_std::ops::RangeInclusive; use sp_std::ops::RangeInclusive;
fn unrewarded_relayers( fn unrewarded_relayers(
@@ -230,9 +238,9 @@ mod tests {
) -> Result<Option<DeliveredMessages>, ReceivalConfirmationError> { ) -> Result<Option<DeliveredMessages>, ReceivalConfirmationError> {
run_test(|| { run_test(|| {
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
assert_eq!(lane.storage.data().latest_generated_nonce, 3); assert_eq!(lane.storage.data().latest_generated_nonce, 3);
assert_eq!(lane.storage.data().latest_received_nonce, 0); assert_eq!(lane.storage.data().latest_received_nonce, 0);
let result = lane.confirm_delivery(3, latest_received_nonce, relayers); let result = lane.confirm_delivery(3, latest_received_nonce, relayers);
@@ -247,7 +255,7 @@ mod tests {
run_test(|| { run_test(|| {
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
assert_eq!(lane.storage.data().latest_generated_nonce, 0); assert_eq!(lane.storage.data().latest_generated_nonce, 0);
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), 1); assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), Ok(1));
assert!(lane.storage.message(&1).is_some()); assert!(lane.storage.message(&1).is_some());
assert_eq!(lane.storage.data().latest_generated_nonce, 1); assert_eq!(lane.storage.data().latest_generated_nonce, 1);
}); });
@@ -257,9 +265,9 @@ mod tests {
fn confirm_delivery_works() { fn confirm_delivery_works() {
run_test(|| { run_test(|| {
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), 1); assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), Ok(1));
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), 2); assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), Ok(2));
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), 3); assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), Ok(3));
assert_eq!(lane.storage.data().latest_generated_nonce, 3); assert_eq!(lane.storage.data().latest_generated_nonce, 3);
assert_eq!(lane.storage.data().latest_received_nonce, 0); assert_eq!(lane.storage.data().latest_received_nonce, 0);
assert_eq!( assert_eq!(
@@ -275,9 +283,9 @@ mod tests {
fn confirm_delivery_rejects_nonce_lesser_than_latest_received() { fn confirm_delivery_rejects_nonce_lesser_than_latest_received() {
run_test(|| { run_test(|| {
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
assert_eq!(lane.storage.data().latest_generated_nonce, 3); assert_eq!(lane.storage.data().latest_generated_nonce, 3);
assert_eq!(lane.storage.data().latest_received_nonce, 0); assert_eq!(lane.storage.data().latest_received_nonce, 0);
assert_eq!( assert_eq!(
@@ -359,9 +367,9 @@ mod tests {
); );
assert_eq!(lane.storage.data().oldest_unpruned_nonce, 1); assert_eq!(lane.storage.data().oldest_unpruned_nonce, 1);
// when nothing is confirmed, nothing is pruned // when nothing is confirmed, nothing is pruned
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
assert!(lane.storage.message(&1).is_some()); assert!(lane.storage.message(&1).is_some());
assert!(lane.storage.message(&2).is_some()); assert!(lane.storage.message(&2).is_some());
assert!(lane.storage.message(&3).is_some()); assert!(lane.storage.message(&3).is_some());
@@ -403,9 +411,9 @@ mod tests {
fn confirm_delivery_detects_when_more_than_expected_messages_are_confirmed() { fn confirm_delivery_detects_when_more_than_expected_messages_are_confirmed() {
run_test(|| { run_test(|| {
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID); let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
lane.send_message(outbound_message_data(REGULAR_PAYLOAD)); assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
assert_eq!( assert_eq!(
lane.confirm_delivery(0, 3, &unrewarded_relayers(1..=3)), lane.confirm_delivery(0, 3, &unrewarded_relayers(1..=3)),
Err(ReceivalConfirmationError::TryingToConfirmMoreMessagesThanExpected), Err(ReceivalConfirmationError::TryingToConfirmMoreMessagesThanExpected),