From a7bd712e7ed14a5d7752a52d5acc2aa96a08f897 Mon Sep 17 00:00:00 2001 From: dung5ire Date: Wed, 21 Aug 2024 15:39:07 +0700 Subject: [PATCH 1/3] chore: fix mock runtime after auditing Signed-off-by: dung5ire --- frame/babe/src/mock.rs | 2 ++ frame/esg/src/mock.rs | 2 +- frame/esg/src/weights.rs | 47 ++++++++++++++++++++++++++++++++++++- frame/grandpa/src/mock.rs | 2 ++ frame/im-online/src/mock.rs | 2 ++ frame/reward/src/mock.rs | 2 ++ frame/staking/src/mock.rs | 2 ++ 7 files changed, 57 insertions(+), 2 deletions(-) diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 46d0f8ca..46133c95 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -275,6 +275,8 @@ impl pallet_esg::Config for Test { type RuntimeEvent = RuntimeEvent; type MaxFileSize = ConstU32<1024000>; type WeightInfo = (); + type MaxNumOfSudoOracles = ConstU32<5>; + type MaxNumOfNonSudoOracles = ConstU32<5>; } impl pallet_offences::Config for Test { diff --git a/frame/esg/src/mock.rs b/frame/esg/src/mock.rs index da64c62c..7637fab1 100644 --- a/frame/esg/src/mock.rs +++ b/frame/esg/src/mock.rs @@ -69,7 +69,7 @@ parameter_types! { pub const MaxNumOfNonSudoOracles: u32 = 100; } -impl pallet_esg::Config for Runtime { +impl pallet_esg::Config for Test { type RuntimeEvent = RuntimeEvent; type MaxFileSize = MaxFileSize; type MaxNumOfSudoOracles = MaxNumOfSudoOracles; diff --git a/frame/esg/src/weights.rs b/frame/esg/src/weights.rs index 3040e056..5b151c81 100644 --- a/frame/esg/src/weights.rs +++ b/frame/esg/src/weights.rs @@ -29,7 +29,7 @@ #![allow(unused_imports)] #![allow(missing_docs)] -use frame_support::{traits::Get, weights::Weight}; +use frame_support::{traits::Get, weights::{constants::RocksDbWeight, Weight}}; use core::marker::PhantomData; pub trait WeightInfo { @@ -82,3 +82,48 @@ impl WeightInfo for SubstrateWeightInfo { .saturating_add(T::DbWeight::get().writes(2)) } } + + + +impl WeightInfo for () { + /// Storage: `EsgScore::SudoOraclesStore` (r:1 w:1) + /// Proof: `EsgScore::SudoOraclesStore` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `EsgScore::NonSudoOraclesStore` (r:1 w:0) + /// Proof: `EsgScore::NonSudoOraclesStore` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn register_an_oracle() -> Weight { + // Proof Size summary in bytes: + // Measured: `2109` + // Estimated: `3594` + // Minimum execution time: 17_000_000 picoseconds. + Weight::from_parts(18_000_000, 0) + .saturating_add(Weight::from_parts(0, 3594)) + .saturating_add(RocksDbWeight::get().reads(2)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + /// Storage: `EsgScore::SudoOraclesStore` (r:1 w:1) + /// Proof: `EsgScore::SudoOraclesStore` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn deregister_an_oracle() -> Weight { + // Proof Size summary in bytes: + // Measured: `238` + // Estimated: `1723` + // Minimum execution time: 14_000_000 picoseconds. + Weight::from_parts(14_000_000, 0) + .saturating_add(Weight::from_parts(0, 1723)) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + /// Storage: `EsgScore::SudoOraclesStore` (r:1 w:0) + /// Proof: `EsgScore::SudoOraclesStore` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `EsgScore::ESGScoresMap` (r:2 w:2) + /// Proof: `EsgScore::ESGScoresMap` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn upsert_esg_scores() -> Weight { + // Proof Size summary in bytes: + // Measured: `238` + // Estimated: `6178` + // Minimum execution time: 1_028_000_000 picoseconds. + Weight::from_parts(1_048_000_000, 0) + .saturating_add(Weight::from_parts(0, 6178)) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(2)) + } +} \ No newline at end of file diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index e0b2c767..fd879284 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -220,6 +220,8 @@ impl pallet_esg::Config for Test { type RuntimeEvent = RuntimeEvent; type MaxFileSize = ConstU32<1024000>; type WeightInfo = (); + type MaxNumOfSudoOracles = ConstU32<5>; + type MaxNumOfNonSudoOracles = ConstU32<5>; } pub struct OnChainSeqPhragmen; diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 4b299c5b..e1bcc470 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -243,6 +243,8 @@ impl pallet_esg::Config for Test { type WeightInfo = (); type MaxFileSize = ConstU32<102400>; type RuntimeEvent = RuntimeEvent; + type MaxNumOfSudoOracles = ConstU32<5>; + type MaxNumOfNonSudoOracles = ConstU32<5>; } type VoterBagsListInstance = pallet_bags_list::Instance1; diff --git a/frame/reward/src/mock.rs b/frame/reward/src/mock.rs index b82aa671..141c04f3 100644 --- a/frame/reward/src/mock.rs +++ b/frame/reward/src/mock.rs @@ -277,6 +277,8 @@ impl pallet_esg::Config for Test { type WeightInfo = (); type RuntimeEvent = RuntimeEvent; type MaxFileSize = ConstU32<1024000>; + type MaxNumOfSudoOracles = ConstU32<5>; + type MaxNumOfNonSudoOracles = ConstU32<5>; } impl pallet_offences::Config for Test { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index b68e17f8..db53c489 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -177,6 +177,8 @@ impl pallet_esg::Config for Test { type WeightInfo = (); type RuntimeEvent = RuntimeEvent; type MaxFileSize = ConstU32<1024000>; + type MaxNumOfSudoOracles = ConstU32<5>; + type MaxNumOfNonSudoOracles = ConstU32<5>; } pub type Extrinsic = sp_runtime::testing::TestXt; From 513780fe823d3b790aa2488e54874bafb3468a6d Mon Sep 17 00:00:00 2001 From: dung5ire Date: Wed, 21 Aug 2024 15:47:50 +0700 Subject: [PATCH 2/3] chore: fix mock esg Signed-off-by: dung5ire --- frame/esg/src/mock.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frame/esg/src/mock.rs b/frame/esg/src/mock.rs index 7637fab1..9af41aa5 100644 --- a/frame/esg/src/mock.rs +++ b/frame/esg/src/mock.rs @@ -27,10 +27,7 @@ type Block = frame_system::mocking::MockBlock; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, + pub enum Test { System: frame_system, Esg: pallet_esg, From 63a05aa6bbd72e35e5a658505b4e738855dde32d Mon Sep 17 00:00:00 2001 From: dung5ire Date: Wed, 21 Aug 2024 21:21:09 +0700 Subject: [PATCH 3/3] feat: fix bug exceeding maximum oracle, non-oracle esg Signed-off-by: dung5ire --- frame/esg/src/lib.rs | 12 +++++-- frame/esg/src/mock.rs | 4 +-- frame/esg/src/tests.rs | 79 ++++++++++++++++++++++++++++++------------ 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/frame/esg/src/lib.rs b/frame/esg/src/lib.rs index 8fc0dc04..ccaafd0b 100644 --- a/frame/esg/src/lib.rs +++ b/frame/esg/src/lib.rs @@ -132,8 +132,11 @@ use frame_support::{ if max_num_of_sudo_oracles > num_of_sudo_oracles_stored { >::mutate(fn_mutate); return Ok(()); + } + else { + return Err(Error::::MaxNumOfSudoOraclesReached.into()); } - Err(Error::::MaxNumOfSudoOraclesReached.into()) + }, false => { @@ -144,7 +147,10 @@ use frame_support::{ >::mutate(fn_mutate); return Ok(()); } - Err(Error::::MaxNumOfNonSudoOraclesReached.into()) + else { + return Err(Error::::MaxNumOfNonSudoOraclesReached.into()); + } + }, } } @@ -263,7 +269,7 @@ use frame_support::{ } if is_root || Self::is_sudo_oracle(&acc_id) { - let _ = Self::store_oracle(&oracle, is_sudo_oracle); + let _ = Self::store_oracle(&oracle, is_sudo_oracle)?; } else { return Err(Error::::CallerNotRootOrSudoOracle.into()) } diff --git a/frame/esg/src/mock.rs b/frame/esg/src/mock.rs index 9af41aa5..6fb56b65 100644 --- a/frame/esg/src/mock.rs +++ b/frame/esg/src/mock.rs @@ -62,8 +62,8 @@ impl system::Config for Test { parameter_types! { pub const MaxFileSize: u32 = 1024000; - pub const MaxNumOfSudoOracles: u32 = 5; - pub const MaxNumOfNonSudoOracles: u32 = 100; + pub const MaxNumOfSudoOracles: u32 = 3; + pub const MaxNumOfNonSudoOracles: u32 = 2; } impl pallet_esg::Config for Test { diff --git a/frame/esg/src/tests.rs b/frame/esg/src/tests.rs index 893619f1..8aea4e64 100644 --- a/frame/esg/src/tests.rs +++ b/frame/esg/src/tests.rs @@ -1,7 +1,7 @@ use fp_account::AccountId20; use sp_runtime::DispatchError; pub use crate::{mock::*, Error}; -use frame_support::{assert_noop, assert_ok, WeakBoundedVec}; +use frame_support::{assert_err, assert_noop, assert_ok, WeakBoundedVec}; const MAX_ESG_SCORE: u16 = 100; @@ -11,14 +11,13 @@ struct Addr { ALICE: AccountId20, SUDO_ORACLE: AccountId20, SUDO_ORACLE_2: AccountId20, + SUDO_ORACLE_3: AccountId20, + SUDO_ORACLE_4: AccountId20, NON_SUDO_ORACLE: AccountId20, DUMMY_SUDO_ORACLE: AccountId20, NON_SUDO_ORACLE_2: AccountId20, NON_SUDO_ORACLE_6: AccountId20, NON_SUDO_ORACLE_7: AccountId20, - NON_SUDO_ORACLE_8: AccountId20, - NON_SUDO_ORACLE_9: AccountId20, - NON_SUDO_ORACLE_10: AccountId20, } impl Default for Addr { @@ -28,14 +27,13 @@ impl Default for Addr { ALICE: AccountId20::from([4u8; 20]), SUDO_ORACLE: AccountId20::from([0u8; 20]), SUDO_ORACLE_2: AccountId20::from([1u8; 20]), + SUDO_ORACLE_3: AccountId20::from([11u8; 20]), + SUDO_ORACLE_4: AccountId20::from([12u8; 20]), NON_SUDO_ORACLE: AccountId20::from([2u8; 20]), DUMMY_SUDO_ORACLE: AccountId20::from([5u8; 20]), NON_SUDO_ORACLE_2: AccountId20::from([3u8; 20]), NON_SUDO_ORACLE_6: AccountId20::from([6u8; 20]), NON_SUDO_ORACLE_7: AccountId20::from([7u8; 20]), - NON_SUDO_ORACLE_8: AccountId20::from([8u8; 20]), - NON_SUDO_ORACLE_9: AccountId20::from([9u8; 20]), - NON_SUDO_ORACLE_10: AccountId20::from([10u8; 20]), } } } @@ -52,28 +50,14 @@ fn it_must_register_oracles_new() { addr.NON_SUDO_ORACLE_6, false )); - assert_ok!(Esg::register_an_oracle( - RuntimeOrigin::signed(addr.SUDO_ORACLE), - addr.NON_SUDO_ORACLE_8, - false - )); - assert_ok!(Esg::register_an_oracle( - RuntimeOrigin::signed(addr.SUDO_ORACLE), - addr.NON_SUDO_ORACLE_9, - false - )); - assert_ok!(Esg::register_an_oracle( - RuntimeOrigin::signed(addr.SUDO_ORACLE), - addr.NON_SUDO_ORACLE_10, - false - )); assert_ok!(Esg::register_an_oracle( RuntimeOrigin::signed(addr.SUDO_ORACLE), addr.NON_SUDO_ORACLE_7, false )); - assert_ok!(Esg::deregister_an_oracle(RuntimeOrigin::root(), addr.NON_SUDO_ORACLE_7, false)); + + }); } @@ -663,3 +647,52 @@ fn it_must_handle_scores_exceeding_set_maximum() { })); }); } + + +#[test] +fn it_must_fail_to_register_oracle_non_oracle_exceeding_maximum(){ + let addr = Addr::default(); + + new_test_ext().execute_with(|| { + System::set_block_number(1); + + + // register sudo as an oracle + assert_ok!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.SUDO_ORACLE, true)); + let one_sudo = Esg::get_oracle_sudo(); + assert_eq!(one_sudo, vec![addr.SUDO_ORACLE]); + assert_ok!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.SUDO_ORACLE_2, true)); + let two_sudoes = Esg::get_oracle_sudo(); + assert_eq!(two_sudoes, vec![addr.SUDO_ORACLE, addr.SUDO_ORACLE_2]); + + assert_ok!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.SUDO_ORACLE_3, true)); + let three_sudoes = Esg::get_oracle_sudo(); + assert_eq!(three_sudoes, vec![addr.SUDO_ORACLE, addr.SUDO_ORACLE_2, addr.SUDO_ORACLE_3]); + + // Return Error due to maximum oracle sudo + assert_noop!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.SUDO_ORACLE_4, true), Error::::MaxNumOfSudoOraclesReached); + + // Deregister oracle sudo + assert_ok!(Esg::deregister_an_oracle(RuntimeOrigin::root(), addr.SUDO_ORACLE_3, true)); + let sudoes_after_deregister = Esg::get_oracle_sudo(); + assert_eq!(sudoes_after_deregister, vec![addr.SUDO_ORACLE, addr.SUDO_ORACLE_2]); + // Add oracle sudo again + assert_ok!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.SUDO_ORACLE_4, true)); + let three_sudoes_after_registering = Esg::get_oracle_sudo(); + assert_eq!(three_sudoes_after_registering, vec![addr.SUDO_ORACLE, addr.SUDO_ORACLE_2, addr.SUDO_ORACLE_4]); + + + + // Register sudo as an non-sudo oracle + assert_ok!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.NON_SUDO_ORACLE, false)); + let one_non_sudo = Esg::get_oracle_nsudo(); + assert_eq!(one_non_sudo, vec![addr.NON_SUDO_ORACLE]); + assert_ok!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.NON_SUDO_ORACLE_2, false)); + let two_non_sudoes = Esg::get_oracle_nsudo(); + assert_eq!(two_non_sudoes, vec![addr.NON_SUDO_ORACLE, addr.NON_SUDO_ORACLE_2]); + + // Return Error due to maximum non oracle sudo + assert_noop!(Esg::register_an_oracle(RuntimeOrigin::root(), addr.NON_SUDO_ORACLE_6, false), Error::::MaxNumOfNonSudoOraclesReached); + + }); +} \ No newline at end of file