Skip to content

Conversation

georgemitenkov
Copy link
Contributor

@georgemitenkov georgemitenkov commented Sep 10, 2025

Description

Small PR to unify change set size tracking and refund calculation via ChangeSetSizeAndRefundTracker. The existing flow has not been changed - when ChangeSetConfigs check change set size, we create a legacy version of ChangeSetSizeAndRefundTracker (no refund calculations) and counts ops/events via same logic.

In the new session model, the flow is:

    fn charge_gas_for_state_changes(
        // self here is a new session structure
        &mut self,
        gas_meter: &mut impl AptosGasMeter,
    ) -> Result<Fee, VMStatus> {
        self.session.materialize_writes(false)?;

        let change_set_configs = &self.session.storage_gas_params.change_set_configs;
        let pricing = &self.session.storage_gas_params.space_pricing;
        let params = &self.session.gas_params.vm.txn;
        let mut size_tracker =
            ChangeSetSizeTracker::new(change_set_configs, pricing, params);

        gas_meter.charge_io_gas_for_transaction(self.txn_metadata.transaction_size())?;

        let event_context = self.session.extensions.get::<NativeEventContext>();
        for event in event_context.events_iter() {
            size_tracker.count_event(event)?;
            gas_meter.charge_io_gas_for_event(event)?;
            size_tracker.record_storage_fee_event(event)?;
        }

        let aggregator_context = self.session.extensions.get::<NativeAggregatorContext>();
        aggregator_context.charge_and_refund_write_ops(&mut size_tracker, gas_meter)?;

        let table_context = self.session.extensions.get::<NativeTableContext>();
        table_context.charge_and_refund_write_ops(&mut size_tracker, gas_meter)?;

        self.session
            .data_cache
            .charge_and_refund_write_ops(&mut size_tracker, gas_meter)?;

        let StorageFeeReceipt {
            fee,
            refund,
        } = size_tracker.calculate_storage_fee(self.txn_metadata.transaction_size())?;
        gas_meter
            .charge_storage_fee(fee, self.txn_metadata.gas_unit_price())
            .map_err(|err| err.finish(Location::Undefined))?;

        Ok(refund)
    }

This way for each versioned value in caches, we compute its metadata and size once, and patch it with refund creation times accordingly.

How Has This Been Tested?

Key Areas to Review

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Performance improvement
  • Refactoring
  • Dependency update
  • Documentation update
  • Tests

Which Components or Systems Does This Change Impact?

  • Validator Node
  • Full Node (API, Indexer, etc.)
  • Move/Aptos Virtual Machine
  • Aptos Framework
  • Aptos CLI/SDK
  • Developer Infrastructure
  • Move Compiler
  • Other (specify)

Checklist

  • I have read and followed the CONTRIBUTING doc
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I identified and added all stakeholders and component owners affected by this change as reviewers
  • I tested both happy and unhappy path of the functionality
  • I have made corresponding changes to the documentation

Copy link
Contributor Author

georgemitenkov commented Sep 10, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant