Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce randomness precompile #1576

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 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,34 @@ pub(super) fn call_actor_shared<RT: Runtime>(

Ok(output)
}

/// Params:
///
/// | Param | Value |
/// |------------------|---------------------------|
/// | randomness_epoch | U256 - low i64 |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting

/// | 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure we want the unwrap or zero here? u32::MAX will turn into 0, I think we want to fail in that case.


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, get_randomness, lookup_delegated_address, resolve_address};

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: re-use 4? This sued to be 4.

]);

/// 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
Loading