Skip to content

Commit

Permalink
boilerplate type changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Aug 31, 2023
1 parent aedd280 commit 251e8cd
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 112 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 36 additions & 36 deletions substrate/frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
data: Vec<u8>,
) -> Result<Contract<T>, &'static str> {
let value = Pallet::<T>::min_balance();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
let salt = vec![0xff];
let addr = Contracts::<T>::contract_address(&caller, &module.hash, &data, &salt);

Expand Down Expand Up @@ -163,7 +163,7 @@ where

/// Set the balance of the contract to the supplied amount.
fn set_balance(&self, balance: BalanceOf<T>) {
T::Currency::set_balance(&self.account_id, balance);
<T as Config>::Currency::set_balance(&self.account_id, balance);
}

/// Returns `true` iff all storage entries related to code storage exist.
Expand Down Expand Up @@ -291,7 +291,7 @@ benchmarks! {
#[pov_mode = Measured]
v14_migration_step {
let account = account::<T::AccountId>("account", 0, 0);
T::Currency::set_balance(&account, caller_funding::<T>());
<T as Config>::Currency::set_balance(&account, caller_funding::<T>());
v14::store_dummy_code::<T, pallet_balances::Pallet<T>>(account);
let mut m = v14::Migration::<T, pallet_balances::Pallet<T>>::default();
}: {
Expand Down Expand Up @@ -406,21 +406,21 @@ benchmarks! {
let salt = vec![42u8; s as usize];
let value = Pallet::<T>::min_balance();
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
let WasmModule { code, hash, .. } = WasmModule::<T>::sized(c, Location::Call);
let origin = RawOrigin::Signed(caller.clone());
let addr = Contracts::<T>::contract_address(&caller, &hash, &input, &salt);
}: _(origin, value, Weight::MAX, None, code, input, salt)
verify {
let deposit = T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr);
let deposit = <T as Config>::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr);
// uploading the code reserves some balance in the callers account
let code_deposit = T::Currency::balance_on_hold(&HoldReason::CodeUploadDepositReserve.into(), &caller);
let code_deposit = <T as Config>::Currency::balance_on_hold(&HoldReason::CodeUploadDepositReserve.into(), &caller);
assert_eq!(
T::Currency::balance(&caller),
<T as Config>::Currency::balance(&caller),
caller_funding::<T>() - value - deposit - code_deposit - Pallet::<T>::min_balance(),
);
// contract has the full value
assert_eq!(T::Currency::balance(&addr), value + Pallet::<T>::min_balance());
assert_eq!(<T as Config>::Currency::balance(&addr), value + Pallet::<T>::min_balance());
}

// Instantiate uses a dummy contract constructor to measure the overhead of the instantiate.
Expand All @@ -434,21 +434,21 @@ benchmarks! {
let salt = vec![42u8; s as usize];
let value = Pallet::<T>::min_balance();
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
let WasmModule { code, hash, .. } = WasmModule::<T>::dummy();
let origin = RawOrigin::Signed(caller.clone());
let addr = Contracts::<T>::contract_address(&caller, &hash, &input, &salt);
Contracts::<T>::store_code_raw(code, caller.clone())?;
}: _(origin, value, Weight::MAX, None, hash, input, salt)
verify {
let deposit = T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr);
let deposit = <T as Config>::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr);
// value was removed from the caller
assert_eq!(
T::Currency::balance(&caller),
<T as Config>::Currency::balance(&caller),
caller_funding::<T>() - value - deposit - Pallet::<T>::min_balance(),
);
// contract has the full value
assert_eq!(T::Currency::balance(&addr), value + Pallet::<T>::min_balance());
assert_eq!(<T as Config>::Currency::balance(&addr), value + Pallet::<T>::min_balance());
}

