@@ -563,6 +563,40 @@ impl ExecuteState {
563563 . ok_or_else ( || format ! ( "User info not found for user '{user}'" ) )
564564 }
565565
566+ #[ cfg_attr( feature = "instrumentation" , tracing:: instrument( skip( self ) ) ) ]
567+ pub fn get_user_names (
568+ & self ,
569+ user_keys : & HashSet < H256 > ,
570+ ) -> Result < HashMap < H256 , String > , String > {
571+ if user_keys. is_empty ( ) {
572+ return Ok ( HashMap :: new ( ) ) ;
573+ }
574+
575+ let mut result = HashMap :: with_capacity ( user_keys. len ( ) ) ;
576+
577+ for user_info in self . users_info . values ( ) {
578+ let key = user_info. get_key ( ) . 0 ;
579+
580+ if user_keys. contains ( & H256 ( key) ) {
581+ result. insert ( H256 ( key) , user_info. user . clone ( ) ) ;
582+ }
583+ }
584+
585+ if result. len ( ) != user_keys. len ( ) {
586+ let missing_keys: Vec < String > = user_keys
587+ . iter ( )
588+ . filter ( |key| !result. contains_key ( key) )
589+ . map ( |key| hex:: encode ( key. as_slice ( ) ) )
590+ . collect ( ) ;
591+
592+ return Err ( format ! (
593+ "No user info found for key(s): {}" ,
594+ missing_keys. join( ", " )
595+ ) ) ;
596+ }
597+
598+ Ok ( result)
599+ }
566600 #[ cfg_attr( feature = "instrumentation" , tracing:: instrument( skip( self ) ) ) ]
567601 pub fn increment_nonce_and_save_user_info (
568602 & mut self ,
@@ -681,11 +715,13 @@ impl ExecuteState {
681715 // Balance change aggregation system based on events
682716 let mut balance_changes: HashMap < Symbol , HashMap < H256 , Balance > > = self . get_balances ( ) ;
683717 let mut touched_accounts: HashMap < Symbol , HashSet < H256 > > = HashMap :: new ( ) ;
718+ let mut user_keys: HashSet < H256 > = HashSet :: new ( ) ;
684719
685720 // Helper function to record balance changes
686721 fn record_balance_change (
687722 balance_changes : & mut HashMap < Symbol , HashMap < H256 , Balance > > ,
688723 touched_accounts : & mut HashMap < Symbol , HashSet < H256 > > ,
724+ user_keys : & mut HashSet < H256 > ,
689725 user_info_key : & H256 ,
690726 symbol : & Symbol ,
691727 amount : i128 ,
@@ -710,20 +746,36 @@ impl ExecuteState {
710746 . entry ( symbol. clone ( ) )
711747 . or_default ( )
712748 . insert ( * user_info_key) ;
749+ user_keys. insert ( * user_info_key) ;
713750 Ok ( ( ) )
714751 }
715752
716753 // Helper function to record transfers between users
717754 fn record_transfer (
718755 balance_changes : & mut HashMap < Symbol , HashMap < H256 , Balance > > ,
719756 touched_accounts : & mut HashMap < Symbol , HashSet < H256 > > ,
757+ user_keys : & mut HashSet < H256 > ,
720758 from : & H256 ,
721759 to : & H256 ,
722760 symbol : & Symbol ,
723761 amount : i128 ,
724762 ) -> Result < ( ) , String > {
725- record_balance_change ( balance_changes, touched_accounts, from, symbol, -amount) ?;
726- record_balance_change ( balance_changes, touched_accounts, to, symbol, amount) ?;
763+ record_balance_change (
764+ balance_changes,
765+ touched_accounts,
766+ user_keys,
767+ from,
768+ symbol,
769+ -amount,
770+ ) ?;
771+ record_balance_change (
772+ balance_changes,
773+ touched_accounts,
774+ user_keys,
775+ to,
776+ symbol,
777+ amount,
778+ ) ?;
727779 Ok ( ( ) )
728780 }
729781
@@ -748,6 +800,7 @@ impl ExecuteState {
748800 record_balance_change (
749801 & mut balance_changes,
750802 & mut touched_accounts,
803+ & mut user_keys,
751804 user_info_key,
752805 & symbol,
753806 quantity,
@@ -777,6 +830,7 @@ impl ExecuteState {
777830 record_transfer (
778831 & mut balance_changes,
779832 & mut touched_accounts,
833+ & mut user_keys,
780834 user_info_key,
781835 executed_order_user_info,
782836 base_symbol,
@@ -786,6 +840,7 @@ impl ExecuteState {
786840 record_balance_change (
787841 & mut balance_changes,
788842 & mut touched_accounts,
843+ & mut user_keys,
789844 user_info_key,
790845 quote_symbol,
791846 ( executed_order. price . unwrap ( ) * executed_order. quantity
@@ -801,6 +856,7 @@ impl ExecuteState {
801856 record_transfer (
802857 & mut balance_changes,
803858 & mut touched_accounts,
859+ & mut user_keys,
804860 user_info_key,
805861 executed_order_user_info,
806862 quote_symbol,
@@ -811,6 +867,7 @@ impl ExecuteState {
811867 record_balance_change (
812868 & mut balance_changes,
813869 & mut touched_accounts,
870+ & mut user_keys,
814871 user_info_key,
815872 base_symbol,
816873 executed_order. quantity as i128 ,
@@ -844,6 +901,7 @@ impl ExecuteState {
844901 record_transfer (
845902 & mut balance_changes,
846903 & mut touched_accounts,
904+ & mut user_keys,
847905 user_info_key,
848906 updated_order_user_info,
849907 base_symbol,
@@ -853,6 +911,7 @@ impl ExecuteState {
853911 record_balance_change (
854912 & mut balance_changes,
855913 & mut touched_accounts,
914+ & mut user_keys,
856915 user_info_key,
857916 quote_symbol,
858917 ( updated_order. price . unwrap ( ) * executed_quantity / base_scale)
@@ -868,6 +927,7 @@ impl ExecuteState {
868927 record_transfer (
869928 & mut balance_changes,
870929 & mut touched_accounts,
930+ & mut user_keys,
871931 user_info_key,
872932 updated_order_user_info,
873933 quote_symbol,
@@ -878,6 +938,7 @@ impl ExecuteState {
878938 record_balance_change (
879939 & mut balance_changes,
880940 & mut touched_accounts,
941+ & mut user_keys,
881942 user_info_key,
882943 base_symbol,
883944 * executed_quantity as i128 ,
@@ -892,6 +953,9 @@ impl ExecuteState {
892953 }
893954 }
894955
956+ // Load user_name from user_key
957+ let user_names = self . get_user_names ( & user_keys) ?;
958+
895959 // Updating balances
896960 for ( symbol, user_keys) in touched_accounts {
897961 let symbol_balances = balance_changes
@@ -906,11 +970,15 @@ impl ExecuteState {
906970 )
907971 } ) ?;
908972
909- let user_name = if user_key == * user_info_key {
910- user_info. user . clone ( )
911- } else {
912- self . get_user_info_from_key ( & user_key) ?. user . clone ( )
913- } ;
973+ let user_name = user_names
974+ . get ( & user_key)
975+ . ok_or_else ( || {
976+ format ! (
977+ "User name for key {} not found" ,
978+ hex:: encode( user_key. as_slice( ) )
979+ )
980+ } ) ?
981+ . clone ( ) ;
914982
915983 events. push ( OrderbookEvent :: BalanceUpdated {
916984 user : user_name,
0 commit comments