mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 20:21:03 +00:00
Remove some expect() statements (#2123)
* Return error on save_message() Prevent unexpected failures. * Remove RefCell * Fix spellcheck * CI fixes
This commit is contained in:
committed by
Bastian Köcher
parent
eec97df358
commit
06e0204694
@@ -20,6 +20,7 @@ use crate::{Config, LOG_TARGET};
|
||||
|
||||
use bp_messages::{
|
||||
DeliveredMessages, LaneId, MessageNonce, MessagePayload, OutboundLaneData, UnrewardedRelayer,
|
||||
VerificationError,
|
||||
};
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
@@ -42,7 +43,11 @@ pub trait OutboundLaneStorage {
|
||||
#[cfg(test)]
|
||||
fn message(&self, nonce: &MessageNonce) -> Option<MessagePayload>;
|
||||
/// 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.
|
||||
fn remove_message(&mut self, nonce: &MessageNonce);
|
||||
}
|
||||
@@ -85,15 +90,18 @@ impl<S: OutboundLaneStorage> OutboundLane<S> {
|
||||
/// Send message over lane.
|
||||
///
|
||||
/// 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 nonce = data.latest_generated_nonce + 1;
|
||||
data.latest_generated_nonce = nonce;
|
||||
|
||||
self.storage.save_message(nonce, message_payload);
|
||||
self.storage.save_message(nonce, message_payload)?;
|
||||
self.storage.set_data(data);
|
||||
|
||||
nonce
|
||||
Ok(nonce)
|
||||
}
|
||||
|
||||
/// Confirm messages delivery.
|
||||
@@ -209,7 +217,7 @@ mod tests {
|
||||
},
|
||||
outbound_lane,
|
||||
};
|
||||
use frame_support::weights::constants::RocksDbWeight;
|
||||
use frame_support::{assert_ok, weights::constants::RocksDbWeight};
|
||||
use sp_std::ops::RangeInclusive;
|
||||
|
||||
fn unrewarded_relayers(
|
||||
@@ -230,9 +238,9 @@ mod tests {
|
||||
) -> Result<Option<DeliveredMessages>, ReceivalConfirmationError> {
|
||||
run_test(|| {
|
||||
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
|
||||
lane.send_message(outbound_message_data(REGULAR_PAYLOAD));
|
||||
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_ok!(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_received_nonce, 0);
|
||||
let result = lane.confirm_delivery(3, latest_received_nonce, relayers);
|
||||
@@ -247,7 +255,7 @@ mod tests {
|
||||
run_test(|| {
|
||||
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
|
||||
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_eq!(lane.storage.data().latest_generated_nonce, 1);
|
||||
});
|
||||
@@ -257,9 +265,9 @@ mod tests {
|
||||
fn confirm_delivery_works() {
|
||||
run_test(|| {
|
||||
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)), 2);
|
||||
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), 3);
|
||||
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), Ok(1));
|
||||
assert_eq!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)), Ok(2));
|
||||
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_received_nonce, 0);
|
||||
assert_eq!(
|
||||
@@ -275,9 +283,9 @@ mod tests {
|
||||
fn confirm_delivery_rejects_nonce_lesser_than_latest_received() {
|
||||
run_test(|| {
|
||||
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
|
||||
lane.send_message(outbound_message_data(REGULAR_PAYLOAD));
|
||||
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_ok!(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_received_nonce, 0);
|
||||
assert_eq!(
|
||||
@@ -359,9 +367,9 @@ mod tests {
|
||||
);
|
||||
assert_eq!(lane.storage.data().oldest_unpruned_nonce, 1);
|
||||
// when nothing is confirmed, nothing is pruned
|
||||
lane.send_message(outbound_message_data(REGULAR_PAYLOAD));
|
||||
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_ok!(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(&2).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() {
|
||||
run_test(|| {
|
||||
let mut lane = outbound_lane::<TestRuntime, _>(TEST_LANE_ID);
|
||||
lane.send_message(outbound_message_data(REGULAR_PAYLOAD));
|
||||
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_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
|
||||
assert_ok!(lane.send_message(outbound_message_data(REGULAR_PAYLOAD)));
|
||||
assert_eq!(
|
||||
lane.confirm_delivery(0, 3, &unrewarded_relayers(1..=3)),
|
||||
Err(ReceivalConfirmationError::TryingToConfirmMoreMessagesThanExpected),
|
||||
|
||||
Reference in New Issue
Block a user