// We just call a dummy contract to measure the overhead of the call extrinsic.
Expand All @@ -467,17 +467,17 @@ benchmarks! {
let value = Pallet::<T>::min_balance();
let origin = RawOrigin::Signed(instance.caller.clone());
let callee = instance.addr.clone();
let before = T::Currency::balance(&instance.account_id);
let before = <T as Config>::Currency::balance(&instance.account_id);
}: _(origin, callee, value, Weight::MAX, None, data)
verify {
let deposit = T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &instance.account_id);
let deposit = <T as Config>::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &instance.account_id);
// value and value transferred via call should be removed from the caller
assert_eq!(
T::Currency::balance(&instance.caller),
<T as Config>::Currency::balance(&instance.caller),
caller_funding::<T>() - instance.value - value - deposit - Pallet::<T>::min_balance(),
);
// contract should have received the value
assert_eq!(T::Currency::balance(&instance.account_id), before + value);
assert_eq!(<T as Config>::Currency::balance(&instance.account_id), before + value);
// contract should still exist
instance.info()?;
}
Expand All @@ -489,13 +489,13 @@ benchmarks! {
upload_code {
let c in 0 .. T::MaxCodeLen::get();
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
let WasmModule { code, hash, .. } = WasmModule::<T>::sized(c, Location::Call);
let origin = RawOrigin::Signed(caller.clone());
}: _(origin, code, None, Determinism::Enforced)
verify {
// uploading the code reserves some balance in the callers account
assert!(T::Currency::total_balance_on_hold(&caller) > 0u32.into());
assert!(<T as Config>::Currency::total_balance_on_hold(&caller) > 0u32.into());
assert!(<Contract<T>>::code_exists(&hash));
}

Expand All @@ -505,17 +505,17 @@ benchmarks! {
#[pov_mode = Measured]
remove_code {
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
let WasmModule { code, hash, .. } = WasmModule::<T>::dummy();
let origin = RawOrigin::Signed(caller.clone());
let uploaded = <Contracts<T>>::bare_upload_code(caller.clone(), code, None, Determinism::Enforced)?;
assert_eq!(uploaded.code_hash, hash);
assert_eq!(uploaded.deposit, T::Currency::total_balance_on_hold(&caller));
assert_eq!(uploaded.deposit, <T as Config>::Currency::total_balance_on_hold(&caller));
assert!(<Contract<T>>::code_exists(&hash));
}: _(origin, hash)
verify {
// removing the code should have unreserved the deposit
assert_eq!(T::Currency::total_balance_on_hold(&caller), 0u32.into());
assert_eq!(<T as Config>::Currency::total_balance_on_hold(&caller), 0u32.into());
assert!(<Contract<T>>::code_removed(&hash));
}

Expand Down Expand Up @@ -889,7 +889,7 @@ benchmarks! {
let beneficiary_len = beneficiary_bytes.len();
let caller = whitelisted_caller();

T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());

// Maximize the delegate_dependencies to account for the worst-case scenario.
let code_hashes = (0..T::MaxDelegateDependencies::get())
Expand Down Expand Up @@ -941,15 +941,15 @@ benchmarks! {
});
let instance = Contract::<T>::new(code, vec![])?;
let origin = RawOrigin::Signed(instance.caller.clone());
assert_eq!(T::Currency::total_balance(&beneficiary), 0u32.into());
assert_eq!(T::Currency::balance(&instance.account_id), Pallet::<T>::min_balance() * 2u32.into());
assert_ne!(T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &instance.account_id), 0u32.into());
assert_eq!(<T as Config>::Currency::total_balance(&beneficiary), 0u32.into());
assert_eq!(<T as Config>::Currency::balance(&instance.account_id), Pallet::<T>::min_balance() * 2u32.into());
assert_ne!(<T as Config>::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &instance.account_id), 0u32.into());
}: call(origin, instance.addr.clone(), 0u32.into(), Weight::MAX, None, vec![])
verify {
if r > 0 {
assert_eq!(T::Currency::total_balance(&instance.account_id), 0u32.into());
assert_eq!(T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &instance.account_id), 0u32.into());
assert_eq!(T::Currency::total_balance(&beneficiary), Pallet::<T>::min_balance() * 2u32.into());
assert_eq!(<T as Config>::Currency::total_balance(&instance.account_id), 0u32.into());
assert_eq!(<T as Config>::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &instance.account_id), 0u32.into());
assert_eq!(<T as Config>::Currency::total_balance(&beneficiary), Pallet::<T>::min_balance() * 2u32.into());
}
}

