Skip to content

Commit

Permalink
MESH-2096/allow-sc-deploy-as-child-id (#1548)
Browse files Browse the repository at this point in the history
* Add instantiate_with_code_as_primary_key

* Call UserBuilder instead of funded_user

* Add instantiate_with_hash_as_primary_key extrinsic; Add benchmarks

* Remove useless clone

---------

Co-authored-by: Robert Gabriel Jakabosky <[email protected]>
Co-authored-by: Adam Dossa <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent 9734963 commit af8f1c3
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 82 deletions.
58 changes: 49 additions & 9 deletions pallets/contracts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ use sp_runtime::Perbill;
use sp_std::prelude::*;
use wasm_instrument::parity_wasm::elements::{Instruction, ValueType};

use polymesh_common_utilities::{
benchs::{cdd_provider, user, AccountIdOf, User},
constants::currency::POLY,
group::GroupTrait,
TestUtilsFn,
};
use pallet_identity::ParentDid;
use polymesh_common_utilities::benchs::{cdd_provider, user, AccountIdOf, User, UserBuilder};
use polymesh_common_utilities::constants::currency::POLY;
use polymesh_common_utilities::group::GroupTrait;
use polymesh_common_utilities::TestUtilsFn;
use polymesh_primitives::identity::limits::{
MAX_ASSETS, MAX_EXTRINSICS, MAX_PALLETS, MAX_PORTFOLIOS,
};
Expand Down Expand Up @@ -73,7 +72,7 @@ where
// Check if contact is already linked.
match Identity::<T>::get_identity(&contract) {
Some(contract_did) => {
if contract_did != did {
if contract_did != did && ParentDid::get(contract_did) != Some(did) {
// Contract address already linked to a different identity.
Err(IdentityError::<T>::AlreadyLinked.into())
} else {
Expand Down Expand Up @@ -188,8 +187,13 @@ where
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
pub fn new(wasm: WasmModule<T>) -> Self {
// Construct a user.
let caller = funded_user::<T>(SEED);
// Construct a user
let caller = UserBuilder::<T>::default()
.seed(SEED)
.generate_did()
.become_cdd_provider()
.build("Caller");
T::Currency::make_free_balance_be(&caller.account(), 1_000_000 * POLY);

// Instantiate the contract.
let account_id = instantiate::<T>(&caller, wasm, salt());
Expand Down Expand Up @@ -523,4 +527,40 @@ benchmarks! {
.map(|id| ([(id & 0xFF) as u8, (id >> 8) as u8].into(), true))
.collect();
}: _(RawOrigin::Root, updates)

instantiate_with_code_as_primary_key {
let c in 0 .. Perbill::from_percent(49).mul_ceil(T::MaxCodeLen::get());
let s in 0 .. max_pages::<T>() * 64 * 1024;

let alice = UserBuilder::<T>::default()
.generate_did()
.become_cdd_provider()
.build("Alice");
T::Currency::make_free_balance_be(&alice.account(), 1_000_000 * POLY);

let salt = vec![42u8; s as usize];
let wasm = WasmModule::<T>::sized(c, Location::Deploy);
let addr = FrameContracts::<T>::contract_address(&alice.account(), &wasm.hash, &[], &salt);
}: _(alice.origin(), ENDOWMENT, Weight::MAX, None, wasm.code, vec![], salt)
verify {
assert_eq!(free_balance::<T>(&addr), ENDOWMENT + 1 as Balance);
}

instantiate_with_hash_as_primary_key {
let s in 0 .. max_pages::<T>() * 64 * 1024;
let salt = vec![42u8; s as usize];

let wasm = WasmModule::<T>::dummy();
let hash = wasm.hash.clone();

// Pre-instantiate a contract so that one with the hash exists.
let contract = Contract::<T>::new(wasm);
let user = contract.caller;

// Calculate new contract's address.
let addr = FrameContracts::<T>::contract_address(&user.account(), &hash, &[], &salt);
}: _(user.origin(), ENDOWMENT, Weight::MAX, None, hash, vec![], salt)
verify {
assert_eq!(free_balance::<T>(&addr), ENDOWMENT + 1 as Balance);
}
}
Loading

0 comments on commit af8f1c3

Please sign in to comment.