Newer
Older
Svyatoslav Nikolsky
committed
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
},
));
// first tx with messages 1+2
assert_ok!(Pallet::<TestRuntime>::receive_messages_delivery_proof(
Origin::signed(1),
TestMessagesDeliveryProof(messages_1_and_2_proof),
UnrewardedRelayersState {
unrewarded_relayer_entries: 1,
total_messages: 2,
..Default::default()
},
));
// second tx with message 3
assert_ok!(Pallet::<TestRuntime>::receive_messages_delivery_proof(
Origin::signed(1),
TestMessagesDeliveryProof(messages_3_proof),
UnrewardedRelayersState {
unrewarded_relayer_entries: 1,
total_messages: 1,
..Default::default()
},
));
// ensure that both callbacks have been called twice: for 1+2, then for 3
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
TestOnDeliveryConfirmed1::ensure_called(&TEST_LANE_ID, &delivered_messages_1_and_2);
TestOnDeliveryConfirmed1::ensure_called(&TEST_LANE_ID, &delivered_message_3);
TestOnDeliveryConfirmed2::ensure_called(&TEST_LANE_ID, &delivered_messages_1_and_2);
TestOnDeliveryConfirmed2::ensure_called(&TEST_LANE_ID, &delivered_message_3);
});
}
fn confirm_3_messages_delivery() -> (Weight, Weight) {
send_regular_message();
send_regular_message();
send_regular_message();
let proof = TestMessagesDeliveryProof(Ok((
TEST_LANE_ID,
InboundLaneData {
last_confirmed_nonce: 0,
relayers: vec![unrewarded_relayer(1, 3, TEST_RELAYER_A)].into_iter().collect(),
},
)));
let relayers_state = UnrewardedRelayersState {
unrewarded_relayer_entries: 1,
total_messages: 3,
..Default::default()
};
let pre_dispatch_weight = <TestRuntime as Config>::WeightInfo::receive_messages_delivery_proof_weight(
&proof,
&relayers_state,
crate::mock::DbWeight::get(),
);
let post_dispatch_weight =
Pallet::<TestRuntime>::receive_messages_delivery_proof(Origin::signed(1), proof, relayers_state)
.expect("confirmation has failed")
.actual_weight
.expect("receive_messages_delivery_proof always returns Some");
(pre_dispatch_weight, post_dispatch_weight)
}
#[test]
fn receive_messages_delivery_proof_refunds_zero_weight() {
run_test(|| {
let (pre_dispatch_weight, post_dispatch_weight) = confirm_3_messages_delivery();
assert_eq!(pre_dispatch_weight, post_dispatch_weight);
});
}
#[test]
fn receive_messages_delivery_proof_refunds_non_zero_weight() {
run_test(|| {
TestOnDeliveryConfirmed1::set_consumed_weight_per_message(crate::mock::DbWeight::get().writes(1));
let (pre_dispatch_weight, post_dispatch_weight) = confirm_3_messages_delivery();
assert_eq!(
pre_dispatch_weight.saturating_sub(post_dispatch_weight),
crate::mock::DbWeight::get().reads(1) * 3
);
});
}
#[test]
#[should_panic]
fn receive_messages_panics_in_debug_mode_if_callback_is_wrong() {
run_test(|| {
TestOnDeliveryConfirmed1::set_consumed_weight_per_message(crate::mock::DbWeight::get().reads_writes(2, 2));
confirm_3_messages_delivery()
Svyatoslav Nikolsky
committed
});
}
Svyatoslav Nikolsky
committed
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
#[test]
fn receive_messages_delivery_proof_rejects_proof_if_trying_to_confirm_more_messages_than_expected() {
run_test(|| {
// send message first to be able to check that delivery_proof fails later
send_regular_message();
// 1) InboundLaneData declares that the `last_confirmed_nonce` is 1;
// 2) InboundLaneData has no entries => `InboundLaneData::last_delivered_nonce()`
// returns `last_confirmed_nonce`;
// 3) it means that we're going to confirm delivery of messages 1..=1;
// 4) so the number of declared messages (see `UnrewardedRelayersState`) is `0` and
// numer of actually confirmed messages is `1`.
assert_noop!(
Pallet::<TestRuntime>::receive_messages_delivery_proof(
Origin::signed(1),
TestMessagesDeliveryProof(Ok((
TEST_LANE_ID,
InboundLaneData {
last_confirmed_nonce: 1,
relayers: Default::default(),
},
))),
UnrewardedRelayersState::default(),
),
Error::<TestRuntime, DefaultInstance>::TryingToConfirmMoreMessagesThanExpected,
);
});
}
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
#[test]
fn increase_message_fee_weight_depends_on_message_size() {
run_test(|| {
let mut small_payload = message_payload(0, 100);
let mut large_payload = message_payload(1, 100);
small_payload.extra = vec![1; 100];
large_payload.extra = vec![2; 16_384];
assert_ok!(Pallet::<TestRuntime>::send_message(
Origin::signed(1),
TEST_LANE_ID,
small_payload,
100,
));
assert_ok!(Pallet::<TestRuntime>::send_message(
Origin::signed(1),
TEST_LANE_ID,
large_payload,
100,
));
let small_weight = Pallet::<TestRuntime>::increase_message_fee(Origin::signed(1), TEST_LANE_ID, 1, 1)
.expect("increase_message_fee has failed")
.actual_weight
.expect("increase_message_fee always returns Some");
let large_weight = Pallet::<TestRuntime>::increase_message_fee(Origin::signed(1), TEST_LANE_ID, 2, 1)
.expect("increase_message_fee has failed")
.actual_weight
.expect("increase_message_fee always returns Some");
assert!(
large_weight > small_weight,
"Actual post-dispatch weigth for larger message {} must be larger than {} for small message",
large_weight,
small_weight,
);
});
}
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
#[test]
fn weight_is_refunded_for_messages_that_are_not_pruned() {
run_test(|| {
// send first MAX messages - no messages are pruned
let max_messages_to_prune = crate::mock::MaxMessagesToPruneAtOnce::get();
let when_zero_messages_are_pruned = send_regular_message();
let mut delivered_messages = DeliveredMessages::new(1, true);
for _ in 1..max_messages_to_prune {
assert_eq!(send_regular_message(), when_zero_messages_are_pruned);
delivered_messages.note_dispatched_message(true);
}
// confirm delivery of all sent messages
assert_ok!(Pallet::<TestRuntime>::receive_messages_delivery_proof(
Origin::signed(1),
TestMessagesDeliveryProof(Ok((
TEST_LANE_ID,
InboundLaneData {
last_confirmed_nonce: 1,
relayers: vec![UnrewardedRelayer {
relayer: 0,
messages: delivered_messages,
}]
.into_iter()
.collect(),
},
))),
UnrewardedRelayersState {
unrewarded_relayer_entries: 1,
total_messages: max_messages_to_prune,
..Default::default()
},
));
// when next message is sent, MAX messages are pruned
let weight_when_max_messages_are_pruned = send_regular_message();
assert_eq!(
weight_when_max_messages_are_pruned,
when_zero_messages_are_pruned + crate::mock::DbWeight::get().writes(max_messages_to_prune),
);
});
}