Skip to content

Commit d0ff13b

Browse files
authored
Merge pull request #610 from Phala-Network/custom-ink-address
pink: Use customized ink address as contract id
2 parents 998d1a9 + 7dd789d commit d0ff13b

File tree

35 files changed

+966
-491
lines changed

35 files changed

+966
-491
lines changed

Cargo.lock

Lines changed: 326 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/phactory/src/contracts/pink.rs

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::contracts;
22
use crate::system::{TransactionError, TransactionResult};
33
use anyhow::{anyhow, Result};
44
use parity_scale_codec::{Decode, Encode};
5-
use phala_mq::{ContractGroupId, MessageOrigin};
5+
use phala_mq::{ContractClusterId, MessageOrigin};
66
use pink::runtime::ExecSideEffects;
77
use runtime::{AccountId, BlockNumber};
88

@@ -32,12 +32,12 @@ pub enum QueryError {
3232
#[derive(Encode, Decode)]
3333
pub struct Pink {
3434
instance: pink::Contract,
35-
group: ContractGroupId,
35+
cluster_id: ContractClusterId,
3636
}
3737

3838
impl Pink {
3939
pub fn instantiate(
40-
group: ContractGroupId,
40+
cluster_id: ContractClusterId,
4141
storage: &mut pink::Storage,
4242
origin: AccountId,
4343
wasm_bin: Vec<u8>,
@@ -56,15 +56,15 @@ impl Pink {
5656
now,
5757
)
5858
.map_err(|err| anyhow!("Instantiate contract failed: {:?} origin={:?}", err, origin,))?;
59-
Ok((Self { group, instance }, effects))
59+
Ok((Self { cluster_id, instance }, effects))
6060
}
6161

62-
pub fn from_address(address: AccountId, group: ContractGroupId) -> Self {
62+
pub fn from_address(address: AccountId, cluster_id: ContractClusterId) -> Self {
6363
let instance = pink::Contract::from_address(address);
64-
Self { instance, group }
64+
Self { instance, cluster_id }
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
}
@@ -86,8 +86,8 @@ impl contracts::NativeContract for Pink {
8686
let origin = origin.ok_or(QueryError::BadOrigin)?;
8787
match req {
8888
Query::InkMessage(input_data) => {
89-
let storage = group_storage(&mut context.contract_groups, &self.group)
90-
.expect("Pink group should always exists!");
89+
let storage = cluster_storage(&mut context.contract_clusters, &self.cluster_id)
90+
.expect("Pink cluster should always exists!");
9191

9292
let (ink_result, _effects) = self.instance.bare_call(
9393
storage,
@@ -118,8 +118,8 @@ impl contracts::NativeContract for Pink {
118118
_ => return Err(TransactionError::BadOrigin),
119119
};
120120

121-
let storage = group_storage(&mut context.contract_groups, &self.group)
122-
.expect("Pink group should always exists!");
121+
let storage = cluster_storage(&mut context.contract_clusters, &self.cluster_id)
122+
.expect("Pink cluster should always exists!");
123123

124124
let (result, effects) = self
125125
.instance
@@ -146,8 +146,8 @@ impl contracts::NativeContract for Pink {
146146
}
147147

148148
fn on_block_end(&mut self, context: &mut contracts::NativeContext) -> TransactionResult {
149-
let storage = group_storage(&mut context.contract_groups, &self.group)
150-
.expect("Pink group should always exists!");
149+
let storage = cluster_storage(&mut context.contract_clusters, &self.cluster_id)
150+
.expect("Pink cluster should always exists!");
151151
let effects = self
152152
.instance
153153
.on_block_end(storage, context.block.block_number, context.block.now_ms)
@@ -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

@@ -169,20 +169,20 @@ impl NativeContractMore for Pink {
169169
}
170170
}
171171

172-
fn group_storage<'a>(
173-
groups: &'a mut group::GroupKeeper,
174-
group_id: &ContractGroupId,
172+
fn cluster_storage<'a>(
173+
clusters: &'a mut cluster::ClusterKeeper,
174+
cluster_id: &ContractClusterId,
175175
) -> Result<&'a mut pink::Storage> {
176-
groups
177-
.get_group_storage_mut(group_id)
178-
.ok_or(anyhow!("Contract group {:?} not found! qed!", group_id))
176+
clusters
177+
.get_cluster_storage_mut(cluster_id)
178+
.ok_or(anyhow!("Contract cluster {:?} not found! qed!", cluster_id))
179179
}
180180

181-
pub mod group {
181+
pub mod cluster {
182182
use super::Pink;
183183

184184
use anyhow::Result;
185-
use phala_mq::{ContractGroupId, ContractId};
185+
use phala_mq::{ContractClusterId, ContractId};
186186
use phala_serde_more as more;
187187
use pink::{runtime::ExecSideEffects, types::AccountId};
188188
use runtime::BlockNumber;
@@ -191,14 +191,14 @@ pub mod group {
191191
use std::collections::{BTreeMap, BTreeSet};
192192

193193
#[derive(Default, Serialize, Deserialize)]
194-
pub struct GroupKeeper {
195-
groups: BTreeMap<ContractGroupId, Group>,
194+
pub struct ClusterKeeper {
195+
clusters: BTreeMap<ContractClusterId, Cluster>,
196196
}
197197

198-
impl GroupKeeper {
198+
impl ClusterKeeper {
199199
pub fn instantiate_contract(
200200
&mut self,
201-
group_id: ContractGroupId,
201+
cluster_id: ContractClusterId,
202202
origin: AccountId,
203203
wasm_bin: Vec<u8>,
204204
input_data: Vec<u8>,
@@ -207,11 +207,10 @@ 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 cluster = self.get_cluster_or_default_mut(&cluster_id, contract_key);
212211
let (_, effects) = Pink::instantiate(
213-
group_id,
214-
&mut group.storage,
212+
cluster_id,
213+
&mut cluster.storage,
215214
origin,
216215
wasm_bin,
217216
input_data,
@@ -222,49 +221,51 @@ pub mod group {
222221
Ok(effects)
223222
}
224223

225-
pub fn get_group_storage_mut(
224+
pub fn get_cluster_storage_mut(
226225
&mut self,
227-
group_id: &ContractGroupId,
226+
cluster_id: &ContractClusterId,
228227
) -> Option<&mut pink::Storage> {
229-
Some(&mut self.groups.get_mut(group_id)?.storage)
228+
Some(&mut self.clusters.get_mut(cluster_id)?.storage)
230229
}
231230

232-
pub fn get_group_mut(&mut self, group_id: &ContractGroupId) -> Option<&mut Group> {
233-
self.groups.get_mut(group_id)
231+
pub fn get_cluster_mut(&mut self, cluster_id: &ContractClusterId) -> Option<&mut Cluster> {
232+
self.clusters.get_mut(cluster_id)
234233
}
235234

236-
pub fn get_group_or_default_mut(
235+
pub fn get_cluster_or_default_mut(
237236
&mut self,
238-
group_id: &ContractGroupId,
237+
cluster_id: &ContractClusterId,
239238
contract_key: &sr25519::Pair,
240-
) -> &mut Group {
241-
self.groups
242-
.entry(group_id.clone())
243-
.or_insert_with(|| Group {
239+
) -> &mut Cluster {
240+
self.clusters.entry(cluster_id.clone()).or_insert_with(|| {
241+
let mut cluster = Cluster {
244242
storage: Default::default(),
245243
contracts: Default::default(),
246244
key: contract_key.clone(),
247-
})
245+
};
246+
cluster.set_id(cluster_id);
247+
cluster
248+
})
248249
}
249250

250251
pub fn commit_changes(&mut self) -> anyhow::Result<()> {
251-
for group in self.groups.values_mut() {
252-
group.commit_changes()?;
252+
for cluster in self.clusters.values_mut() {
253+
cluster.commit_changes()?;
253254
}
254255
Ok(())
255256
}
256257
}
257258

258259
#[derive(Serialize, Deserialize)]
259-
pub struct Group {
260+
pub struct Cluster {
260261
pub storage: pink::Storage,
261262
contracts: BTreeSet<ContractId>,
262263
#[serde(with = "more::key_bytes")]
263264
key: sr25519::Pair,
264265
}
265266

266-
impl Group {
267-
/// Add a new contract to the group. Returns true if the contract is new.
267+
impl Cluster {
268+
/// Add a new contract to the cluster. Returns true if the contract is new.
268269
pub fn add_contract(&mut self, address: ContractId) -> bool {
269270
self.contracts.insert(address)
270271
}
@@ -277,5 +278,9 @@ pub mod group {
277278
self.storage.commit_changes();
278279
Ok(())
279280
}
281+
282+
pub fn set_id(&mut self, id: &ContractClusterId) {
283+
self.storage.set_cluster_id(id.as_bytes());
284+
}
280285
}
281286
}

crates/phactory/src/contracts/support.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ use phala_mq::traits::MessageChannel;
33
use runtime::BlockNumber;
44
use serde::{Deserialize, Serialize};
55

6-
use super::pink::group::GroupKeeper;
6+
use super::pink::cluster::ClusterKeeper;
77
use super::*;
88
use crate::secret_channel::SecretReceiver;
99
use crate::types::BlockInfo;
1010
use phala_serde_more as more;
1111

1212
pub struct ExecuteEnv<'a, 'b> {
1313
pub block: &'a mut BlockInfo<'b>,
14-
pub contract_groups: &'a mut GroupKeeper,
14+
pub contract_clusters: &'a mut ClusterKeeper,
1515
}
1616

1717
pub struct NativeContext<'a, 'b> {
1818
pub block: &'a mut BlockInfo<'b>,
1919
pub mq: &'a SignedMessageChannel,
2020
pub secret_mq: SecretMessageChannel<'a, SignedMessageChannel>,
21-
pub contract_groups: &'a mut GroupKeeper,
21+
pub contract_clusters: &'a mut ClusterKeeper,
2222
pub self_id: ContractId,
2323
}
2424

2525
pub struct QueryContext<'a> {
2626
pub block_number: BlockNumber,
2727
pub now_ms: u64,
28-
pub contract_groups: &'a mut GroupKeeper,
28+
pub contract_clusters: &'a mut ClusterKeeper,
2929
}
3030

3131
impl NativeContext<'_, '_> {
@@ -42,7 +42,7 @@ pub trait Contract {
4242
req: OpaqueQuery,
4343
context: &mut QueryContext,
4444
) -> Result<OpaqueReply, OpaqueError>;
45-
fn group_id(&self) -> phala_mq::ContractGroupId;
45+
fn cluster_id(&self) -> phala_mq::ContractClusterId;
4646
fn process_next_message(&mut self, env: &mut ExecuteEnv) -> Option<TransactionResult>;
4747
fn on_block_end(&mut self, env: &mut ExecuteEnv) -> TransactionResult;
4848
fn push_message(&self, payload: Vec<u8>, topic: Vec<u8>);
@@ -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+
cluster_id: &phala_mq::ContractClusterId,
97+
deployer: sp_core::H256,
98+
salt: &[u8],
99+
id: u32,
100+
) -> Self {
101+
let encoded = (deployer, id, cluster_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

@@ -156,7 +147,7 @@ pub struct NativeCompatContract<Con: NativeContract> {
156147
cmd_rcv_mq: SecretReceiver<Con::Cmd>,
157148
#[serde(with = "crate::secret_channel::ecdh_serde")]
158149
ecdh_key: KeyPair,
159-
group_id: phala_mq::ContractGroupId,
150+
cluster_id: phala_mq::ContractClusterId,
160151
contract_id: phala_mq::ContractId,
161152
}
162153

@@ -166,15 +157,15 @@ impl<Con: NativeContract> NativeCompatContract<Con> {
166157
send_mq: SignedMessageChannel,
167158
cmd_rcv_mq: SecretReceiver<Con::Cmd>,
168159
ecdh_key: KeyPair,
169-
group_id: phala_mq::ContractGroupId,
160+
cluster_id: phala_mq::ContractClusterId,
170161
contract_id: phala_mq::ContractId,
171162
) -> Self {
172163
NativeCompatContract {
173164
contract,
174165
send_mq,
175166
cmd_rcv_mq,
176167
ecdh_key,
177-
group_id,
168+
cluster_id,
178169
contract_id,
179170
}
180171
}
@@ -185,8 +176,8 @@ impl<Con: NativeContract + NativeContractMore> Contract for NativeCompatContract
185176
self.contract_id
186177
}
187178

188-
fn group_id(&self) -> phala_mq::ContractGroupId {
189-
self.group_id
179+
fn cluster_id(&self) -> phala_mq::ContractClusterId {
180+
self.cluster_id
190181
}
191182

192183
fn handle_query(
@@ -208,7 +199,7 @@ impl<Con: NativeContract + NativeContractMore> Contract for NativeCompatContract
208199
block: env.block,
209200
mq: &self.send_mq,
210201
secret_mq,
211-
contract_groups: &mut env.contract_groups,
202+
contract_clusters: &mut env.contract_clusters,
212203
self_id: self.id(),
213204
};
214205

@@ -231,7 +222,7 @@ impl<Con: NativeContract + NativeContractMore> Contract for NativeCompatContract
231222
block: env.block,
232223
mq: &self.send_mq,
233224
secret_mq,
234-
contract_groups: &mut env.contract_groups,
225+
contract_clusters: &mut env.contract_clusters,
235226
self_id: self.id(),
236227
};
237228
self.contract.on_block_end(&mut context)

0 commit comments

Comments
 (0)