@@ -29,6 +29,9 @@ pub type CallOf<T> = <T as Trait>::Call;
2929pub type MomentOf < T > = <T as timestamp:: Trait >:: Moment ;
3030pub type SeedOf < T > = <T as system:: Trait >:: Hash ;
3131
32+ /// A type that represents a topic of an event. At the moment a hash is used.
33+ pub type TopicOf < T > = <T as system:: Trait >:: Hash ;
34+
3235#[ cfg_attr( test, derive( Debug ) ) ]
3336pub struct InstantiateReceipt < AccountId > {
3437 pub address : AccountId ,
@@ -106,8 +109,10 @@ pub trait Ext {
106109 /// Returns a reference to the random seed for the current block
107110 fn random_seed ( & self ) -> & SeedOf < Self :: T > ;
108111
109- /// Deposit an event.
110- fn deposit_event ( & mut self , data : Vec < u8 > ) ;
112+ /// Deposit an event with the given topics.
113+ ///
114+ /// There should not be any duplicates in `topics`.
115+ fn deposit_event ( & mut self , topics : Vec < TopicOf < Self :: T > > , data : Vec < u8 > ) ;
111116
112117 /// Set rent allowance of the contract
113118 fn set_rent_allowance ( & mut self , rent_allowance : BalanceOf < Self :: T > ) ;
@@ -189,6 +194,15 @@ impl VmExecResult {
189194 }
190195}
191196
197+ /// Struct that records a request to deposit an event with a list of topics.
198+ #[ cfg_attr( any( feature = "std" , test) , derive( Debug , PartialEq , Eq ) ) ]
199+ pub struct IndexedEvent < T : Trait > {
200+ /// A list of topics this event will be deposited with.
201+ pub topics : Vec < T :: Hash > ,
202+ /// The event to deposit.
203+ pub event : Event < T > ,
204+ }
205+
192206/// A trait that represent a virtual machine.
193207///
194208/// You can view a virtual machine as something that takes code, an input data buffer,
@@ -238,7 +252,7 @@ pub struct ExecutionContext<'a, T: Trait + 'a, V, L> {
238252 pub self_trie_id : Option < TrieId > ,
239253 pub overlay : OverlayAccountDb < ' a , T > ,
240254 pub depth : usize ,
241- pub events : Vec < Event < T > > ,
255+ pub events : Vec < IndexedEvent < T > > ,
242256 pub calls : Vec < ( T :: AccountId , T :: Call ) > ,
243257 pub config : & ' a Config < T > ,
244258 pub vm : & ' a V ,
@@ -418,7 +432,10 @@ where
418432 . into_result ( ) ?;
419433
420434 // Deposit an instantiation event.
421- nested. events . push ( RawEvent :: Instantiated ( self . self_account . clone ( ) , dest. clone ( ) ) ) ;
435+ nested. events . push ( IndexedEvent {
436+ event : RawEvent :: Instantiated ( self . self_account . clone ( ) , dest. clone ( ) ) ,
437+ topics : Vec :: new ( ) ,
438+ } ) ;
422439
423440 ( nested. overlay . into_change_set ( ) , nested. events , nested. calls )
424441 } ;
@@ -545,8 +562,10 @@ fn transfer<'a, T: Trait, V: Vm<T>, L: Loader<T>>(
545562 if transactor != dest {
546563 ctx. overlay . set_balance ( transactor, new_from_balance) ;
547564 ctx. overlay . set_balance ( dest, new_to_balance) ;
548- ctx. events
549- . push ( RawEvent :: Transfer ( transactor. clone ( ) , dest. clone ( ) , value) ) ;
565+ ctx. events . push ( IndexedEvent {
566+ event : RawEvent :: Transfer ( transactor. clone ( ) , dest. clone ( ) , value) ,
567+ topics : Vec :: new ( ) ,
568+ } ) ;
550569 }
551570
552571 Ok ( ( ) )
@@ -631,8 +650,11 @@ where
631650 & self . timestamp
632651 }
633652
634- fn deposit_event ( & mut self , data : Vec < u8 > ) {
635- self . ctx . events . push ( RawEvent :: Contract ( self . ctx . self_account . clone ( ) , data) ) ;
653+ fn deposit_event ( & mut self , topics : Vec < T :: Hash > , data : Vec < u8 > ) {
654+ self . ctx . events . push ( IndexedEvent {
655+ topics,
656+ event : RawEvent :: Contract ( self . ctx . self_account . clone ( ) , data) ,
657+ } ) ;
636658 }
637659
638660 fn set_rent_allowance ( & mut self , rent_allowance : BalanceOf < T > ) {
@@ -659,7 +681,7 @@ where
659681mod tests {
660682 use super :: {
661683 BalanceOf , ExecFeeToken , ExecutionContext , Ext , Loader , EmptyOutputBuf , TransferFeeKind , TransferFeeToken ,
662- Vm , VmExecResult , InstantiateReceipt , RawEvent ,
684+ Vm , VmExecResult , InstantiateReceipt , RawEvent , IndexedEvent ,
663685 } ;
664686 use crate :: account_db:: AccountDb ;
665687 use crate :: gas:: GasMeter ;
@@ -1262,8 +1284,14 @@ mod tests {
12621284 // there are instantiation event.
12631285 assert_eq ! ( ctx. overlay. get_code_hash( & created_contract_address) . unwrap( ) , dummy_ch) ;
12641286 assert_eq ! ( & ctx. events, & [
1265- RawEvent :: Transfer ( ALICE , created_contract_address, 100 ) ,
1266- RawEvent :: Instantiated ( ALICE , created_contract_address) ,
1287+ IndexedEvent {
1288+ event: RawEvent :: Transfer ( ALICE , created_contract_address, 100 ) ,
1289+ topics: Vec :: new( ) ,
1290+ } ,
1291+ IndexedEvent {
1292+ event: RawEvent :: Instantiated ( ALICE , created_contract_address) ,
1293+ topics: Vec :: new( ) ,
1294+ }
12671295 ] ) ;
12681296 }
12691297 ) ;
@@ -1314,9 +1342,18 @@ mod tests {
13141342 // there are instantiation event.
13151343 assert_eq ! ( ctx. overlay. get_code_hash( & created_contract_address) . unwrap( ) , dummy_ch) ;
13161344 assert_eq ! ( & ctx. events, & [
1317- RawEvent :: Transfer ( ALICE , BOB , 20 ) ,
1318- RawEvent :: Transfer ( BOB , created_contract_address, 15 ) ,
1319- RawEvent :: Instantiated ( BOB , created_contract_address) ,
1345+ IndexedEvent {
1346+ event: RawEvent :: Transfer ( ALICE , BOB , 20 ) ,
1347+ topics: Vec :: new( ) ,
1348+ } ,
1349+ IndexedEvent {
1350+ event: RawEvent :: Transfer ( BOB , created_contract_address, 15 ) ,
1351+ topics: Vec :: new( ) ,
1352+ } ,
1353+ IndexedEvent {
1354+ event: RawEvent :: Instantiated ( BOB , created_contract_address) ,
1355+ topics: Vec :: new( ) ,
1356+ } ,
13201357 ] ) ;
13211358 }
13221359 ) ;
@@ -1362,7 +1399,10 @@ mod tests {
13621399 // The contract wasn't created so we don't expect to see an instantiation
13631400 // event here.
13641401 assert_eq ! ( & ctx. events, & [
1365- RawEvent :: Transfer ( ALICE , BOB , 20 ) ,
1402+ IndexedEvent {
1403+ event: RawEvent :: Transfer ( ALICE , BOB , 20 ) ,
1404+ topics: Vec :: new( ) ,
1405+ } ,
13661406 ] ) ;
13671407 }
13681408 ) ;
0 commit comments