Skip to content

Commit

Permalink
[move] cleanup testsuite initialization of coin (#320)
Browse files Browse the repository at this point in the history
Co-authored-by: sirouk <[email protected]>
  • Loading branch information
dboreham and sirouk authored Jan 8, 2025
1 parent 8c4ab8c commit 6337cc7
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 132 deletions.
4 changes: 4 additions & 0 deletions framework/libra-framework/sources/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ module diem_framework::account {
// The city fathers they're trying to endorse
// The reincarnation of Paul Revere's horse
// But the town has no need to be nervous.

// might unit-test mode, exit gracefully
if (!exists<MigrateOriginatingAddress>(@ol_framework)) return false;

let duplicate_table = &borrow_global<MigrateOriginatingAddress>(@ol_framework).duplicates_map;

let tomb_auth_as_addr = from_bcs::to_address(tomb_auth());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module diem_framework::aggregator_factory {

/// Creates a new factory for aggregators. Can only be called during genesis.
public(friend) fun initialize_aggregator_factory(diem_framework: &signer) {
if (exists<AggregatorFactory>(@diem_framework)) return;
system_addresses::assert_diem_framework(diem_framework);
let aggregator_factory = AggregatorFactory {
phantom_table: table::new()
Expand Down
99 changes: 2 additions & 97 deletions framework/libra-framework/sources/diem_governance.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ module diem_framework::diem_governance {
use ol_framework::musical_chairs;
use ol_framework::testnet;

#[test_only]
use ol_framework::libra_coin::LibraCoin;
#[test_only]
use diem_framework::coin;

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

/// The specified address already been used to vote on the same proposal
Expand Down Expand Up @@ -604,57 +600,6 @@ module diem_framework::diem_governance {
}
}

#[test_only]
public entry fun test_voting_generic(
diem_framework: signer,
proposer: signer,
yes_voter: signer,
no_voter: signer,
multi_step: bool,
use_generic_resolve_function: bool,
) acquires ApprovedExecutionHashes, GovernanceConfig, GovernanceEvents, GovernanceResponsbility, VotingRecords {
setup_voting(&diem_framework, &proposer, &yes_voter, &no_voter);

let execution_hash = vector::empty<u8>();
vector::push_back(&mut execution_hash, 1);

create_proposal_for_test(proposer, multi_step);

vote(&yes_voter, 0, true); //////// 0L ////////
vote(&no_voter, 0, false); //////// 0L ////////

// Once expiration time has passed, the proposal should be considered resolve now as there are more yes votes
// than no.
timestamp::update_global_time_for_test(100001000000);
let proposal_state = voting::get_proposal_state<GovernanceProposal>(signer::address_of(&diem_framework), 0);
// let (yes, no) = voting::get_votes<GovernanceProposal>(signer::address_of(&diem_framework), 0);

assert!(proposal_state == PROPOSAL_STATE_SUCCEEDED, proposal_state);

// Add approved script hash.
add_approved_script_hash(0);
let approved_hashes = borrow_global<ApprovedExecutionHashes>(@diem_framework).hashes;
assert!(*simple_map::borrow(&approved_hashes, &0) == execution_hash, 0);

// Resolve the proposal.
let account = resolve_proposal_for_test(0, @diem_framework, use_generic_resolve_function, true);
assert!(signer::address_of(&account) == @diem_framework, 1);
assert!(voting::is_resolved<GovernanceProposal>(@diem_framework, 0), 2);
let approved_hashes = borrow_global<ApprovedExecutionHashes>(@diem_framework).hashes;
assert!(!simple_map::contains_key(&approved_hashes, &0), 3);
}

#[test(diem_framework = @diem_framework, proposer = @0x123, yes_voter = @0x234, no_voter = @345)]
public entry fun test_voting(
diem_framework: signer,
proposer: signer,
yes_voter: signer,
no_voter: signer,
) acquires ApprovedExecutionHashes, GovernanceConfig, GovernanceEvents, GovernanceResponsbility, VotingRecords {
test_voting_generic(diem_framework, proposer, yes_voter, no_voter, false, false);
}


#[test_only]
//////// 0L //////// remove minimum threshold
public fun initialize_for_test(root: &signer) {
Expand All @@ -668,47 +613,7 @@ module diem_framework::diem_governance {
initialize(root, min_voting_threshold, dummy, voting_duration_secs);
}

#[test_only]
public fun setup_voting(
diem_framework: &signer,
proposer: &signer,
yes_voter: &signer,
no_voter: &signer,
) acquires GovernanceResponsbility {
// use std::vector;
use diem_framework::account;
// use diem_framework::coin;
// use diem_framework::diem_coin::{Self, DiemCoin};

timestamp::set_time_has_started_for_testing(diem_framework);
account::create_account_for_test(signer::address_of(diem_framework));
account::create_account_for_test(signer::address_of(proposer));
account::create_account_for_test(signer::address_of(yes_voter));
account::create_account_for_test(signer::address_of(no_voter));

// Initialize the governance.
let min_voting_threshold = 0;
let dummy = 0; // see code, requires refactor
let voting_duration = 1000;
initialize(diem_framework, min_voting_threshold, dummy, voting_duration);
store_signer_cap(
diem_framework,
@diem_framework,
account::create_test_signer_cap(@diem_framework),
);

let (burn_cap, mint_cap) = libra_coin::initialize_for_test(diem_framework);
coin::register<LibraCoin>(proposer);
coin::register<LibraCoin>(yes_voter);
coin::register<LibraCoin>(no_voter);

libra_coin::test_mint_to(diem_framework, signer::address_of(proposer), 50);
libra_coin::test_mint_to(diem_framework, signer::address_of(yes_voter), 10);
libra_coin::test_mint_to(diem_framework, signer::address_of(no_voter), 5);

coin::destroy_mint_cap<LibraCoin>(mint_cap);
coin::destroy_burn_cap<LibraCoin>(burn_cap);
}
// COMMIT NOTE: remove vendor tests for coin-based voting. Silly rabbit.

#[verify_only]
public fun initialize_for_verification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ module ol_framework::epoch_helper {
}

#[view]
public fun get_current_epoch():u64 acquires EpochHelper{
public fun get_current_epoch():u64 acquires EpochHelper {
if (!exists<EpochHelper>(@ol_framework)) return 0; // for unit tests

let state = borrow_global<EpochHelper>(@ol_framework);
state.epoch
}
}
}
15 changes: 12 additions & 3 deletions framework/libra-framework/sources/ol_sources/infra_escrow.move
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ module ol_framework::infra_escrow{
use ol_framework::ol_account;
use ol_framework::libra_coin::LibraCoin;
use ol_framework::pledge_accounts;
// use ol_framework::slow_wallet;
// use std::fixed_point32;
// use std::signer;

// use diem_std::debug::print;

friend diem_framework::genesis;
friend ol_framework::epoch_boundary;

#[test_only]
friend ol_framework::mock;

const EGENESIS_REWARD: u64 = 0;
/// for use on genesis, creates the infra escrow pledge policy struct
public(friend) fun initialize(framework: &signer) {
Expand Down Expand Up @@ -104,4 +105,12 @@ module ol_framework::infra_escrow{
option::destroy_none(c_opt);
}
}

#[test_only]
// test helper to initialize escrow for unit tests which don't do a full genesis
public fun init_escrow_with_deposit(framework: &signer, depositor: &signer, amount: u64){
pledge_accounts::initialize(framework);
initialize(framework);
user_pledge_infra(depositor, amount);
}
}
20 changes: 5 additions & 15 deletions framework/libra-framework/sources/ol_sources/libra_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module ol_framework::libra_coin {

// Diem framework needs mint cap to mint coins to initial validators. This will be revoked once the validators
// have been initialized.

move_to(diem_framework, MintCapStore { mint_cap });


Expand Down Expand Up @@ -333,7 +334,7 @@ module ol_framework::libra_coin {
use diem_framework::aggregator_factory;

#[test_only]
public fun initialize_for_test(diem_framework: &signer): (BurnCapability<LibraCoin>, MintCapability<LibraCoin>) {
public fun initialize_for_test(diem_framework: &signer): (BurnCapability<LibraCoin>, MintCapability<LibraCoin>) acquires FinalMint {
aggregator_factory::initialize_aggregator_factory_for_test(diem_framework);
let (burn_cap, freeze_cap, mint_cap) = coin::initialize_with_parallelizable_supply<LibraCoin>(
diem_framework,
Expand All @@ -342,22 +343,11 @@ module ol_framework::libra_coin {
8, /* decimals */
true, /* monitor_supply */
);
move_to(diem_framework, MintCapStore { mint_cap });

genesis_set_final_supply(diem_framework, 0);

coin::destroy_freeze_cap(freeze_cap);
(burn_cap, mint_cap)
}
// This is particularly useful if the aggregator_factory is already initialized via another call path.
#[test_only]
public fun initialize_for_test_without_aggregator_factory(diem_framework: &signer): (BurnCapability<LibraCoin>, MintCapability<LibraCoin>) {
let (burn_cap, freeze_cap, mint_cap) = coin::initialize_with_parallelizable_supply<LibraCoin>(
diem_framework,
string::utf8(b"LibraCoin"),
string::utf8(b"LIBRA"),
8, /* decimals */
true, /* monitor_supply */
);
coin::destroy_freeze_cap(freeze_cap);
(burn_cap, mint_cap)
}
// COMMIT NOTE: Deduplicate the initialization by lazy initializing aggregator_factory.move
}
31 changes: 21 additions & 10 deletions framework/libra-framework/sources/ol_sources/mock.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#[test_only]
module ol_framework::mock {
use std::vector;
use std::signer;
use diem_framework::coin;
use diem_framework::block;
use diem_framework::stake;
Expand All @@ -22,7 +21,7 @@ module ol_framework::mock {
use ol_framework::ol_account;
use ol_framework::epoch_helper;
use ol_framework::musical_chairs;
use ol_framework::pledge_accounts;
use ol_framework::infra_escrow;

// use diem_std::debug::print;

Expand Down Expand Up @@ -140,7 +139,7 @@ module ol_framework::mock {
genesis::setup();
genesis::test_end_genesis(root);

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

assert!(!chain_status::is_genesis(), 0);
Expand All @@ -150,7 +149,7 @@ module ol_framework::mock {
public fun ol_initialize_coin(root: &signer) {
system_addresses::assert_ol(root);

let mint_cap = init_coin_impl(root);
let mint_cap = coin_init_minimal(root);

libra_coin::restore_mint_cap(root, mint_cap);
}
Expand All @@ -162,7 +161,12 @@ module ol_framework::mock {
let mint_cap = if (coin::is_coin_initialized<LibraCoin>()) {
libra_coin::extract_mint_cap(root)
} else {
init_coin_impl(root)
coin_init_minimal(root)

};

if (!account::exists_at(addr)) {
ol_account::create_account(root, addr);
};

let c = coin::test_mint(amount, &mint_cap);
Expand All @@ -182,7 +186,7 @@ module ol_framework::mock {
let mint_cap = if (coin::is_coin_initialized<LibraCoin>()) {
libra_coin::extract_mint_cap(root)
} else {
init_coin_impl(root)
coin_init_minimal(root)
};

let vals = stake::get_current_validators();
Expand All @@ -205,10 +209,12 @@ module ol_framework::mock {
}

#[test_only]
fun init_coin_impl(root: &signer): coin::MintCapability<LibraCoin> {
// For unit test, we need to try to initialize the minimal state for
// user transactions. In the case of a unit tests which does a genesis with validators, this will not attempt to re-initialize the state.
fun coin_init_minimal(root: &signer): coin::MintCapability<LibraCoin> {
system_addresses::assert_ol(root);

let (burn_cap, mint_cap) = libra_coin::initialize_for_test_without_aggregator_factory(root);
let (burn_cap, mint_cap) = libra_coin::initialize_for_test(root);
coin::destroy_burn_cap(burn_cap);

transaction_fee::initialize_fee_collection_and_distribution(root, 0);
Expand All @@ -228,8 +234,7 @@ module ol_framework::mock {
ol_account::deposit_coins(bruce_address, fortune_mint);

// Bruce funds infra escrow
let framework = signer::address_of(root);
pledge_accounts::user_pledge(&bruce, framework, 37_000_000_000);
infra_escrow::init_escrow_with_deposit(root, &bruce, 37_000_000_000);

let supply_pre = libra_coin::supply();
assert!(supply_pre == (initial_fees + fortune), ESUPPLY_MISMATCH);
Expand Down Expand Up @@ -389,4 +394,10 @@ module ol_framework::mock {
assert!(entry_fee == 1_000, 73570003);
assert!(median_bid == 3, 73570004);
}


#[test(framework = @0x1, bob = @0x10002)]
fun meta_test_minimal_account_init(framework: &signer, bob: address) {
ol_mint_to(framework, bob, 123);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
friend ol_framework::genesis_migration;
friend ol_framework::genesis;

#[test_only]
friend ol_framework::mock;
// #[test_only]
// friend ol_framework::mock;

/// no policy at this address
const ENO_BENEFICIARY_POLICY: u64 = 1;
Expand Down Expand Up @@ -107,9 +107,11 @@
}

public(friend) fun initialize(framework: &signer) {
move_to(framework, BeneficiaryRegistry {
list: vector::empty()
})
if (!exists<BeneficiaryRegistry>(@ol_framework)) {
move_to(framework, BeneficiaryRegistry {
list: vector::empty()
})
}
}
// beneficiary publishes a policy to their account.
// NOTE: It cannot be modified after a first pledge is made!.
Expand Down Expand Up @@ -661,4 +663,5 @@
// testnet::assert_testnet(vm);
withdraw_from_one_pledge_account(bene, donor, amount)
}

}

0 comments on commit 6337cc7

Please sign in to comment.