sui_adapter_latest/static_programmable_transactions/metering/
typing.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::static_programmable_transactions::{
5    metering::translation_meter::TranslationMeter, typing::ast as T,
6};
7use sui_types::{base_types::TxContextKind, error::ExecutionError};
8
9/// After loading and type checking, we do a second pass over the typed transaction to charge for
10/// type-related properties (before further analysis is done):
11/// - number of type nodes (including nested)
12/// - number of type references. These are charged non-linearly
13pub fn meter(
14    meter: &mut TranslationMeter,
15    transaction: &T::Transaction,
16) -> Result<(), ExecutionError> {
17    let mut num_refs: u64 = 0;
18    let mut num_nodes: u64 = 0;
19
20    for ty in transaction.types() {
21        if ty.is_reference() && ty.is_tx_context() == TxContextKind::None {
22            num_refs = num_refs.saturating_add(1);
23        }
24        num_nodes = num_nodes.saturating_add(ty.node_count());
25    }
26
27    meter.charge_num_type_nodes(num_nodes)?;
28    meter.charge_num_type_references(num_refs)?;
29    Ok(())
30}