Skip to content

Commit

Permalink
add test, fix all the bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Aug 23, 2023
1 parent 9b07567 commit 8ddec2f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 44 deletions.
16 changes: 9 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down
47 changes: 17 additions & 30 deletions runtime/src/runtime/fvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -232,15 +232,21 @@ 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)
}
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(
Expand All @@ -249,15 +255,21 @@ 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)
}
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<Cid, ActorError> {
Expand Down Expand Up @@ -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)
}
63 changes: 63 additions & 0 deletions runtime/src/runtime/randomness.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -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(),
)
);
}
}

0 comments on commit 8ddec2f

Please sign in to comment.