@@ -10,21 +10,41 @@ pub use system::System;
10
10
mod system {
11
11
use super :: pink;
12
12
use alloc:: string:: String ;
13
+ use alloc:: vec:: Vec ;
13
14
use ink:: { codegen:: Env , storage:: Mapping } ;
14
15
use pink:: system:: { CodeType , ContractDeposit , ContractDepositRef , DriverError , Error , Result } ;
15
16
use pink:: { HookPoint , PinkEnvironment } ;
16
17
17
18
use this_crate:: { version_tuple, VersionTuple } ;
18
19
20
+ /// A new driver is set.
21
+ #[ ink( event) ]
22
+ pub struct DriverChanged {
23
+ #[ ink( topic) ]
24
+ name : String ,
25
+ previous : Option < AccountId > ,
26
+ current : AccountId ,
27
+ }
28
+
29
+ /// A new administrator is added.
30
+ #[ ink( event) ]
31
+ pub struct AdministratorAdded {
32
+ user : AccountId ,
33
+ }
34
+
19
35
/// Pink's system contract.
20
36
#[ ink( storage) ]
21
37
pub struct System {
22
38
/// The owner of the contract(the cluster).
23
39
owner : AccountId ,
24
40
/// The administrators
25
41
administrators : Mapping < AccountId , ( ) > ,
26
- /// The drivers
42
+ /// The drivers (deprecated)
27
43
drivers : Mapping < String , AccountId > ,
44
+ /// The drivers
45
+ drivers2 : Mapping < String , ( BlockNumber , AccountId ) > ,
46
+ /// The history of drivers
47
+ drivers_history : Mapping < String , Vec < ( BlockNumber , AccountId ) > > ,
28
48
}
29
49
30
50
impl System {
@@ -34,6 +54,8 @@ mod system {
34
54
owner : Self :: env ( ) . caller ( ) ,
35
55
administrators : Default :: default ( ) ,
36
56
drivers : Default :: default ( ) ,
57
+ drivers2 : Default :: default ( ) ,
58
+ drivers_history : Default :: default ( ) ,
37
59
}
38
60
}
39
61
@@ -96,7 +118,9 @@ mod system {
96
118
#[ ink( message) ]
97
119
fn grant_admin ( & mut self , contract_id : AccountId ) -> Result < ( ) > {
98
120
self . ensure_owner ( ) ?;
99
- self . administrators . insert ( contract_id, & ( ) ) ;
121
+ self . administrators . insert ( & contract_id, & ( ) ) ;
122
+ self . env ( )
123
+ . emit_event ( AdministratorAdded { user : contract_id } ) ;
100
124
Ok ( ( ) )
101
125
}
102
126
@@ -109,13 +133,36 @@ mod system {
109
133
}
110
134
_ => { }
111
135
}
112
- self . drivers . insert ( name, & contract_id) ;
136
+
137
+ let previous = self . get_driver2 ( name. clone ( ) ) ;
138
+ if let Some ( ( block, previous) ) = previous {
139
+ if previous == contract_id {
140
+ return Ok ( ( ) ) ;
141
+ }
142
+ let mut history = self . drivers_history . get ( & name) . unwrap_or_default ( ) ;
143
+ history. push ( ( block, previous) ) ;
144
+ self . drivers_history . insert ( & name, & history) ;
145
+ }
146
+ self . drivers2
147
+ . insert ( & name, & ( self . env ( ) . block_number ( ) , contract_id) ) ;
148
+ self . env ( ) . emit_event ( DriverChanged {
149
+ name,
150
+ previous : previous. map ( |( _, id) | id) ,
151
+ current : contract_id,
152
+ } ) ;
113
153
Ok ( ( ) )
114
154
}
115
155
116
156
#[ ink( message) ]
117
157
fn get_driver ( & self , name : String ) -> Option < AccountId > {
118
- self . drivers . get ( & name)
158
+ self . get_driver2 ( name) . map ( |( _, id) | id)
159
+ }
160
+
161
+ #[ ink( message) ]
162
+ fn get_driver2 ( & self , name : String ) -> Option < ( BlockNumber , AccountId ) > {
163
+ self . drivers2
164
+ . get ( & name)
165
+ . or_else ( || self . drivers . get ( & name) . map ( |id| ( 0 , id) ) )
119
166
}
120
167
121
168
#[ ink( message) ]
@@ -222,6 +269,16 @@ mod system {
222
269
fn code_exists ( & self , code_hash : [ u8 ; 32 ] , code_type : CodeType ) -> bool {
223
270
pink:: ext ( ) . code_exists ( code_hash. into ( ) , code_type. is_sidevm ( ) )
224
271
}
272
+
273
+ #[ ink( message) ]
274
+ fn code_hash ( & self , account : AccountId ) -> Option < ink:: primitives:: Hash > {
275
+ self . env ( ) . code_hash ( & account) . ok ( )
276
+ }
277
+
278
+ #[ ink( message) ]
279
+ fn driver_history ( & self , name : String ) -> Option < Vec < ( BlockNumber , AccountId ) > > {
280
+ self . drivers_history . get ( & name)
281
+ }
225
282
}
226
283
227
284
impl ContractDeposit for System {
0 commit comments