diff --git a/Cargo.lock b/Cargo.lock index 4b8ce5e03..d060135f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1793,11 +1793,13 @@ name = "fil_actors_runtime" version = "12.0.0" dependencies = [ "anyhow", + "base64 0.21.2", "blake2b_simd", "byteorder", "castaway", "cid 0.10.1", "derive_builder", + "fil_actors_runtime", "fvm_ipld_amt", "fvm_ipld_bitfield", "fvm_ipld_blockstore 0.2.0", @@ -2129,7 +2131,7 @@ dependencies = [ [[package]] name = "fvm_ipld_amt" version = "0.6.1" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "anyhow", "cid 0.10.1", @@ -2144,7 +2146,7 @@ dependencies = [ [[package]] name = "fvm_ipld_bitfield" version = "0.5.4" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "fvm_ipld_encoding 0.4.0", "serde", @@ -2166,7 +2168,7 @@ dependencies = [ [[package]] name = "fvm_ipld_blockstore" version = "0.2.0" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "anyhow", "cid 0.10.1", @@ -2208,7 +2210,7 @@ dependencies = [ [[package]] name = "fvm_ipld_encoding" version = "0.4.0" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "anyhow", "cid 0.10.1", @@ -2224,7 +2226,7 @@ dependencies = [ [[package]] name = "fvm_ipld_hamt" version = "0.7.0" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "anyhow", "byteorder", @@ -2261,7 +2263,7 @@ dependencies = [ [[package]] name = "fvm_sdk" version = "3.3.0" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "byteorder", "cid 0.10.1", @@ -2276,7 +2278,7 @@ dependencies = [ [[package]] name = "fvm_shared" version = "3.4.0" -source = "git+https://github.com/filecoin-project/ref-fvm?branch=next#af4269718ba99ec286ef33ec61960e5c5d82c657" +source = "git+https://github.com/filecoin-project/ref-fvm?branch=asr/stop-hashing-randomness#cc35c99d1f80497f313d2d9db06888bb904a1d3f" dependencies = [ "anyhow", "bitflags 2.3.3", diff --git a/Cargo.toml b/Cargo.toml index cca312792..23a8b8e00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -147,13 +147,13 @@ vm_api = { version = "1.0.0", path = "vm_api" } test_vm = { version = "12.0.0", path = "test_vm" } [patch.crates-io] -fvm_shared = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } -fvm_sdk = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } -fvm_ipld_hamt = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } -fvm_ipld_amt = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } -fvm_ipld_bitfield = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } -fvm_ipld_encoding = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } -fvm_ipld_blockstore = { git = "https://github.com/filecoin-project/ref-fvm", branch = "next" } +fvm_shared = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } +fvm_sdk = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } +fvm_ipld_hamt = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } +fvm_ipld_amt = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } +fvm_ipld_bitfield = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } +fvm_ipld_encoding = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } +fvm_ipld_blockstore = { git = "https://github.com/filecoin-project/ref-fvm", branch = "asr/stop-hashing-randomness" } #fvm_actor_utils = { git = "https://github.com/helix-onchain/filecoin", branch = "alex/update-fvm-ipld" } #frc42_dispatch = { git = "https://github.com/helix-onchain/filecoin", branch = "alex/update-fvm-ipld" } #frc46_token = { git = "https://github.com/helix-onchain/filecoin", branch = "alex/update-fvm-ipld" } diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index ddc9c6c4d..1ac4dbc68 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -44,6 +44,7 @@ blake2b_simd = { workspace = true, optional = true } hex = { workspace = true, optional = true } pretty_env_logger = { workspace = true, optional = true } rand = { workspace = true, optional = true } +base64 = "0.21.2" [dependencies.libsecp256k1] workspace = true @@ -54,6 +55,8 @@ optional = true derive_builder = { workspace = true } hex = { workspace = true } rand = { workspace = true } +# Enable the test_utils feature when testing. +fil_actors_runtime = { workspace = true, features = ["test_utils"] } [features] default = [] diff --git a/runtime/src/runtime/fvm.rs b/runtime/src/runtime/fvm.rs index 5ceb07d9b..da4e39e9f 100644 --- a/runtime/src/runtime/fvm.rs +++ b/runtime/src/runtime/fvm.rs @@ -33,10 +33,10 @@ use serde::Serialize; #[cfg(feature = "fake-proofs")] use sha2::{Digest, Sha256}; use std::cell::RefCell; -use std::io::Write; use crate::runtime::actor_blockstore::ActorBlockstore; use crate::runtime::builtins::Type; +use crate::runtime::randomness::draw_randomness; use crate::runtime::{ ActorCode, ConsensusFault, DomainSeparationTag, MessageInfo, Policy, Primitives, RuntimePolicy, Verifier, @@ -232,7 +232,7 @@ where rand_epoch: ChainEpoch, entropy: &[u8], ) -> Result<[u8; RANDOMNESS_LENGTH], ActorError> { - let digest = fvm::rand::get_chain_randomness(personalization as i64, rand_epoch, entropy).map_err(|e| { + let digest = fvm::rand::get_chain_randomness(rand_epoch).map_err(|e| { match e { ErrorNumber::LimitExceeded => { actor_error!(illegal_argument; "randomness lookback exceeded: {}", e) @@ -240,7 +240,13 @@ where e => actor_error!(assertion_failed; "get chain randomness failed with an unexpected error: {}", e), } })?; - Ok(draw_randomness(&digest, personalization, rand_epoch, entropy)) + Ok(draw_randomness( + fvm::crypto::hash_blake2b, + &digest, + personalization, + rand_epoch, + entropy, + )) } fn get_randomness_from_beacon( @@ -249,7 +255,7 @@ where rand_epoch: ChainEpoch, entropy: &[u8], ) -> Result<[u8; RANDOMNESS_LENGTH], ActorError> { - let digest = fvm::rand::get_beacon_randomness(personalization as i64, rand_epoch, entropy).map_err(|e| { + let digest = fvm::rand::get_beacon_randomness(rand_epoch).map_err(|e| { match e { ErrorNumber::LimitExceeded => { actor_error!(illegal_argument; "randomness lookback exceeded: {}", e) @@ -257,7 +263,13 @@ where e => actor_error!(assertion_failed; "get beacon randomness failed with an unexpected error: {}", e), } })?; - Ok(draw_randomness(&digest, personalization, rand_epoch, entropy)) + Ok(draw_randomness( + fvm::crypto::hash_blake2b, + &digest, + personalization, + rand_epoch, + entropy, + )) } fn get_state_root(&self) -> Result { @@ -643,28 +655,3 @@ fn init_logging(actor_name: &'static str) { log::set_max_level(log::LevelFilter::Trace); } } - -fn draw_randomness( - rbase: &[u8; RANDOMNESS_LENGTH], - pers: DomainSeparationTag, - round: ChainEpoch, - entropy: &[u8], -) -> [u8; RANDOMNESS_LENGTH] { - let mut data = Vec::with_capacity(RANDOMNESS_LENGTH + 8 + 8 + entropy.len()); - - // Append the randomness - data.extend_from_slice(rbase); - - // Append the personalization value - let i64_bytes = (pers as i64).to_le_bytes(); - data.extend_from_slice(&i64_bytes); - - // Append the round - let i64_bytes = round.to_be_bytes(); - data.extend_from_slice(&i64_bytes); - - // Append the entropy - data.extend_from_slice(entropy); - - fvm::crypto::hash_blake2b(&data) -} diff --git a/runtime/src/runtime/randomness.rs b/runtime/src/runtime/randomness.rs index b303c68e7..bb7d05d84 100644 --- a/runtime/src/runtime/randomness.rs +++ b/runtime/src/runtime/randomness.rs @@ -1,6 +1,8 @@ // Copyright 2019-2022 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +use fvm_shared::clock::ChainEpoch; +use fvm_shared::randomness::RANDOMNESS_LENGTH; use num_derive::FromPrimitive; use serde_repr::*; @@ -19,3 +21,64 @@ pub enum DomainSeparationTag { PoStChainCommit = 9, EvmPrevRandao = 10, } + +#[allow(unused)] +pub fn draw_randomness( + hasher: impl FnOnce(&[u8]) -> [u8; 32], + rbase: &[u8; RANDOMNESS_LENGTH], + pers: DomainSeparationTag, + round: ChainEpoch, + entropy: &[u8], +) -> [u8; RANDOMNESS_LENGTH] { + let mut data = Vec::with_capacity(RANDOMNESS_LENGTH + 8 + 8 + entropy.len()); + + // Append the personalization value + let i64_bytes = (pers as i64).to_be_bytes(); + data.extend_from_slice(&i64_bytes); + + // Append the randomness + data.extend_from_slice(rbase); + + // Append the round + let i64_bytes = round.to_be_bytes(); + data.extend_from_slice(&i64_bytes); + + // Append the entropy + data.extend_from_slice(entropy); + + hasher(&data) + // + // fvm::crypto::hash_blake2b(&data) +} + +#[cfg(test)] +mod tests { + use crate::runtime::randomness::draw_randomness; + use crate::runtime::DomainSeparationTag; + use crate::test_utils::blake2b_256; + use base64::Engine; + + #[test] + fn draw_randomness_test() { + let expected_randomness = base64::engine::general_purpose::STANDARD + .decode("3MCqcLHKZ+pil4MqTS9wjsd+yPvTuTrq8PkGjEo3tYQ=") + .unwrap(); + + let digest = base64::engine::general_purpose::STANDARD + .decode("GOobxkrhS1hiFA1EYUKZM3xsyVfy5Xy3bQ0gLPnecYs=") + .unwrap(); + + let entropy = base64::engine::general_purpose::STANDARD.decode("RACZyzQ=").unwrap(); + + assert_eq!( + expected_randomness, + draw_randomness( + blake2b_256, + <&[u8; 32]>::try_from(digest.as_slice()).unwrap(), + DomainSeparationTag::SealRandomness, + 2797727, + entropy.as_slice(), + ) + ); + } +}