From 85340b36986d7c08ba69ff2117f01e2abc980fd5 Mon Sep 17 00:00:00 2001 From: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com> Date: Tue, 24 Oct 2023 23:52:05 +0000 Subject: [PATCH] init burn tracker --- .../sources/ol_sources/libra_coin.move | 19 ++++++-- .../sources/ol_sources/ol_account.move | 46 +++++++++++++------ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/framework/libra-framework/sources/ol_sources/libra_coin.move b/framework/libra-framework/sources/ol_sources/libra_coin.move index 9d7c07fe6..cef2c767b 100644 --- a/framework/libra-framework/sources/ol_sources/libra_coin.move +++ b/framework/libra-framework/sources/ol_sources/libra_coin.move @@ -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; @@ -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, MintCapability) { + public(friend) fun initialize_for_core(diem_framework: &signer): + (BurnCapability, MintCapability) { system_addresses::assert_diem_framework(diem_framework); let (burn_cap, freeze_cap, mint_cap) = coin::initialize_with_parallelizable_supply( @@ -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(@ol_framework)) { move_to(diem_framework, FinalSupply { value: final_supply }); + } else { + let state = borrow_global_mut(@ol_framework); + state.value = final_supply } } @@ -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 acquires MintCapStore { + system_addresses::assert_diem_framework(diem_framework); + // move_to(diem_framework, MintCapStore { mint_cap }); + let state = borrow_global_mut(@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 diff --git a/framework/libra-framework/sources/ol_sources/ol_account.move b/framework/libra-framework/sources/ol_sources/ol_account.move index 5f4ee3ea0..bcef4b237 100644 --- a/framework/libra-framework/sources/ol_sources/ol_account.move +++ b/framework/libra-framework/sources/ol_sources/ol_account.move @@ -89,6 +89,7 @@ module ol_framework::ol_account { let new_signer = account::create_account(auth_key); coin::register(&new_signer); receipts::user_init(&new_signer); + init_burn_tracker(&new_signer); } // #[test_only] @@ -123,6 +124,7 @@ module ol_framework::ol_account { ); coin::register(&new_signer); + init_burn_tracker(&new_signer); new_signer } @@ -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
, amounts: vector) { + public entry fun batch_transfer(source: &signer, recipients: + vector
, amounts: vector) acquires BurnTracker { let recipients_len = vector::length(&recipients); assert!( recipients_len == vector::length(&amounts), @@ -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(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 @@ -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(addr); + if (exists(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() {} @@ -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"); @@ -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(resource_acc_addr), 0); @@ -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); @@ -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);