Skip to main content

sui_adapter_latest/static_programmable_transactions/metering/typing/
mod.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    execution_mode::ExecutionMode,
6    static_programmable_transactions::{
7        metering::translation_meter::TranslationMeter, typing::ast as T,
8    },
9};
10use sui_protocol_config::ProtocolConfig;
11use sui_types::base_types::TxContextKind;
12
13mod live_references;
14
15/// After loading and type checking, we do a second pass over the typed transaction to charge for
16/// type-related properties (before further analysis is done):
17/// - number of type nodes (including nested)
18/// - number of type references. These are charged non-linearly
19/// - number of references live at each command. These are charged non-linearly and limited, along
20///   with the number of references returned by each command. See `live_references` module.
21pub fn meter<Mode: ExecutionMode>(
22    meter: &mut TranslationMeter,
23    protocol_config: &ProtocolConfig,
24    transaction: &T::Transaction,
25) -> Result<(), Mode::Error> {
26    let mut num_refs: u64 = 0;
27    let mut num_nodes: u64 = 0;
28
29    for ty in transaction.types() {
30        if ty.is_reference() && ty.is_tx_context() == TxContextKind::None {
31            num_refs = num_refs.saturating_add(1);
32        }
33        num_nodes = num_nodes.saturating_add(ty.node_count());
34    }
35
36    meter.charge_num_type_nodes(num_nodes)?;
37    meter.charge_num_type_references(num_refs)?;
38    if protocol_config
39        .max_ptb_live_references_as_option()
40        .is_some()
41    {
42        live_references::meter::<Mode::Error>(meter, protocol_config, transaction)?;
43    }
44    Ok(())
45}