From 3e5c1e567b2b463e22e68b1e4283a724d6d0deb5 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 18 Jan 2024 12:37:38 +0200 Subject: [PATCH] configurable gas limit --- esdt-safe/src/lib.rs | 4 +++ esdt-safe/src/to_sovereign/create_tx.rs | 33 +++++++++++++++++++++++-- esdt-safe/wasm/src/lib.rs | 5 ++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/esdt-safe/src/lib.rs b/esdt-safe/src/lib.rs index 3680f3b7..dd79e22e 100644 --- a/esdt-safe/src/lib.rs +++ b/esdt-safe/src/lib.rs @@ -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; @@ -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); diff --git a/esdt-safe/src/to_sovereign/create_tx.rs b/esdt-safe/src/to_sovereign/create_tx.rs index e5cd13f7..29704482 100644 --- a/esdt-safe/src/to_sovereign/create_tx.rs +++ b/esdt-safe/src/to_sovereign/create_tx.rs @@ -1,3 +1,4 @@ +use bls_signature::BlsSignature; use fee_market::subtract_fee::{FinalPayment, ProxyTrait as _}; use transaction::{GasLimit, StolenFromFrameworkEsdtTokenData, Transaction, TransferData}; @@ -5,7 +6,6 @@ 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] @@ -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>, + ) { + 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] @@ -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" ); @@ -109,4 +135,7 @@ pub trait CreateTxModule: #[storage_mapper("feeMarketAddress")] fn fee_market_address(&self) -> SingleValueMapper; + + #[storage_mapper("maxUserTxGasLimit")] + fn max_user_tx_gas_limit(&self) -> SingleValueMapper; } diff --git a/esdt-safe/wasm/src/lib.rs b/esdt-safe/wasm/src/lib.rs index 8a6af10c..dcaea5b5 100644 --- a/esdt-safe/wasm/src/lib.rs +++ b/esdt-safe/wasm/src/lib.rs @@ -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)] @@ -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