-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
use aptos_native_interface::{ | ||
safely_pop_arg, RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeResult, | ||
}; | ||
use aptos_types::on_chain_config::OnChainConsensusConfig; | ||
use move_vm_runtime::native_functions::NativeFunction; | ||
use move_vm_types::{loaded_data::runtime_types::Type, values::Value}; | ||
use smallvec::{smallvec, SmallVec}; | ||
use std::collections::VecDeque; | ||
|
||
pub fn validator_txn_enabled( | ||
_context: &mut SafeNativeContext, | ||
_ty_args: Vec<Type>, | ||
mut args: VecDeque<Value>, | ||
) -> SafeNativeResult<SmallVec<[Value; 1]>> { | ||
let config_bytes = safely_pop_arg!(args, Vec<u8>); | ||
let config = bcs::from_bytes::<OnChainConsensusConfig>(&config_bytes).unwrap_or_default(); | ||
Ok(smallvec![Value::bool(config.is_vtxn_enabled())]) | ||
} | ||
|
||
pub fn make_all( | ||
builder: &SafeNativeBuilder, | ||
) -> impl Iterator<Item = (String, NativeFunction)> + '_ { | ||
let natives = vec![( | ||
"validator_txn_enabled_internal", | ||
validator_txn_enabled as RawSafeNative, | ||
)]; | ||
|
||
builder.make_named_natives(natives) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
use aptos_native_interface::{ | ||
RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeResult, | ||
}; | ||
use better_any::{Tid, TidAble}; | ||
use move_vm_runtime::native_functions::NativeFunction; | ||
use move_vm_types::{loaded_data::runtime_types::Type, values::Value}; | ||
use smallvec::{smallvec, SmallVec}; | ||
use std::collections::VecDeque; | ||
|
||
/// A txn-local counter that increments each time a random 32-byte blob is requested. | ||
#[derive(Tid, Default)] | ||
pub struct RandomnessContext { | ||
txn_local_state: Vec<u8>, // 8-byte counter | ||
} | ||
|
||
impl RandomnessContext { | ||
pub fn new() -> Self { | ||
Self { | ||
txn_local_state: vec![0; 8], | ||
} | ||
} | ||
|
||
pub fn increment(&mut self) { | ||
for byte in self.txn_local_state.iter_mut() { | ||
if *byte < 255 { | ||
*byte += 1; | ||
break; | ||
} else { | ||
*byte = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn fetch_and_increment_txn_counter( | ||
context: &mut SafeNativeContext, | ||
_ty_args: Vec<Type>, | ||
_args: VecDeque<Value>, | ||
) -> SafeNativeResult<SmallVec<[Value; 1]>> { | ||
// TODO: charge gas? | ||
let rand_ctxt = context.extensions_mut().get_mut::<RandomnessContext>(); | ||
let ret = rand_ctxt.txn_local_state.to_vec(); | ||
rand_ctxt.increment(); | ||
Ok(smallvec![Value::vector_u8(ret)]) | ||
} | ||
|
||
pub fn make_all( | ||
builder: &SafeNativeBuilder, | ||
) -> impl Iterator<Item = (String, NativeFunction)> + '_ { | ||
let natives = vec![( | ||
"fetch_and_increment_txn_counter", | ||
fetch_and_increment_txn_counter as RawSafeNative, | ||
)]; | ||
|
||
builder.make_named_natives(natives) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters