Skip to content

Commit

Permalink
adding gas charging and review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-aptos committed Oct 4, 2024
1 parent 1cf338a commit 68bb8e0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,8 @@ crate::gas_schedule::macros::define_gas_parameters!(
[object_exists_at_per_item_loaded: InternalGas, { 7.. => "object.exists_at.per_item_loaded" }, 1470],
[string_utils_base: InternalGas, { 8.. => "string_utils.format.base" }, 1102],
[string_utils_per_byte: InternalGasPerByte, { 8.. =>"string_utils.format.per_byte" }, 3],

[cmp_compare_base: InternalGas, "cmp.base", 367],
[cmp_compare_per_abs_val_unit: InternalGasPerAbstractValueUnit, "cmp.per_abs_val_unit", 14],
]
);
7 changes: 7 additions & 0 deletions aptos-move/aptos-native-interface/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ impl<'a, 'b, 'c, 'd> SafeNativeContext<'a, 'b, 'c, 'd> {
.abstract_value_size(val, self.gas_feature_version)
}

/// Computes the abstract size of the input value.
pub fn abs_val_size_dereferenced(&self, val: &Value) -> AbstractValueSize {
self.misc_gas_params
.abs_val
.abstract_value_size_dereferenced(val, self.gas_feature_version)
}

/// Returns the current gas feature version.
pub fn gas_feature_version(&self) -> u64 {
self.gas_feature_version
Expand Down
10 changes: 7 additions & 3 deletions aptos-move/framework/src/natives/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

//! Implementation of native functions for utf8 strings.
use aptos_gas_schedule::gas_params::natives::aptos_framework::{CMP_COMPARE_BASE, CMP_COMPARE_PER_ABS_VAL_UNIT};
use aptos_native_interface::{
RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeError, SafeNativeResult,
};
Expand All @@ -33,7 +34,7 @@ use std::collections::VecDeque;
*
**************************************************************************************************/
fn native_compare_impl(
_context: &mut SafeNativeContext,
context: &mut SafeNativeContext,
_ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
Expand All @@ -44,8 +45,11 @@ fn native_compare_impl(
)));
}

// TODO: how to charge same cost as equality?
// context.charge()?;
let cost = CMP_COMPARE_BASE
+ CMP_COMPARE_PER_ABS_VAL_UNIT
* (context.abs_val_size_dereferenced(&args[0])
+ context.abs_val_size_dereferenced(&args[1]));
context.charge(cost)?;

let ordering = args[0].compare(&args[1])?;
let result = match ordering {
Expand Down
8 changes: 2 additions & 6 deletions third_party/move/move-vm/types/src/values/values_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,7 @@ impl Container {
}
}

let len_cmp = l.len().cmp(&r.len());
if len_cmp.is_ne() {
return Ok(len_cmp);
}
Ordering::Equal
l.len().cmp(&r.len())
},
(VecU8(l), VecU8(r)) => l.borrow().cmp(&*r.borrow()),
(VecU16(l), VecU16(r)) => l.borrow().cmp(&*r.borrow()),
Expand Down Expand Up @@ -839,7 +835,7 @@ impl IndexedRef {
(VecBool(r1), VecBool(r2)) => r1.borrow()[self.idx].cmp(&r2.borrow()[other.idx]),
(VecAddress(r1), VecAddress(r2)) => r1.borrow()[self.idx].cmp(&r2.borrow()[other.idx]),

// Equality between a generic and a specialized container.
// Comparison between a generic and a specialized container.
(Locals(r1), VecU8(r2)) | (Struct(r1), VecU8(r2)) => r1.borrow()[self.idx]
.as_value_ref::<u8>()?
.cmp(&r2.borrow()[other.idx]),
Expand Down

0 comments on commit 68bb8e0

Please sign in to comment.