Skip to content

Commit

Permalink
[gas] remove test_only and optional, and support versioned gas schedu…
Browse files Browse the repository at this point in the history
…le (#5794)

* [gas] remove test_only

* [gas] versioned gas schedule -- params

* [gas] versioned gas schedule -- natives
  • Loading branch information
vgao1996 authored Dec 18, 2022
1 parent 41bdd02 commit 7f74006
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 268 deletions.
5 changes: 3 additions & 2 deletions api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,14 +911,15 @@ impl Context {

let gas_schedule_params =
match GasScheduleV2::fetch_config(&storage_adapter).and_then(|gas_schedule| {
let feature_version = gas_schedule.feature_version;
let gas_schedule = gas_schedule.to_btree_map();
AptosGasParameters::from_on_chain_gas_schedule(&gas_schedule)
AptosGasParameters::from_on_chain_gas_schedule(&gas_schedule, feature_version)
}) {
Some(gas_schedule) => Ok(gas_schedule),
None => GasSchedule::fetch_config(&storage_adapter)
.and_then(|gas_schedule| {
let gas_schedule = gas_schedule.to_btree_map();
AptosGasParameters::from_on_chain_gas_schedule(&gas_schedule)
AptosGasParameters::from_on_chain_gas_schedule(&gas_schedule, 0)
})
.ok_or_else(|| {
E::internal_with_code(
Expand Down
18 changes: 9 additions & 9 deletions aptos-move/aptos-gas/src/aptos_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ crate::natives::define_gas_parameters_for_natives!(GasParameters, "aptos_framewo
[.hash.sip_hash.base, "hash.sip_hash.base", 1000 * MUL],
[.hash.sip_hash.per_byte, "hash.sip_hash.per_byte", 20 * MUL],

[.hash.keccak256.base, optional "hash.keccak256.base", 4000 * MUL],
[.hash.keccak256.per_byte, optional "hash.keccak256.per_byte", 45 * MUL],
[.hash.keccak256.base, { 1.. => "hash.keccak256.base" }, 4000 * MUL],
[.hash.keccak256.per_byte, { 1.. => "hash.keccak256.per_byte" }, 45 * MUL],

[.type_info.type_of.base, "type_info.type_of.base", 300 * MUL],
// TODO(Gas): the on-chain name is wrong...
[.type_info.type_of.per_byte_in_str, "type_info.type_of.per_abstract_memory_unit", 5 * MUL],
[.type_info.type_name.base, "type_info.type_name.base", 300 * MUL],
// TODO(Gas): the on-chain name is wrong...
[.type_info.type_name.per_byte_in_str, "type_info.type_name.per_abstract_memory_unit", 5 * MUL],
[.type_info.chain_id.base, optional "type_info.chain_id.base", 150 * MUL],
[.type_info.chain_id.base, { 4.. => "type_info.chain_id.base" }, 150 * MUL],

// Reusing SHA2-512's cost from Ristretto
[.hash.sha2_512.base, optional "hash.sha2_512.base", 3_240],
[.hash.sha2_512.per_byte, optional "hash.sha2_512.per_byte", 60],
[.hash.sha2_512.base, { 4.. => "hash.sha2_512.base" }, 3_240],
[.hash.sha2_512.per_byte, { 4.. => "hash.sha2_512.per_byte" }, 60],
// Back-of-the-envelop approximation from SHA3-256's (4000 base, 45 per-byte) costs
[.hash.sha3_512.base, optional "hash.sha3_512.base", 4_500],
[.hash.sha3_512.per_byte, optional "hash.sha3_512.per_byte", 50],
[.hash.sha3_512.base, { 4.. => "hash.sha3_512.base" }, 4_500],
[.hash.sha3_512.per_byte, { 4.. => "hash.sha3_512.per_byte" }, 50],
// Using SHA2-256's cost
[.hash.ripemd160.base, optional "hash.ripemd160.base", 3000],
[.hash.ripemd160.per_byte, optional "hash.ripemd160.per_byte", 50],
[.hash.ripemd160.base, { 4.. => "hash.ripemd160.base" }, 3000],
[.hash.ripemd160.per_byte, { 4.. => "hash.ripemd160.per_byte" }, 50],

[.util.from_bytes.base, "util.from_bytes.base", 300 * MUL],
[.util.from_bytes.per_byte, "util.from_bytes.per_byte", 5 * MUL],
Expand Down
70 changes: 50 additions & 20 deletions aptos-move/aptos-gas/src/gas_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ pub trait FromOnChainGasSchedule: Sized {
/// Constructs a value of this type from a map representation of the on-chain gas schedule.
/// `None` should be returned when the gas schedule is missing some required entries.
/// Unused entries should be safely ignored.
fn from_on_chain_gas_schedule(gas_schedule: &BTreeMap<String, u64>) -> Option<Self>;
fn from_on_chain_gas_schedule(
gas_schedule: &BTreeMap<String, u64>,
feature_version: u64,
) -> Option<Self>;
}

/// A trait for converting to a list of entries of the on-chain gas schedule.
pub trait ToOnChainGasSchedule {
/// Converts `self` into a list of entries of the on-chain gas schedule.
/// Each entry is a key-value pair where the key is a string representing the name of the
/// parameter, where the value is the gas parameter itself.
fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)>;
fn to_on_chain_gas_schedule(&self, feature_version: u64) -> Vec<(String, u64)>;
}

/// A trait for defining an initial value to be used in the genesis.
Expand All @@ -81,20 +84,35 @@ pub struct NativeGasParameters {
}

impl FromOnChainGasSchedule for NativeGasParameters {
fn from_on_chain_gas_schedule(gas_schedule: &BTreeMap<String, u64>) -> Option<Self> {
fn from_on_chain_gas_schedule(
gas_schedule: &BTreeMap<String, u64>,
feature_version: u64,
) -> Option<Self> {
Some(Self {
move_stdlib: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
aptos_framework: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
table: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
move_stdlib: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
aptos_framework: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
table: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
})
}
}

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.aptos_framework.to_on_chain_gas_schedule());
entries.extend(self.table.to_on_chain_gas_schedule());
fn to_on_chain_gas_schedule(&self, feature_version: u64) -> Vec<(String, u64)> {
let mut entries = self.move_stdlib.to_on_chain_gas_schedule(feature_version);
entries.extend(
self.aptos_framework
.to_on_chain_gas_schedule(feature_version),
);
entries.extend(self.table.to_on_chain_gas_schedule(feature_version));
entries
}
}
Expand Down Expand Up @@ -130,22 +148,34 @@ pub struct AptosGasParameters {
}

impl FromOnChainGasSchedule for AptosGasParameters {
fn from_on_chain_gas_schedule(gas_schedule: &BTreeMap<String, u64>) -> Option<Self> {
fn from_on_chain_gas_schedule(
gas_schedule: &BTreeMap<String, u64>,
feature_version: u64,
) -> Option<Self> {
Some(Self {
misc: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
instr: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
txn: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
natives: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
misc: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
instr: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
txn: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule, feature_version)?,
natives: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
})
}
}

impl ToOnChainGasSchedule for AptosGasParameters {
fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)> {
let mut entries = self.instr.to_on_chain_gas_schedule();
entries.extend(self.txn.to_on_chain_gas_schedule());
entries.extend(self.natives.to_on_chain_gas_schedule());
entries.extend(self.misc.to_on_chain_gas_schedule());
fn to_on_chain_gas_schedule(&self, feature_version: u64) -> Vec<(String, u64)> {
let mut entries = self.instr.to_on_chain_gas_schedule(feature_version);
entries.extend(self.txn.to_on_chain_gas_schedule(feature_version));
entries.extend(self.natives.to_on_chain_gas_schedule(feature_version));
entries.extend(self.misc.to_on_chain_gas_schedule(feature_version));
entries
}
}
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/aptos-gas/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::path::{Path, PathBuf};
pub fn current_gas_schedule() -> GasScheduleV2 {
GasScheduleV2 {
feature_version: LATEST_GAS_FEATURE_VERSION,
entries: AptosGasParameters::initial().to_on_chain_gas_schedule(),
entries: AptosGasParameters::initial().to_on_chain_gas_schedule(LATEST_GAS_FEATURE_VERSION),
}
}

Expand Down
16 changes: 8 additions & 8 deletions aptos-move/aptos-gas/src/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ crate::params::define_gas_parameters!(
// stack
[pop: InternalGas, "pop", 40 * MUL],
[ld_u8: InternalGas, "ld_u8", 60 * MUL],
[ld_u16: InternalGas, optional "ld_u16", 60 * MUL],
[ld_u32: InternalGas, optional "ld_u32", 60 * MUL],
[ld_u16: InternalGas, { 5.. => "ld_u16" }, 60 * MUL],
[ld_u32: InternalGas, { 5.. => "ld_u32" }, 60 * MUL],
[ld_u64: InternalGas, "ld_u64", 60 * MUL],
[ld_u128: InternalGas, "ld_u128", 80 * MUL],
[ld_u256: InternalGas, optional "ld_u256", 80 * MUL],
[ld_u256: InternalGas, { 5.. => "ld_u256" }, 80 * MUL],
[ld_true: InternalGas, "ld_true", 60 * MUL],
[ld_false: InternalGas, "ld_false", 60 * MUL],
[ld_const_base: InternalGas, "ld_const.base", 650 * MUL],
Expand Down Expand Up @@ -69,7 +69,7 @@ crate::params::define_gas_parameters!(
// call
[call_base: InternalGas, "call.base", 1000 * MUL],
[call_per_arg: InternalGasPerArg, "call.per_arg", 100 * MUL],
[call_per_local: InternalGasPerArg, optional "call.per_local", 100 * MUL],
[call_per_local: InternalGasPerArg, { 1.. => "call.per_local" }, 100 * MUL],
[call_generic_base: InternalGas, "call_generic.base", 1000 * MUL],
[
call_generic_per_ty_arg: InternalGasPerArg,
Expand All @@ -81,7 +81,7 @@ crate::params::define_gas_parameters!(
"call_generic.per_arg",
100 * MUL
],
[call_generic_per_local: InternalGasPerArg, optional "call_generic.per_local", 100 * MUL],
[call_generic_per_local: InternalGasPerArg, { 1.. => "call_generic.per_local" }, 100 * MUL],
// struct
[pack_base: InternalGas, "pack.base", 220 * MUL],
[pack_per_field: InternalGasPerArg, "pack.per_field", 40 * MUL],
Expand Down Expand Up @@ -110,11 +110,11 @@ crate::params::define_gas_parameters!(
[freeze_ref: InternalGas, "freeze_ref", 10 * MUL],
// casting
[cast_u8: InternalGas, "cast_u8", 120 * MUL],
[cast_u16: InternalGas, optional "cast_u16", 120 * MUL],
[cast_u32: InternalGas, optional "cast_u32", 120 * MUL],
[cast_u16: InternalGas, { 5.. => "cast_u16" }, 120 * MUL],
[cast_u32: InternalGas, { 5.. => "cast_u32" }, 120 * MUL],
[cast_u64: InternalGas, "cast_u64", 120 * MUL],
[cast_u128: InternalGas, "cast_u128", 120 * MUL],
[cast_u256: InternalGas, optional "cast_u256", 120 * MUL],
[cast_u256: InternalGas, { 5.. => "cast_u256" }, 120 * MUL],
// arithmetic
[add: InternalGas, "add", 160 * MUL],
[sub: InternalGas, "sub", 160 * MUL],
Expand Down
26 changes: 16 additions & 10 deletions aptos-move/aptos-gas/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ crate::params::define_gas_parameters!(
[
// abstract value size
[u8: AbstractValueSize, "u8", 40],
[u16: AbstractValueSize, optional "u16", 40],
[u32: AbstractValueSize, optional "u32", 40],
[u16: AbstractValueSize, { 5.. => "u16" }, 40],
[u32: AbstractValueSize, { 5.. => "u32" }, 40],
[u64: AbstractValueSize, "u64", 40],
[u128: AbstractValueSize, "u128", 40],
[u256: AbstractValueSize, optional "u256", 40],
[u256: AbstractValueSize, { 5.. => "u256" }, 40],
[bool: AbstractValueSize, "bool", 40],
[address: AbstractValueSize, "address", 40],
[struct_: AbstractValueSize, "struct", 40],
[vector: AbstractValueSize, "vector", 40],
[reference: AbstractValueSize, "reference", 40],
[per_u8_packed: AbstractValueSizePerArg, "per_u8_packed", 1],
[per_u16_packed: AbstractValueSizePerArg, optional "per_u16_packed", 2],
[per_u32_packed: AbstractValueSizePerArg, optional "per_u32_packed", 4],
[per_u16_packed: AbstractValueSizePerArg, { 5.. => "per_u16_packed" }, 2],
[per_u32_packed: AbstractValueSizePerArg, { 5.. => "per_u32_packed" }, 4],
[per_u64_packed: AbstractValueSizePerArg, "per_u64_packed", 8],
[
per_u128_packed: AbstractValueSizePerArg,
"per_u128_packed",
16
],
[per_u256_packed: AbstractValueSizePerArg, optional "per_u256_packed", 32],
[per_u256_packed: AbstractValueSizePerArg, { 5.. => "per_u256_packed" }, 32],
[
per_bool_packed: AbstractValueSizePerArg,
"per_bool_packed",
Expand Down Expand Up @@ -579,16 +579,22 @@ pub struct MiscGasParameters {
}

impl FromOnChainGasSchedule for MiscGasParameters {
fn from_on_chain_gas_schedule(gas_schedule: &BTreeMap<String, u64>) -> Option<Self> {
fn from_on_chain_gas_schedule(
gas_schedule: &BTreeMap<String, u64>,
feature_version: u64,
) -> Option<Self> {
Some(Self {
abs_val: FromOnChainGasSchedule::from_on_chain_gas_schedule(gas_schedule)?,
abs_val: FromOnChainGasSchedule::from_on_chain_gas_schedule(
gas_schedule,
feature_version,
)?,
})
}
}

impl ToOnChainGasSchedule for MiscGasParameters {
fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)> {
self.abs_val.to_on_chain_gas_schedule()
fn to_on_chain_gas_schedule(&self, feature_version: u64) -> Vec<(String, u64)> {
self.abs_val.to_on_chain_gas_schedule(feature_version)
}
}

Expand Down
13 changes: 7 additions & 6 deletions aptos-move/aptos-gas/src/move_stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
use crate::gas_meter::EXECUTION_GAS_MULTIPLIER as MUL;
use move_stdlib::natives::GasParameters;

#[cfg(all(test, not(feature = "testing")))]
const UNIT_TEST_ENTRIES: usize = 0;

#[cfg(all(test, feature = "testing"))]
const UNIT_TEST_ENTRIES: usize = 2;

crate::natives::define_gas_parameters_for_natives!(GasParameters, "move_stdlib", [
[.bcs.to_bytes.per_byte_serialized, "bcs.to_bytes.per_byte_serialized", 10 * MUL],
[.bcs.to_bytes.failure, "bcs.to_bytes.failure", 1000 * MUL],
Expand All @@ -25,9 +31,4 @@ crate::natives::define_gas_parameters_for_natives!(GasParameters, "move_stdlib",
[.string.index_of.base, "string.index_of.base", 400 * MUL],
[.string.index_of.per_byte_pattern, "string.index_of.per_byte_pattern", 20 * MUL],
[.string.index_of.per_byte_searched, "string.index_of.per_byte_searched", 10 * MUL],

// TODO(Gas): these should only be enabled when feature "testing" is present
// TODO(Gas): rename these in the move repo
[test_only .unit_test.create_signers_for_testing.base_cost, "unit_test.create_signers_for_testing.base", 1],
[test_only .unit_test.create_signers_for_testing.unit_cost, "unit_test.create_signers_for_testing.unit", 1]
], allow_unmapped = 1 /* bcs */ + 2 /* hash */ + 8 /* vector */ + 2 /* type_name */);
], allow_unmapped = 1 /* bcs */ + 2 /* hash */ + 8 /* vector */ + 2 /* type_name */ + UNIT_TEST_ENTRIES);
Loading

0 comments on commit 7f74006

Please sign in to comment.