diff --git a/ree-cookie-canister/src/canister.rs b/ree-cookie-canister/src/canister.rs index 4ad32b7..509b23c 100644 --- a/ree-cookie-canister/src/canister.rs +++ b/ree-cookie-canister/src/canister.rs @@ -12,9 +12,9 @@ use crate::{ memory::{ mutate_state, read_state, set_state, ADDRESS_PRINCIPLE_MAP, BLOCKS, GAMER, TX_RECORDS, }, - state::{ExchangeState, GameStatus, PoolState}, + state::{ExchangeState, PoolState}, utils::{ - calculate_premine_rune_amount, tweak_pubkey_with_empty, AddLiquidityInfo, RegisterInfo, + calculate_premine_rune_amount, tweak_pubkey_with_empty, AddLiquidityInfo, ExecuteTxGuard, RegisterInfo }, ExchangeError, Seconds, MIN_BTC_VALUE, }; @@ -291,6 +291,9 @@ pub async fn execute_tx(args: ExecuteTxArgs) -> ExecuteTxResponse { output_coins, } = intention; + let _guard = ExecuteTxGuard::new(pool_address.clone()) + .ok_or(format!("Pool {0} Executing", pool_address).to_string())?; + read_state(|s| { return s .address diff --git a/ree-cookie-canister/src/memory.rs b/ree-cookie-canister/src/memory.rs index 09c4b08..2c318f9 100644 --- a/ree-cookie-canister/src/memory.rs +++ b/ree-cookie-canister/src/memory.rs @@ -1,4 +1,5 @@ pub(crate) use std::cell::RefCell; +use std::collections::HashSet; use candid::Principal; use ic_stable_structures::{ @@ -52,6 +53,8 @@ thread_local! { MEMORY_MANAGER.with(|m| m.borrow().get(TX_RECORDS_MEMORY_ID)), ) ); + + pub static EXECUTING_POOLS: RefCell> = RefCell::new(HashSet::new()); } @@ -94,4 +97,4 @@ where let state = get_state(); let r = f(&state); r -} +} \ No newline at end of file diff --git a/ree-cookie-canister/src/state.rs b/ree-cookie-canister/src/state.rs index b19eafb..de67818 100644 --- a/ree-cookie-canister/src/state.rs +++ b/ree-cookie-canister/src/state.rs @@ -1,7 +1,6 @@ use ic_cdk::api::management_canister::bitcoin::Satoshi; use ic_stable_structures::storable::Bound; use ic_stable_structures::Storable; -use itertools::Itertools; use ree_types::{CoinBalance, CoinId, InputCoin, OutputCoin}; use std::borrow::Cow; diff --git a/ree-cookie-canister/src/utils.rs b/ree-cookie-canister/src/utils.rs index 9546bc4..3d6dd67 100644 --- a/ree-cookie-canister/src/utils.rs +++ b/ree-cookie-canister/src/utils.rs @@ -1,7 +1,33 @@ use ic_cdk::api::management_canister::bitcoin::{BitcoinNetwork, Satoshi}; use ree_types::bitcoin::key::{Secp256k1, TapTweak, TweakedPublicKey}; -use crate::{memory::read_state, *}; +use crate::{ + memory::{read_state, EXECUTING_POOLS}, + *, +}; + +#[must_use] +pub struct ExecuteTxGuard(String); + +impl ExecuteTxGuard { + pub fn new(pool_address: String) -> Option { + EXECUTING_POOLS.with(|executing_pools| { + if executing_pools.borrow().contains(&pool_address) { + return None; + } + executing_pools.borrow_mut().insert(pool_address.clone()); + return Some(ExecuteTxGuard(pool_address)); + }) + } +} + +impl Drop for ExecuteTxGuard { + fn drop(&mut self) { + EXECUTING_POOLS.with_borrow_mut(|executing_pools| { + executing_pools.remove(&self.0); + }); + } +} pub(crate) fn tweak_pubkey_with_empty(untweaked: Pubkey) -> TweakedPublicKey { let secp = Secp256k1::new();