Skip to content

Commit

Permalink
Add upgradable-polymesh-ink contract at genesis (#1606)
Browse files Browse the repository at this point in the history
* Update whitelist

* Add upgradable contract

* Linting

* Update storage.rs

* Update polymesh-upgradable-ink contract to use feature `use_call_runtime_with_error`

---------

Co-authored-by: Robert Gabriel Jakabosky <[email protected]>
  • Loading branch information
adamdossa and Neopallium authored Feb 20, 2024
1 parent 36eb76c commit eec088c
Show file tree
Hide file tree
Showing 12 changed files with 3,281 additions and 25 deletions.
10 changes: 5 additions & 5 deletions contracts/upgradeable-polymesh-ink/Cargo.lock

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

27 changes: 26 additions & 1 deletion pallets/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use frame_support::traits::Get;
use frame_support::weights::Weight;
use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure};
use frame_system::ensure_root;
use pallet_contracts::Determinism;
use scale_info::TypeInfo;
use sp_core::crypto::UncheckedFrom;
use sp_runtime::traits::Hash;
Expand Down Expand Up @@ -112,6 +113,12 @@ impl<T: Config> Default for ApiCodeHash<T> {
}
}

impl<T: Config> ApiCodeHash<T> {
pub fn new(hash: CodeHash<T>) -> Self {
ApiCodeHash { hash }
}
}

impl<T: Config> sp_std::fmt::Debug for ApiCodeHash<T> {
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
write!(f, "hash: {:?}", self.hash)
Expand Down Expand Up @@ -381,10 +388,28 @@ decl_storage! {
}
add_extra_genesis {
config(call_whitelist): Vec<ExtrinsicId>;
build(|config: &GenesisConfig| {
config(upgradable_code): Vec<u8>;
config(upgradable_owner): Option<T::AccountId>;
config(upgradable_description): [u8; 4];
config(upgradable_major): u32;
build(|config: &GenesisConfig<T>| {
for ext_id in &config.call_whitelist {
CallRuntimeWhitelist::insert(ext_id, true);
}

if let Some(ref owner) = config.upgradable_owner {
let code_result = FrameContracts::<T>::bare_upload_code(
owner.clone(),
config.upgradable_code.clone(),
None,
Determinism::Deterministic,
).unwrap();
log::info!("Uploaded upgradeable code {}", code_result.code_hash);
let api_code_hash: ApiCodeHash<T> = ApiCodeHash::new(code_result.code_hash);
let api: Api = Api::new(config.upgradable_description, config.upgradable_major);
CurrentApiHash::insert(api, api_code_hash);
}

});
}
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/runtime/develop/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ construct_runtime!(

// Contracts
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>} = 46,
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config},
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config<T>},

// Preimage register. Used by `pallet_scheduler`.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>},
Expand Down
2 changes: 1 addition & 1 deletion pallets/runtime/mainnet/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ construct_runtime!(

// Contracts
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>} = 46,
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config},
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config<T>},

// Preimage register. Used by `pallet_scheduler`.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>},
Expand Down
2 changes: 1 addition & 1 deletion pallets/runtime/testnet/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ construct_runtime!(

// Contracts
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>} = 46,
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config},
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config<T>},

// Preimage register. Used by `pallet_scheduler`.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>},
Expand Down
2 changes: 1 addition & 1 deletion pallets/runtime/tests/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ frame_support::construct_runtime!(

// Contracts
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>} = 46,
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config} = 47,
PolymeshContracts: polymesh_contracts::{Pallet, Call, Storage, Event<T>, Config<T>} = 47,

// Preimage register. Used by `pallet_scheduler`.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 48,
Expand Down
27 changes: 20 additions & 7 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,26 @@ macro_rules! protocol_fee {
}

macro_rules! polymesh_contracts {
() => {
($root_key:expr) => {
polymesh_contracts::GenesisConfig {
call_whitelist: contracts_call_whitelist(),
upgradable_code: contracts_upgradable_code(),
upgradable_description: "POLY"
.as_bytes()
.try_into()
.expect("Wrong Length - should be length 4"),
upgradable_major: 6,
upgradable_owner: $root_key,
}
};
}

fn contracts_upgradable_code() -> Vec<u8> {
// NB - Contract should match the `upgradable_major` version above.
let upgradable_code = include_bytes!("data/contracts/polymesh_ink_6.wasm").to_vec();
upgradable_code
}

fn contracts_call_whitelist() -> Vec<polymesh_contracts::ExtrinsicId> {
let whitelist_file = include_str!("data/contracts_call_whitelist.json");
serde_json::from_str::<Vec<polymesh_contracts::ExtrinsicId>>(&whitelist_file)
Expand Down Expand Up @@ -602,7 +615,7 @@ pub mod general {
},
indices: pallet_indices::GenesisConfig { indices: vec![] },
sudo: pallet_sudo::GenesisConfig {
key: Some(root_key),
key: Some(root_key.clone()),
},
session: session!(initial_authorities, session_keys),
staking: staking!(
Expand Down Expand Up @@ -642,7 +655,7 @@ pub mod general {
transaction_version: 1,
},
corporate_action: corporate_actions!(),
polymesh_contracts: polymesh_contracts!(),
polymesh_contracts: polymesh_contracts!(Some(root_key)),
}
}

Expand Down Expand Up @@ -796,7 +809,7 @@ pub mod testnet {
transaction_version: 1,
},
corporate_action: corporate_actions!(),
polymesh_contracts: polymesh_contracts!(),
polymesh_contracts: polymesh_contracts!(Some(root_key)),
}
}

Expand Down Expand Up @@ -979,7 +992,7 @@ pub mod mainnet {
transaction_version: 1,
},
corporate_action: corporate_actions!(),
polymesh_contracts: polymesh_contracts!(),
polymesh_contracts: polymesh_contracts!(Some(root_key)),
}
}

Expand Down Expand Up @@ -1134,7 +1147,7 @@ pub mod general {
},
indices: pallet_indices::GenesisConfig { indices: vec![] },
sudo: pallet_sudo::GenesisConfig {
key: Some(root_key),
key: Some(root_key.clone()),
},
session: session!(initial_authorities, session_keys),
staking: staking!(initial_authorities, stakers, PerThing::zero()),
Expand Down Expand Up @@ -1170,7 +1183,7 @@ pub mod general {
transaction_version: 1,
},
corporate_action: corporate_actions!(),
polymesh_contracts: polymesh_contracts!(),
polymesh_contracts: polymesh_contracts!(Some(root_key)),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/data/contracts/polymesh_ink_6.contract

Large diffs are not rendered by default.

Loading

0 comments on commit eec088c

Please sign in to comment.