Skip to content

Commit

Permalink
init burn tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Oct 24, 2023
1 parent 735b6f3 commit 85340b3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
19 changes: 16 additions & 3 deletions framework/libra-framework/sources/ol_sources/libra_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ 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 Down Expand Up @@ -174,12 +176,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>) {
system_addresses::assert_diem_framework(diem_framework);

let (burn_cap, freeze_cap, mint_cap) = coin::initialize_with_parallelizable_supply<LibraCoin>(
Expand Down Expand Up @@ -214,11 +216,14 @@ module ol_framework::gas_coin {
// at genesis we need to init the final supply
// done at genesis_migration
public(friend) fun genesis_set_final_supply(diem_framework: &signer,
final_supply: u64) {
final_supply: u64) acquires FinalSupply {
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
}
}

Expand Down Expand Up @@ -247,6 +252,14 @@ module ol_framework::gas_coin {
move_to(diem_framework, MintCapStore { mint_cap });
}

#[test_only]
public fun borrow_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
}
/// 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
46 changes: 32 additions & 14 deletions framework/libra-framework/sources/ol_sources/ol_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module ol_framework::ol_account {
let new_signer = account::create_account(auth_key);
coin::register<GasCoin>(&new_signer);
receipts::user_init(&new_signer);
init_burn_tracker(&new_signer);
}

// #[test_only]
Expand Down Expand Up @@ -123,6 +124,7 @@ module ol_framework::ol_account {
);

coin::register<GasCoin>(&new_signer);
init_burn_tracker(&new_signer);
new_signer
}

Expand All @@ -131,7 +133,8 @@ module ol_framework::ol_account {

#[test_only]
/// Batch version of GAS transfer.
public entry fun batch_transfer(source: &signer, recipients: vector<address>, amounts: vector<u64>) {
public entry fun batch_transfer(source: &signer, recipients:
vector<address>, amounts: vector<u64>) acquires BurnTracker {
let recipients_len = vector::length(&recipients);
assert!(
recipients_len == vector::length(&amounts),
Expand All @@ -149,9 +152,14 @@ module ol_framework::ol_account {

/// Convenient function to transfer GAS to a recipient account that might not exist.
/// This would create the recipient account first, which also registers it to receive GAS, before transferring.
public entry fun transfer(sender: &signer, to: address, amount: u64) {
transfer_checks(signer::address_of(sender), to, amount);
coin::transfer<GasCoin>(sender, to, amount);
public entry fun transfer(sender: &signer, to: address, amount: u64)
acquires BurnTracker {
let payer = signer::address_of(sender);
transfer_checks(payer, to, amount);
// both update burn tracker
let c = withdraw(sender, amount);
deposit_coins(to, c);
slow_wallet::maybe_track_slow_transfer(payer, to, amount);
}

// transfer with capability, and do appropriate checks on both sides, and track the slow wallet
Expand Down Expand Up @@ -310,16 +318,22 @@ module ol_framework::ol_account {
}

// on new account creation we need the burn tracker created
public fun init_burn_tracker(sig: &signer) acquires BurnTracker {
// note return quietly if it's already initialized, so we can use it
// in the creation and tx flow
public fun init_burn_tracker(sig: &signer) {
let addr = signer::address_of(sig);
let state = borrow_global_mut<BurnTracker>(addr);
if (exists<BurnTracker>(addr)) return;

let (_, total_balance) = balance(addr);
state.prev_supply = gas_coin::supply();
state.prev_balance = total_balance;
state.burn_at_last_calc = 0;
state.cumu_burn = 0;
move_to(sig, BurnTracker {
prev_supply: gas_coin::supply(),
prev_balance: total_balance,
burn_at_last_calc: 0,
cumu_burn: 0,
})
}


/// TODO: the user may update the tracker outside of transactions
public fun user_update_burn_tracker() {}

Expand Down Expand Up @@ -442,7 +456,8 @@ 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(root: &signer, alice: &signer, core: &signer)
acquires BurnTracker {
let bob = from_bcs::to_address(x"0000000000000000000000000000000000000000000000000000000000000b0b");
let carol = from_bcs::to_address(x"00000000000000000000000000000000000000000000000000000000000ca501");

Expand All @@ -463,7 +478,8 @@ module ol_framework::ol_account {
}

#[test(root = @ol_framework, alice = @0xa11ce, core = @0x1)]
public fun test_transfer_to_resource_account(root: &signer, alice: &signer, core: &signer) {
public fun test_transfer_to_resource_account(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);
Expand All @@ -479,7 +495,8 @@ 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) {
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);
create_account(root, signer::address_of(from));
let recipient_1_addr = signer::address_of(recipient_1);
Expand Down Expand Up @@ -550,7 +567,8 @@ module ol_framework::ol_account {
// }

#[test(root = @ol_framework, user = @0x123)]
public fun test_set_allow_direct_coin_transfers(root: &signer, user: &signer) acquires DirectTransferConfig {
public fun test_set_allow_direct_coin_transfers(root: &signer, user:
&signer) acquires DirectTransferConfig {
let addr = signer::address_of(user);
create_account(root, addr);
set_allow_direct_coin_transfers(user, true);
Expand Down

0 comments on commit 85340b3

Please sign in to comment.