@@ -16,7 +16,11 @@ use ink_engine::ext::Engine;
1616#[ cfg( feature = "unstable-hostfn" ) ]
1717use ink_primitives:: types:: AccountIdMapper ;
1818use ink_primitives:: {
19- abi:: AbiEncodeWith ,
19+ abi:: {
20+ AbiEncodeWith ,
21+ Ink ,
22+ Sol ,
23+ } ,
2024 types:: Environment ,
2125 Address ,
2226 SolEncode ,
@@ -47,6 +51,7 @@ use crate::{
4751 } ,
4852 event:: {
4953 Event ,
54+ TopicEncoder ,
5055 TopicsBuilderBackend ,
5156 } ,
5257 hash:: {
@@ -56,7 +61,6 @@ use crate::{
5661 Keccak256 ,
5762 Sha2x256 ,
5863 } ,
59- Clear ,
6064 DecodeDispatch ,
6165 DispatchError ,
6266 EnvBackend ,
@@ -247,46 +251,68 @@ impl CryptoHash for Keccak256 {
247251
248252#[ derive( Default ) ]
249253pub struct TopicsBuilder {
250- pub topics : Vec < Vec < u8 > > ,
254+ pub topics : Vec < [ u8 ; 32 ] > ,
251255}
252256
253- impl < E > TopicsBuilderBackend < E > for TopicsBuilder
257+ impl < E , Abi > TopicsBuilderBackend < E , Abi > for TopicsBuilder
254258where
255259 E : Environment ,
260+ Abi : TopicEncoder ,
256261{
257- type Output = Vec < u8 > ;
262+ type Output = Vec < [ u8 ; 32 ] > ;
258263
259264 fn push_topic < T > ( & mut self , topic_value : & T )
260265 where
261- T : scale :: Encode ,
266+ T : AbiEncodeWith < Abi > ,
262267 {
263- // todo
264- let encoded = topic_value. encode ( ) ;
265- let len_encoded = encoded. len ( ) ;
266- let mut result = <E as Environment >:: Hash :: CLEAR_HASH ;
267- let len_result = result. as_ref ( ) . len ( ) ;
268- if len_encoded <= len_result {
269- result. as_mut ( ) [ ..len_encoded] . copy_from_slice ( & encoded[ ..] ) ;
270- } else {
271- let mut hash_output = <Blake2x256 as HashOutput >:: Type :: default ( ) ;
272- <Blake2x256 as CryptoHash >:: hash ( & encoded[ ..] , & mut hash_output) ;
273- let copy_len = core:: cmp:: min ( hash_output. len ( ) , len_result) ;
274- result. as_mut ( ) [ 0 ..copy_len] . copy_from_slice ( & hash_output[ 0 ..copy_len] ) ;
275- }
276- let off_hash = result. as_ref ( ) ;
277- let off_hash = off_hash. to_vec ( ) ;
278- self . topics . push ( off_hash) ;
268+ let output = <Abi as TopicEncoder >:: encode_topic ( topic_value) ;
269+ self . topics . push ( output) ;
279270 }
280271
281272 fn output ( self ) -> Self :: Output {
282- let mut all: Vec < u8 > = Vec :: new ( ) ;
273+ self . topics
274+ }
275+ }
283276
284- let topics_len_compact = & scale:: Compact ( self . topics . len ( ) as u32 ) ;
285- let topics_encoded = & scale:: Encode :: encode ( & topics_len_compact) [ ..] ;
286- all. append ( & mut topics_encoded. to_vec ( ) ) ;
277+ impl TopicEncoder for Ink {
278+ const REQUIRES_BUFFER : bool = false ;
287279
288- self . topics . into_iter ( ) . for_each ( |mut v| all. append ( & mut v) ) ;
289- all
280+ fn encode_topic < T > ( value : & T ) -> [ u8 ; 32 ]
281+ where
282+ T : AbiEncodeWith < Self > ,
283+ {
284+ value. encode_topic ( <Blake2x256 as CryptoHash >:: hash)
285+ }
286+
287+ fn encode_topic_with_hash_buffer < T > (
288+ _value : & T ,
289+ _output : & mut [ u8 ; 32 ] ,
290+ _buffer : & mut [ u8 ] ,
291+ ) where
292+ T : AbiEncodeWith < Self > ,
293+ {
294+ unreachable ! ( "not required, `encode_topic` has been implemented." ) ;
295+ }
296+ }
297+
298+ impl TopicEncoder for Sol {
299+ const REQUIRES_BUFFER : bool = false ;
300+
301+ fn encode_topic < T > ( value : & T ) -> [ u8 ; 32 ]
302+ where
303+ T : AbiEncodeWith < Self > ,
304+ {
305+ value. encode_topic ( <Keccak256 as CryptoHash >:: hash)
306+ }
307+
308+ fn encode_topic_with_hash_buffer < T > (
309+ _value : & T ,
310+ _output : & mut [ u8 ; 32 ] ,
311+ _buffer : & mut [ u8 ] ,
312+ ) where
313+ T : AbiEncodeWith < Self > ,
314+ {
315+ unreachable ! ( "not required, `encode_topic` has been implemented." ) ;
290316 }
291317}
292318
@@ -607,15 +633,17 @@ impl TypedEnvBackend for EnvInstance {
607633 } )
608634 }
609635
610- fn emit_event < E , Evt > ( & mut self , event : Evt )
636+ fn emit_event < E , Evt , Abi > ( & mut self , event : & Evt )
611637 where
612638 E : Environment ,
613- Evt : Event ,
639+ Evt : Event < Abi > ,
640+ Abi : TopicEncoder ,
614641 {
615642 let builder = TopicsBuilder :: default ( ) ;
616643 let enc_topics = event. topics :: < E , _ > ( builder. into ( ) ) ;
617- let enc_data = & scale:: Encode :: encode ( & event) [ ..] ;
618- self . engine . deposit_event ( & enc_topics[ ..] , enc_data) ;
644+ let enc_data = event. encode_data ( ) ;
645+ self . engine
646+ . deposit_event ( & enc_topics[ ..] , enc_data. as_slice ( ) ) ;
619647 }
620648
621649 fn invoke_contract < E , Args , R , Abi > (
0 commit comments