Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding try_state hook for Tips pallet #1871

Merged
merged 17 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions substrate/frame/tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use sp_std::prelude::*;

use codec::{Decode, Encode};
use frame_support::{
ensure,
traits::{
ContainsLengthBound, Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, Get,
OnUnbalanced, ReservableCurrency, SortedMembers,
Expand All @@ -76,6 +77,9 @@ use frame_support::{
};
use frame_system::pallet_prelude::BlockNumberFor;

#[cfg(any(feature = "try-runtime", test))]
use sp_runtime::TryRuntimeError;

pub use pallet::*;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -465,6 +469,14 @@ pub mod pallet {
Ok(())
}
}

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), TryRuntimeError> {
Self::do_try_state()
}
}
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
Expand Down Expand Up @@ -611,4 +623,25 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Tips::<T, I>::insert(hash, new_tip)
}
}

/// Ensure the correctness of the state of this pallet.
///
/// This should be valid before or after each state transition of this pallet.
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
///
/// ## Invariants:
///
/// If `OpenTip.finders_fee` is true, then OpenTip.deposit should be greater that zero.
#[cfg(any(feature = "try-runtime", test))]
pub fn do_try_state() -> Result<(), TryRuntimeError> {
for tips in Tips::<T, I>::iter_keys() {
let open_tip = Tips::<T, I>::get(&tips).unwrap();
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
if open_tip.finders_fee {
ensure!(
!open_tip.deposit.is_zero(),
TryRuntimeError::Other("finder's fee not present")
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
Ok(())
}
}
30 changes: 21 additions & 9 deletions substrate/frame/tips/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Config for Test {
type Tippers = TenToFourteen;
type TipCountdown = ConstU64<1>;
type TipFindersFee = TipFindersFee;
type TipReportDepositBase = ConstU64<1>;
type TipReportDepositBase = ConstU64<0>;
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
type DataDepositPerByte = ConstU64<1>;
type MaxTipAmount = ConstU64<10_000_000>;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -263,8 +263,8 @@ fn report_awesome_and_tip_works() {
new_test_ext().execute_with(|| {
Balances::make_free_balance_be(&Treasury::account_id(), 101);
assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"awesome.dot".to_vec(), 3));
assert_eq!(Balances::reserved_balance(0), 12);
assert_eq!(Balances::free_balance(0), 88);
assert_eq!(Balances::reserved_balance(0), 11);
assert_eq!(Balances::free_balance(0), 89);

// other reports don't count.
assert_noop!(
Expand All @@ -290,8 +290,8 @@ fn report_awesome_from_beneficiary_and_tip_works() {
new_test_ext().execute_with(|| {
Balances::make_free_balance_be(&Treasury::account_id(), 101);
assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"awesome.dot".to_vec(), 0));
assert_eq!(Balances::reserved_balance(0), 12);
assert_eq!(Balances::free_balance(0), 88);
assert_eq!(Balances::reserved_balance(0), 11);
assert_eq!(Balances::free_balance(0), 89);
let h = BlakeTwo256::hash_of(&(BlakeTwo256::hash(b"awesome.dot"), 0u128));
assert_ok!(Tips::tip(RuntimeOrigin::signed(10), h, 10));
assert_ok!(Tips::tip(RuntimeOrigin::signed(11), h, 10));
Expand Down Expand Up @@ -353,8 +353,8 @@ fn slash_tip_works() {

assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"awesome.dot".to_vec(), 3));

assert_eq!(Balances::reserved_balance(0), 12);
assert_eq!(Balances::free_balance(0), 88);
assert_eq!(Balances::reserved_balance(0), 11);
assert_eq!(Balances::free_balance(0), 89);

let h = tip_hash();
assert_eq!(last_event(), TipEvent::NewTip { tip_hash: h });
Expand All @@ -364,11 +364,11 @@ fn slash_tip_works() {

// can remove from root.
assert_ok!(Tips::slash_tip(RuntimeOrigin::root(), h));
assert_eq!(last_event(), TipEvent::TipSlashed { tip_hash: h, finder: 0, deposit: 12 });
assert_eq!(last_event(), TipEvent::TipSlashed { tip_hash: h, finder: 0, deposit: 11 });

// tipper slashed
assert_eq!(Balances::reserved_balance(0), 0);
assert_eq!(Balances::free_balance(0), 88);
assert_eq!(Balances::free_balance(0), 89);
});
}

Expand Down Expand Up @@ -654,3 +654,15 @@ fn report_awesome_and_tip_works_second_instance() {
assert_eq!(Balances::free_balance(&Treasury1::account_id()), 191);
});
}

#[test]
fn try_state_invariant_works() {
new_test_ext().execute_with(|| {
use frame_support::pallet_prelude::DispatchError::Other;
Balances::make_free_balance_be(&Treasury::account_id(), 101);

assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"".to_vec(), 3));

assert_eq!(Tips::do_try_state(), Err(Other("finder's fee not present")))
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
})
}