Skip to main content

oasis_runtime_sdk/modules/core/
mod.rs

1//! Core definitions module.
2use std::{
3    collections::{BTreeMap, BTreeSet},
4    convert::{TryFrom, TryInto},
5    fmt::Display,
6};
7
8use anyhow::anyhow;
9use oasis_runtime_sdk_macros::{handler, sdk_derive};
10use thiserror::Error;
11
12use crate::{
13    callformat,
14    context::Context,
15    core::consensus::beacon::EpochTime,
16    dispatcher,
17    error::Error as SDKError,
18    keymanager, migration,
19    module::{
20        self, CallResult, InvariantHandler as _, MethodHandler as _, Module as _,
21        ModuleInfoHandler as _,
22    },
23    sender::SenderMeta,
24    state::{CurrentState, Mode, Options, TransactionInfo},
25    storage::{self},
26    types::{
27        token::{self, Denomination},
28        transaction::{
29            self, AddressSpec, AuthProof, Call, CallFormat, CallerAddress, SignerInfo, Transaction,
30            UnverifiedTransaction,
31        },
32    },
33    Runtime,
34};
35
36use self::types::RuntimeInfoResponse;
37
38#[cfg(test)]
39mod test;
40pub mod types;
41
42/// Unique module name.
43pub const MODULE_NAME: &str = "core";
44
45/// Errors emitted by the core module.
46#[derive(Error, Debug, oasis_runtime_sdk_macros::Error)]
47pub enum Error {
48    #[error("malformed transaction: {0}")]
49    #[sdk_error(code = 1)]
50    MalformedTransaction(#[source] anyhow::Error),
51
52    #[error("invalid transaction: {0}")]
53    #[sdk_error(code = 2)]
54    InvalidTransaction(#[from] transaction::Error),
55
56    #[error("invalid method: {0}")]
57    #[sdk_error(code = 3)]
58    InvalidMethod(String),
59
60    #[error("invalid nonce")]
61    #[sdk_error(code = 4)]
62    InvalidNonce,
63
64    #[error("insufficient balance to pay fees")]
65    #[sdk_error(code = 5)]
66    InsufficientFeeBalance,
67
68    #[error("out of message slots")]
69    #[sdk_error(code = 6)]
70    OutOfMessageSlots,
71
72    #[error("message handler not invoked")]
73    #[sdk_error(code = 8)]
74    MessageHandlerNotInvoked,
75
76    #[error("missing message handler")]
77    #[sdk_error(code = 9)]
78    MessageHandlerMissing(u32),
79
80    #[error("invalid argument: {0}")]
81    #[sdk_error(code = 10)]
82    InvalidArgument(#[source] anyhow::Error),
83
84    #[error("gas overflow")]
85    #[sdk_error(code = 11)]
86    GasOverflow,
87
88    #[error("out of gas (limit: {0} wanted: {1})")]
89    #[sdk_error(code = 12)]
90    OutOfGas(u64, u64),
91
92    #[error("too many authentication slots")]
93    #[sdk_error(code = 15)]
94    TooManyAuth,
95
96    #[error("multisig too many signers")]
97    #[sdk_error(code = 16)]
98    MultisigTooManySigners,
99
100    #[error("invariant violation: {0}")]
101    #[sdk_error(code = 17)]
102    InvariantViolation(String),
103
104    #[error("invalid call format: {0}")]
105    #[sdk_error(code = 18)]
106    InvalidCallFormat(#[source] anyhow::Error),
107
108    #[error("{0}")]
109    #[sdk_error(transparent, abort)]
110    Abort(#[source] dispatcher::Error),
111
112    #[error("no module could authenticate the transaction")]
113    #[sdk_error(code = 19)]
114    NotAuthenticated,
115
116    #[error("gas price too low")]
117    #[sdk_error(code = 20)]
118    GasPriceTooLow,
119
120    #[error("forbidden in secure build")]
121    #[sdk_error(code = 21)]
122    ForbiddenInSecureBuild,
123
124    #[error("forbidden by node policy")]
125    #[sdk_error(code = 22)]
126    Forbidden,
127
128    #[error("transaction is too large")]
129    #[sdk_error(code = 23)]
130    OversizedTransaction,
131
132    #[error("transaction is expired or not yet valid")]
133    #[sdk_error(code = 24)]
134    ExpiredTransaction,
135
136    #[error("read-only transaction attempted modifications")]
137    #[sdk_error(code = 25)]
138    ReadOnlyTransaction,
139
140    #[error("future nonce")]
141    #[sdk_error(code = 26)]
142    FutureNonce,
143
144    #[error("call depth exceeded (depth: {0} max: {1})")]
145    #[sdk_error(code = 27)]
146    CallDepthExceeded(u16, u16),
147
148    #[error("{0}")]
149    #[sdk_error(transparent)]
150    TxSimulationFailed(#[from] TxSimulationFailure),
151}
152
153impl Error {
154    /// Generate a proper OutOfGas error, depending on whether the module is configured to emit gas
155    /// use information or not.
156    pub fn out_of_gas<Cfg: Config>(limit: u64, wanted: u64) -> Self {
157        if Cfg::EMIT_GAS_USED_EVENTS {
158            Self::OutOfGas(limit, wanted)
159        } else {
160            // Mask gas used information.
161            Self::OutOfGas(0, 0)
162        }
163    }
164}
165
166/// Simulation failure error.
167#[derive(Error, Debug)]
168pub struct TxSimulationFailure {
169    message: String,
170    module_name: String,
171    code: u32,
172}
173
174impl TxSimulationFailure {
175    /// Returns true if the failure is "core::Error::OutOfGas".
176    pub fn is_error_core_out_of_gas(&self) -> bool {
177        self.module_name == MODULE_NAME && self.code == 12
178    }
179}
180
181impl Display for TxSimulationFailure {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        write!(f, "{}", self.message)
184    }
185}
186
187impl SDKError for TxSimulationFailure {
188    fn module_name(&self) -> &str {
189        &self.module_name
190    }
191
192    fn code(&self) -> u32 {
193        self.code
194    }
195}
196
197impl TryFrom<CallResult> for TxSimulationFailure {
198    type Error = anyhow::Error;
199
200    fn try_from(value: CallResult) -> Result<Self, Self::Error> {
201        match value {
202            CallResult::Failed {
203                module,
204                code,
205                message,
206            } => Ok(TxSimulationFailure {
207                code,
208                module_name: module,
209                message,
210            }),
211            _ => Err(anyhow!("CallResult not Failed")),
212        }
213    }
214}
215
216/// Events emitted by the core module.
217#[derive(Debug, PartialEq, Eq, cbor::Encode, oasis_runtime_sdk_macros::Event)]
218#[cbor(untagged)]
219pub enum Event {
220    #[sdk_event(code = 1)]
221    GasUsed { amount: u64 },
222}
223
224/// Gas costs.
225#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
226pub struct GasCosts {
227    pub tx_byte: u64,
228    pub storage_byte: u64,
229
230    pub auth_signature: u64,
231    pub auth_multisig_signer: u64,
232
233    pub callformat_x25519_deoxysii: u64,
234}
235
236/// Dynamic min gas price parameters.
237#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
238pub struct DynamicMinGasPrice {
239    /// Enables the dynamic min gas price feature which dynamically adjusts the minimum gas price
240    /// based on block fullness, inspired by EIP-1559.
241    ///
242    /// Only takes effect if `min_gas_price`(s) are set.
243    pub enabled: bool,
244
245    /// Target block gas usage indicates the desired block gas usage as a percentage of the total
246    /// block gas limit.
247    ///
248    /// The min gas price will adjust up or down depending on whether the actual gas usage is above
249    /// or below this target.
250    pub target_block_gas_usage_percentage: u8,
251    /// Represents a constant value used to limit the rate at which the min price can change
252    /// between blocks.
253    ///
254    /// For example, if `min_price_max_change_denominator` is set to 8, the maximum change in
255    /// min price is 12.5% between blocks.
256    pub min_price_max_change_denominator: u8,
257}
258
259/// Errors emitted during core parameter validation.
260#[derive(Error, Debug)]
261pub enum ParameterValidationError {
262    #[error("invalid dynamic target block gas usage percentage (10-100)")]
263    InvalidTargetBlockGasUsagePercentage,
264    #[error("invalid dynamic min price max change denominator (1-50)")]
265    InvalidMinPriceMaxChangeDenominator,
266}
267/// Parameters for the core module.
268#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
269pub struct Parameters {
270    pub max_batch_gas: u64,
271    pub max_tx_size: u32,
272    pub max_tx_signers: u32,
273    pub max_multisig_signers: u32,
274    pub gas_costs: GasCosts,
275    pub min_gas_price: BTreeMap<token::Denomination, u128>,
276    pub dynamic_min_gas_price: DynamicMinGasPrice,
277}
278
279impl module::Parameters for Parameters {
280    type Error = ParameterValidationError;
281
282    fn validate_basic(&self) -> Result<(), Self::Error> {
283        // Validate dynamic min gas price parameters.
284        let dmgp = &self.dynamic_min_gas_price;
285        if dmgp.enabled {
286            if dmgp.target_block_gas_usage_percentage < 10
287                || dmgp.target_block_gas_usage_percentage > 100
288            {
289                return Err(ParameterValidationError::InvalidTargetBlockGasUsagePercentage);
290            }
291            if dmgp.min_price_max_change_denominator < 1
292                || dmgp.min_price_max_change_denominator > 50
293            {
294                return Err(ParameterValidationError::InvalidMinPriceMaxChangeDenominator);
295            }
296        }
297        Ok(())
298    }
299}
300
301/// Interface that can be called from other modules.
302pub trait API {
303    /// Module configuration.
304    type Config: Config;
305
306    /// Attempt to use gas. If the gas specified would cause either total used to exceed
307    /// its limit, fails with Error::OutOfGas or Error::BatchOutOfGas, and neither gas usage is
308    /// increased.
309    fn use_batch_gas(gas: u64) -> Result<(), Error>;
310
311    /// Attempt to use gas. If the gas specified would cause either total used to exceed
312    /// its limit, fails with Error::OutOfGas or Error::BatchOutOfGas, and neither gas usage is
313    /// increased.
314    fn use_tx_gas(gas: u64) -> Result<(), Error>;
315
316    /// Returns the remaining batch-wide gas.
317    fn remaining_batch_gas() -> u64;
318
319    /// Returns the total batch-wide gas used.
320    fn used_batch_gas() -> u64;
321
322    /// Return the remaining tx-wide gas.
323    fn remaining_tx_gas() -> u64;
324
325    /// Return the used tx-wide gas.
326    fn used_tx_gas() -> u64;
327
328    /// Configured maximum amount of gas that can be used in a batch.
329    fn max_batch_gas() -> u64;
330
331    /// Configured minimum gas price.
332    fn min_gas_price(denom: &token::Denomination) -> Option<u128>;
333
334    /// Sets the transaction priority to the provided amount.
335    fn set_priority(priority: u64);
336
337    /// Takes and returns the stored transaction priority.
338    fn take_priority() -> u64;
339
340    /// Set transaction sender metadata.
341    fn set_sender_meta(meta: SenderMeta);
342
343    /// Takes and returns the stored transaction sender metadata.
344    fn take_sender_meta() -> SenderMeta;
345
346    /// Returns the configured max iterations in the binary search for the estimate
347    /// gas.
348    fn estimate_gas_search_max_iters<C: Context>(ctx: &C) -> u64;
349
350    /// Check whether the epoch has changed since last processed block.
351    fn has_epoch_changed() -> bool;
352}
353
354/// Genesis state for the accounts module.
355#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
356pub struct Genesis {
357    pub parameters: Parameters,
358}
359
360/// Local configuration that can be provided by the node operator.
361#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
362pub struct LocalConfig {
363    /// Minimum gas price to accept.
364    #[cbor(optional)]
365    pub min_gas_price: BTreeMap<token::Denomination, u128>,
366
367    /// When estimating gas in `core.EstimateGas`, simulate the tx (and report) only up to this much
368    /// used gas. This limit is more likely to be relevant if `estimate_gas_by_simulating_contracts` is
369    /// enabled in the local config. The special value of 0 means that the maximum amount of gas in a
370    /// batch will be used.
371    #[cbor(optional)]
372    pub max_estimated_gas: u64,
373
374    /// The maximum number of iterations of the binary search to be done when simulating contracts for
375    /// gas estimation in `core.EstimateGas`.
376    /// The special value of 0 means that binary search won't be performed, and the transaction will be
377    /// simulated using maximum possible gas, which might return an overestimation in some special cases.
378    /// This setting should likely be kept at 0, unless the runtime is using the EVM module.
379    #[cbor(optional)]
380    pub estimate_gas_search_max_iters: u64,
381}
382
383/// State schema constants.
384pub mod state {
385    /// Runtime metadata.
386    pub const METADATA: &[u8] = &[0x01];
387    /// Map of message idx to message handlers for messages emitted in previous round.
388    pub const MESSAGE_HANDLERS: &[u8] = &[0x02];
389    /// Last processed epoch for detecting epoch changes.
390    pub const LAST_EPOCH: &[u8] = &[0x03];
391    /// Dynamic min gas price.
392    pub const DYNAMIC_MIN_GAS_PRICE: &[u8] = &[0x04];
393}
394
395/// Module configuration.
396#[allow(clippy::declare_interior_mutable_const)]
397pub trait Config: 'static {
398    /// Default local minimum gas price configuration that is used in case no overrides are set in
399    /// local per-node configuration.
400    const DEFAULT_LOCAL_MIN_GAS_PRICE: once_cell::unsync::Lazy<
401        BTreeMap<token::Denomination, u128>,
402    > = once_cell::unsync::Lazy::new(BTreeMap::new);
403
404    /// Default local estimate gas max search iterations configuration that is used in case no overrides
405    /// are set in the local per-node configuration.
406    const DEFAULT_LOCAL_ESTIMATE_GAS_SEARCH_MAX_ITERS: u64 = 0;
407
408    /// Estimated gas amount to be added to failed transaction simulations for selected methods.
409    const ESTIMATE_GAS_EXTRA_FAIL: once_cell::unsync::Lazy<BTreeMap<&'static str, u64>> =
410        once_cell::unsync::Lazy::new(BTreeMap::new);
411
412    /// Methods which are exempt from minimum gas price requirements.
413    const MIN_GAS_PRICE_EXEMPT_METHODS: once_cell::unsync::Lazy<BTreeSet<&'static str>> =
414        once_cell::unsync::Lazy::new(BTreeSet::new);
415
416    /// Whether gas used events should be emitted for every transaction.
417    ///
418    /// Confidential runtimes may want to disable this as it is a possible side channel.
419    const EMIT_GAS_USED_EVENTS: bool = true;
420
421    /// Whether to allow submission of read-only transactions in an interactive way.
422    ///
423    /// Note that execution of such transactions is allowed to access confidential state.
424    const ALLOW_INTERACTIVE_READ_ONLY_TRANSACTIONS: bool = false;
425
426    /// The gas cost of the internal call to retrieve the current calldata public key.
427    const GAS_COST_CALL_CALLDATA_PUBLIC_KEY: u64 = 20;
428    /// The gas cost of the internal call to retrieve the current epoch.
429    const GAS_COST_CALL_CURRENT_EPOCH: u64 = 10;
430}
431
432pub struct Module<Cfg: Config> {
433    _cfg: std::marker::PhantomData<Cfg>,
434}
435
436const CONTEXT_KEY_GAS_USED: &str = "core.GasUsed";
437const CONTEXT_KEY_PRIORITY: &str = "core.Priority";
438const CONTEXT_KEY_SENDER_META: &str = "core.SenderMeta";
439const CONTEXT_KEY_EPOCH_CHANGED: &str = "core.EpochChanged";
440
441impl<Cfg: Config> API for Module<Cfg> {
442    type Config = Cfg;
443
444    fn use_batch_gas(gas: u64) -> Result<(), Error> {
445        // Do not enforce batch limits for checks.
446        if CurrentState::with_env(|env| env.is_check_only()) {
447            return Ok(());
448        }
449        let batch_gas_limit = Self::params().max_batch_gas;
450        let batch_gas_used = Self::used_batch_gas();
451        // NOTE: Going over the batch limit should trigger an abort as the scheduler should never
452        //       allow scheduling past the batch limit but a malicious proposer might include too
453        //       many transactions. Make sure to vote for failure in this case.
454        let batch_new_gas_used = batch_gas_used
455            .checked_add(gas)
456            .ok_or(Error::Abort(dispatcher::Error::BatchOutOfGas))?;
457        if batch_new_gas_used > batch_gas_limit {
458            return Err(Error::Abort(dispatcher::Error::BatchOutOfGas));
459        }
460
461        CurrentState::with(|state| {
462            state
463                .block_value::<u64>(CONTEXT_KEY_GAS_USED)
464                .set(batch_new_gas_used);
465        });
466
467        Ok(())
468    }
469
470    fn use_tx_gas(gas: u64) -> Result<(), Error> {
471        let (gas_limit, gas_used) = CurrentState::with(|state| {
472            (
473                state.env().tx_auth_info().fee.gas,
474                *state.local_value::<u64>(CONTEXT_KEY_GAS_USED).or_default(),
475            )
476        });
477        let new_gas_used = {
478            let sum = gas_used.checked_add(gas).ok_or(Error::GasOverflow)?;
479            if sum > gas_limit {
480                return Err(Error::out_of_gas::<Cfg>(gas_limit, sum));
481            }
482            sum
483        };
484
485        Self::use_batch_gas(gas)?;
486
487        CurrentState::with(|state| {
488            *state.local_value::<u64>(CONTEXT_KEY_GAS_USED).or_default() = new_gas_used;
489        });
490
491        Ok(())
492    }
493
494    fn remaining_batch_gas() -> u64 {
495        let batch_gas_limit = Self::params().max_batch_gas;
496        batch_gas_limit.saturating_sub(Self::used_batch_gas())
497    }
498
499    fn used_batch_gas() -> u64 {
500        CurrentState::with(|state| {
501            state
502                .block_value::<u64>(CONTEXT_KEY_GAS_USED)
503                .get()
504                .cloned()
505                .unwrap_or_default()
506        })
507    }
508
509    fn remaining_tx_gas() -> u64 {
510        let (gas_limit, gas_used) = CurrentState::with(|state| {
511            (
512                state.env().tx_auth_info().fee.gas,
513                *state.local_value::<u64>(CONTEXT_KEY_GAS_USED).or_default(),
514            )
515        });
516        let remaining_tx = gas_limit.saturating_sub(gas_used);
517        // Also check remaining batch gas limit and return the minimum of the two.
518        let remaining_batch = Self::remaining_batch_gas();
519        std::cmp::min(remaining_tx, remaining_batch)
520    }
521
522    fn used_tx_gas() -> u64 {
523        CurrentState::with(|state| *state.local_value::<u64>(CONTEXT_KEY_GAS_USED).or_default())
524    }
525
526    fn max_batch_gas() -> u64 {
527        Self::params().max_batch_gas
528    }
529
530    fn min_gas_price(denom: &token::Denomination) -> Option<u128> {
531        Self::min_gas_prices().get(denom).copied()
532    }
533
534    fn set_priority(priority: u64) {
535        CurrentState::with(|state| {
536            state.block_value::<u64>(CONTEXT_KEY_PRIORITY).set(priority);
537        })
538    }
539
540    fn take_priority() -> u64 {
541        CurrentState::with(|state| {
542            state
543                .block_value::<u64>(CONTEXT_KEY_PRIORITY)
544                .take()
545                .unwrap_or_default()
546        })
547    }
548
549    fn set_sender_meta(meta: SenderMeta) {
550        CurrentState::with(|state| {
551            state
552                .block_value::<SenderMeta>(CONTEXT_KEY_SENDER_META)
553                .set(meta);
554        });
555    }
556
557    fn take_sender_meta() -> SenderMeta {
558        CurrentState::with(|state| {
559            state
560                .block_value::<SenderMeta>(CONTEXT_KEY_SENDER_META)
561                .take()
562                .unwrap_or_default()
563        })
564    }
565
566    fn estimate_gas_search_max_iters<C: Context>(ctx: &C) -> u64 {
567        ctx.local_config(MODULE_NAME)
568            .as_ref()
569            .map(|cfg: &LocalConfig| cfg.estimate_gas_search_max_iters)
570            .unwrap_or(Cfg::DEFAULT_LOCAL_ESTIMATE_GAS_SEARCH_MAX_ITERS)
571    }
572
573    fn has_epoch_changed() -> bool {
574        CurrentState::with(|state| {
575            *state
576                .block_value(CONTEXT_KEY_EPOCH_CHANGED)
577                .get()
578                .unwrap_or(&false)
579        })
580    }
581}
582
583#[sdk_derive(Module)]
584impl<Cfg: Config> Module<Cfg> {
585    const NAME: &'static str = MODULE_NAME;
586    type Error = Error;
587    type Event = Event;
588    type Parameters = Parameters;
589    type Genesis = Genesis;
590
591    #[migration(init)]
592    pub fn init(genesis: Genesis) {
593        // Set genesis parameters.
594        Self::set_params(genesis.parameters);
595    }
596
597    /// Run a transaction in simulation and return how much gas it uses. This looks up the method
598    /// in the context's method registry. Transactions that fail still use gas, and this query will
599    /// estimate that and return successfully, so do not use this query to see if a transaction will
600    /// succeed.
601    #[handler(query = "core.EstimateGas", allow_private_km)]
602    pub fn query_estimate_gas<C: Context>(
603        ctx: &C,
604        mut args: types::EstimateGasQuery,
605    ) -> Result<u64, Error> {
606        let mut extra_gas = 0u64;
607        // In case the runtime is confidential we are unable to authenticate the caller so we must
608        // make sure to zeroize it to avoid leaking private information.
609        if ctx.is_confidential() {
610            args.caller = Some(
611                args.caller
612                    .unwrap_or_else(|| {
613                        args.tx
614                            .auth_info
615                            .signer_info
616                            .first()
617                            .map(|si| si.address_spec.caller_address())
618                            .unwrap_or(CallerAddress::Address(Default::default()))
619                    })
620                    .zeroized(),
621            );
622            args.propagate_failures = false; // Likely to fail as caller is zeroized.
623        }
624        // Assume maximum amount of gas in a batch, a reasonable maximum fee and maximum amount of consensus messages.
625        args.tx.auth_info.fee.gas = {
626            let local_max_estimated_gas = Self::get_local_max_estimated_gas(ctx);
627            if local_max_estimated_gas == 0 {
628                Self::params().max_batch_gas
629            } else {
630                local_max_estimated_gas
631            }
632        };
633        args.tx.auth_info.fee.amount = token::BaseUnits::native(u64::MAX.into());
634        args.tx.auth_info.fee.consensus_messages = ctx.max_messages();
635        // Estimate transaction size. Since the transaction given to us is not signed, we need to
636        // estimate how large each of the auth proofs would be.
637        let auth_proofs: Result<_, Error> = args
638            .tx
639            .auth_info
640            .signer_info
641            .iter()
642            .map(|si| match si.address_spec {
643                // For the signature address spec we assume a signature auth proof of 64 bytes.
644                transaction::AddressSpec::Signature(_) => {
645                    Ok(transaction::AuthProof::Signature(vec![0; 64].into()))
646                }
647                // For the multisig address spec assume all the signers sign with a 64-byte signature.
648                transaction::AddressSpec::Multisig(ref cfg) => {
649                    Ok(transaction::AuthProof::Multisig(
650                        cfg.signers
651                            .iter()
652                            .map(|_| Some(vec![0; 64].into()))
653                            .collect(),
654                    ))
655                }
656                // Internal address specs should never appear as they are not serializable.
657                transaction::AddressSpec::Internal(_) => Err(Error::MalformedTransaction(anyhow!(
658                    "internal address spec used"
659                ))),
660            })
661            .collect();
662        let tx_envelope =
663            transaction::UnverifiedTransaction(cbor::to_vec(args.tx.clone()), auth_proofs?);
664        let tx_size: u32 = cbor::to_vec(tx_envelope)
665            .len()
666            .try_into()
667            .map_err(|_| Error::InvalidArgument(anyhow!("transaction too large")))?;
668        let propagate_failures = args.propagate_failures;
669        let bs_max_iters = Self::estimate_gas_search_max_iters(ctx);
670
671        // Update the address used within the transaction when caller address is passed.
672        if let Some(caller) = args.caller.clone() {
673            // Include additional gas for each signature verification since we will be overwriting
674            // the signer infos below.
675            extra_gas = extra_gas.saturating_add(
676                Self::compute_signature_verification_cost(
677                    &Self::params(),
678                    &args.tx.auth_info.signer_info,
679                )
680                .unwrap_or_default(),
681            );
682
683            args.tx.auth_info.signer_info = vec![transaction::SignerInfo {
684                address_spec: transaction::AddressSpec::Internal(caller),
685                nonce: args
686                    .tx
687                    .auth_info
688                    .signer_info
689                    .first()
690                    .map(|si| si.nonce)
691                    .unwrap_or_default(),
692            }];
693        }
694
695        // Determine if we need to add any extra gas for failing calls.
696        #[allow(clippy::borrow_interior_mutable_const)]
697        let extra_gas_fail = *Cfg::ESTIMATE_GAS_EXTRA_FAIL
698            .get(args.tx.call.method.as_str())
699            .unwrap_or(&0);
700
701        // Simulates transaction with a specific gas limit.
702        let simulate = |tx: &transaction::Transaction, gas: u64, report_failure: bool| {
703            let mut tx = tx.clone();
704            tx.auth_info.fee.gas = gas;
705            let (tx_info, call) = TransactionInfo::from_tx(tx, tx_size, 0, Default::default());
706
707            CurrentState::with_transaction_opts(
708                Options::new()
709                    .with_mode(Mode::Simulate)
710                    .with_tx(tx_info.into()),
711                || {
712                    let (result, _) = dispatcher::Dispatcher::<C::Runtime>::dispatch_tx_call(
713                        ctx,
714                        call,
715                        &Default::default(),
716                    );
717                    if !result.is_success() && report_failure {
718                        // Report failure.
719                        let err: TxSimulationFailure = result.try_into().unwrap(); // Guaranteed to be a Failed CallResult.
720                        return Err(Error::TxSimulationFailed(err));
721                    }
722                    // Don't report success or failure. If the call fails, we still report
723                    // how much gas it uses while it fails.
724                    let gas_used = Self::used_batch_gas();
725                    if result.is_success() {
726                        Ok(gas_used)
727                    } else {
728                        Ok(gas_used.saturating_add(extra_gas_fail).clamp(0, gas))
729                    }
730                },
731            )
732        };
733
734        // Do a binary search for exact gas limit.
735        let (cap, mut lo, mut hi) = (
736            args.tx.auth_info.fee.gas,
737            10_u128,
738            args.tx.auth_info.fee.gas as u128,
739        ); // Use u128 to avoid overflows when computing the mid point.
740
741        // Count iterations, and remember if fast path was tried.
742        let (mut iters, mut fast_path_tried) = (0, false);
743        // The following two variables are used to control the special case where a transaction fails
744        // and we check if the error is due to out-of-gas by re-simulating the transaction with maximum
745        // gas limit. This is needed due to EVM transactions failing with a "reverted" error when
746        // not having enough gas for EIP-150 (and not with "out-of-gas").
747        let (mut has_succeeded, mut tried_with_max_gas) = (false, false);
748        while (lo + 1 < hi) && iters < bs_max_iters {
749            iters += 1;
750
751            let mid = (hi + lo) / 2;
752            match simulate(&args.tx, mid as u64, true) {
753                Ok(r) => {
754                    // Estimate success. Try with lower gas.
755                    hi = mid;
756
757                    // The transaction succeeded at least once, meaning any future failure is due
758                    // to insufficient gas limit.
759                    has_succeeded = true;
760
761                    // Optimization: In vast majority of cases the initially returned gas estimate
762                    // might already be a good one. Check if this is the case to speed up the convergence.
763                    if !fast_path_tried && (lo + 1 < hi) {
764                        fast_path_tried = true;
765
766                        // If simulate with the returned estimate succeeds, we can further shrink the
767                        // high limit of the binary search.
768                        match simulate(&args.tx, r, true) {
769                            Ok(_) => hi = r as u128,
770                            _ => continue,
771                        }
772                        // If simulate with one unit of gas smaller fails, we know the exact estimate.
773                        match simulate(&args.tx, r - 1, true) {
774                            Err(_) => {
775                                // Stop the gas search.
776                                break;
777                            }
778                            _ => continue,
779                        }
780                    }
781                }
782                Err(_) if has_succeeded => {
783                    // Transaction previously succeeded. Transaction failed due to insufficient gas limit,
784                    // regardless of the actual returned error.
785                    // Try with higher gas.
786                    lo = mid
787                }
788                Err(Error::TxSimulationFailed(failure)) if failure.is_error_core_out_of_gas() => {
789                    // Estimate failed due to insufficient gas limit. Try with higher gas.
790                    lo = mid
791                }
792                r @ Err(_) => {
793                    let mut res = r;
794                    if !tried_with_max_gas {
795                        tried_with_max_gas = true;
796                        // Transaction failed and simulation with max gas was not yet tried.
797                        // Try simulating with maximum gas once:
798                        //  - if fails, the transaction will always fail, stop the binary search.
799                        //  - if succeeds, remember that transaction is failing due to insufficient gas
800                        //    and continue the search.
801                        res = simulate(&args.tx, cap, true)
802                    }
803                    match res {
804                        Ok(_) => {
805                            has_succeeded = true;
806                            // Transaction can succeed. Try with higher gas.
807                            lo = mid
808                        }
809                        err if propagate_failures => {
810                            // Estimate failed (not with out-of-gas) and caller wants error propagation -> early exit and return the error.
811                            return err;
812                        }
813                        _ => {
814                            // Estimate failed (not with out-of-gas) but caller wants to know the gas usage.
815                            // Exit loop and do one final estimate without error propagation.
816                            // NOTE: don't continue the binary search for failing transactions as the convergence
817                            // for these could take somewhat long and the estimate with default max gas is likely good.
818                            break;
819                        }
820                    }
821                }
822            }
823        }
824
825        // hi == cap if binary search is disabled or this is a failing transaction.
826        let result = if hi == Into::<u128>::into(cap) {
827            // Simulate one last time with maximum gas limit.
828            simulate(&args.tx, cap, propagate_failures)
829        } else {
830            Ok(hi as u64)
831        };
832
833        // Make sure the final result is clamped.
834        result.map(|est| est.saturating_add(extra_gas).clamp(0, cap))
835    }
836
837    /// Check invariants of all modules in the runtime.
838    #[handler(query = "core.CheckInvariants", expensive)]
839    fn query_check_invariants<C: Context>(ctx: &C, _args: ()) -> Result<(), Error> {
840        <C::Runtime as Runtime>::Modules::check_invariants(ctx)
841    }
842
843    fn calldata_public_key_common<C: Context>(
844        ctx: &C,
845    ) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
846        let key_manager = ctx
847            .key_manager()
848            .ok_or_else(|| Error::InvalidArgument(anyhow!("key manager not available")))?;
849        let epoch = ctx.epoch();
850        let public_key = key_manager
851            .get_public_ephemeral_key(callformat::get_key_pair_id(epoch), epoch)
852            .map_err(|err| match err {
853                keymanager::KeyManagerError::InvalidEpoch(..) => {
854                    Error::InvalidCallFormat(anyhow!("invalid epoch"))
855                }
856                _ => Error::Abort(err.into()),
857            })?;
858
859        Ok(types::CallDataPublicKeyQueryResponse { public_key, epoch })
860    }
861
862    /// Retrieve the public key for encrypting call data.
863    #[handler(query = "core.CallDataPublicKey")]
864    fn query_calldata_public_key<C: Context>(
865        ctx: &C,
866        _args: (),
867    ) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
868        Self::calldata_public_key_common(ctx)
869    }
870
871    /// Retrieve the public key for encrypting call data (internally exposed call).
872    #[handler(call = "core.CallDataPublicKey", internal)]
873    fn internal_calldata_public_key<C: Context>(
874        ctx: &C,
875        _args: (),
876    ) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
877        <C::Runtime as Runtime>::Core::use_tx_gas(Cfg::GAS_COST_CALL_CALLDATA_PUBLIC_KEY)?;
878        Self::calldata_public_key_common(ctx)
879    }
880
881    /// Retrieve the current epoch.
882    #[handler(call = "core.CurrentEpoch", internal)]
883    fn internal_current_epoch<C: Context>(ctx: &C, _args: ()) -> Result<u64, Error> {
884        <C::Runtime as Runtime>::Core::use_tx_gas(Cfg::GAS_COST_CALL_CURRENT_EPOCH)?;
885        Ok(ctx.epoch())
886    }
887
888    /// Query the minimum gas price.
889    #[handler(query = "core.MinGasPrice")]
890    fn query_min_gas_price<C: Context>(
891        ctx: &C,
892        _args: (),
893    ) -> Result<BTreeMap<token::Denomination, u128>, Error> {
894        let mut mgp = Self::min_gas_prices();
895
896        // Generate a combined view with local overrides.
897        for (denom, price) in mgp.iter_mut() {
898            let local_mgp = Self::get_local_min_gas_price(ctx, denom);
899            if local_mgp > *price {
900                *price = local_mgp;
901            }
902        }
903
904        Ok(mgp)
905    }
906
907    /// Return basic information about the module and the containing runtime.
908    #[handler(query = "core.RuntimeInfo")]
909    fn query_runtime_info<C: Context>(ctx: &C, _args: ()) -> Result<RuntimeInfoResponse, Error> {
910        Ok(RuntimeInfoResponse {
911            runtime_version: <C::Runtime as Runtime>::VERSION,
912            state_version: <C::Runtime as Runtime>::STATE_VERSION,
913            modules: <C::Runtime as Runtime>::Modules::module_info(ctx),
914        })
915    }
916
917    /// Execute a read-only transaction in an interactive mode.
918    ///
919    /// # Warning
920    ///
921    /// This query is allowed access to private key manager state.
922    #[handler(query = "core.ExecuteReadOnlyTx", expensive, allow_private_km)]
923    fn query_execute_read_only_tx<C: Context>(
924        ctx: &C,
925        args: types::ExecuteReadOnlyTxQuery,
926    ) -> Result<types::ExecuteReadOnlyTxResponse, Error> {
927        if !Cfg::ALLOW_INTERACTIVE_READ_ONLY_TRANSACTIONS {
928            return Err(Error::Forbidden);
929        }
930
931        CurrentState::with_transaction_opts(Options::new().with_mode(Mode::Simulate), || {
932            // TODO: Use separate batch gas limit for query execution.
933
934            // Decode transaction and verify signature.
935            let tx_size = args
936                .tx
937                .len()
938                .try_into()
939                .map_err(|_| Error::OversizedTransaction)?;
940            let tx = dispatcher::Dispatcher::<C::Runtime>::decode_tx(ctx, &args.tx)?;
941
942            // Only read-only transactions are allowed in interactive queries.
943            if !tx.call.read_only {
944                return Err(Error::InvalidArgument(anyhow::anyhow!(
945                    "only read-only transactions are allowed"
946                )));
947            }
948            // Only transactions with expiry are allowed in interactive queries.
949            if tx.auth_info.not_before.is_none() || tx.auth_info.not_after.is_none() {
950                return Err(Error::InvalidArgument(anyhow::anyhow!(
951                    "only read-only transactions with expiry are allowed"
952                )));
953            }
954
955            // Execute transaction.
956            let (result, _) = dispatcher::Dispatcher::<C::Runtime>::execute_tx_opts(
957                ctx,
958                tx,
959                &dispatcher::DispatchOptions {
960                    tx_size,
961                    method_authorizer: Some(&|method| {
962                        // Ensure that the inner method is allowed to be called from an interactive
963                        // context to avoid unexpected pitfalls.
964                        <C::Runtime as Runtime>::Modules::is_allowed_interactive_call(method)
965                            && <C::Runtime as Runtime>::is_allowed_interactive_call(method)
966                    }),
967                    ..Default::default()
968                },
969            )
970            .map_err(|err| Error::InvalidArgument(err.into()))?;
971
972            Ok(types::ExecuteReadOnlyTxResponse { result })
973        })
974    }
975}
976
977impl<Cfg: Config> Module<Cfg> {
978    fn min_gas_prices() -> BTreeMap<Denomination, u128> {
979        let params = Self::params();
980        if params.dynamic_min_gas_price.enabled {
981            CurrentState::with_store(|store| {
982                let store =
983                    storage::TypedStore::new(storage::PrefixStore::new(store, &MODULE_NAME));
984                store
985                    .get(state::DYNAMIC_MIN_GAS_PRICE)
986                    // Use static min gas price when dynamic price was not yet computed.
987                    .unwrap_or(params.min_gas_price)
988            })
989        } else {
990            params.min_gas_price
991        }
992    }
993
994    fn get_local_min_gas_price<C: Context>(ctx: &C, denom: &token::Denomination) -> u128 {
995        #[allow(clippy::borrow_interior_mutable_const)]
996        ctx.local_config(MODULE_NAME)
997            .as_ref()
998            .map(|cfg: &LocalConfig| cfg.min_gas_price.get(denom).copied())
999            .unwrap_or_else(|| Cfg::DEFAULT_LOCAL_MIN_GAS_PRICE.get(denom).copied())
1000            .unwrap_or_default()
1001    }
1002
1003    fn get_local_max_estimated_gas<C: Context>(ctx: &C) -> u64 {
1004        ctx.local_config(MODULE_NAME)
1005            .as_ref()
1006            .map(|cfg: &LocalConfig| cfg.max_estimated_gas)
1007            .unwrap_or_default()
1008    }
1009
1010    fn enforce_min_gas_price<C: Context>(ctx: &C, call: &Call) -> Result<(), Error> {
1011        // If the method is exempt from min gas price requirements, checks always pass.
1012        #[allow(clippy::borrow_interior_mutable_const)]
1013        if Cfg::MIN_GAS_PRICE_EXEMPT_METHODS.contains(call.method.as_str()) {
1014            return Ok(());
1015        }
1016
1017        let fee = CurrentState::with_env(|env| env.tx_auth_info().fee.clone());
1018        let denom = fee.amount.denomination();
1019
1020        match Self::min_gas_price(denom) {
1021            // If the denomination is not among the global set, reject.
1022            None => return Err(Error::GasPriceTooLow),
1023
1024            // Otherwise, allow overrides during local checks.
1025            Some(min_gas_price) => {
1026                if CurrentState::with_env(|env| env.is_check_only()) {
1027                    let local_mgp = Self::get_local_min_gas_price(ctx, denom);
1028
1029                    // Reject during local checks.
1030                    if fee.gas_price() < local_mgp {
1031                        return Err(Error::GasPriceTooLow);
1032                    }
1033                }
1034
1035                if fee.gas_price() < min_gas_price {
1036                    return Err(Error::GasPriceTooLow);
1037                }
1038            }
1039        }
1040
1041        Ok(())
1042    }
1043
1044    fn compute_signature_verification_cost(
1045        params: &Parameters,
1046        signer_info: &[SignerInfo],
1047    ) -> Option<u64> {
1048        let mut num_signature: u64 = 0;
1049        let mut num_multisig_signer: u64 = 0;
1050        for si in signer_info {
1051            match &si.address_spec {
1052                AddressSpec::Signature(_) => {
1053                    num_signature = num_signature.checked_add(1)?;
1054                }
1055                AddressSpec::Multisig(config) => {
1056                    num_multisig_signer =
1057                        num_multisig_signer.checked_add(config.signers.len() as u64)?;
1058                }
1059                AddressSpec::Internal(_) => {}
1060            }
1061        }
1062
1063        let signature_cost = num_signature.checked_mul(params.gas_costs.auth_signature)?;
1064        let multisig_signer_cost =
1065            num_multisig_signer.checked_mul(params.gas_costs.auth_multisig_signer)?;
1066        let sum = signature_cost.checked_add(multisig_signer_cost)?;
1067
1068        Some(sum)
1069    }
1070}
1071
1072impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {
1073    fn approve_raw_tx<C: Context>(_ctx: &C, tx: &[u8]) -> Result<(), Error> {
1074        let params = Self::params();
1075        if tx.len() > TryInto::<usize>::try_into(params.max_tx_size).unwrap() {
1076            return Err(Error::OversizedTransaction);
1077        }
1078        Ok(())
1079    }
1080
1081    fn approve_unverified_tx<C: Context>(
1082        _ctx: &C,
1083        utx: &UnverifiedTransaction,
1084    ) -> Result<(), Error> {
1085        let params = Self::params();
1086        if utx.1.len() > params.max_tx_signers as usize {
1087            return Err(Error::TooManyAuth);
1088        }
1089        for auth_proof in &utx.1 {
1090            if let AuthProof::Multisig(config) = auth_proof {
1091                if config.len() > params.max_multisig_signers as usize {
1092                    return Err(Error::MultisigTooManySigners);
1093                }
1094            }
1095        }
1096        Ok(())
1097    }
1098
1099    fn authenticate_tx<C: Context>(
1100        ctx: &C,
1101        tx: &Transaction,
1102    ) -> Result<module::AuthDecision, Error> {
1103        // Check whether the transaction is currently valid.
1104        let round = ctx.runtime_header().round;
1105        if let Some(not_before) = tx.auth_info.not_before {
1106            if round < not_before {
1107                // Too early.
1108                return Err(Error::ExpiredTransaction);
1109            }
1110        }
1111        if let Some(not_after) = tx.auth_info.not_after {
1112            if round > not_after {
1113                // Too late.
1114                return Err(Error::ExpiredTransaction);
1115            }
1116        }
1117
1118        Ok(module::AuthDecision::Continue)
1119    }
1120
1121    fn before_handle_call<C: Context>(ctx: &C, call: &Call) -> Result<(), Error> {
1122        // Ensure that specified gas limit is not greater than batch gas limit.
1123        let params = Self::params();
1124        let fee = CurrentState::with_env(|env| env.tx_auth_info().fee.clone());
1125        if fee.gas > params.max_batch_gas {
1126            return Err(Error::GasOverflow);
1127        }
1128        if fee.consensus_messages > ctx.max_messages() {
1129            return Err(Error::OutOfMessageSlots);
1130        }
1131
1132        // Skip additional checks/gas payment for internally generated transactions.
1133        if CurrentState::with_env(|env| env.is_internal()) {
1134            return Ok(());
1135        }
1136
1137        // Enforce minimum gas price constraints.
1138        Self::enforce_min_gas_price(ctx, call)?;
1139
1140        // Charge gas for transaction size.
1141        let tx_size = CurrentState::with_env(|env| env.tx_size());
1142        Self::use_tx_gas(
1143            params
1144                .gas_costs
1145                .tx_byte
1146                .checked_mul(tx_size.into())
1147                .ok_or(Error::GasOverflow)?,
1148        )?;
1149
1150        // Charge gas for signature verification.
1151        let total = CurrentState::with_env(|env| {
1152            Self::compute_signature_verification_cost(&params, &env.tx_auth_info().signer_info)
1153        })
1154        .ok_or(Error::GasOverflow)?;
1155        Self::use_tx_gas(total)?;
1156
1157        // Charge gas for callformat.
1158        match call.format {
1159            CallFormat::Plain => {} // No additional gas required.
1160            CallFormat::EncryptedX25519DeoxysII => {
1161                Self::use_tx_gas(params.gas_costs.callformat_x25519_deoxysii)?
1162            }
1163        }
1164
1165        Ok(())
1166    }
1167
1168    fn after_handle_call<C: Context>(ctx: &C, _result: &module::CallResult) -> Result<(), Error> {
1169        // Skip handling for internally generated calls.
1170        if CurrentState::with_env(|env| env.is_internal()) {
1171            return Ok(());
1172        }
1173
1174        let params = Self::params();
1175
1176        // Compute storage update gas cost.
1177        let storage_gas = if params.gas_costs.storage_byte > 0 {
1178            let storage_update_bytes =
1179                CurrentState::with(|state| state.pending_store_update_byte_size());
1180            params
1181                .gas_costs
1182                .storage_byte
1183                .saturating_mul(storage_update_bytes as u64)
1184        } else {
1185            0
1186        };
1187
1188        // Compute message gas cost.
1189        let message_gas = {
1190            let emitted_message_count =
1191                CurrentState::with(|state| state.emitted_messages_local_count());
1192            // Determine how much each message emission costs based on max_batch_gas and the number
1193            // of messages that can be emitted per batch.
1194            let message_gas_cost = params
1195                .max_batch_gas
1196                .checked_div(ctx.max_messages().into())
1197                .unwrap_or(u64::MAX); // If no messages are allowed, cost is infinite.
1198            message_gas_cost.saturating_mul(emitted_message_count as u64)
1199        };
1200
1201        // Compute the gas amount that the transaction should pay in the end.
1202        let used_gas = Self::used_tx_gas();
1203        let max_gas = std::cmp::max(used_gas, std::cmp::max(storage_gas, message_gas));
1204
1205        // Make sure the transaction actually pays for the maximum gas. Note that failure here is
1206        // fine since the extra resources (storage updates or emitted consensus messages) have not
1207        // actually been spent yet (this happens at the end of the round).
1208        let maybe_out_of_gas = Self::use_tx_gas(max_gas - used_gas); // Cannot overflow as max_gas >= used_gas.
1209
1210        // Emit gas used event.
1211        if Cfg::EMIT_GAS_USED_EVENTS {
1212            let used_gas = Self::used_tx_gas();
1213            CurrentState::with(|state| {
1214                state.emit_unconditional_event(Event::GasUsed { amount: used_gas });
1215            });
1216        }
1217
1218        // Evaluate the result of the above `use_tx_gas` here to make sure we emit the event.
1219        maybe_out_of_gas?;
1220
1221        Ok(())
1222    }
1223}
1224
1225/// Computes the new minimum gas price based on the current gas usage and the target gas usage.
1226///
1227/// The new price is computed as follows (inspired by EIP-1559):
1228///  - If the actual gas used is greater than the target gas used, increase the minimum gas price.
1229///  - If the actual gas used is less than the target gas used, decrease the minimum gas price.
1230///
1231/// The price change is controlled by the `min_price_max_change_denominator` parameter.
1232fn min_gas_price_update(
1233    gas_used: u128,
1234    target_gas_used: u128,
1235    min_price_max_change_denominator: u128,
1236    current_price: u128,
1237) -> u128 {
1238    // If the target gas used is zero or the denominator is zero, don't change the price.
1239    if target_gas_used == 0 || min_price_max_change_denominator == 0 {
1240        return current_price;
1241    }
1242
1243    // Calculate the difference (as a percentage) between the actual gas used in the block and the target gas used.
1244    let delta = (gas_used.max(target_gas_used) - gas_used.min(target_gas_used)).saturating_mul(100)
1245        / target_gas_used;
1246
1247    // Calculate the change in gas price and divide by `min_price_max_change_denominator`.
1248    let price_change =
1249        (current_price.saturating_mul(delta) / 100) / min_price_max_change_denominator;
1250
1251    // Adjust the current price based on whether the gas used was above or below the target.
1252    if gas_used > target_gas_used {
1253        current_price.saturating_add(price_change)
1254    } else {
1255        current_price.saturating_sub(price_change)
1256    }
1257}
1258
1259impl<Cfg: Config> module::BlockHandler for Module<Cfg> {
1260    fn begin_block<C: Context>(ctx: &C) {
1261        CurrentState::with(|state| {
1262            let epoch = ctx.epoch();
1263
1264            // Load previous epoch.
1265            let mut store = storage::PrefixStore::new(state.store(), &MODULE_NAME);
1266            let mut tstore = storage::TypedStore::new(&mut store);
1267            let previous_epoch: EpochTime = tstore.get(state::LAST_EPOCH).unwrap_or_default();
1268            if epoch != previous_epoch {
1269                tstore.insert(state::LAST_EPOCH, epoch);
1270            }
1271
1272            // Set the epoch changed key as needed.
1273            state
1274                .block_value(CONTEXT_KEY_EPOCH_CHANGED)
1275                .set(epoch != previous_epoch);
1276        });
1277    }
1278
1279    fn end_block<C: Context>(_ctx: &C) {
1280        let params = Self::params();
1281        if !params.dynamic_min_gas_price.enabled {
1282            return;
1283        }
1284
1285        // Update dynamic min gas price for next block, inspired by EIP-1559.
1286        //
1287        // Adjust the min gas price for each block based on the gas used in the previous block and the desired target
1288        // gas usage set by `target_block_gas_usage_percentage`.
1289        let gas_used = Self::used_batch_gas() as u128;
1290        let max_batch_gas = Self::max_batch_gas() as u128;
1291        let target_gas_used = max_batch_gas.saturating_mul(
1292            params
1293                .dynamic_min_gas_price
1294                .target_block_gas_usage_percentage as u128,
1295        ) / 100;
1296
1297        // Compute new prices.
1298        let mut mgp = Self::min_gas_prices();
1299        mgp.iter_mut().for_each(|(d, price)| {
1300            let mut new_min_price = min_gas_price_update(
1301                gas_used,
1302                target_gas_used,
1303                params
1304                    .dynamic_min_gas_price
1305                    .min_price_max_change_denominator as u128,
1306                *price,
1307            );
1308
1309            // Ensure that the new price is at least the minimum gas price.
1310            if let Some(min_price) = params.min_gas_price.get(d) {
1311                if new_min_price < *min_price {
1312                    new_min_price = *min_price;
1313                }
1314            }
1315            *price = new_min_price;
1316        });
1317
1318        // Update min prices.
1319        CurrentState::with_store(|store| {
1320            let mut store = storage::PrefixStore::new(store, &MODULE_NAME);
1321            let mut tstore = storage::TypedStore::new(&mut store);
1322            tstore.insert(state::DYNAMIC_MIN_GAS_PRICE, mgp);
1323        });
1324    }
1325}
1326
1327impl<Cfg: Config> module::InvariantHandler for Module<Cfg> {}