From 8c877be1c4078b7a1b7a6361037b8ff9db74d93c Mon Sep 17 00:00:00 2001 From: bear Date: Mon, 30 May 2022 17:32:11 +0800 Subject: [PATCH] Check order capacity when relayer enroll (#93) * Add check logic * Update test file --- modules/fee-market/src/lib.rs | 8 ++++++++ modules/fee-market/src/tests.rs | 16 ++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/modules/fee-market/src/lib.rs b/modules/fee-market/src/lib.rs index c692902ac..348f4da04 100644 --- a/modules/fee-market/src/lib.rs +++ b/modules/fee-market/src/lib.rs @@ -117,6 +117,8 @@ pub mod pallet { AlreadyEnrolled, /// This relayer doesn't enroll ever. NotEnrolled, + /// Locked collateral is too low to cover one order. + CollateralTooLow, /// Update locked collateral is not allow since some orders are not confirm. StillHasOrdersNotConfirmed, /// The fee is lower than MinimumRelayFee. @@ -207,6 +209,12 @@ pub mod pallet { T::Currency::free_balance(&who) >= lock_collateral, >::InsufficientBalance ); + + ensure!( + Self::collateral_to_order_capacity(lock_collateral) > 0, + >::CollateralTooLow + ); + if let Some(fee) = relay_fee { ensure!(fee >= T::MinimumRelayFee::get(), >::RelayFeeTooLow); } diff --git a/modules/fee-market/src/tests.rs b/modules/fee-market/src/tests.rs index 0ca3f5eb4..9146e5da0 100644 --- a/modules/fee-market/src/tests.rs +++ b/modules/fee-market/src/tests.rs @@ -410,7 +410,6 @@ impl SenderOrigin for Origin { } frame_support::parameter_types! { - pub const FeeMarketPalletId: PalletId = PalletId(*b"da/feemk"); pub const TreasuryPalletId: PalletId = PalletId(*b"da/trsry"); pub const FeeMarketLockId: LockIdentifier = *b"da/feelf"; pub const MinimumRelayFee: Balance = 30; @@ -524,6 +523,10 @@ fn test_call_relayer_enroll_works() { FeeMarket::enroll_and_lock_collateral(Origin::signed(1), 200, None), >::InsufficientBalance ); + assert_err!( + FeeMarket::enroll_and_lock_collateral(Origin::signed(1), 99, None), + >::CollateralTooLow + ); assert_ok!(FeeMarket::enroll_and_lock_collateral(Origin::signed(1), 100, None)); assert!(FeeMarket::is_enrolled(&1)); @@ -536,10 +539,6 @@ fn test_call_relayer_enroll_works() { FeeMarket::enroll_and_lock_collateral(Origin::signed(1), 100, None), >::AlreadyEnrolled ); - - assert_ok!(FeeMarket::enroll_and_lock_collateral(Origin::signed(3), 250, None)); - - assert_ok!(FeeMarket::enroll_and_lock_collateral(Origin::signed(4), 0, None),); }); } @@ -554,14 +553,11 @@ fn test_call_relayer_increase_lock_collateral_works() { let _ = FeeMarket::enroll_and_lock_collateral(Origin::signed(12), 200, None); assert_eq!(FeeMarket::relayer_locked_collateral(&12), 200); - // Increase locked balance from 200 to 500 + // Increase locked collateral from 200 to 500 assert_ok!(FeeMarket::update_locked_collateral(Origin::signed(12), 500)); assert_eq!(FeeMarket::relayer_locked_collateral(&12), 500); - // Increase locked balance from 20 to 200 - let _ = FeeMarket::enroll_and_lock_collateral(Origin::signed(13), 20, None); - assert_ok!(FeeMarket::update_locked_collateral(Origin::signed(13), 200)); - + let _ = FeeMarket::enroll_and_lock_collateral(Origin::signed(13), 200, None); let _ = FeeMarket::enroll_and_lock_collateral(Origin::signed(14), 300, None); let market_fee = FeeMarket::market_fee().unwrap(); let _ = send_regular_message(market_fee);