mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
contracts: Remove ConfigCache (#8047)
* contracts: Remove ConfigCache * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_contracts --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/contracts/src/weights.rs --template=./.maintain/frame-weight-template.hbs * Fixup test Co-authored-by: Parity Benchmarking Bot <admin@parity.io>
This commit is contained in:
committed by
GitHub
parent
dfefa163f8
commit
1b31f7c5d1
@@ -588,7 +588,7 @@ fn deploying_wasm_contract_should_work() {
|
||||
&[],
|
||||
);
|
||||
|
||||
let subsistence = pallet_contracts::ConfigCache::<Runtime>::subsistence_threshold_uncached();
|
||||
let subsistence = pallet_contracts::Module::<Runtime>::subsistence_threshold();
|
||||
|
||||
let b = construct_block(
|
||||
&mut new_test_ext(compact_code_unwrap(), false),
|
||||
|
||||
@@ -1179,7 +1179,7 @@ benchmarks! {
|
||||
.collect::<Vec<_>>();
|
||||
let account_len = accounts.get(0).map(|i| i.encode().len()).unwrap_or(0);
|
||||
let account_bytes = accounts.iter().flat_map(|x| x.encode()).collect();
|
||||
let value = ConfigCache::<T>::subsistence_threshold_uncached();
|
||||
let value = Contracts::<T>::subsistence_threshold();
|
||||
assert!(value > 0u32.into());
|
||||
let value_bytes = value.encode();
|
||||
let value_len = value_bytes.len();
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{
|
||||
CodeHash, ConfigCache, Event, RawEvent, Config, Module as Contracts,
|
||||
CodeHash, Event, RawEvent, Config, Module as Contracts,
|
||||
TrieId, BalanceOf, ContractInfo, gas::GasMeter, rent::Rent, storage::{self, Storage},
|
||||
Error, ContractInfoOf, Schedule,
|
||||
};
|
||||
@@ -28,7 +28,7 @@ use sp_std::{
|
||||
use sp_runtime::traits::{Bounded, Zero, Convert, Saturating};
|
||||
use frame_support::{
|
||||
dispatch::{DispatchResult, DispatchError},
|
||||
traits::{ExistenceRequirement, Currency, Time, Randomness},
|
||||
traits::{ExistenceRequirement, Currency, Time, Randomness, Get},
|
||||
weights::Weight,
|
||||
ensure, StorageMap,
|
||||
};
|
||||
@@ -245,7 +245,7 @@ pub struct ExecutionContext<'a, T: Config + 'a, E> {
|
||||
pub self_account: T::AccountId,
|
||||
pub self_trie_id: Option<TrieId>,
|
||||
pub depth: usize,
|
||||
pub config: &'a ConfigCache<T>,
|
||||
pub schedule: &'a Schedule<T>,
|
||||
pub timestamp: MomentOf<T>,
|
||||
pub block_number: T::BlockNumber,
|
||||
_phantom: PhantomData<E>,
|
||||
@@ -261,13 +261,13 @@ where
|
||||
///
|
||||
/// The specified `origin` address will be used as `sender` for. The `origin` must be a regular
|
||||
/// account (not a contract).
|
||||
pub fn top_level(origin: T::AccountId, cfg: &'a ConfigCache<T>) -> Self {
|
||||
pub fn top_level(origin: T::AccountId, schedule: &'a Schedule<T>) -> Self {
|
||||
ExecutionContext {
|
||||
caller: None,
|
||||
self_trie_id: None,
|
||||
self_account: origin,
|
||||
depth: 0,
|
||||
config: &cfg,
|
||||
schedule,
|
||||
timestamp: T::Time::now(),
|
||||
block_number: <frame_system::Module<T>>::block_number(),
|
||||
_phantom: Default::default(),
|
||||
@@ -282,7 +282,7 @@ where
|
||||
self_trie_id: Some(trie_id),
|
||||
self_account: dest,
|
||||
depth: self.depth + 1,
|
||||
config: self.config,
|
||||
schedule: self.schedule,
|
||||
timestamp: self.timestamp.clone(),
|
||||
block_number: self.block_number.clone(),
|
||||
_phantom: Default::default(),
|
||||
@@ -297,7 +297,7 @@ where
|
||||
gas_meter: &mut GasMeter<T>,
|
||||
input_data: Vec<u8>,
|
||||
) -> ExecResult {
|
||||
if self.depth == self.config.max_depth as usize {
|
||||
if self.depth == T::MaxDepth::get() as usize {
|
||||
Err(Error::<T>::MaxCallDepthReached)?
|
||||
}
|
||||
|
||||
@@ -305,7 +305,7 @@ where
|
||||
.and_then(|contract| contract.get_alive())
|
||||
.ok_or(Error::<T>::NotCallable)?;
|
||||
|
||||
let executable = E::from_storage(contract.code_hash, &self.config.schedule)?;
|
||||
let executable = E::from_storage(contract.code_hash, &self.schedule)?;
|
||||
|
||||
// This charges the rent and denies access to a contract that is in need of
|
||||
// eviction by returning `None`. We cannot evict eagerly here because those
|
||||
@@ -320,13 +320,12 @@ where
|
||||
|
||||
self.with_nested_context(dest.clone(), contract.trie_id.clone(), |nested| {
|
||||
if value > BalanceOf::<T>::zero() {
|
||||
transfer(
|
||||
transfer::<T>(
|
||||
TransferCause::Call,
|
||||
transactor_kind,
|
||||
&caller,
|
||||
&dest,
|
||||
value,
|
||||
nested,
|
||||
)?
|
||||
}
|
||||
|
||||
@@ -348,7 +347,7 @@ where
|
||||
input_data: Vec<u8>,
|
||||
salt: &[u8],
|
||||
) -> Result<(T::AccountId, ExecReturnValue), ExecError> {
|
||||
if self.depth == self.config.max_depth as usize {
|
||||
if self.depth == T::MaxDepth::get() as usize {
|
||||
Err(Error::<T>::MaxCallDepthReached)?
|
||||
}
|
||||
|
||||
@@ -372,13 +371,12 @@ where
|
||||
|
||||
// Send funds unconditionally here. If the `endowment` is below existential_deposit
|
||||
// then error will be returned here.
|
||||
transfer(
|
||||
transfer::<T>(
|
||||
TransferCause::Instantiate,
|
||||
transactor_kind,
|
||||
&caller,
|
||||
&dest,
|
||||
endowment,
|
||||
nested,
|
||||
)?;
|
||||
|
||||
// Cache the value before calling into the constructor because that
|
||||
@@ -489,17 +487,15 @@ enum TransferCause {
|
||||
/// is specified as `Terminate`. Otherwise, any transfer that would bring the sender below the
|
||||
/// subsistence threshold (for contracts) or the existential deposit (for plain accounts)
|
||||
/// results in an error.
|
||||
fn transfer<'a, T: Config, E>(
|
||||
fn transfer<T: Config>(
|
||||
cause: TransferCause,
|
||||
origin: TransactorKind,
|
||||
transactor: &T::AccountId,
|
||||
dest: &T::AccountId,
|
||||
value: BalanceOf<T>,
|
||||
ctx: &mut ExecutionContext<'a, T, E>,
|
||||
) -> DispatchResult
|
||||
where
|
||||
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
|
||||
E: Executable<T>,
|
||||
{
|
||||
use self::TransferCause::*;
|
||||
use self::TransactorKind::*;
|
||||
@@ -511,7 +507,7 @@ where
|
||||
(_, Contract) => {
|
||||
ensure!(
|
||||
T::Currency::total_balance(transactor).saturating_sub(value) >=
|
||||
ctx.config.subsistence_threshold(),
|
||||
Contracts::<T>::subsistence_threshold(),
|
||||
Error::<T>::BelowSubsistenceThreshold,
|
||||
);
|
||||
ExistenceRequirement::KeepAlive
|
||||
@@ -586,7 +582,7 @@ where
|
||||
input_data: Vec<u8>,
|
||||
salt: &[u8],
|
||||
) -> Result<(AccountIdOf<T>, ExecReturnValue), ExecError> {
|
||||
let executable = E::from_storage(code_hash, &self.ctx.config.schedule)?;
|
||||
let executable = E::from_storage(code_hash, &self.ctx.schedule)?;
|
||||
let result = self.ctx.instantiate(endowment, gas_meter, executable, input_data, salt)?;
|
||||
Ok(result)
|
||||
}
|
||||
@@ -596,13 +592,12 @@ where
|
||||
to: &T::AccountId,
|
||||
value: BalanceOf<T>,
|
||||
) -> DispatchResult {
|
||||
transfer(
|
||||
transfer::<T>(
|
||||
TransferCause::Call,
|
||||
TransactorKind::Contract,
|
||||
&self.ctx.self_account.clone(),
|
||||
to,
|
||||
value,
|
||||
self.ctx,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -617,13 +612,12 @@ where
|
||||
return Err(Error::<T>::ReentranceDenied.into());
|
||||
}
|
||||
}
|
||||
transfer(
|
||||
transfer::<T>(
|
||||
TransferCause::Terminate,
|
||||
TransactorKind::Contract,
|
||||
&self_id,
|
||||
beneficiary,
|
||||
value,
|
||||
self.ctx,
|
||||
)?;
|
||||
if let Some(ContractInfo::Alive(info)) = ContractInfoOf::<T>::take(&self_id) {
|
||||
Storage::<T>::queue_trie_for_deletion(&info)?;
|
||||
@@ -708,11 +702,11 @@ where
|
||||
}
|
||||
|
||||
fn minimum_balance(&self) -> BalanceOf<T> {
|
||||
self.ctx.config.existential_deposit
|
||||
T::Currency::minimum_balance()
|
||||
}
|
||||
|
||||
fn tombstone_deposit(&self) -> BalanceOf<T> {
|
||||
self.ctx.config.tombstone_deposit
|
||||
T::TombstoneDeposit::get()
|
||||
}
|
||||
|
||||
fn deposit_event(&mut self, topics: Vec<T::Hash>, data: Vec<u8>) {
|
||||
@@ -741,7 +735,7 @@ where
|
||||
fn block_number(&self) -> T::BlockNumber { self.block_number }
|
||||
|
||||
fn max_value_size(&self) -> u32 {
|
||||
self.ctx.config.max_value_size
|
||||
T::MaxValueSize::get()
|
||||
}
|
||||
|
||||
fn get_weight_price(&self, weight: Weight) -> BalanceOf<Self::T> {
|
||||
@@ -749,7 +743,7 @@ where
|
||||
}
|
||||
|
||||
fn schedule(&self) -> &Schedule<Self::T> {
|
||||
&self.ctx.config.schedule
|
||||
&self.ctx.schedule
|
||||
}
|
||||
}
|
||||
|
||||
@@ -898,8 +892,8 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
place_contract(&BOB, exec_ch);
|
||||
|
||||
assert_matches!(
|
||||
@@ -919,18 +913,15 @@ mod tests {
|
||||
let dest = BOB;
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(origin.clone(), &cfg);
|
||||
set_balance(&origin, 100);
|
||||
set_balance(&dest, 0);
|
||||
|
||||
super::transfer(
|
||||
super::transfer::<Test>(
|
||||
super::TransferCause::Call,
|
||||
super::TransactorKind::PlainAccount,
|
||||
&origin,
|
||||
&dest,
|
||||
55,
|
||||
&mut ctx,
|
||||
).unwrap();
|
||||
|
||||
assert_eq!(get_balance(&origin), 45);
|
||||
@@ -950,8 +941,8 @@ mod tests {
|
||||
);
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(origin.clone(), &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(origin.clone(), &schedule);
|
||||
place_contract(&BOB, return_ch);
|
||||
set_balance(&origin, 100);
|
||||
let balance = get_balance(&dest);
|
||||
@@ -979,17 +970,14 @@ mod tests {
|
||||
let dest = BOB;
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(origin.clone(), &cfg);
|
||||
set_balance(&origin, 0);
|
||||
|
||||
let result = super::transfer(
|
||||
let result = super::transfer::<Test>(
|
||||
super::TransferCause::Call,
|
||||
super::TransactorKind::PlainAccount,
|
||||
&origin,
|
||||
&dest,
|
||||
100,
|
||||
&mut ctx,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
@@ -1012,8 +1000,8 @@ mod tests {
|
||||
);
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(origin, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(origin, &schedule);
|
||||
place_contract(&BOB, return_ch);
|
||||
|
||||
let result = ctx.call(
|
||||
@@ -1040,8 +1028,8 @@ mod tests {
|
||||
);
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(origin, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(origin, &schedule);
|
||||
place_contract(&BOB, return_ch);
|
||||
|
||||
let result = ctx.call(
|
||||
@@ -1066,8 +1054,8 @@ mod tests {
|
||||
|
||||
// This one tests passing the input data into a contract via call.
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
place_contract(&BOB, input_data_ch);
|
||||
|
||||
let result = ctx.call(
|
||||
@@ -1089,15 +1077,16 @@ mod tests {
|
||||
|
||||
// This one tests passing the input data into a contract via instantiate.
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let subsistence = Contracts::<Test>::subsistence_threshold();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
|
||||
set_balance(&ALICE, cfg.subsistence_threshold() * 10);
|
||||
set_balance(&ALICE, subsistence * 10);
|
||||
|
||||
let result = ctx.instantiate(
|
||||
cfg.subsistence_threshold() * 3,
|
||||
subsistence * 3,
|
||||
&mut GasMeter::<Test>::new(GAS_LIMIT),
|
||||
MockExecutable::from_storage(input_data_ch, &cfg.schedule).unwrap(),
|
||||
MockExecutable::from_storage(input_data_ch, &schedule).unwrap(),
|
||||
vec![1, 2, 3, 4],
|
||||
&[],
|
||||
);
|
||||
@@ -1137,8 +1126,8 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&BOB, 1);
|
||||
place_contract(&BOB, recurse_ch);
|
||||
|
||||
@@ -1185,9 +1174,8 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
|
||||
let mut ctx = MockContext::top_level(origin.clone(), &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(origin.clone(), &schedule);
|
||||
place_contract(&dest, bob_ch);
|
||||
place_contract(&CHARLIE, charlie_ch);
|
||||
|
||||
@@ -1224,8 +1212,8 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
place_contract(&BOB, bob_ch);
|
||||
place_contract(&CHARLIE, charlie_ch);
|
||||
|
||||
@@ -1245,14 +1233,14 @@ mod tests {
|
||||
let dummy_ch = MockLoader::insert(|_| exec_success());
|
||||
|
||||
ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
|
||||
assert_matches!(
|
||||
ctx.instantiate(
|
||||
0, // <- zero endowment
|
||||
&mut GasMeter::<Test>::new(GAS_LIMIT),
|
||||
MockExecutable::from_storage(dummy_ch, &cfg.schedule).unwrap(),
|
||||
MockExecutable::from_storage(dummy_ch, &schedule).unwrap(),
|
||||
vec![],
|
||||
&[],
|
||||
),
|
||||
@@ -1268,15 +1256,15 @@ mod tests {
|
||||
);
|
||||
|
||||
ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&ALICE, 1000);
|
||||
|
||||
let instantiated_contract_address = assert_matches!(
|
||||
ctx.instantiate(
|
||||
100,
|
||||
&mut GasMeter::<Test>::new(GAS_LIMIT),
|
||||
MockExecutable::from_storage(dummy_ch, &cfg.schedule).unwrap(),
|
||||
MockExecutable::from_storage(dummy_ch, &schedule).unwrap(),
|
||||
vec![],
|
||||
&[],
|
||||
),
|
||||
@@ -1299,15 +1287,15 @@ mod tests {
|
||||
);
|
||||
|
||||
ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&ALICE, 1000);
|
||||
|
||||
let instantiated_contract_address = assert_matches!(
|
||||
ctx.instantiate(
|
||||
100,
|
||||
&mut GasMeter::<Test>::new(GAS_LIMIT),
|
||||
MockExecutable::from_storage(dummy_ch, &cfg.schedule).unwrap(),
|
||||
MockExecutable::from_storage(dummy_ch, &schedule).unwrap(),
|
||||
vec![],
|
||||
&[],
|
||||
),
|
||||
@@ -1331,7 +1319,7 @@ mod tests {
|
||||
// Instantiate a contract and save it's address in `instantiated_contract_address`.
|
||||
let (address, output) = ctx.ext.instantiate(
|
||||
dummy_ch,
|
||||
ConfigCache::<Test>::subsistence_threshold_uncached() * 3,
|
||||
Contracts::<Test>::subsistence_threshold() * 3,
|
||||
ctx.gas_meter,
|
||||
vec![],
|
||||
&[48, 49, 50],
|
||||
@@ -1343,9 +1331,9 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
set_balance(&ALICE, cfg.subsistence_threshold() * 100);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&ALICE, Contracts::<Test>::subsistence_threshold() * 100);
|
||||
place_contract(&BOB, instantiator_ch);
|
||||
|
||||
assert_matches!(
|
||||
@@ -1392,8 +1380,8 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&ALICE, 1000);
|
||||
set_balance(&BOB, 100);
|
||||
place_contract(&BOB, instantiator_ch);
|
||||
@@ -1420,15 +1408,15 @@ mod tests {
|
||||
.existential_deposit(15)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&ALICE, 1000);
|
||||
|
||||
assert_eq!(
|
||||
ctx.instantiate(
|
||||
100,
|
||||
&mut GasMeter::<Test>::new(GAS_LIMIT),
|
||||
MockExecutable::from_storage(terminate_ch, &cfg.schedule).unwrap(),
|
||||
MockExecutable::from_storage(terminate_ch, &schedule).unwrap(),
|
||||
vec![],
|
||||
&[],
|
||||
),
|
||||
@@ -1445,7 +1433,8 @@ mod tests {
|
||||
#[test]
|
||||
fn rent_allowance() {
|
||||
let rent_allowance_ch = MockLoader::insert(|ctx| {
|
||||
let allowance = ConfigCache::<Test>::subsistence_threshold_uncached() * 3;
|
||||
let subsistence = Contracts::<Test>::subsistence_threshold();
|
||||
let allowance = subsistence * 3;
|
||||
assert_eq!(ctx.ext.rent_allowance(), <BalanceOf<Test>>::max_value());
|
||||
ctx.ext.set_rent_allowance(allowance);
|
||||
assert_eq!(ctx.ext.rent_allowance(), allowance);
|
||||
@@ -1453,14 +1442,15 @@ mod tests {
|
||||
});
|
||||
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = MockContext::top_level(ALICE, &cfg);
|
||||
set_balance(&ALICE, cfg.subsistence_threshold() * 10);
|
||||
let subsistence = Contracts::<Test>::subsistence_threshold();
|
||||
let schedule = Contracts::current_schedule();
|
||||
let mut ctx = MockContext::top_level(ALICE, &schedule);
|
||||
set_balance(&ALICE, subsistence * 10);
|
||||
|
||||
let result = ctx.instantiate(
|
||||
cfg.subsistence_threshold() * 5,
|
||||
subsistence * 5,
|
||||
&mut GasMeter::<Test>::new(GAS_LIMIT),
|
||||
MockExecutable::from_storage(rent_allowance_ch, &cfg.schedule).unwrap(),
|
||||
MockExecutable::from_storage(rent_allowance_ch, &schedule).unwrap(),
|
||||
vec![],
|
||||
&[],
|
||||
);
|
||||
|
||||
@@ -628,7 +628,7 @@ decl_module! {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let mut gas_meter = GasMeter::new(gas_limit);
|
||||
let result = Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| {
|
||||
let executable = PrefabWasmModule::from_storage(code_hash, &ctx.config.schedule)?;
|
||||
let executable = PrefabWasmModule::from_storage(code_hash, &ctx.schedule)?;
|
||||
let result = ctx.instantiate(endowment, gas_meter, executable, data, &salt)
|
||||
.map(|(_address, output)| output)?;
|
||||
Ok(result)
|
||||
@@ -764,6 +764,17 @@ where
|
||||
.collect();
|
||||
UncheckedFrom::unchecked_from(T::Hashing::hash(&buf))
|
||||
}
|
||||
|
||||
/// Subsistence threshold is the extension of the minimum balance (aka existential deposit)
|
||||
/// by the tombstone deposit, required for leaving a tombstone.
|
||||
///
|
||||
/// Rent or any contract initiated balance transfer mechanism cannot make the balance lower
|
||||
/// than the subsistence threshold in order to guarantee that a tombstone is created.
|
||||
///
|
||||
/// The only way to completely kill a contract without a tombstone is calling `seal_terminate`.
|
||||
pub fn subsistence_threshold() -> BalanceOf<T> {
|
||||
T::Currency::minimum_balance().saturating_add(T::TombstoneDeposit::get())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> Module<T>
|
||||
@@ -778,8 +789,8 @@ where
|
||||
&mut GasMeter<T>,
|
||||
) -> ExecResult,
|
||||
) -> ExecResult {
|
||||
let cfg = ConfigCache::preload();
|
||||
let mut ctx = ExecutionContext::top_level(origin, &cfg);
|
||||
let schedule = <Module<T>>::current_schedule();
|
||||
let mut ctx = ExecutionContext::top_level(origin, &schedule);
|
||||
func(&mut ctx, gas_meter)
|
||||
}
|
||||
}
|
||||
@@ -875,49 +886,3 @@ decl_storage! {
|
||||
pub DeletionQueue: Vec<storage::DeletedContract>;
|
||||
}
|
||||
}
|
||||
|
||||
/// In-memory cache of configuration values.
|
||||
///
|
||||
/// We assume that these values can't be changed in the
|
||||
/// course of transaction execution.
|
||||
pub struct ConfigCache<T: Config> {
|
||||
pub schedule: Schedule<T>,
|
||||
pub existential_deposit: BalanceOf<T>,
|
||||
pub tombstone_deposit: BalanceOf<T>,
|
||||
pub max_depth: u32,
|
||||
pub max_value_size: u32,
|
||||
}
|
||||
|
||||
impl<T: Config> ConfigCache<T>
|
||||
where
|
||||
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>
|
||||
{
|
||||
fn preload() -> ConfigCache<T> {
|
||||
ConfigCache {
|
||||
schedule: <Module<T>>::current_schedule(),
|
||||
existential_deposit: T::Currency::minimum_balance(),
|
||||
tombstone_deposit: T::TombstoneDeposit::get(),
|
||||
max_depth: T::MaxDepth::get(),
|
||||
max_value_size: T::MaxValueSize::get(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Subsistence threshold is the extension of the minimum balance (aka existential deposit) by the
|
||||
/// tombstone deposit, required for leaving a tombstone.
|
||||
///
|
||||
/// Rent or any contract initiated balance transfer mechanism cannot make the balance lower
|
||||
/// than the subsistence threshold in order to guarantee that a tombstone is created.
|
||||
///
|
||||
/// The only way to completely kill a contract without a tombstone is calling `seal_terminate`.
|
||||
pub fn subsistence_threshold(&self) -> BalanceOf<T> {
|
||||
self.existential_deposit.saturating_add(self.tombstone_deposit)
|
||||
}
|
||||
|
||||
/// The same as `subsistence_threshold` but without the need for a preloaded instance.
|
||||
///
|
||||
/// This is for cases where this value is needed in rent calculation rather than
|
||||
/// during contract execution.
|
||||
pub fn subsistence_threshold_uncached() -> BalanceOf<T> {
|
||||
T::Currency::minimum_balance().saturating_add(T::TombstoneDeposit::get())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
use crate::{
|
||||
AliveContractInfo, BalanceOf, ContractInfo, ContractInfoOf, Module, RawEvent,
|
||||
TombstoneContractInfo, Config, CodeHash, ConfigCache, Error,
|
||||
TombstoneContractInfo, Config, CodeHash, Error,
|
||||
storage::Storage, wasm::PrefabWasmModule, exec::Executable,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
@@ -125,7 +125,7 @@ where
|
||||
free_balance: &BalanceOf<T>,
|
||||
contract: &AliveContractInfo<T>,
|
||||
) -> Option<BalanceOf<T>> {
|
||||
let subsistence_threshold = ConfigCache::<T>::subsistence_threshold_uncached();
|
||||
let subsistence_threshold = Module::<T>::subsistence_threshold();
|
||||
// Reserved balance contributes towards the subsistence threshold to stay consistent
|
||||
// with the existential deposit where the reserved balance is also counted.
|
||||
if *total_balance < subsistence_threshold {
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
use crate::{
|
||||
BalanceOf, ContractInfo, ContractInfoOf, Module,
|
||||
RawAliveContractInfo, RawEvent, Config, Schedule, gas::Gas,
|
||||
Error, ConfigCache, RuntimeReturnCode, storage::Storage,
|
||||
Error, RuntimeReturnCode, storage::Storage,
|
||||
chain_extension::{
|
||||
Result as ExtensionResult, Environment, ChainExtension, Ext, SysConfig, RetVal,
|
||||
UncheckedFrom, InitState, ReturnFlags,
|
||||
@@ -67,10 +67,10 @@ frame_support::construct_runtime!(
|
||||
pub mod test_utils {
|
||||
use super::{Test, Balances};
|
||||
use crate::{
|
||||
ConfigCache,
|
||||
ContractInfoOf, CodeHash,
|
||||
storage::Storage,
|
||||
exec::{StorageKey, AccountIdOf},
|
||||
Module as Contracts,
|
||||
};
|
||||
use frame_support::{StorageMap, traits::Currency};
|
||||
|
||||
@@ -84,7 +84,7 @@ pub mod test_utils {
|
||||
}
|
||||
pub fn place_contract(address: &AccountIdOf<Test>, code_hash: CodeHash<Test>) {
|
||||
let trie_id = Storage::<Test>::generate_trie_id(address);
|
||||
set_balance(address, ConfigCache::<Test>::subsistence_threshold_uncached() * 10);
|
||||
set_balance(address, Contracts::<Test>::subsistence_threshold() * 10);
|
||||
Storage::<Test>::place_contract(&address, trie_id, code_hash).unwrap();
|
||||
}
|
||||
pub fn set_balance(who: &AccountIdOf<Test>, amount: u64) {
|
||||
@@ -451,7 +451,7 @@ fn instantiate_and_call_and_deposit_event() {
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
|
||||
// Check at the end to get hash on error easily
|
||||
let creation = Contracts::instantiate_with_code(
|
||||
@@ -566,7 +566,7 @@ fn deposit_event_max_value_limit() {
|
||||
#[test]
|
||||
fn run_out_of_gas() {
|
||||
let (wasm, code_hash) = compile_module::<Test>("run_out_of_gas").unwrap();
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(50)
|
||||
@@ -902,7 +902,7 @@ fn removals(trigger_call: impl Fn(AccountIdOf<Test>) -> bool) {
|
||||
.unwrap().get_alive().unwrap().rent_allowance;
|
||||
let balance = Balances::free_balance(&addr);
|
||||
|
||||
let subsistence_threshold = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence_threshold = Module::<Test>::subsistence_threshold();
|
||||
|
||||
// Trigger rent must have no effect
|
||||
assert!(!trigger_call(addr.clone()));
|
||||
@@ -991,7 +991,7 @@ fn removals(trigger_call: impl Fn(AccountIdOf<Test>) -> bool) {
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// Create
|
||||
let subsistence_threshold = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence_threshold = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, subsistence_threshold * 1000);
|
||||
assert_ok!(Contracts::instantiate_with_code(
|
||||
Origin::signed(ALICE),
|
||||
@@ -1878,7 +1878,7 @@ fn crypto_hashes() {
|
||||
fn transfer_return_code() {
|
||||
let (wasm, code_hash) = compile_module::<Test>("transfer_return_code").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
|
||||
assert_ok!(
|
||||
@@ -1925,7 +1925,7 @@ fn call_return_code() {
|
||||
let (caller_code, caller_hash) = compile_module::<Test>("call_return_code").unwrap();
|
||||
let (callee_code, callee_hash) = compile_module::<Test>("ok_trap_revert").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
let _ = Balances::deposit_creating(&CHARLIE, 1000 * subsistence);
|
||||
|
||||
@@ -2018,7 +2018,7 @@ fn instantiate_return_code() {
|
||||
let (caller_code, caller_hash) = compile_module::<Test>("instantiate_return_code").unwrap();
|
||||
let (callee_code, callee_hash) = compile_module::<Test>("ok_trap_revert").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
let _ = Balances::deposit_creating(&CHARLIE, 1000 * subsistence);
|
||||
let callee_hash = callee_hash.as_ref().to_vec();
|
||||
@@ -2109,7 +2109,7 @@ fn instantiate_return_code() {
|
||||
fn disabled_chain_extension_wont_deploy() {
|
||||
let (code, _hash) = compile_module::<Test>("chain_extension").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
TestExtension::disable();
|
||||
assert_err_ignore_postinfo!(
|
||||
@@ -2130,7 +2130,7 @@ fn disabled_chain_extension_wont_deploy() {
|
||||
fn disabled_chain_extension_errors_on_call() {
|
||||
let (code, hash) = compile_module::<Test>("chain_extension").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
assert_ok!(
|
||||
Contracts::instantiate_with_code(
|
||||
@@ -2161,7 +2161,7 @@ fn disabled_chain_extension_errors_on_call() {
|
||||
fn chain_extension_works() {
|
||||
let (code, hash) = compile_module::<Test>("chain_extension").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
assert_ok!(
|
||||
Contracts::instantiate_with_code(
|
||||
@@ -2230,7 +2230,7 @@ fn chain_extension_works() {
|
||||
fn lazy_removal_works() {
|
||||
let (code, hash) = compile_module::<Test>("self_destruct").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
|
||||
assert_ok!(
|
||||
@@ -2290,7 +2290,7 @@ fn lazy_removal_partial_remove_works() {
|
||||
let mut ext = ExtBuilder::default().existential_deposit(50).build();
|
||||
|
||||
let trie = ext.execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
|
||||
assert_ok!(
|
||||
@@ -2371,7 +2371,7 @@ fn lazy_removal_partial_remove_works() {
|
||||
fn lazy_removal_does_no_run_on_full_block() {
|
||||
let (code, hash) = compile_module::<Test>("self_destruct").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
|
||||
assert_ok!(
|
||||
@@ -2455,7 +2455,7 @@ fn lazy_removal_does_no_run_on_full_block() {
|
||||
fn lazy_removal_does_not_use_all_weight() {
|
||||
let (code, hash) = compile_module::<Test>("self_destruct").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
|
||||
assert_ok!(
|
||||
@@ -2525,7 +2525,7 @@ fn lazy_removal_does_not_use_all_weight() {
|
||||
fn deletion_queue_full() {
|
||||
let (code, hash) = compile_module::<Test>("self_destruct").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence);
|
||||
|
||||
assert_ok!(
|
||||
@@ -2651,7 +2651,7 @@ fn refcounter() {
|
||||
let (wasm, code_hash) = compile_module::<Test>("self_destruct").unwrap();
|
||||
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
|
||||
let subsistence = ConfigCache::<Test>::subsistence_threshold_uncached();
|
||||
let subsistence = Module::<Test>::subsistence_threshold();
|
||||
|
||||
// Create two contracts with the same code and check that they do in fact share it.
|
||||
assert_ok!(Contracts::instantiate_with_code(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user