Skip to content

Commit a9c61c5

Browse files
committed
Use customized ink address as contract id
1 parent 1f598b2 commit a9c61c5

File tree

8 files changed

+94
-39
lines changed

8 files changed

+94
-39
lines changed

crates/phactory/src/contracts/pink.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Pink {
6464
Self { instance, group }
6565
}
6666

67-
pub fn address_to_id(address: &AccountId) -> contracts::NativeContractId {
67+
pub fn address_to_id(address: &AccountId) -> contracts::ContractId {
6868
let inner: &[u8; 32] = address.as_ref();
6969
inner.into()
7070
}
@@ -160,7 +160,7 @@ impl contracts::NativeContract for Pink {
160160
}
161161

162162
impl NativeContractMore for Pink {
163-
fn id(&self) -> contracts::NativeContractId {
163+
fn id(&self) -> phala_mq::ContractId {
164164
Pink::address_to_id(&self.instance.address)
165165
}
166166

@@ -207,8 +207,7 @@ pub mod group {
207207
block_number: BlockNumber,
208208
now: u64,
209209
) -> Result<ExecSideEffects> {
210-
let group = self
211-
.get_group_or_default_mut(&group_id, contract_key);
210+
let group = self.get_group_or_default_mut(&group_id, contract_key);
212211
let (_, effects) = Pink::instantiate(
213212
group_id,
214213
&mut group.storage,
@@ -238,13 +237,15 @@ pub mod group {
238237
group_id: &ContractGroupId,
239238
contract_key: &sr25519::Pair,
240239
) -> &mut Group {
241-
self.groups
242-
.entry(group_id.clone())
243-
.or_insert_with(|| Group {
240+
self.groups.entry(group_id.clone()).or_insert_with(|| {
241+
let mut group = Group {
244242
storage: Default::default(),
245243
contracts: Default::default(),
246244
key: contract_key.clone(),
247-
})
245+
};
246+
group.set_id(group_id);
247+
group
248+
})
248249
}
249250

250251
pub fn commit_changes(&mut self) -> anyhow::Result<()> {
@@ -277,5 +278,9 @@ pub mod group {
277278
self.storage.commit_changes();
278279
Ok(())
279280
}
281+
282+
pub fn set_id(&mut self, id: &ContractGroupId) {
283+
self.storage.set_group_id(id.as_bytes());
284+
}
280285
}
281286
}

crates/phactory/src/contracts/support.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,8 @@ pub trait Contract {
5555
fn set_on_block_end_selector(&mut self, selector: u32);
5656
}
5757

58-
#[derive(Encode, Decode, Clone, Debug, Copy, derive_more::From)]
59-
pub struct NativeContractId(sp_core::H256);
60-
61-
impl From<&[u8; 32]> for NativeContractId {
62-
fn from(bytes: &[u8; 32]) -> Self {
63-
NativeContractId(bytes.into())
64-
}
65-
}
66-
67-
impl NativeContractId {
68-
pub fn to_contract_id(&self, group_id: &phala_mq::ContractGroupId) -> phala_mq::ContractId {
69-
sp_core::blake2_256(&(group_id, &self.0).encode()).into()
70-
}
71-
}
72-
7358
pub trait NativeContractMore {
74-
fn id(&self) -> NativeContractId;
59+
fn id(&self) -> phala_mq::ContractId;
7560
fn set_on_block_end_selector(&mut self, _selector: u32) {}
7661
}
7762

@@ -106,8 +91,14 @@ pub struct NativeContractWrapper<Con> {
10691
}
10792

10893
impl<Con> NativeContractWrapper<Con> {
109-
pub fn new(inner: Con, deployer: sp_core::H256, salt: &[u8], id: u32) -> Self {
110-
let encoded = (deployer, id, salt).encode();
94+
pub fn new(
95+
inner: Con,
96+
group_id: &phala_mq::ContractGroupId,
97+
deployer: sp_core::H256,
98+
salt: &[u8],
99+
id: u32,
100+
) -> Self {
101+
let encoded = (deployer, id, group_id, salt).encode();
111102
let id = sp_core::blake2_256(&encoded).into();
112103
NativeContractWrapper { inner, id }
113104
}
@@ -142,8 +133,8 @@ impl<Con: NativeContract> NativeContract for NativeContractWrapper<Con> {
142133
}
143134

144135
impl<Con: NativeContract> NativeContractMore for NativeContractWrapper<Con> {
145-
fn id(&self) -> NativeContractId {
146-
self.id.into()
136+
fn id(&self) -> phala_mq::ContractId {
137+
self.id
147138
}
148139
}
149140

crates/phactory/src/system/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ impl<Platform: pal::Platform> System<Platform> {
865865
$id => {
866866
let contract = NativeContractWrapper::new(
867867
$contract,
868+
&group_id,
868869
deployer,
869870
&salt,
870871
$id,
@@ -1074,7 +1075,7 @@ pub fn apply_pink_side_effects(
10741075
}
10751076

10761077
for (address, event) in effects.pink_events {
1077-
let id = Pink::address_to_id(&address).to_contract_id(&group_id);
1078+
let id = Pink::address_to_id(&address);
10781079
let contract = match contracts.get_mut(&id) {
10791080
Some(contract) => contract,
10801081
None => {
@@ -1117,7 +1118,7 @@ where
11171118
<Contract as NativeContract>::Cmd: Send,
11181119
contracts::AnyContract: From<contracts::NativeCompatContract<Contract>>,
11191120
{
1120-
let contract_id = contract.id().to_contract_id(&group_id);
1121+
let contract_id = contract.id();
11211122
if contracts.get(&contract_id).is_some() {
11221123
return Err(anyhow::anyhow!("Contract already exists"));
11231124
}

crates/pink/src/runtime.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod extension;
22
mod mock_types;
3+
mod pallet_pink;
34

45
use crate::types::{AccountId, Balance, BlockNumber, Hash, Hashing, Index};
56
use frame_support::weights::Weight;
@@ -27,6 +28,7 @@ frame_support::construct_runtime! {
2728
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
2829
Randomness: pallet_randomness_collective_flip::{Pallet, Storage},
2930
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>},
31+
Pink: pallet_pink::{Pallet, Storage},
3032
}
3133
}
3234

@@ -37,6 +39,8 @@ parameter_types! {
3739
pub static ExistentialDeposit: u64 = 0;
3840
}
3941

42+
impl pallet_pink::Config for PinkRuntime {}
43+
4044
impl frame_system::Config for PinkRuntime {
4145
type BaseCallFilter = frame_support::traits::Everything;
4246
type BlockWeights = BlockWeights;
@@ -113,6 +117,7 @@ impl Config for PinkRuntime {
113117
type DeletionWeightLimit = DeletionWeightLimit;
114118
type Schedule = DefaultSchedule;
115119
type ContractDeposit = ();
120+
type AddressGenerator = Pink;
116121
}
117122

118123
#[cfg(test)]
@@ -212,10 +217,9 @@ mod tests {
212217
// We offset data in the contract tables by 1.
213218
let mut params = vec![(n + 1) as u8];
214219
params.extend_from_slice(input);
215-
let result =
216-
Contracts::bare_call(ALICE, addr.clone(), 0, GAS_LIMIT, params, false)
217-
.result
218-
.unwrap();
220+
let result = Contracts::bare_call(ALICE, addr.clone(), 0, GAS_LIMIT, params, false)
221+
.result
222+
.unwrap();
219223
assert!(result.is_success());
220224
let expected = hash_fn(input.as_ref());
221225
assert_eq!(&result.data[..*expected_size], &*expected);
@@ -225,9 +229,7 @@ mod tests {
225229

226230
pub mod exec {
227231
use sp_runtime::traits::BlakeTwo256;
228-
use sp_state_machine::{
229-
Backend, Ext, OverlayedChanges, StorageTransactionCache,
230-
};
232+
use sp_state_machine::{Backend, Ext, OverlayedChanges, StorageTransactionCache};
231233
pub type InMemoryBackend = sp_state_machine::InMemoryBackend<BlakeTwo256>;
232234

233235
pub fn execute_with<R>(f: impl FnOnce() -> R) -> R {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
pub use pallet::*;
2+
3+
#[frame_support::pallet]
4+
pub mod pallet {
5+
use frame_support::pallet_prelude::*;
6+
use pallet_contracts::AddressGenerator;
7+
use sp_core::crypto::UncheckedFrom;
8+
use sp_runtime::traits::Hash as _;
9+
10+
type CodeHash<T> = <T as frame_system::Config>::Hash;
11+
12+
#[pallet::config]
13+
pub trait Config: frame_system::Config {}
14+
15+
#[pallet::storage]
16+
pub(crate) type GroupId<T: Config> = StorageValue<_, Vec<u8>, ValueQuery>;
17+
18+
#[pallet::pallet]
19+
pub struct Pallet<T>(PhantomData<T>);
20+
21+
impl<T: Config> AddressGenerator<T> for Pallet<T>
22+
where
23+
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
24+
{
25+
fn generate_address(
26+
deploying_address: &T::AccountId,
27+
code_hash: &CodeHash<T>,
28+
salt: &[u8],
29+
) -> T::AccountId {
30+
let group_id = <GroupId<T>>::get();
31+
let buf: Vec<_> = deploying_address
32+
.as_ref()
33+
.iter()
34+
.chain(code_hash.as_ref())
35+
.chain(&group_id)
36+
.chain(salt)
37+
.cloned()
38+
.collect();
39+
UncheckedFrom::unchecked_from(<T as frame_system::Config>::Hashing::hash(
40+
&buf,
41+
))
42+
}
43+
}
44+
45+
impl<T: Config> Pallet<T> {
46+
pub fn set_group_id(group_id: &[u8]) {
47+
<GroupId<T>>::put(group_id.to_vec());
48+
}
49+
}
50+
}

crates/pink/src/storage.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ where
8787
self.commit_transaction(root, transaction);
8888
self.clear_changes();
8989
}
90+
91+
pub fn set_group_id(&mut self, group_id: &[u8]) {
92+
self.execute_with(false, || {
93+
crate::runtime::Pink::set_group_id(group_id);
94+
});
95+
}
9096
}
9197

9298
impl Serialize for Storage<InMemoryBackend> {

pallets/utility/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,6 @@ impl<T: Config> Pallet<T> {
477477
/// Derive a derivative account ID from the owner account and the sub-account index.
478478
pub fn derivative_account_id(who: T::AccountId, index: u16) -> T::AccountId {
479479
let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256);
480-
T::AccountId::decode(&mut &entropy[..]).unwrap_or_default()
480+
T::AccountId::decode(&mut &entropy[..]).expect("Should always be able to decode the derived account ID.")
481481
}
482482
}

substrate

Submodule substrate updated 615 files

0 commit comments

Comments
 (0)