Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bridge-proxy: Clear storages after refund #263

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions bridge-proxy/src/bridge-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use transaction::{CallData, EthTransaction};
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;

#[multiversx_sc::contract]
pub trait BridgeProxyContract:
Expand Down Expand Up @@ -103,38 +102,21 @@ pub trait BridgeProxyContract:
tx_call.register_promise();
}

// TODO: will activate endpoint in a future release
// #[endpoint(cancel)]
fn cancel(&self, tx_id: usize) {
let tx_start_round = self.ongoing_execution(tx_id).get();
let current_block_round = self.blockchain().get_block_round();
require!(
current_block_round - tx_start_round > DELAY_BEFORE_OWNER_CAN_CANCEL_TRANSACTION,
"Transaction can't be cancelled yet"
);

let tx = self.get_pending_transaction_by_id(tx_id);
let payment = self.payments(tx_id).get();
self.tx().to(tx.to).payment(payment).transfer();
self.cleanup_transaction(tx_id);
}

#[promises_callback]
fn execution_callback(&self, #[call_result] result: ManagedAsyncCallResult<()>, tx_id: usize) {
if result.is_err() {
self.add_pending_tx_to_refund(tx_id);
}
self.cleanup_transaction(tx_id);
self.pending_transactions().remove(&tx_id);
}

#[endpoint(executeRefundTransaction)]
fn execute_refund_transaction(&self, tx_id: usize) {
let tx = self.get_refund_transaction_by_id(tx_id);

let esdt_safe_contract_address = self.get_esdt_safe_address();

let unwrapped_token = self.unwrap_token(&tx.token_id, tx_id);
let batch_id = self.batch_id(tx_id).get();

self.tx()
.to(esdt_safe_contract_address)
.typed(esdt_safe_proxy::EsdtSafeProxy)
Expand All @@ -152,6 +134,8 @@ pub trait BridgeProxyContract:
&unwrapped_token.amount,
)
.sync_call();

self.finish_refund(tx_id);
}

fn unwrap_token(&self, requested_token: &TokenIdentifier, tx_id: usize) -> EsdtTokenPayment {
Expand Down Expand Up @@ -188,19 +172,21 @@ pub trait BridgeProxyContract:

fn finish_execute_gracefully(&self, tx_id: usize) {
self.add_pending_tx_to_refund(tx_id);
self.cleanup_transaction(tx_id);
self.pending_transactions().remove(&tx_id);
}

fn finish_refund(&self, tx_id: usize) {
self.ongoing_execution(tx_id).clear();
self.payments(tx_id).clear();
self.batch_id(tx_id).clear();
self.refund_transactions().remove(&tx_id);
}

fn add_pending_tx_to_refund(&self, tx_id: usize) {
let tx = self.get_pending_transaction_by_id(tx_id);
self.refund_transactions().insert(tx_id, tx);
}

fn cleanup_transaction(&self, tx_id: usize) {
self.pending_transactions().remove(&tx_id);
self.ongoing_execution(tx_id).clear();
}

fn get_next_tx_id(&self) -> usize {
let mut next_tx_id = self.highest_tx_id().get();
next_tx_id += 1;
Expand Down
10 changes: 9 additions & 1 deletion bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use multiversx_sc_scenario::{
scenario_model::*,
ContractInfo, ScenarioWorld,
};
use multiversx_sc_scenario::{ExpectValue, ScenarioTxRun};
use multiversx_sc_scenario::{ExpectError, ExpectValue, ScenarioTxRun};

use eth_address::*;
use mock_proxies::mock_multisig_proxy;
Expand All @@ -43,6 +43,7 @@ use transaction::{CallData, EthTransaction};
const BRIDGE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("BRIDGE-123456");
const WBRIDGE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("WBRIDGE-123456");

const DELAY_BEFORE_OWNER_CAN_REFUND_TRANSACTION: u64 = 300;
const GAS_LIMIT: u64 = 10_000_000;
const TOO_SMALL_GAS_LIMIT: u64 = 1_000_000;

Expand Down Expand Up @@ -238,6 +239,9 @@ impl BridgeProxyTestState {

self
}
fn set_block_round(&mut self, block_round_expr: u64) {
self.world.current_block().block_round(block_round_expr);
}
}

#[test]
Expand Down Expand Up @@ -962,4 +966,8 @@ fn bridge_proxy_refund_tx_test() {
test.world
.check_account(BRIDGE_PROXY_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, BigUint::zero());

test.world
.check_account(BRIDGE_PROXY_ADDRESS)
.check_storage("str:highestTxId", "1");
}