Skip to content

Commit

Permalink
Merge pull request #21 from multiversx/configurable-gas-limit
Browse files Browse the repository at this point in the history
configurable gas limit
  • Loading branch information
dorin-iancu authored Jan 23, 2024
2 parents 8bbc758 + 3e5c1e5 commit 466ff94
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use transaction::GasLimit;
use tx_batch_module::FIRST_BATCH_ID;

const DEFAULT_MAX_TX_BATCH_SIZE: usize = 10;
const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = 100; // ~10 minutes
const DEFAULT_MAX_USER_TX_GAS_LIMIT: GasLimit = 300_000_000;

pub mod from_sovereign;
pub mod to_sovereign;
Expand Down Expand Up @@ -40,6 +42,8 @@ pub trait EsdtSafe:
self.max_tx_batch_size().set(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_block_duration()
.set(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);
self.max_user_tx_gas_limit()
.set(DEFAULT_MAX_USER_TX_GAS_LIMIT);

// batch ID 0 is considered invalid
self.first_batch_id().set(FIRST_BATCH_ID);
Expand Down
33 changes: 31 additions & 2 deletions esdt-safe/src/to_sovereign/create_tx.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use bls_signature::BlsSignature;
use fee_market::subtract_fee::{FinalPayment, ProxyTrait as _};
use transaction::{GasLimit, StolenFromFrameworkEsdtTokenData, Transaction, TransferData};

use crate::to_sovereign::events::DepositEvent;

multiversx_sc::imports!();

const MAX_USER_TX_GAS_LIMIT: GasLimit = 300_000_000;
const MAX_TRANSFERS_PER_TX: usize = 10;

#[multiversx_sc::module]
Expand All @@ -19,6 +19,31 @@ pub trait CreateTxModule:
+ utils::UtilsModule
+ multiversx_sc_modules::pause::PauseModule
{
#[endpoint(setMaxUserTxGasLimit)]
fn set_max_user_tx_gas_limit(
&self,
new_value: GasLimit,
opt_sig: OptionalValue<BlsSignature<Self::Api>>,
) {
if !self.is_setup_phase_complete() {
self.require_caller_initiator();
self.max_user_tx_gas_limit().set(new_value);

return;
}

let opt_signature = opt_sig.into_option();
require!(opt_signature.is_some(), "Must provide signature");

let signature = unsafe { opt_signature.unwrap_unchecked() };
let mut signature_data = ManagedBuffer::new();
let _ = new_value.dep_encode(&mut signature_data);

self.multi_verify_signature(&signature_data, &signature);

self.max_user_tx_gas_limit().set(new_value);
}

/// Create an Elrond -> Sovereign transaction.
#[payable("*")]
#[endpoint]
Expand All @@ -37,8 +62,9 @@ pub trait CreateTxModule:

let opt_gas_limit = match &opt_transfer_data {
OptionalValue::Some(transfer_data) => {
let max_gas_limit = self.max_user_tx_gas_limit().get();
require!(
transfer_data.gas_limit <= MAX_USER_TX_GAS_LIMIT,
transfer_data.gas_limit <= max_gas_limit,
"Gas limit too high"
);

Expand Down Expand Up @@ -109,4 +135,7 @@ pub trait CreateTxModule:

#[storage_mapper("feeMarketAddress")]
fn fee_market_address(&self) -> SingleValueMapper<ManagedAddress>;

#[storage_mapper("maxUserTxGasLimit")]
fn max_user_tx_gas_limit(&self) -> SingleValueMapper<GasLimit>;
}
5 changes: 3 additions & 2 deletions esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 31
// Endpoints: 32
// Async Callback: 1
// Promise callbacks: 1
// Total number of exported functions: 34
// Total number of exported functions: 35

#![no_std]
#![allow(internal_features)]
Expand All @@ -23,6 +23,7 @@ multiversx_sc_wasm_adapter::endpoints! {
init => init
setFeeMarketAddress => set_fee_market_address
upgrade => upgrade
setMaxUserTxGasLimit => set_max_user_tx_gas_limit
deposit => deposit
claimRefund => claim_refund
setTransactionBatchStatus => set_transaction_batch_status
Expand Down

0 comments on commit 466ff94

Please sign in to comment.