@@ -2,7 +2,7 @@ use crate::contracts;
22use crate :: system:: { TransactionError , TransactionResult } ;
33use anyhow:: { anyhow, Result } ;
44use parity_scale_codec:: { Decode , Encode } ;
5- use phala_mq:: { ContractGroupId , MessageOrigin } ;
5+ use phala_mq:: { ContractClusterId , MessageOrigin } ;
66use pink:: runtime:: ExecSideEffects ;
77use runtime:: { AccountId , BlockNumber } ;
88
@@ -32,12 +32,12 @@ pub enum QueryError {
3232#[ derive( Encode , Decode ) ]
3333pub struct Pink {
3434 instance : pink:: Contract ,
35- group : ContractGroupId ,
35+ cluster_id : ContractClusterId ,
3636}
3737
3838impl 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
162162impl 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}
0 commit comments