Skip to content

Commit

Permalink
randomness native gas (#15078)
Browse files Browse the repository at this point in the history
* randomness native gas

* lint
  • Loading branch information
zjma authored Oct 30, 2024
1 parent 2d6f3f5 commit 0b4ae7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{
gas_feature_versions::{RELEASE_V1_14, RELEASE_V1_8, RELEASE_V1_9_SKIPPED},
gas_schedule::NativeGasParameters,
ver::gas_feature_versions::{RELEASE_V1_12, RELEASE_V1_13},
ver::gas_feature_versions::{RELEASE_V1_12, RELEASE_V1_13, RELEASE_V1_23},
};
use aptos_gas_algebra::{
InternalGas, InternalGasPerAbstractValueUnit, InternalGasPerArg, InternalGasPerByte,
Expand Down Expand Up @@ -322,5 +322,7 @@ 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],

[randomness_fetch_and_inc_counter: InternalGas, { RELEASE_V1_23.. => "randomness.fetch_and_inc_counter" }, 1],
]
);
9 changes: 8 additions & 1 deletion aptos-move/framework/src/natives/randomness.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use aptos_gas_schedule::{
gas_feature_versions::RELEASE_V1_23,
gas_params::natives::aptos_framework::RANDOMNESS_FETCH_AND_INC_COUNTER,
};
use aptos_native_interface::{
RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeError, SafeNativeResult,
};
Expand Down Expand Up @@ -55,14 +59,17 @@ pub fn fetch_and_increment_txn_counter(
_ty_args: Vec<Type>,
_args: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
if context.gas_feature_version() >= RELEASE_V1_23 {
context.charge(RANDOMNESS_FETCH_AND_INC_COUNTER)?;
}

let ctx = context.extensions_mut().get_mut::<RandomnessContext>();
if !ctx.is_unbiasable() {
return Err(SafeNativeError::Abort {
abort_code: E_API_USE_SUSCEPTIBLE_TO_TEST_AND_ABORT,
});
}

// TODO: charge gas?
let ret = ctx.txn_local_state.to_vec();
ctx.increment();
Ok(smallvec![Value::vector_u8(ret)])
Expand Down

0 comments on commit 0b4ae7e

Please sign in to comment.