Skip to content

Commit

Permalink
Merge pull request #4 from initia-labs/feat/statistics-for-gas-consum…
Browse files Browse the repository at this point in the history
…ption

feat: enable statistic and use initia gas meter for unit test
  • Loading branch information
beer-1 authored Mar 7, 2024
2 parents 9dbf084 + 0cf2c91 commit 78f4615
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 66 deletions.
58 changes: 29 additions & 29 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,35 @@ bigdecimal = "0.4"
# Note: the BEGIN and END comments below are required for external tooling. Do not remove.
# BEGIN MOVE DEPENDENCIES

# move-abigen = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-bytecode-utils = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-errmapgen = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-ir-compiler = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-prover-test-utils = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-stackless-bytecode-interpreter = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-transactional-test-runner = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# read-write-set = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# read-write-set-dynamic = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-binary-format = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-bytecode-verifier = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-cli = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-command-line-common = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-compiler = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-core-types = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-docgen = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-model = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-package = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-prover = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-prover-boogie-backend = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-prover-bytecode-pipeline = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-resource-viewer = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-stackless-bytecode = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-stdlib = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-symbol-pool = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-unit-test = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-vm-runtime = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-vm-test-utils = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
move-vm-types = { git = "https://github.com/initia-labs/move.git", rev = "c27b20c33a8dea33716bf3da931ca8229ac88e00" }
# move-abigen = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# move-bytecode-utils = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# move-errmapgen = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# move-ir-compiler = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# move-prover-test-utils = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# move-stackless-bytecode-interpreter = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# move-transactional-test-runner = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# read-write-set = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
# read-write-set-dynamic = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-binary-format = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-bytecode-verifier = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-cli = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-command-line-common = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-compiler = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-core-types = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-docgen = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-model = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-package = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-prover = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-prover-boogie-backend = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-prover-bytecode-pipeline = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-resource-viewer = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-stackless-bytecode = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-stdlib = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-symbol-pool = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-unit-test = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-vm-runtime = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-vm-test-utils = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }
move-vm-types = { git = "https://github.com/initia-labs/move.git", rev = "e589831fb47c909baebc907753a2f1e8423cdd70" }

# END MOVE DEPENDENCIES

Expand Down
23 changes: 16 additions & 7 deletions crates/compiler/src/test_package.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::path::PathBuf;

