From 33f3fbb75d8a223ad102e811e4846bb8c914ff95 Mon Sep 17 00:00:00 2001 From: Amin Moghaddam Date: Thu, 29 Feb 2024 14:02:47 +0100 Subject: [PATCH 1/4] fix(auction-server): Use dashmap for bid status --- auction-server/src/state.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/auction-server/src/state.rs b/auction-server/src/state.rs index b7b3ac95..2832769d 100644 --- a/auction-server/src/state.rs +++ b/auction-server/src/state.rs @@ -143,17 +143,17 @@ pub enum BidStatus { } pub struct BidStatusStore { - pub bids_status: RwLock>, + pub bids_status: DashMap, pub event_sender: broadcast::Sender, } impl BidStatusStore { pub async fn get_status(&self, id: &BidId) -> Option { - self.bids_status.read().await.get(id).cloned() + self.bids_status.get(id).map(|entry| entry.clone()) } pub async fn set_and_broadcast(&self, id: BidId, status: BidStatus) { - self.bids_status.write().await.insert(id, status.clone()); + self.bids_status.insert(id, status.clone()); match self .event_sender .send(UpdateEvent::BidStatusUpdate { id, status }) From be2cb731c4cd0d46ec70d5bb69a64b9ac7bd7530 Mon Sep 17 00:00:00 2001 From: Amin Moghaddam Date: Thu, 29 Feb 2024 14:05:15 +0100 Subject: [PATCH 2/4] fix(auction-server): Use dashmap for token spoof info --- auction-server/src/liquidation_adapter.rs | 9 ++------- auction-server/src/state.rs | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/auction-server/src/liquidation_adapter.rs b/auction-server/src/liquidation_adapter.rs index 71966798..9bde0dae 100644 --- a/auction-server/src/liquidation_adapter.rs +++ b/auction-server/src/liquidation_adapter.rs @@ -142,13 +142,12 @@ pub async fn verify_opportunity( ) .tx; let mut state = spoof::State::default(); - let token_spoof_info = chain_store.token_spoof_info.read().await.clone(); for crate::state::TokenQty { contract: token, amount, } in opportunity.repay_tokens.into_iter() { - let spoof_info = match token_spoof_info.get(&token) { + let spoof_info = match chain_store.token_spoof_info.get(&token) { Some(info) => info.clone(), None => { let result = token_spoof::find_spoof_info(token, client.clone()) @@ -158,11 +157,7 @@ pub async fn verify_opportunity( SpoofInfo::UnableToSpoof }); - chain_store - .token_spoof_info - .write() - .await - .insert(token, result.clone()); + chain_store.token_spoof_info.insert(token, result.clone()); result } }; diff --git a/auction-server/src/state.rs b/auction-server/src/state.rs index 2832769d..3f4872e2 100644 --- a/auction-server/src/state.rs +++ b/auction-server/src/state.rs @@ -119,7 +119,7 @@ pub struct ChainStore { pub provider: Provider, pub network_id: u64, pub config: EthereumConfig, - pub token_spoof_info: RwLock>, + pub token_spoof_info: DashMap, pub bids: RwLock>>, } From 13155382a008e4114f6d218a26260f9a437a9dee Mon Sep 17 00:00:00 2001 From: Amin Moghaddam Date: Thu, 29 Feb 2024 14:43:44 +0100 Subject: [PATCH 3/4] fix(auction-server): Use dashmap for bids --- auction-server/src/auction.rs | 18 ++++++++---------- auction-server/src/state.rs | 9 +++------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/auction-server/src/auction.rs b/auction-server/src/auction.rs index d3c932b7..7697b484 100644 --- a/auction-server/src/auction.rs +++ b/auction-server/src/auction.rs @@ -222,16 +222,12 @@ pub async fn run_submission_loop(store: Arc) -> Result<()> { tokio::select! { _ = submission_interval.tick() => { for (chain_id, chain_store) in &store.chains { - let permission_bids = chain_store.bids.read().await.clone(); - // release lock asap tracing::info!( "Chain: {chain_id} Auctions to process {auction_len}", chain_id = chain_id, - auction_len = permission_bids.len() + auction_len = chain_store.bids.len() ); - for (permission_key, bids) in permission_bids.iter() { - let mut cloned_bids = bids.clone(); - let permission_key = permission_key.clone(); + for (permission_key, mut cloned_bids) in chain_store.bids.clone().into_iter() { cloned_bids.sort_by(|a, b| b.bid.cmp(&a.bid)); // TODO: simulate all bids together and keep the successful ones @@ -253,20 +249,24 @@ pub async fn run_submission_loop(store: Arc) -> Result<()> { Some(receipt) => { tracing::debug!("Submitted transaction: {:?}", receipt); let winner_ids:Vec = winner_bids.iter().map(|b| b.id).collect(); - for bid in cloned_bids { + for bid in &cloned_bids { let status = match winner_ids.contains(&bid.id) { true => BidStatus::Submitted(receipt.transaction_hash), false => BidStatus::Lost }; store.bid_status_store.set_and_broadcast(bid.id, status).await; } - chain_store.bids.write().await.remove(&permission_key); + chain_store.bids.remove_if_mut(&permission_key, |_, bids| { + bids.retain(|b| !&cloned_bids.contains(b)); + bids.is_empty() + }); } None => { tracing::error!("Failed to receive transaction receipt"); } }, Err(err) => { + //TODO: handle error, remove invalid bids, set bid status, etc tracing::error!("Transaction failed to submit: {:?}", err); } } @@ -337,8 +337,6 @@ pub async fn handle_bid(store: Arc, bid: Bid) -> result::Result, - pub bids: RwLock>>, + pub bids: DashMap>, } #[derive(Default)] From f622901b2da020b0c800a2570e6e1329134848e1 Mon Sep 17 00:00:00 2001 From: Amin Moghaddam Date: Thu, 29 Feb 2024 15:01:06 +0100 Subject: [PATCH 4/4] Add todo --- auction-server/src/auction.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/auction-server/src/auction.rs b/auction-server/src/auction.rs index 7697b484..ce562540 100644 --- a/auction-server/src/auction.rs +++ b/auction-server/src/auction.rs @@ -248,6 +248,7 @@ pub async fn run_submission_loop(store: Arc) -> Result<()> { Ok(receipt) => match receipt { Some(receipt) => { tracing::debug!("Submitted transaction: {:?}", receipt); + //TODO: inspect the receipt and see which bids were successful on chain let winner_ids:Vec = winner_bids.iter().map(|b| b.id).collect(); for bid in &cloned_bids { let status = match winner_ids.contains(&bid.id) {