Skip to content

Commit 44f6892

Browse files
committed
⚡️ Optimize user_info key reading
1 parent 4560b68 commit 44f6892

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

contracts/orderbook/src/model.rs

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

elf/orderbook

-85.2 KB
Binary file not shown.

elf/orderbook_vk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"vk":{"commit":{"value":[909531707,105056579,1962904955,1542859704,1066099516,1043024588,748284390,1738796954],"_marker":null},"pc_start":2623676,"initial_global_cumulative_sum":{"x":[1917873776,710306118,400858579,1794466670,14578491,1469539344,1635026457],"y":[1985895937,139560005,202868420,1741638123,535884106,651431360,455852058]},"chip_information":[["Program",{"log_n":19,"shift":1},{"width":14,"height":524288}],["Byte",{"log_n":16,"shift":1},{"width":11,"height":65536}]],"chip_ordering":{"Program":0,"Byte":1}}}
1+
{"vk":{"commit":{"value":[871584694,1543804611,1059774216,1575177085,1836201521,420322979,1490376350,1591510928],"_marker":null},"pc_start":2560940,"initial_global_cumulative_sum":{"x":[600791029,839870708,598965969,121540150,1029841009,1817419662,900021918],"y":[1832841068,1684197396,446963316,1405210179,464282710,951658127,433335093]},"chip_information":[["Program",{"log_n":19,"shift":1},{"width":14,"height":524288}],["Byte",{"log_n":16,"shift":1},{"width":11,"height":65536}]],"chip_ordering":{"Program":0,"Byte":1}}}

0 commit comments

Comments
 (0)