@@ -10,21 +10,41 @@ pub use system::System;
1010mod system {
1111 use super :: pink;
1212 use alloc:: string:: String ;
13+ use alloc:: vec:: Vec ;
1314 use ink:: { codegen:: Env , storage:: Mapping } ;
1415 use pink:: system:: { CodeType , ContractDeposit , ContractDepositRef , DriverError , Error , Result } ;
1516 use pink:: { HookPoint , PinkEnvironment } ;
1617
1718 use this_crate:: { version_tuple, VersionTuple } ;
1819
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+
1935 /// Pink's system contract.
2036 #[ ink( storage) ]
2137 pub struct System {
2238 /// The owner of the contract(the cluster).
2339 owner : AccountId ,
2440 /// The administrators
2541 administrators : Mapping < AccountId , ( ) > ,
26- /// The drivers
42+ /// The drivers (deprecated)
2743 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 ) > > ,
2848 }
2949
3050 impl System {
@@ -34,6 +54,8 @@ mod system {
3454 owner : Self :: env ( ) . caller ( ) ,
3555 administrators : Default :: default ( ) ,
3656 drivers : Default :: default ( ) ,
57+ drivers2 : Default :: default ( ) ,
58+ drivers_history : Default :: default ( ) ,
3759 }
3860 }
3961
@@ -96,7 +118,9 @@ mod system {
96118 #[ ink( message) ]
97119 fn grant_admin ( & mut self , contract_id : AccountId ) -> Result < ( ) > {
98120 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 } ) ;
100124 Ok ( ( ) )
101125 }
102126
@@ -109,13 +133,36 @@ mod system {
109133 }
110134 _ => { }
111135 }
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+ } ) ;
113153 Ok ( ( ) )
114154 }
115155
116156 #[ ink( message) ]
117157 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) ) )
119166 }
120167
121168 #[ ink( message) ]
@@ -222,6 +269,16 @@ mod system {
222269 fn code_exists ( & self , code_hash : [ u8 ; 32 ] , code_type : CodeType ) -> bool {
223270 pink:: ext ( ) . code_exists ( code_hash. into ( ) , code_type. is_sidevm ( ) )
224271 }
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+ }
225282 }
226283
227284 impl ContractDeposit for System {
0 commit comments