@@ -37,30 +37,40 @@ impl HitHandler {
3737 Self :: default ( )
3838 }
3939
40- fn address ( & self ) -> H256 {
41- ActionDataKeyBuilder :: new ( CUSTOM_ACTION_HANDLER_ID , 1 ) . append ( & "metadata hit" ) . into_key ( )
40+ fn hit_count ( & self ) -> H256 {
41+ ActionDataKeyBuilder :: new ( CUSTOM_ACTION_HANDLER_ID , 1 ) . append ( & "hit count" ) . into_key ( )
42+ }
43+
44+ fn close_count ( & self ) -> H256 {
45+ ActionDataKeyBuilder :: new ( CUSTOM_ACTION_HANDLER_ID , 1 ) . append ( & "close count" ) . into_key ( )
4246 }
4347}
4448
4549impl ActionHandler for HitHandler {
50+ fn name ( & self ) -> & ' static str {
51+ "hit handler"
52+ }
53+
4654 fn handler_id ( & self ) -> u64 {
4755 CUSTOM_ACTION_HANDLER_ID
4856 }
4957
5058 fn init ( & self , state : & mut TopLevelState ) -> StateResult < ( ) > {
51- let existing = state. action_data ( & self . address ( ) ) ;
59+ let existing = state. action_data ( & self . hit_count ( ) ) ;
5260 debug_assert_eq ! ( Ok ( None ) , existing) ;
53- state. update_action_data ( & self . address ( ) , 1u32 . rlp_bytes ( ) . to_vec ( ) ) ?;
61+ state. update_action_data ( & self . hit_count ( ) , 1u32 . rlp_bytes ( ) . to_vec ( ) ) ?;
62+ state. update_action_data ( & self . close_count ( ) , 1u32 . rlp_bytes ( ) . to_vec ( ) ) ?;
5463 Ok ( ( ) )
5564 }
5665
5766 /// `bytes` must be valid encoding of HitAction
5867 fn execute ( & self , bytes : & [ u8 ] , state : & mut TopLevelState , _sender : & Address ) -> StateResult < ( ) > {
68+ let address = self . hit_count ( ) ;
5969 let action = HitAction :: decode ( & UntrustedRlp :: new ( bytes) ) . expect ( "Verification passed" ) ;
60- let action_data = state. action_data ( & self . address ( ) ) ?. unwrap_or_default ( ) ;
70+ let action_data = state. action_data ( & address) ?. unwrap_or_default ( ) ;
6171 let prev_counter: u32 = rlp:: decode ( & * action_data) ;
6272 let increase = u32:: from ( action. increase ) ;
63- state. update_action_data ( & self . address ( ) , ( prev_counter + increase) . rlp_bytes ( ) . to_vec ( ) ) ?;
73+ state. update_action_data ( & address, ( prev_counter + increase) . rlp_bytes ( ) . to_vec ( ) ) ?;
6474 Ok ( ( ) )
6575 }
6676
@@ -69,4 +79,12 @@ impl ActionHandler for HitHandler {
6979 . map_err ( |err| SyntaxError :: InvalidCustomAction ( err. to_string ( ) ) ) ?;
7080 Ok ( ( ) )
7181 }
82+
83+ fn on_close_block ( & self , state : & mut TopLevelState ) -> StateResult < ( ) > {
84+ let address = self . close_count ( ) ;
85+ let action_data = state. action_data ( & address) ?. unwrap_or_default ( ) ;
86+ let prev_counter: u32 = rlp:: decode ( & * action_data) ;
87+ state. update_action_data ( & address, ( prev_counter + 1 ) . rlp_bytes ( ) . to_vec ( ) ) ?;
88+ Ok ( ( ) )
89+ }
7290}
0 commit comments