Skip to content

Commit

Permalink
Fix the saturating U256 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay111meher committed Nov 29, 2024
1 parent 4dd4ecc commit d889a89
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ test.sh
test_*.sh

/app
/app/*
/app/*
*.log
26 changes: 20 additions & 6 deletions matching_engine/src/generator_lib/generator_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,26 @@ impl GeneratorStore {
) {
if let Some(generator) = self.generators.get_mut(generator_address) {
match source {
super::delegation::Source::Native => generator
.total_native_stake
.sub_token_saturating(token_address, amount),
super::delegation::Source::Symbiotic => generator
.total_symbiotic_stake
.sub_token_saturating(token_address, amount),
super::delegation::Source::Native => {
log::debug!("Existing Native Stake: {:?}", generator.total_native_stake);
log::debug!("Token to remove: {:?}, Amount: {:?}", token_address, amount);
generator
.total_native_stake
.sub_token(token_address, amount)
.unwrap(); // unwraping because this should never come
}

super::delegation::Source::Symbiotic => {
log::debug!(
"Existing Symbiotic Stake: {:?}",
generator.total_symbiotic_stake
);
log::debug!("Token to remove: {:?}, Amount: {:?}", token_address, amount);
generator
.total_symbiotic_stake
.sub_token(token_address, amount)
.unwrap();
}
}

self.delegation_store.add_delegation(
Expand Down
4 changes: 2 additions & 2 deletions matching_engine/src/generator_lib/symbiotic_stake_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl SymbioticStakeStore {
) {
// Check if the operator exists in the HashMap
if let Some(token_tracker) = self.operators.get_mut(operator) {
// If it exists, add the token and amount to the existing TokenTracker
token_tracker.add_token(token_address, absolute_stake);
// If it exists, replaces the token and amount to the existing TokenTracker
token_tracker.replace_token(token_address, absolute_stake);
} else {
// If it does not exist, create a new TokenTracker and add the token and amount
let mut new_tracker = TokenTracker::new();
Expand Down
39 changes: 38 additions & 1 deletion matching_engine/src/log_processor/ss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub async fn process_symbiotic_staking_logs(
.unwrap();

log::debug!(
"operator:{}, snapshot token:{}, amount: {}",
"operator:{:?}, snapshot token:{:?}, amount: {:?}",
&operator,
&stake_token,
vault_snapshot_amount.to_string()
Expand All @@ -216,6 +216,24 @@ pub async fn process_symbiotic_staking_logs(
symbiotic_stake_store.get_latest_stake_info(&operator, &stake_token);

if vault_snapshot_amount.gt(&last_stored_staking_info) {
log::debug!(
"Operator: {:?} Token:{:?} existing stake {:?}",
&operator,
&stake_token,
last_stored_staking_info
);
log::debug!(
"Operator: {:?} Token:{:?} new stake {:?}",
&operator,
&stake_token,
vault_snapshot_amount
);
log::debug!(
"Operator: {:?} Token:{:?} recevied extra stake {:?}",
&operator,
&stake_token,
vault_snapshot_amount - last_stored_staking_info
);
generator_store.add_extra_stake(
&operator,
&stake_token,
Expand All @@ -227,6 +245,25 @@ pub async fn process_symbiotic_staking_logs(
delegation::Source::Symbiotic,
);
} else if vault_snapshot_amount.lt(&last_stored_staking_info) {
log::debug!(
"Operator: {:?} Token:{:?} existing stake {:?}",
&operator,
&stake_token,
last_stored_staking_info
);
log::debug!(
"Operator: {:?} Token:{:?} new stake {:?}",
&operator,
&stake_token,
vault_snapshot_amount
);
log::debug!(
"Operator: {:?} Token:{:?} removed stake {:?}",
&operator,
&stake_token,
last_stored_staking_info - vault_snapshot_amount
);

generator_store.remove_stake(
&operator,
&stake_token,
Expand Down
6 changes: 6 additions & 0 deletions matching_engine/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ impl TokenTracker {
*entry += *amount; // Increment the token amount
}

pub fn replace_token(&mut self, token: &Address, amount: &U256) {
log::warn!("Replace token value is being called in TokenTracker, avoid using this function and modify the app's code");
let entry = self.tokens.entry(*token).or_insert(U256::zero());
*entry = *amount; // replace with whatever value provided.
}

pub fn sub_token(&mut self, token: &Address, amount: &U256) -> Result<(), String> {
if let Some(entry) = self.tokens.get_mut(token) {
if *entry >= *amount {
Expand Down

0 comments on commit d889a89

Please sign in to comment.