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

Use parameter_types instead of thread_local #2720

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 8 additions & 9 deletions modules/auction-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use sp_runtime::{
traits::{AccountIdConversion, IdentityLookup, One as OneT},
BuildStorage,
};
use sp_std::cell::RefCell;

pub type AccountId = u128;
pub type BlockNumber = u64;
Expand Down Expand Up @@ -119,19 +118,19 @@ impl module_cdp_treasury::Config for Runtime {
type StableAsset = MockStableAsset<CurrencyId, Balance, AccountId, BlockNumber>;
}

thread_local! {
static RELATIVE_PRICE: RefCell<Option<Price>> = RefCell::new(Some(Price::one()));
parameter_types! {
static RelativePrice: Option<Price> = Some(Price::one());
}

pub struct MockPriceSource;
impl MockPriceSource {
pub fn set_relative_price(price: Option<Price>) {
RELATIVE_PRICE.with(|v| *v.borrow_mut() = price);
RelativePrice::mutate(|v| *v = price);
}
}
impl PriceProvider<CurrencyId> for MockPriceSource {
fn get_relative_price(_base: CurrencyId, _quote: CurrencyId) -> Option<Price> {
RELATIVE_PRICE.with(|v| *v.borrow_mut())
RelativePrice::get()
}

fn get_price(_currency_id: CurrencyId) -> Option<Price> {
Expand Down Expand Up @@ -163,18 +162,18 @@ impl module_dex::Config for Runtime {
type OnLiquidityPoolUpdated = ();
}

thread_local! {
static IS_SHUTDOWN: RefCell<bool> = RefCell::new(false);
parameter_types! {
static IsShutdown: bool = false;
}

pub fn mock_shutdown() {
IS_SHUTDOWN.with(|v| *v.borrow_mut() = true)
IsShutdown::mutate(|v| *v = true)
}

pub struct MockEmergencyShutdown;
impl EmergencyShutdown for MockEmergencyShutdown {
fn is_shutdown() -> bool {
IS_SHUTDOWN.with(|v| *v.borrow_mut())
IsShutdown::get()
}
}

Expand Down
85 changes: 41 additions & 44 deletions modules/cdp-engine/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use sp_runtime::{
traits::{AccountIdConversion, IdentityLookup, One as OneT},
BuildStorage,
};
use sp_std::{cell::RefCell, str::FromStr};
use sp_std::str::FromStr;

pub type AccountId = AccountId32;
pub type BlockNumber = u64;
Expand Down Expand Up @@ -127,49 +127,46 @@ impl module_loans::Config for Runtime {
type OnUpdateLoan = ();
}

thread_local! {
static BTC_PRICE: RefCell<Option<Price>> = RefCell::new(Some(Price::one()));
static DOT_PRICE: RefCell<Option<Price>> = RefCell::new(Some(Price::one()));
static LP_AUSD_DOT_PRICE: RefCell<Option<Price>> = RefCell::new(Some(Price::one()));
static LP_DOT_BTC_PRICE: RefCell<Option<Price>> = RefCell::new(Some(Price::one()));
parameter_types! {
static BtcPrice: Option<Price> = Some(Price::one());
static DotPrice: Option<Price> = Some(Price::one());
static LpAusdDotPrice: Option<Price> = Some(Price::one());
static LpDotBtcPrice: Option<Price> = Some(Price::one());
}

pub struct MockPriceSource;
impl MockPriceSource {
pub fn set_price(currency_id: CurrencyId, price: Option<Price>) {
match currency_id {
BTC => BTC_PRICE.with(|v| *v.borrow_mut() = price),
DOT => DOT_PRICE.with(|v| *v.borrow_mut() = price),
LP_AUSD_DOT => LP_AUSD_DOT_PRICE.with(|v| *v.borrow_mut() = price),
LP_DOT_BTC => LP_DOT_BTC_PRICE.with(|v| *v.borrow_mut() = price),
BTC => BtcPrice::mutate(|v| *v = price),
DOT => DotPrice::mutate(|v| *v = price),
LP_AUSD_DOT => LpAusdDotPrice::mutate(|v| *v = price),
LP_DOT_BTC => LpDotBtcPrice::mutate(|v| *v = price),
_ => {}
}
}
}
impl PriceProvider<CurrencyId> for MockPriceSource {
fn get_price(currency_id: CurrencyId) -> Option<Price> {
match currency_id {
BTC => BTC_PRICE.with(|v| *v.borrow()),
DOT => DOT_PRICE.with(|v| *v.borrow()),
BTC => BtcPrice::get(),
DOT => DotPrice::get(),
AUSD => Some(Price::one()),
LP_AUSD_DOT => LP_AUSD_DOT_PRICE.with(|v| *v.borrow()),
LP_DOT_BTC => LP_DOT_BTC_PRICE.with(|v| *v.borrow()),
LP_AUSD_DOT => LpAusdDotPrice::get(),
LP_DOT_BTC => LpDotBtcPrice::get(),
_ => None,
}
}
}

thread_local! {
pub static AUCTION: RefCell<Option<(AccountId, CurrencyId, Balance, Balance)>> = RefCell::new(None);
parameter_types! {
pub static Auction: Option<(AccountId, CurrencyId, Balance, Balance)> = None;
}

pub struct MockAuctionManager;
impl MockAuctionManager {
pub fn auction() -> Option<(AccountId, CurrencyId, Balance, Balance)> {
AUCTION.with(|v| {
let cloned = v.borrow().clone();
cloned
})
Auction::get()
}
}
impl AuctionManager<AccountId> for MockAuctionManager {
Expand All @@ -183,12 +180,12 @@ impl AuctionManager<AccountId> for MockAuctionManager {
amount: Self::Balance,
target: Self::Balance,
) -> DispatchResult {
AUCTION.with(|v| *v.borrow_mut() = Some((refund_recipient.clone(), currency_id, amount, target)));
Auction::mutate(|v| *v = Some((refund_recipient.clone(), currency_id, amount, target)));
Ok(())
}

fn cancel_auction(_id: Self::AuctionId) -> DispatchResult {
AUCTION.with(|v| *v.borrow_mut() = None);
Auction::mutate(|v| *v = None);
Ok(())
}

Expand Down Expand Up @@ -308,12 +305,12 @@ impl module_evm_bridge::Config for Runtime {
type EVM = EVM;
}

thread_local! {
static IS_SHUTDOWN: RefCell<bool> = RefCell::new(false);
parameter_types! {
static IsShutdown: bool = false;
}

pub fn mock_shutdown() {
IS_SHUTDOWN.with(|v| *v.borrow_mut() = true)
IsShutdown::mutate(|v| *v = true)
}

pub fn liquidation_contract_addr() -> EvmAddress {
Expand All @@ -323,38 +320,38 @@ pub fn liquidation_contract_addr() -> EvmAddress {
pub struct MockEmergencyShutdown;
impl EmergencyShutdown for MockEmergencyShutdown {
fn is_shutdown() -> bool {
IS_SHUTDOWN.with(|v| *v.borrow_mut())
IsShutdown::get()
}
}

thread_local! {
static LIQUIDATED: RefCell<(EvmAddress, EvmAddress, Balance, Balance)> = RefCell::new((EvmAddress::default(), EvmAddress::default(), 0, 0));
static TRANSFERRED: RefCell<(EvmAddress, Balance)> = RefCell::new((EvmAddress::default(), 0));
static REFUNDED: RefCell<(EvmAddress, Balance)> = RefCell::new((EvmAddress::default(), 0));
static LIQUIDATION_RESULT: RefCell<DispatchResult> = RefCell::new(Err(Error::<Runtime>::LiquidationFailed.into()));
static REPAYMENT: RefCell<Option<Balance>> = RefCell::new(None);
parameter_types! {
static LIQUIDATED: (EvmAddress, EvmAddress, Balance, Balance) = (EvmAddress::default(), EvmAddress::default(), 0, 0);
static TRANSFERRED: (EvmAddress, Balance) = (EvmAddress::default(), 0);
static REFUNDED: (EvmAddress, Balance) = (EvmAddress::default(), 0);
static LiquidationResult: DispatchResult = Err(Error::<Runtime>::LiquidationFailed.into());
static REPAYMENT: Option<Balance> = None;
}

pub struct MockLiquidationEvmBridge;
impl MockLiquidationEvmBridge {
pub fn liquidated() -> (EvmAddress, EvmAddress, Balance, Balance) {
LIQUIDATED.with(|v| v.borrow().clone())
LIQUIDATED::get()
}
pub fn transferred() -> (EvmAddress, Balance) {
TRANSFERRED.with(|v| v.borrow().clone())
TRANSFERRED::get()
}
pub fn refunded() -> (EvmAddress, Balance) {
REFUNDED.with(|v| v.borrow().clone())
REFUNDED::get()
}
pub fn reset() {
LIQUIDATION_RESULT.with(|v| *v.borrow_mut() = Err(Error::<Runtime>::LiquidationFailed.into()));
REPAYMENT.with(|v| *v.borrow_mut() = None);
LiquidationResult::mutate(|v| *v = Err(Error::<Runtime>::LiquidationFailed.into()));
REPAYMENT::mutate(|v| *v = None);
}
pub fn set_liquidation_result(r: DispatchResult) {
LIQUIDATION_RESULT.with(|v| *v.borrow_mut() = r);
LiquidationResult::mutate(|v| *v = r);
}
pub fn set_repayment(repayment: Balance) {
REPAYMENT.with(|v| *v.borrow_mut() = Some(repayment));
REPAYMENT::mutate(|v| *v = Some(repayment));
}
}
impl LiquidationEvmBridge for MockLiquidationEvmBridge {
Expand All @@ -365,23 +362,23 @@ impl LiquidationEvmBridge for MockLiquidationEvmBridge {
amount: Balance,
min_repayment: Balance,
) -> DispatchResult {
let result = LIQUIDATION_RESULT.with(|v| v.borrow().clone());
let result = LiquidationResult::get();
if result.is_ok() {
let repayment = if let Some(r) = REPAYMENT.with(|v| v.borrow().clone()) {
let repayment = if let Some(r) = REPAYMENT::get() {
r
} else {
min_repayment
};
let _ = Currencies::deposit(GetStableCurrencyId::get(), &CDPEngineModule::account_id(), repayment);
}
LIQUIDATED.with(|v| *v.borrow_mut() = (collateral, repay_dest, amount, min_repayment));
LIQUIDATED::mutate(|v| *v = (collateral, repay_dest, amount, min_repayment));
result
}
fn on_collateral_transfer(_context: InvokeContext, collateral: EvmAddress, amount: Balance) {
TRANSFERRED.with(|v| *v.borrow_mut() = (collateral, amount));
TRANSFERRED::mutate(|v| *v = (collateral, amount));
}
fn on_repayment_refund(_context: InvokeContext, collateral: EvmAddress, repayment: Balance) {
REFUNDED.with(|v| *v.borrow_mut() = (collateral, repayment));
REFUNDED::mutate(|v| *v = (collateral, repayment));
}
}

Expand Down
15 changes: 5 additions & 10 deletions modules/cdp-treasury/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use nutsfinance_stable_asset::{
use orml_traits::parameter_type_with_key;
use primitives::{DexShare, TokenSymbol, TradingPair};
use sp_runtime::{traits::IdentityLookup, BuildStorage};
use sp_std::cell::RefCell;

pub type AccountId = u128;
pub type BlockNumber = u64;
Expand Down Expand Up @@ -138,9 +137,9 @@ impl module_dex::Config for Runtime {
type OnLiquidityPoolUpdated = ();
}

thread_local! {
pub static TOTAL_COLLATERAL_AUCTION: RefCell<u32> = RefCell::new(0);
pub static TOTAL_COLLATERAL_IN_AUCTION: RefCell<Balance> = RefCell::new(0);
parameter_types! {
pub static TotalCollateralAuction: u32 = 0;
pub static TotalCollateralInAuction: Balance = 0;
}

pub struct MockAuctionManager;
Expand All @@ -155,8 +154,8 @@ impl AuctionManager<AccountId> for MockAuctionManager {
amount: Self::Balance,
_target: Self::Balance,
) -> DispatchResult {
TOTAL_COLLATERAL_AUCTION.with(|v| *v.borrow_mut() += 1);
TOTAL_COLLATERAL_IN_AUCTION.with(|v| *v.borrow_mut() += amount);
TotalCollateralAuction::mutate(|v| *v += 1);
TotalCollateralInAuction::mutate(|v| *v += amount);
Ok(())
}

Expand Down Expand Up @@ -185,10 +184,6 @@ parameter_types! {
];
}

thread_local! {
static IS_SHUTDOWN: RefCell<bool> = RefCell::new(false);
}

impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Currencies;
Expand Down
15 changes: 7 additions & 8 deletions modules/dex-oracle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use sp_runtime::{
traits::{IdentityLookup, Zero},
BuildStorage, DispatchError,
};
use sp_std::cell::RefCell;

pub type AccountId = u128;

Expand Down Expand Up @@ -64,16 +63,16 @@ impl pallet_timestamp::Config for Runtime {
type WeightInfo = ();
}

thread_local! {
static AUSD_DOT_POOL: RefCell<(Balance, Balance)> = RefCell::new((Zero::zero(), Zero::zero()));
static ACA_DOT_POOL: RefCell<(Balance, Balance)> = RefCell::new((Zero::zero(), Zero::zero()));
parameter_types! {
static AusdDotPool: (Balance, Balance) = (Zero::zero(), Zero::zero());
static AcaDotPool: (Balance, Balance) = (Zero::zero(), Zero::zero());
}

pub fn set_pool(trading_pair: &TradingPair, pool_0: Balance, pool_1: Balance) {
if *trading_pair == AUSDDOTPair::get() {
AUSD_DOT_POOL.with(|v| *v.borrow_mut() = (pool_0, pool_1));
AusdDotPool::mutate(|v| *v = (pool_0, pool_1));
} else if *trading_pair == ACADOTPair::get() {
ACA_DOT_POOL.with(|v| *v.borrow_mut() = (pool_0, pool_1));
AcaDotPool::mutate(|v| *v = (pool_0, pool_1));
}
}

Expand All @@ -83,9 +82,9 @@ impl DEXManager<AccountId, Balance, CurrencyId> for MockDEX {
TradingPair::from_currency_ids(currency_id_0, currency_id_1)
.map(|trading_pair| {
if trading_pair == AUSDDOTPair::get() {
AUSD_DOT_POOL.with(|v| *v.borrow())
AusdDotPool::get()
} else if trading_pair == ACADOTPair::get() {
ACA_DOT_POOL.with(|v| *v.borrow())
AcaDotPool::get()
} else {
(0, 0)
}
Expand Down
7 changes: 3 additions & 4 deletions modules/dex/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use module_support::{mocks::MockErc20InfoMapping, SpecificJointsSwap};
use orml_traits::{parameter_type_with_key, MultiReservableCurrency};
use primitives::{Amount, TokenSymbol};
use sp_runtime::{traits::IdentityLookup, BuildStorage};
use sp_std::cell::RefCell;

pub type BlockNumber = u64;
pub type AccountId = u128;
Expand Down Expand Up @@ -105,16 +104,16 @@ parameter_types! {
];
}

thread_local! {
pub static AUSD_DOT_POOL_RECORD: RefCell<(Balance, Balance)> = RefCell::new((0, 0));
parameter_types! {
pub static AusdDotPoolRecord: (Balance, Balance) = (0, 0);
}

pub struct MockOnLiquidityPoolUpdated;
impl Happened<(TradingPair, Balance, Balance)> for MockOnLiquidityPoolUpdated {
fn happened(info: &(TradingPair, Balance, Balance)) {
let (trading_pair, new_pool_0, new_pool_1) = info;
if *trading_pair == AUSDDOTPair::get() {
AUSD_DOT_POOL_RECORD.with(|v| *v.borrow_mut() = (*new_pool_0, *new_pool_1));
AusdDotPoolRecord::mutate(|v| *v = (*new_pool_0, *new_pool_1));
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions modules/evm-accounts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ fn claim_account_work() {
#[test]
fn claim_account_should_not_work() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(
EvmAccountsModule::claim_account(
RuntimeOrigin::signed(ALICE),
EvmAccountsModule::eth_address(&bob()),
[0u8; 65]
),
Error::<Runtime>::BadSignature
);
assert_noop!(
EvmAccountsModule::claim_account(
RuntimeOrigin::signed(ALICE),
Expand Down
Loading
Loading