Skip to content

Commit 8567643

Browse files
committed
feat: destroy_account_from function to decommission core resources account
1 parent c66675e commit 8567643

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

aptos-move/framework/aptos-framework/sources/account.move

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,28 @@ module aptos_framework::account {
240240
new_account
241241
}
242242

243+
/// Destroy the Account resource from a given account.
244+
/// Used to destroy the core resources account on mainnet.
245+
public fun destroy_account_from(account: &signer, from: address) acquires Account {
246+
system_addresses::assert_aptos_framework(account);
247+
248+
let Account {
249+
authentication_key: _,
250+
sequence_number: _,
251+
guid_creation_num: _,
252+
coin_register_events,
253+
key_rotation_events,
254+
rotation_capability_offer,
255+
signer_capability_offer,
256+
} = move_from<Account>(from);
257+
258+
event::destroy_event_handle(account, coin_register_events);
259+
event::destroy_event_handle(account, key_rotation_events);
260+
261+
let CapabilityOffer { for: _ } = rotation_capability_offer;
262+
let CapabilityOffer { for: _ } = signer_capability_offer;
263+
}
264+
243265
#[view]
244266
public fun exists_at(addr: address): bool {
245267
exists<Account>(addr)
@@ -953,6 +975,26 @@ module aptos_framework::account {
953975
);
954976
}
955977

978+
#[test(aptos_framework = @aptos_framework, from = @0xdead)]
979+
public entry fun test_destroy_account_from(
980+
aptos_framework: &signer,
981+
from: &signer,
982+
) acquires Account {
983+
// Ensure the Account resource exists under the from account
984+
if (!exists<Account>(signer::address_of(from))) {
985+
create_account(signer::address_of(from));
986+
};
987+
988+
// Confirm it now exists
989+
assert!(exists<Account>(signer::address_of(from)), 1);
990+
991+
// Destroy the Account resource
992+
destroy_account_from(aptos_framework, signer::address_of(from));
993+
994+
// Confirm the resource has been removed
995+
assert!(!exists<Account>(signer::address_of(from)), 2);
996+
}
997+
956998
#[test_only]
957999
struct DummyResource has key {}
9581000

aptos-move/framework/aptos-framework/sources/event.move

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module aptos_framework::event {
66
use std::bcs;
77

88
use aptos_framework::guid::GUID;
9+
use aptos_framework::system_addresses;
10+
use aptos_framework::guid;
911

1012
friend aptos_framework::account;
1113
friend aptos_framework::object;
@@ -38,6 +40,12 @@ module aptos_framework::event {
3840
guid: GUID,
3941
}
4042

43+
public fun destroy_event_handle<T: drop + store>(account: &signer, handle: EventHandle<T>) {
44+
system_addresses::assert_aptos_framework(account);
45+
46+
let EventHandle { counter: _, guid: _ } = handle;
47+
}
48+
4149
#[deprecated]
4250
/// Use EventHandleGenerator to generate a unique event handle for `sig`
4351
public(friend) fun new_event_handle<T: drop + store>(guid: GUID): EventHandle<T> {
@@ -89,4 +97,21 @@ module aptos_framework::event {
8997
use std::vector;
9098
vector::contains(&emitted_events_by_handle(handle), msg)
9199
}
100+
101+
#[test_only]
102+
struct TestEvent has drop, store {}
103+
104+
#[test_only]
105+
public fun create_test_event_handle<T: drop + store>(): EventHandle<T> {
106+
let dummy_address = @0x1;
107+
let dummy_creation_num = 0;
108+
let guid = guid::create(dummy_address, &mut dummy_creation_num);
109+
new_event_handle<T>(guid)
110+
}
111+
112+
#[test(account = @0x1)]
113+
public entry fun test_destroy_event_handle(account: signer) {
114+
let handle = create_test_event_handle<TestEvent>();
115+
destroy_event_handle(&account, handle);
116+
}
92117
}

aptos-move/framework/aptos-framework/sources/guid.move

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module aptos_framework::guid {
33
friend aptos_framework::account;
44
friend aptos_framework::object;
5+
friend aptos_framework::event;
56

67
/// A globally unique identifier derived from the sender's address and a counter
78
struct GUID has drop, store {

0 commit comments

Comments
 (0)