diff --git a/bridge-proxy/src/bridge-proxy.rs b/bridge-proxy/src/bridge-proxy.rs index e89877a0..17f5bec7 100644 --- a/bridge-proxy/src/bridge-proxy.rs +++ b/bridge-proxy/src/bridge-proxy.rs @@ -1,6 +1,5 @@ #![no_std] use multiversx_sc::imports::*; -use multiversx_sc_modules::ongoing_operation::*; pub mod bridge_proxy_contract_proxy; pub mod bridged_tokens_wrapper_proxy; @@ -12,19 +11,15 @@ const MIN_GAS_LIMIT_FOR_SC_CALL: u64 = 10_000_000; const MAX_GAS_LIMIT_FOR_SC_CALL: u64 = 249999999; const DEFAULT_GAS_LIMIT_FOR_REFUND_CALLBACK: u64 = 20_000_000; // 20 million const DELAY_BEFORE_OWNER_CAN_CANCEL_TRANSACTION: u64 = 300; -const MIN_GAS_TO_SAVE_PROGRESS: u64 = 1_000_000; -const MAX_ITERATIONS: usize = 1000; #[multiversx_sc::contract] pub trait BridgeProxyContract: config::ConfigModule + multiversx_sc_modules::pause::PauseModule - + multiversx_sc_modules::ongoing_operation::OngoingOperationModule { #[init] fn init(&self, opt_multi_transfer_address: OptionalValue) { self.set_multi_transfer_contract_address(opt_multi_transfer_address); - self.lowest_tx_id().set(1); self.set_paused(true); } @@ -43,9 +38,10 @@ pub trait BridgeProxyContract: caller == self.multi_transfer_address().get(), "Only MultiTransfer can do deposits" ); - let tx_id = self.pending_transactions().push(ð_tx); - self.payments(tx_id).set(&payment); - self.batch_id(tx_id).set(batch_id); + let next_tx_id = self.get_next_tx_id(); + self.pending_transactions().insert(next_tx_id, eth_tx); + self.payments(next_tx_id).set(&payment); + self.batch_id(next_tx_id).set(batch_id); } #[endpoint(execute)] @@ -194,59 +190,31 @@ pub trait BridgeProxyContract: } fn cleanup_transaction(&self, tx_id: usize) { - self.pending_transactions().clear_entry_unchecked(tx_id); - self.update_lowest_tx_id(); + self.pending_transactions().remove(&tx_id); self.ongoing_execution(tx_id).clear(); } - #[endpoint(updateLowestTxId)] - fn update_lowest_tx_id(&self) { - let mut new_lowest = self.lowest_tx_id().get(); - let len = self.pending_transactions().len(); - - let capped_len = if len > new_lowest + MAX_ITERATIONS { - new_lowest + MAX_ITERATIONS - } else { - len - }; - - self.run_while_it_has_gas(MIN_GAS_TO_SAVE_PROGRESS, || { - if !self.empty_element(new_lowest, capped_len) { - return STOP_OP; - } - - new_lowest += 1; - - CONTINUE_OP - }); - - self.lowest_tx_id().set(new_lowest); - } - - fn empty_element(&self, current_index: usize, len: usize) -> bool { - current_index < len && self.pending_transactions().item_is_empty(current_index) + fn get_next_tx_id(&self) -> usize { + let mut next_tx_id = self.highest_tx_id().get(); + next_tx_id += 1; + self.highest_tx_id().set(next_tx_id); + next_tx_id } #[view(getPendingTransactionById)] fn get_pending_transaction_by_id(&self, tx_id: usize) -> EthTransaction { - self.pending_transactions() - .get_or_else(tx_id, || panic!("Invalid tx id")) + let tx = self.pending_transactions().get(&tx_id); + require!(tx.is_some(), "Invalid tx id"); + tx.unwrap() } #[view(getPendingTransactions)] fn get_pending_transactions( &self, ) -> MultiValueEncoded>> { - let lowest_tx_id = self.lowest_tx_id().get(); - let len = self.pending_transactions().len(); - let mut transactions = MultiValueEncoded::new(); - for i in lowest_tx_id..=len { - if self.pending_transactions().item_is_empty(i) { - continue; - } - let tx = self.pending_transactions().get_unchecked(i); - transactions.push(MultiValue2((i, tx))); + for (tx_id, tx) in self.pending_transactions().iter() { + transactions.push(MultiValue2((tx_id, tx))); } transactions } diff --git a/bridge-proxy/src/bridge_proxy_contract_proxy.rs b/bridge-proxy/src/bridge_proxy_contract_proxy.rs index 768889b6..3cb21d18 100644 --- a/bridge-proxy/src/bridge_proxy_contract_proxy.rs +++ b/bridge-proxy/src/bridge_proxy_contract_proxy.rs @@ -113,15 +113,6 @@ where .original_result() } - pub fn update_lowest_tx_id( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("updateLowestTxId") - .original_result() - } - pub fn get_pending_transaction_by_id< Arg0: ProxyArg, >( @@ -210,12 +201,12 @@ where .original_result() } - pub fn lowest_tx_id( + pub fn highest_tx_id( self, ) -> TxTypedCall { self.wrapped_tx .payment(NotPayable) - .raw_call("lowestTxId") + .raw_call("highestTxId") .original_result() } diff --git a/bridge-proxy/src/config.rs b/bridge-proxy/src/config.rs index 9712ae91..6c81b837 100644 --- a/bridge-proxy/src/config.rs +++ b/bridge-proxy/src/config.rs @@ -68,7 +68,7 @@ pub trait ConfigModule { fn esdt_safe_contract_address(&self) -> SingleValueMapper; #[storage_mapper("pending_transactions")] - fn pending_transactions(&self) -> VecMapper>; + fn pending_transactions(&self) -> MapMapper>; #[storage_mapper("payments")] fn payments(&self, tx_id: usize) -> SingleValueMapper>; @@ -76,9 +76,9 @@ pub trait ConfigModule { #[storage_mapper("batch_id")] fn batch_id(&self, tx_id: usize) -> SingleValueMapper; - #[view(lowestTxId)] - #[storage_mapper("lowest_tx_id")] - fn lowest_tx_id(&self) -> SingleValueMapper; + #[view(highestTxId)] + #[storage_mapper("highest_tx_id")] + fn highest_tx_id(&self) -> SingleValueMapper; #[storage_mapper("ongoingExecution")] fn ongoing_execution(&self, tx_id: usize) -> SingleValueMapper; diff --git a/bridge-proxy/tests/bridge_proxy_blackbox_test.rs b/bridge-proxy/tests/bridge_proxy_blackbox_test.rs index 55256b58..2e542e45 100644 --- a/bridge-proxy/tests/bridge_proxy_blackbox_test.rs +++ b/bridge-proxy/tests/bridge_proxy_blackbox_test.rs @@ -425,7 +425,7 @@ fn multiple_deposit_test() { } #[test] -fn test_lowest_tx_id() { +fn test_highest_tx_id() { let mut test = BridgeProxyTestState::new(); test.bridge_proxy_deploy(); @@ -441,9 +441,9 @@ fn test_lowest_tx_id() { }; let call_data = ManagedSerializer::new().top_encode_to_managed_buffer(&call_data); - // Generate 100 transactions + // Generate 1600 transactions let mut transactions = Vec::new(); - for i in 1..=100 { + for i in 1..=1600 { let eth_tx = EthTransaction { from: EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), @@ -456,8 +456,16 @@ fn test_lowest_tx_id() { }; transactions.push(eth_tx); } + test.world + .query() + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .highest_tx_id() + .returns(ExpectValue(0usize)) + .run(); // Deposit all transactions + let mut expected_tx_id = 1usize; for tx in &transactions { test.world .tx() @@ -471,40 +479,19 @@ fn test_lowest_tx_id() { &BigUint::from(5u64), ) .run(); - } - - // Check the lowest_tx_id - test.world - .query() - .to(BRIDGE_PROXY_ADDRESS) - .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) - .lowest_tx_id() - .returns(ExpectValue(1usize)) - .run(); - // Execute the first 50 transactions - for i in 1..=50usize { test.world - .tx() - .from(OWNER_ADDRESS) + .query() .to(BRIDGE_PROXY_ADDRESS) - .gas(200_000_000) .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) - .execute(i) + .highest_tx_id() + .returns(ExpectValue(expected_tx_id)) .run(); + expected_tx_id += 1; } - // Check the lowest_tx_id again - test.world - .query() - .to(BRIDGE_PROXY_ADDRESS) - .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) - .lowest_tx_id() - .returns(ExpectValue(51usize)) - .run(); - - // Execute transactions 75 to 100 - for i in 75..=100usize { + // Execute all transactions + for i in (1..=1600usize).rev() { test.world .tx() .from(OWNER_ADDRESS) @@ -514,13 +501,4 @@ fn test_lowest_tx_id() { .execute(i) .run(); } - - // Check the lowest_tx_id one last time - test.world - .query() - .to(BRIDGE_PROXY_ADDRESS) - .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) - .lowest_tx_id() - .returns(ExpectValue(51usize)) - .run(); } diff --git a/bridge-proxy/wasm/src/lib.rs b/bridge-proxy/wasm/src/lib.rs index bcfd0f92..7036dacd 100644 --- a/bridge-proxy/wasm/src/lib.rs +++ b/bridge-proxy/wasm/src/lib.rs @@ -6,10 +6,10 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 15 +// Endpoints: 14 // Async Callback (empty): 1 // Promise callbacks: 1 -// Total number of exported functions: 19 +// Total number of exported functions: 18 #![no_std] @@ -23,7 +23,6 @@ multiversx_sc_wasm_adapter::endpoints! { upgrade => upgrade deposit => deposit execute => execute - updateLowestTxId => update_lowest_tx_id getPendingTransactionById => get_pending_transaction_by_id getPendingTransactions => get_pending_transactions setMultiTransferAddress => set_multi_transfer_contract_address @@ -32,7 +31,7 @@ multiversx_sc_wasm_adapter::endpoints! { getMultiTransferAddress => multi_transfer_address getBridgedTokensWrapperAddress => bridged_tokens_wrapper_address getEsdtSafeContractAddress => esdt_safe_contract_address - lowestTxId => lowest_tx_id + highestTxId => highest_tx_id pause => pause_endpoint unpause => unpause_endpoint isPaused => paused_status diff --git a/multi-transfer-esdt/src/bridge_proxy_contract_proxy.rs b/multi-transfer-esdt/src/bridge_proxy_contract_proxy.rs index 768889b6..3cb21d18 100644 --- a/multi-transfer-esdt/src/bridge_proxy_contract_proxy.rs +++ b/multi-transfer-esdt/src/bridge_proxy_contract_proxy.rs @@ -113,15 +113,6 @@ where .original_result() } - pub fn update_lowest_tx_id( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("updateLowestTxId") - .original_result() - } - pub fn get_pending_transaction_by_id< Arg0: ProxyArg, >( @@ -210,12 +201,12 @@ where .original_result() } - pub fn lowest_tx_id( + pub fn highest_tx_id( self, ) -> TxTypedCall { self.wrapped_tx .payment(NotPayable) - .raw_call("lowestTxId") + .raw_call("highestTxId") .original_result() } diff --git a/multisig/src/bridge_proxy_contract_proxy.rs b/multisig/src/bridge_proxy_contract_proxy.rs index 768889b6..3cb21d18 100644 --- a/multisig/src/bridge_proxy_contract_proxy.rs +++ b/multisig/src/bridge_proxy_contract_proxy.rs @@ -113,15 +113,6 @@ where .original_result() } - pub fn update_lowest_tx_id( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("updateLowestTxId") - .original_result() - } - pub fn get_pending_transaction_by_id< Arg0: ProxyArg, >( @@ -210,12 +201,12 @@ where .original_result() } - pub fn lowest_tx_id( + pub fn highest_tx_id( self, ) -> TxTypedCall { self.wrapped_tx .payment(NotPayable) - .raw_call("lowestTxId") + .raw_call("highestTxId") .original_result() }