Skip to content

Commit

Permalink
[move] burn tracker (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: 0o-de-lally <[email protected]>
  • Loading branch information
sirouk and 0o-de-lally committed Aug 8, 2024
1 parent df1e32f commit 8200a23
Show file tree
Hide file tree
Showing 23 changed files with 360 additions and 187 deletions.
20 changes: 2 additions & 18 deletions framework/libra-framework/sources/modified_source/genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,12 @@ module diem_framework::genesis {
// we are leaving vendor's coin in place.
/// Genesis step 2: Initialize Diem coin.
fun initialize_diem_coin(diem_framework: &signer) {

// let (burn_cap, mint_cap) = diem_coin::initialize(diem_framework);
// Give stake module MintCapability<GasCoin> so it can mint rewards.
// stake::store_diem_coin_mint_cap(diem_framework, mint_cap);
// coin::destroy_mint_cap(mint_cap);
// Give transaction_fee module BurnCapability<GasCoin> so it can burn gas.
// transaction_fee::store_diem_coin_burn_cap(diem_framework, burn_cap);
// coin::destroy_burn_cap(burn_cap);

// 0L: genesis ceremony is calling this
// NOTE 0L: genesis ceremony is calling this
gas_coin::initialize(diem_framework);
// Give stake module MintCapability<GasCoin> so it can mint rewards.
// stake::store_diem_coin_mint_cap(diem_framework, mint_cap);
// gas_coin::restore_mint_cap(diem_framework, mint_cap);
// coin::destroy_burn_cap(burn_cap);

transaction_fee::initialize_fee_collection_and_distribution(diem_framework, 0);
}

// TODO: 0L: replace this with gas coin. using vendor's to preserve tests while WIP.
/// Only called for testnets and e2e tests.
fun initialize_core_resources_and_diem_coin(
diem_framework: &signer,
Expand All @@ -199,9 +186,6 @@ module diem_framework::genesis {

let core_resources = account::create_account(@core_resources);
account::rotate_authentication_key_internal(&core_resources, core_resources_auth_key);
// diem_coin::configure_accounts_for_test(diem_framework, &core_resources, mint_cap);
// coin::destroy_mint_cap(mint_cap);
// coin::destroy_burn_cap(burn_cap);

// initialize gas
let (burn_cap_two, mint_cap_two) = gas_coin::initialize_for_core(diem_framework);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ module ol_framework::genesis_migration {
fun rounding_mint(root: &signer, target_supply: u64) {
let existing_supply = gas_coin::supply();

assert!(target_supply >= existing_supply, error::invalid_state(EMINTED_OVER_TARGET));
// we should not ever have migrated more coins than expected
// this should abort the genesis process
assert!(existing_supply <= target_supply,
error::invalid_state(EMINTED_OVER_TARGET));

if (target_supply > existing_supply) {
let coin = coin::vm_mint<GasCoin>(root, target_supply - existing_supply);
transaction_fee::vm_pay_fee(root, @ol_framework, coin);

}
};
}

/// for an uprade using an escrow percent. Only to be called at genesis
Expand Down
80 changes: 76 additions & 4 deletions framework/libra-framework/sources/ol_sources/libra_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ module ol_framework::gas_coin {
use std::signer;
use std::vector;
use std::option::{Self, Option};
// use diem_std::debug::print;

use diem_framework::coin::{Self, MintCapability, BurnCapability};
use diem_framework::system_addresses;
Expand All @@ -129,15 +130,23 @@ module ol_framework::gas_coin {
friend diem_framework::genesis;
friend ol_framework::genesis_migration;

const MAX_U64: u128 = 18446744073709551615;

/// Account does not have mint capability
const ENO_CAPABILITIES: u64 = 1;
/// Mint capability has already been delegated to this specified address
const EALREADY_DELEGATED: u64 = 2;
/// Cannot find delegation of mint capability to this account
const EDELEGATION_NOT_FOUND: u64 = 3;
/// Supply somehow above MAX_U64
const ESUPPLY_OVERFLOW: u64 = 4;

struct LibraCoin has key {}

struct FinalMint has key {
value: u64,
}

struct MintCapStore has key {
mint_cap: MintCapability<LibraCoin>,
}
Expand Down Expand Up @@ -170,12 +179,12 @@ module ol_framework::gas_coin {

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

/// FOR TESTS ONLY
/// Can only called during genesis to initialize the Diem coin.
public(friend) fun initialize_for_core(diem_framework: &signer): (BurnCapability<LibraCoin>, MintCapability<LibraCoin>) {
public(friend) fun initialize_for_core(diem_framework: &signer):
(BurnCapability<LibraCoin>, MintCapability<LibraCoin>) acquires FinalMint {
system_addresses::assert_diem_framework(diem_framework);

let (burn_cap, freeze_cap, mint_cap) = coin::initialize_with_parallelizable_supply<LibraCoin>(
Expand All @@ -192,6 +201,9 @@ module ol_framework::gas_coin {

coin::destroy_freeze_cap(freeze_cap);

genesis_set_final_supply(diem_framework, 100); // TODO: set this number
// in testnets

(burn_cap, mint_cap)
}

Expand All @@ -207,14 +219,61 @@ module ol_framework::gas_coin {
coin::destroy_mint_cap(mint_cap);
}

// at genesis we need to init the final supply
// done at genesis_migration
fun genesis_set_final_supply(diem_framework: &signer,
final_supply: u64) acquires FinalMint {
system_addresses::assert_ol(diem_framework);

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

if (!exists<FinalMint>(@ol_framework)) {
move_to(diem_framework, FinalMint {
value: final_supply
});
} else {
let state = borrow_global_mut<FinalMint>(@ol_framework);
state.value = final_supply
}
}

#[view]
/// get the original final supply from genesis
public fun get_final_supply(): u64 acquires FinalMint{
borrow_global<FinalMint>(@ol_framework).value
}


#[view]
/// get the gas coin supply. Helper which wraps coin::supply and extracts option type
// NOTE: there is casting between u128 and u64, but 0L has final supply below the u64.
public fun supply(): u64 {
let supply_opt = coin::supply<LibraCoin>();
if (option::is_some(&supply_opt)) {
return (*option::borrow(&supply_opt) as u64)
let value = *option::borrow(&supply_opt);
assert!(value <= MAX_U64, ESUPPLY_OVERFLOW);
return (value as u64)
};
0
}
#[view]
/// debugging view
public fun supply_128(): u128 {
let supply_opt = coin::supply<LibraCoin>();
if (option::is_some(&supply_opt)) {
return *option::borrow(&supply_opt)
};
0
}
Expand All @@ -226,6 +285,14 @@ module ol_framework::gas_coin {
move_to(diem_framework, MintCapStore { mint_cap });
}

#[test_only]
public fun extract_mint_cap(diem_framework: &signer):
MintCapability<LibraCoin> acquires MintCapStore {
system_addresses::assert_diem_framework(diem_framework);
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 All @@ -241,7 +308,8 @@ module ol_framework::gas_coin {
coin::register<LibraCoin>(core_resources);

let coins = coin::mint<LibraCoin>(
18446744073709551615,
1000000 * 1000000, // core resources can have 1M coins, MAX_U64 was
// causing arthmetic errors calling supply() on downcast
&mint_cap,
);
coin::deposit<LibraCoin>(signer::address_of(core_resources), coins);
Expand Down Expand Up @@ -271,6 +339,7 @@ module ol_framework::gas_coin {
dst_addr: address,
amount: u64,
) acquires MintCapStore {
let _s = supply(); // check we didn't overflow supply

let account_addr = signer::address_of(root);

Expand All @@ -282,6 +351,9 @@ module ol_framework::gas_coin {
let mint_cap = &borrow_global<MintCapStore>(account_addr).mint_cap;
let coins_minted = coin::mint<LibraCoin>(amount, mint_cap);
coin::deposit<LibraCoin>(dst_addr, coins_minted);

// TODO: update the final supply for tests
// genesis_set_final_supply(root, supply());
}

#[test_only]
Expand Down
30 changes: 19 additions & 11 deletions framework/libra-framework/sources/ol_sources/mock.move
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ module ol_framework::mock {
use ol_framework::musical_chairs;
use ol_framework::globals;
use diem_framework::block;
// use diem_framework::chain_status;
use diem_std::debug::print;

// use diem_std::debug::print;

const ENO_GENESIS_END_MARKER: u64 = 1;
const EDID_NOT_ADVANCE_EPOCH: u64 = 1;
const EDID_NOT_ADVANCE_EPOCH: u64 = 2;
/// coin supply does not match expected
const ESUPPLY_MISMATCH: u64 = 3;

#[test_only]
public fun reset_val_perf_one(vm: &signer, addr: address) {
Expand Down Expand Up @@ -150,6 +152,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 +164,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 @@ -195,14 +205,14 @@ module ol_framework::mock {
let (burn_cap, mint_cap) = gas_coin::initialize_for_test_without_aggregator_factory(root);
coin::destroy_burn_cap(burn_cap);


transaction_fee::initialize_fee_collection_and_distribution(root, 0);

let initial_fees = 1000000 * 100; // coin scaling * 100 coins
let tx_fees = coin::test_mint(initial_fees, &mint_cap);
transaction_fee::vm_pay_fee(root, @ol_framework, tx_fees);
let supply_pre = gas_coin::supply();
assert!(supply_pre == initial_fees, 666);
assert!(supply_pre == initial_fees, ESUPPLY_MISMATCH);
gas_coin::test_set_final_supply(root, initial_fees);

mint_cap
}
Expand Down Expand Up @@ -290,7 +300,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 All @@ -302,7 +312,6 @@ module ol_framework::mock {
// genesis();

let set = genesis_n_vals(&root, 4);
print(&set);
assert!(vector::length(&set) == 4, 7357001);

let addr = vector::borrow(&set, 0);
Expand Down Expand Up @@ -353,5 +362,4 @@ module ol_framework::mock {
assert!(entry_fee == 999, 73570003);
assert!(median_bid == 3, 73570004);
}

}
Loading

0 comments on commit 8200a23

Please sign in to comment.