Skip to content

Commit

Permalink
Reintroduce randomness precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenGround0 committed Aug 26, 2024
1 parent 6906288 commit a567c8a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
28 changes: 28 additions & 0 deletions actors/evm/src/interpreter/precompiles/fvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,31 @@ pub(super) fn call_actor_shared<RT: Runtime>(

Ok(output)
}

/// Params:
///
/// | Param | Value |
/// |------------------|---------------------------|
/// | randomness_epoch | U256 - low i64 |
/// | entropy_length | U256 - low u32 |
/// | entropy | input\[32..] (right padded)|
///
/// any bytes in between values are ignored
///
/// Returns empty array if invalid randomness type
/// Errors if unable to fetch randomness
pub(super) fn get_randomness<RT: Runtime>(
system: &mut System<RT>,
input: &[u8],
_: PrecompileContext,
) -> PrecompileResult {

let mut input_params = ValueReader::new(input);

let randomness_epoch = input_params.read_value()?;
let entropy_length: u32 = input_params.read_value()?;
let entropy = input_params.read_padded(entropy_length.try_into().unwrap_or(0));

let randomness = system.rt.get_randomness_from_beacon(fil_actors_runtime::runtime::DomainSeparationTag::EvmRandPrecompile, randomness_epoch, &entropy);
randomness.map(|r| r.to_vec()).map_err(|_| PrecompileError::InvalidInput)
}
5 changes: 3 additions & 2 deletions actors/evm/src/interpreter/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod evm;
mod fvm;

use evm::{blake2f, ec_add, ec_mul, ec_pairing, ec_recover, identity, modexp, ripemd160, sha256};
use fvm::{call_actor, call_actor_id, lookup_delegated_address, resolve_address};
use fvm::{call_actor, call_actor_id, lookup_delegated_address, resolve_address, get_randomness};

type PrecompileFn<RT> = fn(&mut System<RT>, &[u8], PrecompileContext) -> PrecompileResult;
pub type PrecompileResult = Result<Vec<u8>, PrecompileError>;
Expand Down Expand Up @@ -41,12 +41,13 @@ pub struct Precompiles<RT>(PhantomData<RT>);

impl<RT: Runtime> Precompiles<RT> {
/// FEVM specific precompiles (0xfe prefix)
const NATIVE_PRECOMPILES: PrecompileTable<RT, 5> = PrecompileTable([
const NATIVE_PRECOMPILES: PrecompileTable<RT, 6> = PrecompileTable([
Some(resolve_address::<RT>), // 0xfe00..01
Some(lookup_delegated_address::<RT>), // 0xfe00..02
Some(call_actor::<RT>), // 0xfe00..03
None, // 0xfe00..04 DISABLED
Some(call_actor_id::<RT>), // 0xfe00..05
Some(get_randomness::<RT>), // 0xfe00..06
]);

/// EVM specific precompiles
Expand Down
1 change: 1 addition & 0 deletions runtime/src/runtime/randomness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum DomainSeparationTag {
MarketDealCronSeed = 8,
PoStChainCommit = 9,
EvmPrevRandao = 10,
EvmRandPrecompile = 11,
}

#[allow(unused)]
Expand Down

0 comments on commit a567c8a

Please sign in to comment.