Skip to content

Commit

Permalink
patch tests with initialization of randomness and safe calling
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Jan 19, 2025
1 parent 887821e commit 1179acf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion framework/libra-framework/sources/ol_sources/mock.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module ol_framework::mock {
use ol_framework::epoch_helper;
use ol_framework::musical_chairs;
use ol_framework::infra_escrow;
use ol_framework::randomness;

// use diem_std::debug::print;

Expand Down Expand Up @@ -108,7 +109,6 @@ module ol_framework::mock {

#[test_only]
public fun mock_bids(vals: &vector<address>): (vector<u64>, vector<u64>) {
// system_addresses::assert_ol(vm);
let bids = vector::empty<u64>();
let expiry = vector::empty<u64>();
let i = 0;
Expand Down Expand Up @@ -139,6 +139,8 @@ module ol_framework::mock {
genesis::setup();
genesis::test_end_genesis(root);

randomness::initialize_for_testing(root);

let mint_cap = coin_init_minimal(root);
libra_coin::restore_mint_cap(root, mint_cap);

Expand Down
17 changes: 10 additions & 7 deletions framework/libra-framework/sources/randomness.move
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ module diem_framework::randomness {
use diem_framework::system_addresses;
use diem_framework::transaction_context;

#[test_only]
use diem_std::debug;
#[test_only]
use diem_std::table_with_length;

Expand Down Expand Up @@ -256,6 +254,9 @@ module diem_framework::randomness {
/// NOTE: The uniformity is not perfect, but it can be proved that the bias is negligible.
/// If you need perfect uniformity, consider implement your own via rejection sampling.
public(friend) fun u8_range(min_incl: u8, max_excl: u8): u8 acquires PerBlockRandomness {
// let the caller decide how to handle
if (max_excl<= min_incl) { return 0 };

let range = ((max_excl - min_incl) as u256);
let sample = ((u256_integer_internal() % range) as u8);

Expand Down Expand Up @@ -294,16 +295,16 @@ module diem_framework::randomness {
///
/// NOTE: The uniformity is not perfect, but it can be proved that the bias is negligible.
/// If you need perfect uniformity, consider implement your own via rejection sampling.
public (friend) fun u64_range(min_incl: u64, max_excl: u64): u64 acquires PerBlockRandomness {
// event::emit_event(RandomnessGeneratedEvent {});
public(friend) fun u64_range(min_incl: u64, max_excl: u64): u64 acquires PerBlockRandomness {
// let the caller decide how to handle
if (max_excl<= min_incl) { return 0 };

u64_range_internal(min_incl, max_excl)
}

fun u64_range_internal(min_incl: u64, max_excl: u64): u64 acquires PerBlockRandomness {
let range = ((max_excl - min_incl) as u256);
let sample = ((u256_integer_internal() % range) as u64);

min_incl + sample
}

Expand Down Expand Up @@ -457,8 +458,10 @@ module diem_framework::randomness {
set_seed(x"0000000000000000000000000000000000000000000000000000000000000000");
// Test cases should always have no bias for any randomness call.
// assert!(is_unbiasable(), 0);
let num = u64_integer();
debug::print(&num);
let num1 = u64_integer();
let num2 = u64_integer();
assert!(num1 != num2, 7357001);

}

// #[test_only]
Expand Down

0 comments on commit 1179acf

Please sign in to comment.