Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support freeze package upgrading #1808

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions frameworks/moveos-stdlib/doc/module_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [Function `publish_modules`](#0x2_module_store_publish_modules)
- [Function `publish_modules_entry`](#0x2_module_store_publish_modules_entry)
- [Function `publish_modules_internal`](#0x2_module_store_publish_modules_internal)
- [Function `freeze_package`](#0x2_module_store_freeze_package)
- [Function `add_to_allowlist`](#0x2_module_store_add_to_allowlist)
- [Function `remove_from_allowlist`](#0x2_module_store_remove_from_allowlist)
- [Function `is_in_allowlist`](#0x2_module_store_is_in_allowlist)
Expand Down Expand Up @@ -198,6 +199,17 @@ Return true if the modules are upgraded



<a name="0x2_module_store_freeze_package"></a>

## Function `freeze_package`



<pre><code><b>public</b> <b>fun</b> <a href="module_store.md#0x2_module_store_freeze_package">freeze_package</a>(package: <a href="object.md#0x2_object_Object">object::Object</a>&lt;<a href="module_store.md#0x2_module_store_Package">module_store::Package</a>&gt;)
</code></pre>



<a name="0x2_module_store_add_to_allowlist"></a>

## Function `add_to_allowlist`
Expand Down
45 changes: 38 additions & 7 deletions frameworks/moveos-stdlib/sources/module_store.move
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,21 @@ module moveos_std::module_store {

/// Publish modules to the module object's storage
/// Return true if the modules are upgraded
public(friend) fun publish_modules_internal(module_object: &mut Object<ModuleStore>, package_id: address, modules: vector<MoveModule>) : bool {
public(friend) fun publish_modules_internal(
module_object: &mut Object<ModuleStore>, package_id: address, modules: vector<MoveModule>
) : bool {
let i = 0;
let len = vector::length(&modules);
let (module_names, module_names_with_init_fn, indices) = move_module::sort_and_verify_modules(&modules, package_id);

let is_upgrade = true;
if (!exists_package(module_object, package_id)) {
let package = object::add_object_field_with_id(module_object, package_id, Package {});
object::transfer_extend(package, tx_context::sender());
// TODO: should we transfer the Package object to tx sender or package id address?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package id address must equal the tx sender?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not always the same. For example, sequencer publish the framework modules, the tx_sender should be sequencer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sequencer can only upgrade the framework modules and can not publish a new package with the framework address.

create_package(module_object, package_id, tx_context::sender());
is_upgrade = false;
};
let package = borrow_mut_package(module_object, package_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if the Package is frozen and abort the transaction with an error code from the module_store.


let upgrade_flag = false;
while (i < len) {
let module_name = vector::pop_back(&mut module_names);
let index = vector::pop_back(&mut indices);
Expand All @@ -129,7 +132,6 @@ module moveos_std::module_store {
if (object::contains_field(package, module_name)) {
let old_m = remove_module(package, module_name);
move_module::check_comatibility(m, &old_m);
upgrade_flag = true;
} else {
// request init function invoking
if (vector::contains(&module_names_with_init_fn, &module_name)) {
Expand All @@ -140,7 +142,12 @@ module moveos_std::module_store {
add_module(package, module_name, *m);
i = i + 1;
};
upgrade_flag
is_upgrade
}

fun create_package(module_object: &mut Object<ModuleStore>, package_id: address, owner: address) {
let package = object::add_object_field_with_id(module_object, package_id, Package {});
object::transfer_extend(package, owner);
}

fun borrow_package(module_store: &Object<ModuleStore>, package_id: address): &Object<Package> {
Expand Down Expand Up @@ -173,6 +180,12 @@ module moveos_std::module_store {
object::borrow_mut<Allowlist>(allowlist_obj)
}

public fun freeze_package(package: Object<Package>) {
object::to_frozen(package);
}

/************************ allowlist functions *************************/

/// Add an account to the allowlist. Only account in allowlist can publish modules.
/// This is only valid when module_publishing_allowlist_enabled feature is enabled.
public fun add_to_allowlist(account: &signer, publisher: address) {
Expand Down Expand Up @@ -213,7 +226,6 @@ module moveos_std::module_store {
#[test_only]
const COUNTER_MV_BYTES: vector<u8> = x"a11ceb0b060000000b01000402040403082b04330605391c07557908ce0140068e02220ab002050cb502640d9903020000010100020c000003000000000400000000050100000006010000000700020001080506010c01090700010c010a0508010c0504060407040001060c01030107080001080001050107090002060c09000106090007636f756e746572076163636f756e7407436f756e74657208696e63726561736509696e6372656173655f04696e69740d696e69745f666f725f746573740576616c756513626f72726f775f6d75745f7265736f75726365106d6f76655f7265736f757263655f746f0f626f72726f775f7265736f757263650000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000000205200000000000000000000000000000000000000000000000000000000000000042000201070300010400000211010201010000030c070038000c000a00100014060100000000000000160b000f0015020200000000050b0006000000000000000012003801020301000000050b0006000000000000000012003801020401000000050700380210001402000000";


#[test_only]
fun drop_module_store(self: Object<ModuleStore>) {
let ModuleStore {} = object::drop_unchecked(self);
Expand Down Expand Up @@ -275,5 +287,24 @@ module moveos_std::module_store {
remove_from_allowlist(&system_account, @0x42);
assert!(!is_in_allowlist(@0x42), 3);
}

#[test(account=@0x42)]
#[expected_failure(abort_code = 9, location = object)]
fun test_frozen_package(account: &signer) {
init_module_store();
features::init_feature_store_for_test();

let module_object = borrow_mut_module_store();
let module_bytes = COUNTER_MV_BYTES;
let m: MoveModule = move_module::new(module_bytes);

publish_modules(module_object, account, vector::singleton(m));
publish_modules(module_object, account, vector::singleton(m));

let package_obj_id = object::custom_child_object_id<address, Package>(object::id(module_object), signer::address_of(account));
let (_, package) = object::take_object_extend< Package>(package_obj_id);
freeze_package(package);
publish_modules(module_object, account, vector::singleton(m));
}
}

Loading