Repository: aptos-labs/aptos-core
File: aptos-move/framework/src/natives/state_storage.rs (Rust)

Code before:

use aptos_gas_schedule::gas_params::natives::aptos_framework::*;
use aptos_native_interface::{
    RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeResult,
};
use aptos_types::{state_store::state_key::StateKey, vm_status::StatusCode};
use aptos_vm_types::resolver::StateStorageView;
use better_any::{Tid, TidAble};
use move_binary_format::errors::PartialVMError;
use move_vm_runtime::{
    native_extensions::VersionControlledNativeExtension, native_functions::NativeFunction,
};
use move_vm_types::{
    loaded_data::runtime_types::Type,
    values::{Struct, Value},
};
use smallvec::{smallvec, SmallVec};
use std::collections::VecDeque;

/// Exposes the ability to query state storage utilization info to native functions.
#[derive(Tid)]
pub struct NativeStateStorageContext<'a> {
    resolver: &'a dyn StateStorageView<Key = StateKey>,
}

impl<'a> VersionControlledNativeExtension for NativeStateStorageContext<'a> {
    fn undo(&mut self) {
        // No-op: nothing to undo.
    }

    fn save(&mut self) {
        // No-op: nothing to save.
    }

    fn reset(&mut self, _txn_hash: &[u8; 32], _script_hash: &[u8], _session_counter: u8) {
        // No-op: nothing to reset.
    }
}

impl<'a> NativeStateStorageContext<'a> {
    pub fn new(resolver: &'a dyn StateStorageView<Key = StateKey>) -> Self {
        Self { resolver }
    }
}

/***************************************************************************************************
 * native get_state_storage_usage_only_at_epoch_beginning
 *
 *   gas cost: base_cost
 *
 **************************************************************************************************/

Review comment (suggestion):
I wanted to ask for a comment why nothing to do/undo/etc, but going down the rabbit hole, why is this an extension, and any idea how does the comment below 

/// Warning: the result returned is based on the base state view held by the
/// VM for the entire block or chunk of transactions, it's only deterministic
/// if called from the first transaction of the block because the execution layer
/// guarantees a fresh state view then.

make sense?
+@zekun000 

Code after:

use aptos_gas_schedule::gas_params::natives::aptos_framework::*;
use aptos_native_interface::{
    RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeResult,
};
use aptos_types::{state_store::state_key::StateKey, vm_status::StatusCode};
use aptos_vm_types::resolver::StateStorageView;
use better_any::{Tid, TidAble};
use move_binary_format::errors::PartialVMError;
use move_vm_runtime::{native_extensions::SessionListener, native_functions::NativeFunction};
use move_vm_types::{
    loaded_data::runtime_types::Type,
    values::{Struct, Value},
};
use smallvec::{smallvec, SmallVec};
use std::collections::VecDeque;

/// Exposes the ability to query state storage utilization info to native functions.
#[derive(Tid)]
pub struct NativeStateStorageContext<'a> {
    resolver: &'a dyn StateStorageView<Key = StateKey>,
}

// This extension only carries a reference to pre-block view, so all session-related APIs are
// no-ops.
impl<'a> SessionListener for NativeStateStorageContext<'a> {
    fn start(&mut self, _session_hash: &[u8; 32], _script_hash: &[u8], _session_counter: u8) {}

    fn finish(&mut self) {}

    fn abort(&mut self) {}
}

impl<'a> NativeStateStorageContext<'a> {
    pub fn new(resolver: &'a dyn StateStorageView<Key = StateKey>) -> Self {
        Self { resolver }
    }
}

/***************************************************************************************************
 * native get_state_storage_usage_only_at_epoch_beginning
 *
 *   gas cost: base_cost
 *
 **************************************************************************************************/
/// Warning: the result returned is based on the base state view held by the
/// VM for the entire block or chunk of transactions, it's only deterministic
/// if called from the first transaction of the block because the execution layer
/// guarantees a fresh state view then.
fn native_get_usage(
    context: &mut SafeNativeContext,