Expand Down Expand Up @@ -1718,12 +1718,12 @@ benchmarks! {
instance.set_balance(value * (r + 1).into());
let origin = RawOrigin::Signed(instance.caller.clone());
for account in &accounts {
assert_eq!(T::Currency::total_balance(account), 0u32.into());
assert_eq!(<T as Config>::Currency::total_balance(account), 0u32.into());
}
}: call(origin, instance.addr, 0u32.into(), Weight::MAX, None, vec![])
verify {
for account in &accounts {
assert_eq!(T::Currency::total_balance(account), value);
assert_eq!(<T as Config>::Currency::total_balance(account), value);
}
}

Expand Down Expand Up @@ -1812,7 +1812,7 @@ benchmarks! {
.map(|i| {
let code = WasmModule::<T>::dummy_with_bytes(i);
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
Contracts::<T>::store_code_raw(code.code, caller)?;
Ok(code.hash)
})
Expand Down Expand Up @@ -1934,7 +1934,7 @@ benchmarks! {
.. Default::default()
});
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
Contracts::<T>::store_code_raw(code.code, caller)?;
Ok(code.hash)
})
Expand Down Expand Up @@ -2040,7 +2040,7 @@ benchmarks! {
let hash_bytes = callee_code.hash.encode();
let hash_len = hash_bytes.len();
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
Contracts::<T>::store_code_raw(callee_code.code, caller)?;
let value: BalanceOf<T> = t.into();
let value_bytes = value.encode();
Expand Down Expand Up @@ -2384,7 +2384,7 @@ benchmarks! {
.map(|i| {
let new_code = WasmModule::<T>::dummy_with_bytes(i);
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
Contracts::<T>::store_code_raw(new_code.code, caller)?;
Ok(new_code.hash)
})
Expand Down Expand Up @@ -2427,7 +2427,7 @@ benchmarks! {
.map(|i| {
let new_code = WasmModule::<T>::dummy_with_bytes(65 + i);
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
Contracts::<T>::store_code_raw(new_code.code, caller)?;
Ok(new_code.hash)
})
Expand Down Expand Up @@ -2465,7 +2465,7 @@ benchmarks! {
.map(|i| {
let new_code = WasmModule::<T>::dummy_with_bytes(65 + i);
let caller = whitelisted_caller();
T::Currency::set_balance(&caller, caller_funding::<T>());
<T as Config>::Currency::set_balance(&caller, caller_funding::<T>());
Contracts::<T>::store_code_raw(new_code.code, caller)?;
Ok(new_code.hash)
})
Expand Down
9 changes: 5 additions & 4 deletions substrate/frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ where
value: BalanceOf<T>,
) -> DispatchResult {
if !value.is_zero() && from != to {
T::Currency::transfer(from, to, value, preservation)
<T as Config>::Currency::transfer(from, to, value, preservation)
.map_err(|_| Error::<T>::TransferFailed)?;
}
Ok(())
Expand Down Expand Up @@ -1368,7 +1368,7 @@ where
}

fn balance(&self) -> BalanceOf<T> {
T::Currency::balance(&self.top_frame().account_id)
<T as Config>::Currency::balance(&self.top_frame().account_id)
}

fn value_transferred(&self) -> BalanceOf<T> {
Expand All @@ -1384,7 +1384,7 @@ where
}

fn minimum_balance(&self) -> BalanceOf<T> {
T::Currency::minimum_balance()
<T as Config>::Currency::minimum_balance()
}

fn deposit_event(&mut self, topics: Vec<T::Hash>, data: Vec<u8>) {
Expand Down Expand Up @@ -1441,7 +1441,8 @@ where
}

fn call_runtime(&self, call: <Self::T as Config>::RuntimeCall) -> DispatchResultWithPostInfo {
let mut origin: T::RuntimeOrigin = RawOrigin::Signed(self.address().clone()).into();
let mut origin: <T as frame_system::Config>::RuntimeOrigin =
RawOrigin::Signed(self.address().clone()).into();
origin.add_filter(T::CallFilter::contains);
call.dispatch(origin)
}
Expand Down
Loading

0 comments on commit 251e8cd

Please sign in to comment.