use anyhow::bail;
use initia_move_gas::{MiscGasParameters, NativeGasParameters};
use initia_move_gas::{
Gas, InitiaGasMeter, InitiaGasParameters, InitialGasSchedule, MiscGasParameters,
NativeGasParameters,
};
use initia_move_natives::all_natives;
use initia_move_types::metadata;
use move_cli::base::{
coverage::{Coverage, CoverageSummaryOptions},
test::{run_move_unit_tests, Test, UnitTestResult},
test::{run_move_unit_tests_with_gas_meter, Test, UnitTestResult},
};
use move_core_types::effects::ChangeSet;
use move_package::BuildConfig;
Expand All @@ -31,23 +34,29 @@ impl TestPackage {

configure_for_unit_test();

let gas_params = NativeGasParameters::zeros();
let misc_gas_params = MiscGasParameters::zeros();
let result = run_move_unit_tests(
let gas_limit = 1_000_000_000u64;
let gas_params = InitiaGasParameters::initial();
let gas_meter = InitiaGasMeter::new(gas_params, Gas::new(gas_limit));

let native_gas_params = NativeGasParameters::initial();
let misc_gas_params = MiscGasParameters::initial();
let result = run_move_unit_tests_with_gas_meter(
&self.package_path,
new_build_config,
UnitTestingConfig {
filter: self.test_config.filter.clone(),
report_stacktrace_on_abort: true,
report_statistics: self.test_config.report_statistics,
report_storage_on_error: self.test_config.report_storage_on_error,
ignore_compile_warnings: self.test_config.ignore_compile_warnings,
..UnitTestingConfig::default_with_bound(None)
..UnitTestingConfig::default_with_bound(Some(gas_limit))
},
all_natives(gas_params, misc_gas_params),
all_natives(native_gas_params, misc_gas_params),
ChangeSet::new(),
None,
self.test_config.compute_coverage,
&mut std::io::stdout(),
Some(gas_meter),
)?;

if self.test_config.compute_coverage {
Expand Down
33 changes: 18 additions & 15 deletions crates/e2e-move-tests/src/tests/move_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
// SPDX-License-Identifier: BUSL-1.1

use crate::test_utils::mock_chain::{BlankAPIImpl, BlankTableViewImpl};
use initia_move_gas::NativeGasParameters;
use initia_move_gas::{
Gas, InitiaGasMeter, InitiaGasParameters, InitialGasSchedule, MiscGasParameters,
NativeGasParameters,
};
use initia_move_natives::{
account::NativeAccountContext, all_natives, block::NativeBlockContext, code::NativeCodeContext,
cosmos::NativeCosmosContext, event::NativeEventContext, oracle::NativeOracleContext,
staking::NativeStakingContext, table::NativeTableContext,
transaction_context::NativeTransactionContext,
};
use move_cli::base::test::{run_move_unit_tests, UnitTestResult};
use move_cli::base::test::{run_move_unit_tests_with_gas_meter, UnitTestResult};
use move_core_types::effects::ChangeSet;
use move_unit_test::UnitTestingConfig;
use move_vm_runtime::{
native_extensions::NativeContextExtensions, native_functions::NativeFunctionTable,
};
use move_vm_runtime::native_extensions::NativeContextExtensions;
use std::path::PathBuf;
use tempfile::tempdir;

Expand All @@ -42,30 +43,32 @@ fn unit_test_extensions_hook(exts: &mut NativeContextExtensions) {
exts.add(NativeOracleContext::new(&BLANK_API.oracle_api));
}

fn initia_move_test_natives() -> NativeFunctionTable {
configure_for_unit_test();
let gas_params = NativeGasParameters::zeros();
let misc_gas_params = initia_move_gas::MiscGasParameters::zeros();
all_natives(gas_params, misc_gas_params)
}

fn run_tests_for_pkg(path_to_pkg: impl Into<String>) {
let pkg_path = path_in_crate(path_to_pkg);

let res = run_move_unit_tests(
configure_for_unit_test();

let gas_limit = 1_000_000_000u64;
let gas_params = InitiaGasParameters::initial();
let gas_meter = InitiaGasMeter::new(gas_params, Gas::new(gas_limit));

let native_gas_params = NativeGasParameters::initial();
let misc_gas_params = MiscGasParameters::initial();
let res = run_move_unit_tests_with_gas_meter(
&pkg_path,
move_package::BuildConfig {
test_mode: true,
install_dir: Some(tempdir().unwrap().path().to_path_buf()),
..Default::default()
},
UnitTestingConfig::default_with_bound(Some(100_000)),
initia_move_test_natives(),
UnitTestingConfig::default_with_bound(Some(gas_limit)),
all_natives(native_gas_params, misc_gas_params),
ChangeSet::new(),
// TODO(Gas): we may want to switch to non-zero costs in the future
None,
/* compute_coverage */ false,
&mut std::io::stdout(),
Some(gas_meter),
)
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions crates/gas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ move-core-types = { workspace = true }
move-binary-format = { workspace = true }
move-stdlib = { workspace = true }
move-vm-types = { workspace = true }
move-vm-test-utils = { workspace = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion crates/gas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use algebra::*;
pub use instr::InstructionGasParameters;
pub use meter::{
FromOnChainGasSchedule, InitiaGasMeter, InitiaGasParameters, InitialGasSchedule,
NativeGasParameters, ToOnChainGasSchedule,
NativeGasParameters, ToOnChainGasSchedule, GAS_UNIT_SCALING_FACTOR,
};
pub use misc::{AbstractValueSizeGasParameters, MiscGasParameters};
pub use move_core_types::gas_algebra::{
Expand Down
27 changes: 21 additions & 6 deletions crates/gas/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use move_core_types::{
language_storage::ModuleId,
vm_status::StatusCode,
};
use move_vm_test_utils::gas_schedule::TestGasMeter;
use move_vm_types::{
gas::{GasMeter, SimpleInstruction},
views::{TypeView, ValueView},
Expand All @@ -34,7 +35,7 @@ use std::collections::BTreeMap;
///
/// Cosmos gas is 100x bigger than Aptos gas unit
///
pub(crate) const GAS_UNIT_SCALING_FACTOR: u64 = 100;
pub const GAS_UNIT_SCALING_FACTOR: u64 = 100;

/// A trait for converting from a map representation of the on-chain gas schedule.
pub trait FromOnChainGasSchedule: Sized {
Expand Down Expand Up @@ -80,7 +81,7 @@ impl ToOnChainGasSchedule for NativeGasParameters {
fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)> {
let mut entries = self.move_stdlib.to_on_chain_gas_schedule();
entries.extend(self.initia_stdlib.to_on_chain_gas_schedule());
// entries.extend(self.table.to_on_chain_gas_schedule());
entries.extend(self.table.to_on_chain_gas_schedule());
entries
}
}
Expand Down Expand Up @@ -163,6 +164,14 @@ impl InitialGasSchedule for InitiaGasParameters {
}
}

#[derive(Clone)]
struct Frame {
module_id: ModuleId,
start_gas: InternalGas, /* start_gas */
call_gas: InternalGas, /* call_gas which is gas_used during inner call */
}

#[derive(Clone)]
/// The official gas meter used inside the Initia VM.
/// It maintains an internal gas counter, measured in internal gas units, and carries an environment
/// consisting all the gas parameters, which it can lookup when performing gas calcuations.
Expand All @@ -185,10 +194,16 @@ pub struct InitiaGasMeter {
total_dependency_size: NumBytes,
}

struct Frame {
module_id: ModuleId,
start_gas: InternalGas, /* start_gas */
call_gas: InternalGas, /* call_gas which is gas_used during inner call */
// gas meter required for testing gas metering
impl TestGasMeter for InitiaGasMeter {
fn instantiate(&self) -> Self {
self.clone()
}

fn remaining_gas(&self) -> move_vm_test_utils::gas_schedule::Gas {
let remaining_gas: u64 = self.balance().into();
remaining_gas.into()
}
}

impl InitiaGasMeter {
Expand Down
6 changes: 2 additions & 4 deletions crates/gas/src/table.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::gas_params::table::*;

crate::natives::define_gas_parameters_for_natives!(GasParameters, "table", [
// Note(Gas): These are legacy parameters for loading from storage so they do not
// need to be multiplied.
[.common.load_base, "common.load.base", 302385],
[.common.load_per_byte, "common.load.per_byte", 151],
[.common.load_base, "common.load.base", 4411],
[.common.load_per_byte, "common.load.per_byte", 36],
[.common.load_failure, "common.load.failure", 0],

[.new_table_handle.base, "new_table_handle.base", 3676],
Expand Down
5 changes: 1 addition & 4 deletions crates/natives/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use better_any::{Tid, TidAble};
use initia_move_gas::InternalGas;
use initia_move_gas::{InternalGas, GAS_UNIT_SCALING_FACTOR};
use initia_move_types::query::*;
use move_binary_format::errors::PartialVMError;
use move_core_types::gas_algebra::NumBytes;
Expand All @@ -19,9 +19,6 @@ use crate::{
safely_pop_arg,
};

// defined in initia_move_gas::meter
const GAS_UNIT_SCALING_FACTOR: u64 = 100;

// See stdlib/error.move
const ECATEGORY_INVALID_ARGUMENT: u64 = 0x1;

Expand Down

0 comments on commit 78f4615

Please sign in to comment.