Skip to content

Commit 78aee75

Browse files
authored
Feat support weight test (#7769)
* update support weights_are_correct * add support polynomial_does_not_underflow
1 parent 02c4ddb commit 78aee75

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/weights.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ mod tests {
866866
fn f12(_origin, _a: u32, _eb: u32) { unimplemented!(); }
867867

868868
#[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + 10_000]
869-
fn f2(_origin) { unimplemented!(); }
869+
fn f20(_origin) { unimplemented!(); }
870870

871871
#[weight = T::DbWeight::get().reads_writes(6, 5) + 40_000]
872872
fn f21(_origin) { unimplemented!(); }
@@ -900,13 +900,29 @@ mod tests {
900900
assert_eq!(info.class, DispatchClass::Operational);
901901
assert_eq!(info.pays_fee, Pays::No);
902902

903-
assert_eq!(Call::<TraitImpl>::f11(10, 20).get_dispatch_info().weight, 120);
904-
assert_eq!(Call::<TraitImpl>::f11(10, 20).get_dispatch_info().class, DispatchClass::Normal);
905-
assert_eq!(Call::<TraitImpl>::f12(10, 20).get_dispatch_info().weight, 0);
906-
assert_eq!(Call::<TraitImpl>::f12(10, 20).get_dispatch_info().class, DispatchClass::Operational);
907-
assert_eq!(Call::<TraitImpl>::f2().get_dispatch_info().weight, 12300);
908-
assert_eq!(Call::<TraitImpl>::f21().get_dispatch_info().weight, 45600);
909-
assert_eq!(Call::<TraitImpl>::f2().get_dispatch_info().class, DispatchClass::Normal);
903+
// #[weight = ((_a * 10 + _eb * 1) as Weight, DispatchClass::Normal, Pays::Yes)]
904+
let info = Call::<TraitImpl>::f11(13, 20).get_dispatch_info();
905+
assert_eq!(info.weight, 150); // 13*10 + 20
906+
assert_eq!(info.class, DispatchClass::Normal);
907+
assert_eq!(info.pays_fee, Pays::Yes);
908+
909+
// #[weight = (0, DispatchClass::Operational, Pays::Yes)]
910+
let info = Call::<TraitImpl>::f12(10, 20).get_dispatch_info();
911+
assert_eq!(info.weight, 0);
912+
assert_eq!(info.class, DispatchClass::Operational);
913+
assert_eq!(info.pays_fee, Pays::Yes);
914+
915+
// #[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + 10_000]
916+
let info = Call::<TraitImpl>::f20().get_dispatch_info();
917+
assert_eq!(info.weight, 12300); // 100*3 + 1000*2 + 10_1000
918+
assert_eq!(info.class, DispatchClass::Normal);
919+
assert_eq!(info.pays_fee, Pays::Yes);
920+
921+
// #[weight = T::DbWeight::get().reads_writes(6, 5) + 40_000]
922+
let info = Call::<TraitImpl>::f21().get_dispatch_info();
923+
assert_eq!(info.weight, 45600); // 100*6 + 1000*5 + 40_1000
924+
assert_eq!(info.class, DispatchClass::Normal);
925+
assert_eq!(info.pays_fee, Pays::Yes);
910926
}
911927

912928
#[test]
@@ -938,7 +954,7 @@ mod tests {
938954

939955
type Balance = u64;
940956

941-
// 0.5x^3 + 2.333x2 + 7x - 10_000
957+
// 0.5x^3 + 2.333x^2 + 7x - 10_000
942958
struct Poly;
943959
impl WeightToFeePolynomial for Poly {
944960
type Balance = Balance;
@@ -975,13 +991,16 @@ mod tests {
975991

976992
#[test]
977993
fn polynomial_works() {
994+
// 100^3/2=500000 100^2*(2+1/3)=23333 700 -10000
978995
assert_eq!(Poly::calc(&100), 514033);
996+
// 10123^3/2=518677865433 10123^2*(2+1/3)=239108634 70861 -10000
979997
assert_eq!(Poly::calc(&10_123), 518917034928);
980998
}
981999

9821000
#[test]
9831001
fn polynomial_does_not_underflow() {
9841002
assert_eq!(Poly::calc(&0), 0);
1003+
assert_eq!(Poly::calc(&10), 0);
9851004
}
9861005

9871006
#[test]

0 commit comments

Comments
 (0)