Skip to content

Commit

Permalink
refactor coin initialization in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Oct 25, 2023
1 parent 85340b3 commit 761e7b5
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 113 deletions.
28 changes: 21 additions & 7 deletions framework/libra-framework/sources/ol_sources/libra_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ module ol_framework::gas_coin {

friend diem_framework::genesis;
friend ol_framework::genesis_migration;
#[test_only]
friend ol_framework::mock;

/// Account does not have mint capability
const ENO_CAPABILITIES: u64 = 1;
Expand All @@ -141,7 +139,7 @@ module ol_framework::gas_coin {
struct LibraCoin has key {}

struct FinalSupply has key {
value: u64,
value: u64,
}

struct MintCapStore has key {
Expand Down Expand Up @@ -217,6 +215,22 @@ module ol_framework::gas_coin {
// done at genesis_migration
public(friend) fun genesis_set_final_supply(diem_framework: &signer,
final_supply: u64) acquires FinalSupply {
system_addresses::assert_ol(diem_framework);

if (!exists<FinalSupply>(@ol_framework)) {
move_to(diem_framework, FinalSupply {
value: final_supply
});
} else {
let state = borrow_global_mut<FinalSupply>(@ol_framework);
state.value = final_supply
}
}
#[test_only]
public fun test_set_final_supply(diem_framework: &signer,
final_supply: u64) acquires FinalSupply {
system_addresses::assert_ol(diem_framework);

if (!exists<FinalSupply>(@ol_framework)) {
move_to(diem_framework, FinalSupply {
value: final_supply
Expand Down Expand Up @@ -253,13 +267,13 @@ module ol_framework::gas_coin {
}

#[test_only]
public fun borrow_mint_cap(diem_framework: &signer):
public fun extract_mint_cap(diem_framework: &signer):
MintCapability<LibraCoin> acquires MintCapStore {
system_addresses::assert_diem_framework(diem_framework);
// move_to(diem_framework, MintCapStore { mint_cap });
let state = borrow_global_mut<MintCapStore>(@diem_framework);
state.mint_cap
let MintCapStore { mint_cap } = move_from<MintCapStore>(@diem_framework);
mint_cap
}

/// FOR TESTS ONLY
/// The `core addresses` sudo account is used to execute system transactions for testing
/// Can only be called during genesis for tests to grant mint capability to diem framework and core resources
Expand Down
17 changes: 13 additions & 4 deletions framework/libra-framework/sources/ol_sources/mock.move
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ module ol_framework::mock {
genesis::setup();
genesis::test_end_genesis(root);

let mint_cap = init_coin_impl(root);
gas_coin::restore_mint_cap(root, mint_cap);

assert!(!chain_status::is_genesis(), 0);
}

Expand All @@ -159,15 +162,20 @@ module ol_framework::mock {

let mint_cap = init_coin_impl(root);

coin::destroy_mint_cap(mint_cap);
gas_coin::restore_mint_cap(root, mint_cap);
}

#[test_only]
public fun ol_initialize_coin_and_fund_vals(root: &signer, amount: u64, drip: bool) {
public fun ol_initialize_coin_and_fund_vals(root: &signer, amount: u64,
drip: bool) {
system_addresses::assert_ol(root);


let mint_cap = init_coin_impl(root);
let mint_cap = if (coin::is_coin_initialized<GasCoin>()) {
gas_coin::extract_mint_cap(root)
} else {
init_coin_impl(root)
};

let vals = stake::get_current_validators();
let i = 0;
Expand Down Expand Up @@ -203,6 +211,7 @@ module ol_framework::mock {
transaction_fee::vm_pay_fee(root, @ol_framework, tx_fees);
let supply_pre = gas_coin::supply();
assert!(supply_pre == initial_fees, 666);
gas_coin::test_set_final_supply(root, initial_fees);

mint_cap
}
Expand Down Expand Up @@ -290,7 +299,7 @@ module ol_framework::mock {
public fun meta_epoch(root: signer) {
ol_test_genesis(&root);
musical_chairs::initialize(&root, 10);
ol_initialize_coin(&root);
// ol_initialize_coin(&root);
let epoch = reconfiguration::current_epoch();
trigger_epoch(&root);
let new_epoch = reconfiguration::current_epoch();
Expand Down
75 changes: 20 additions & 55 deletions framework/libra-framework/sources/ol_sources/ol_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module ol_framework::ol_account {
use diem_framework::coin::{Self, Coin};
use diem_framework::event::{EventHandle, emit_event};
use diem_framework::system_addresses;
// use diem_framework::chain_status;
use std::error;
use std::signer;
use std::option::{Self, Option};
Expand Down Expand Up @@ -350,7 +349,8 @@ module ol_framework::ol_account {
if (original_supply > current_supply) {
let burn_in_period = original_supply - current_supply;

if (burn_in_period> 0 && burn_in_period > state.prev_balance ) {
if (burn_in_period > 0 && burn_in_period > state.prev_balance &&
state.prev_balance > 0 ) {
let attributed_burn = burn_in_period / state.prev_balance;
// attributed burn may be zero because of rounding effects
// in that case we should skip the updating altogether and
Expand Down Expand Up @@ -456,12 +456,14 @@ module ol_framework::ol_account {
struct FakeCoin {}

#[test(root = @ol_framework, alice = @0xa11ce, core = @0x1)]
public fun test_transfer(root: &signer, alice: &signer, core: &signer)
public fun test_transfer_ol(root: &signer, alice: &signer, core: &signer)
acquires BurnTracker {
let bob = from_bcs::to_address(x"0000000000000000000000000000000000000000000000000000000000000b0b");
let carol = from_bcs::to_address(x"00000000000000000000000000000000000000000000000000000000000ca501");

let (burn_cap, mint_cap) = ol_framework::gas_coin::initialize_for_test(core);
let (burn_cap, mint_cap) =
ol_framework::gas_coin::initialize_for_test(core);
gas_coin::test_set_final_supply(root, 1000); // dummy to prevent fail
create_account(root, signer::address_of(alice));
create_account(root, bob);
create_account(root, carol);
Expand All @@ -478,13 +480,15 @@ module ol_framework::ol_account {
}

#[test(root = @ol_framework, alice = @0xa11ce, core = @0x1)]
public fun test_transfer_to_resource_account(root: &signer, alice: &signer,
public fun test_transfer_to_resource_account_ol(root: &signer, alice: &signer,
core: &signer) acquires BurnTracker{
let (resource_account, _) = ol_create_resource_account(alice, vector[]);
let resource_acc_addr = signer::address_of(&resource_account);
// assert!(!coin::is_account_registered<GasCoin>(resource_acc_addr), 0);

let (burn_cap, mint_cap) = ol_framework::gas_coin::initialize_for_test(core);
let (burn_cap, mint_cap) =
ol_framework::gas_coin::initialize_for_test(core);
gas_coin::test_set_final_supply(root, 1000); // dummy to prevent fail

create_account(root, signer::address_of(alice));
coin::deposit(signer::address_of(alice), coin::mint(10000, &mint_cap));
transfer(alice, resource_acc_addr, 500);
Expand All @@ -497,7 +501,10 @@ module ol_framework::ol_account {
#[test(root = @ol_framework, from = @0x123, core = @0x1, recipient_1 = @0x124, recipient_2 = @0x125)]
public fun test_batch_transfer(root: &signer, from: &signer, core: &signer,
recipient_1: &signer, recipient_2: &signer) acquires BurnTracker{
let (burn_cap, mint_cap) = diem_framework::gas_coin::initialize_for_test(core);
let (burn_cap, mint_cap) =
diem_framework::gas_coin::initialize_for_test(core);
gas_coin::test_set_final_supply(root, 1000); // dummy to prevent fail

create_account(root, signer::address_of(from));
let recipient_1_addr = signer::address_of(recipient_1);
let recipient_2_addr = signer::address_of(recipient_2);
Expand Down Expand Up @@ -570,6 +577,11 @@ module ol_framework::ol_account {
public fun test_set_allow_direct_coin_transfers(root: &signer, user:
&signer) acquires DirectTransferConfig {
let addr = signer::address_of(user);
let (b, m) = gas_coin::initialize_for_test(root);
coin::destroy_burn_cap(b);
coin::destroy_mint_cap(m);
gas_coin::test_set_final_supply(root, 1000); // dummy to prevent fail

create_account(root, addr);
set_allow_direct_coin_transfers(user, true);
assert!(can_receive_direct_coin_transfers(addr), 0);
Expand All @@ -578,51 +590,4 @@ module ol_framework::ol_account {
set_allow_direct_coin_transfers(user, true);
assert!(can_receive_direct_coin_transfers(addr), 2);
}

// #[test(root = @ol_framework, from = @0x1, to = @0x12)]
// public fun test_direct_coin_transfers_with_explicit_direct_coin_transfer_config(
// root: &signer, from: &signer, to: &signer) acquires DirectTransferConfig {
// let (burn_cap, freeze_cap, mint_cap) = coin::initialize<FakeCoin>(
// from,
// utf8(b"FC"),
// utf8(b"FC"),
// 10,
// true,
// );
// create_account(root, signer::address_of(from));
// create_account(root, signer::address_of(to));
// set_allow_direct_coin_transfers(from, true);
// deposit_coins(signer::address_of(from), coin::mint(1000, &mint_cap));
// // Recipient account did not explicit register for the coin.
// let to_addr = signer::address_of(to);
// transfer_coins<FakeCoin>(from, to_addr, 500);
// assert!(coin::balance<FakeCoin>(to_addr) == 500, 0);

// coin::destroy_burn_cap(burn_cap);
// coin::destroy_mint_cap(mint_cap);
// coin::destroy_freeze_cap(freeze_cap);
// }

// #[test(root = @ol_framework, from = @0x1, to = @0x12)]
// #[expected_failure(abort_code = 0x50003, location = Self)]
// public fun test_direct_coin_transfers_fail_if_recipient_opted_out(
// root: &signer, from: &signer, to: &signer) acquires DirectTransferConfig {
// let (burn_cap, freeze_cap, mint_cap) = coin::initialize<FakeCoin>(
// from,
// utf8(b"FC"),
// utf8(b"FC"),
// 10,
// true,
// );
// create_account(root, signer::address_of(from));
// create_account(root, signer::address_of(to));
// set_allow_direct_coin_transfers(from, false);
// deposit_coins(signer::address_of(from), coin::mint(1000, &mint_cap));
// // This should fail as the to account has explicitly opted out of receiving arbitrary coins.
// transfer_coins<FakeCoin>(from, signer::address_of(to), 500);

// coin::destroy_burn_cap(burn_cap);
// coin::destroy_mint_cap(mint_cap);
// coin::destroy_freeze_cap(freeze_cap);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module ol_framework::test_meta {
// #[test(root = @ol_framework)]
// fun test_reconfigure_mock_trigger(root: signer) {
// mock::ol_test_genesis(&root);
// mock::ol_initialize_coin(&root);
// // mock::ol_initialize_coin(&root);
// let a = reconfiguration::get_current_epoch();

// mock::trigger_epoch(&root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@ module ol_framework::test_migration {
use ol_framework::genesis_migration;
use ol_framework::infra_escrow;
use ol_framework::slow_wallet;
use diem_framework::genesis;
use std::fixed_point32;
use ol_framework::mock;
use diem_framework::coin;
use ol_framework::gas_coin::LibraCoin as GasCoin;
use std::signer;
use std::bcs;

// use diem_std::debug::print;

#[test(root = @ol_framework, user = @0xaaaaaa)]
fun test_migration_balance_end_user(root: signer, user: signer) {
let temp_auth_key = bcs::to_bytes(&signer::address_of(&user));

mock::genesis_n_vals(&root, 4);
genesis::test_initalize_coins(&root);

genesis_migration::migrate_legacy_user(
&root,
&user,
temp_auth_key,
20000,
// 5,
)
}

Expand All @@ -34,10 +29,7 @@ module ol_framework::test_migration {
fun test_migration_balance_validators(root: signer, vm: signer, marlon_rando: signer) {

let _vals = mock::genesis_n_vals(&root, 4);
// mock::ol_initialize_coin_and_fund_vals(&root, 1000000);
genesis::test_initalize_coins(&root);

// let alice = vector::borrow(&vals, 0);
let addr = signer::address_of(&marlon_rando);
let temp_auth_key = bcs::to_bytes(&addr);

Expand All @@ -49,9 +41,6 @@ module ol_framework::test_migration {
&marlon_rando,
temp_auth_key,
init_balance,
// true, // is_validator
// scale_integer * 1000000,// of 1m
// escrow_pct * 10000, // of 1m
);

let user_balance = coin::balance<GasCoin>(addr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
#[test_only]
module ol_framework::test_rewards {
// #[test_only]
// use std::vector;
#[test_only]
use ol_framework::rewards;
// #[test_only]
// use ol_framework::ol_account;
#[test_only]
use ol_framework::gas_coin::{Self, LibraCoin as GasCoin};
#[test_only]
use diem_framework::coin;
#[test_only]
use ol_framework::mock;

// #[test_only]
// use ol_framework::gas_coin::LibraCoin as GasCoin;
#[test_only]
use diem_framework::stake;


// #[test_only]
// use diem_std::debug::print;

#[test(root = @ol_framework)]
fun test_pay_reward_single(root: signer) {

let alice = @0x1000a;

mock::genesis_n_vals(&root, 1);
let mint_cap = gas_coin::extract_mint_cap(&root);

let uid_before = stake::get_reward_event_guid(alice);

let (burn_cap, mint_cap) = gas_coin::initialize_for_test_without_aggregator_factory(&root);
let new_coin = coin::test_mint(10000, &mint_cap);
coin::destroy_burn_cap(burn_cap);

coin::destroy_mint_cap(mint_cap);

rewards::test_helper_pay_reward(&root, alice, new_coin, 1);
Expand All @@ -50,12 +40,11 @@ module ol_framework::test_rewards {
let dave = @0x1000d;

let vals = mock::genesis_n_vals(root, 4);
let mint_cap = gas_coin::extract_mint_cap(root);
let uid_before = stake::get_reward_event_guid(dave);
assert!(uid_before == 0, 7357000);

let (burn_cap, mint_cap) = gas_coin::initialize_for_test_without_aggregator_factory(root);
let new_coin = coin::test_mint(10000, &mint_cap);
coin::destroy_burn_cap(burn_cap);

coin::destroy_mint_cap(mint_cap);

rewards::test_process_recipients(root, vals, 1000, &mut new_coin, 1);
Expand Down
Loading

0 comments on commit 761e7b5

Please sign in to comment.