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

ObjectCodeDeployment API cleanup update #12133

Merged
merged 1 commit into from
Feb 21, 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: 6 additions & 6 deletions aptos-move/e2e-move-tests/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl MoveHarness {
account: &Account,
package: &BuiltPackage,
mut patch_metadata: impl FnMut(&mut PackageMetadata),
publisher_ref: AccountAddress,
code_object: AccountAddress,
) -> SignedTransaction {
let code = package.extract_code();
let mut metadata = package
Expand All @@ -424,7 +424,7 @@ impl MoveHarness {
aptos_stdlib::object_code_deployment_upgrade(
bcs::to_bytes(&metadata).expect("PackageMetadata has BCS"),
code,
publisher_ref,
code_object,
),
)
}
Expand All @@ -451,15 +451,15 @@ impl MoveHarness {
path: &Path,
options: BuildOptions,
patch_metadata: impl FnMut(&mut PackageMetadata),
publisher_ref: AccountAddress,
code_object: AccountAddress,
) -> SignedTransaction {
let package =
build_package(path.to_owned(), options).expect("building package must succeed");
self.create_object_code_upgrade_built_package(
account,
&package,
patch_metadata,
publisher_ref,
code_object,
)
}

Expand Down Expand Up @@ -531,10 +531,10 @@ impl MoveHarness {
account: &Account,
path: &Path,
options: BuildOptions,
publisher_ref: AccountAddress,
code_object: AccountAddress,
) -> TransactionStatus {
let txn =
self.create_object_code_upgrade_package(account, path, options, |_| {}, publisher_ref);
self.create_object_code_upgrade_package(account, path, options, |_| {}, code_object);
self.run(txn)
}

Expand Down
14 changes: 7 additions & 7 deletions aptos-move/e2e-move-tests/src/tests/object_code_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{assert_abort, assert_success, assert_vm_status, tests::common, MoveH
use aptos_framework::{
natives::{
code::{PackageRegistry, UpgradePolicy},
object_code_deployment::PublisherRef,
object_code_deployment::ManagingRefs,
},
BuildOptions,
};
Expand Down Expand Up @@ -150,16 +150,16 @@ fn object_code_deployment_publish_package(enabled: Vec<FeatureFlag>, disabled: V
assert_eq!(registry.packages[0].modules.len(), 1);
assert_eq!(registry.packages[0].modules[0].name, "test");

let publisher_ref: PublisherRef = context
let code_object: ManagingRefs = context
.harness
.read_resource_from_resource_group(
&context.object_address,
parse_struct_tag("0x1::object::ObjectGroup").unwrap(),
parse_struct_tag("0x1::object_code_deployment::PublisherRef").unwrap(),
parse_struct_tag("0x1::object_code_deployment::ManagingRefs").unwrap(),
)
.unwrap();
// Verify the object created owns the `PublisherRef`
assert_eq!(publisher_ref, PublisherRef::new(context.object_address));
// Verify the object created owns the `ManagingRefs`
assert_eq!(code_object, ManagingRefs::new(context.object_address));

let module_address = context.object_address.to_string();
assert_success!(context.harness.run_entry_function(
Expand Down Expand Up @@ -244,8 +244,8 @@ fn object_code_deployment_upgrade_fail_when_publisher_ref_does_not_exist() {
let mut context = TestContext::new(None, None);
let acc = context.account.clone();

// We should not be able to `upgrade` as `PublisherRef` does not exist.
// `PublisherRef` is only created when calling `publish` first, i.e. deploying a package.
// We should not be able to `upgrade` as `ManagingRefs` does not exist.
// `ManagingRefs` is only created when calling `publish` first, i.e. deploying a package.
let status = context.execute_object_code_action(
&acc,
"object_code_deployment.data/pack_initial",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Publishing modules flow:
1. Create a new object with the address derived from the publisher address and the object seed.
2. Publish the module passed in the function via <code>metadata_serialized</code> and <code><a href="code.md#0x1_code">code</a></code> to the newly created object.
3. Emits 'Publish' event with the address of the newly created object.
4. Create a <code><a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a></code> which stores the extend ref of the newly created object.
4. Create a <code><a href="object_code_deployment.md#0x1_object_code_deployment_ManagingRefs">ManagingRefs</a></code> which stores the extend ref of the newly created object.
Note: This is needed to upgrade the code as the signer must be generated to upgrade the existing code in an object.

Upgrading modules flow:
Expand All @@ -35,7 +35,7 @@ Note: There is no unfreeze function as this gives no benefit if the user can fre
Once modules are marked as immutable, they cannot be made mutable again.


- [Resource `PublisherRef`](#0x1_object_code_deployment_PublisherRef)
- [Resource `ManagingRefs`](#0x1_object_code_deployment_ManagingRefs)
- [Struct `Publish`](#0x1_object_code_deployment_Publish)
- [Struct `Upgrade`](#0x1_object_code_deployment_Upgrade)
- [Struct `Freeze`](#0x1_object_code_deployment_Freeze)
Expand All @@ -59,15 +59,15 @@ Once modules are marked as immutable, they cannot be made mutable again.



<a id="0x1_object_code_deployment_PublisherRef"></a>
<a id="0x1_object_code_deployment_ManagingRefs"></a>

## Resource `PublisherRef`
## Resource `ManagingRefs`

Object which contains the code deployed, along with the extend ref to upgrade the code.
Internal struct, attached to the object, that holds Refs we need to manage the code deployment (i.e. upgrades).


<pre><code>#[resource_group_member(#[group = <a href="object.md#0x1_object_ObjectGroup">0x1::object::ObjectGroup</a>])]
<b>struct</b> <a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a> <b>has</b> key
<b>struct</b> <a href="object_code_deployment.md#0x1_object_code_deployment_ManagingRefs">ManagingRefs</a> <b>has</b> key
</code></pre>


Expand Down Expand Up @@ -256,7 +256,7 @@ the code to be published via <code><a href="code.md#0x1_code">code</a></code>. T

<a href="event.md#0x1_event_emit">event::emit</a>(<a href="object_code_deployment.md#0x1_object_code_deployment_Publish">Publish</a> { object_address: <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(code_signer), });

<b>move_to</b>(code_signer, <a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a> {
<b>move_to</b>(code_signer, <a href="object_code_deployment.md#0x1_object_code_deployment_ManagingRefs">ManagingRefs</a> {
extend_ref: <a href="object.md#0x1_object_generate_extend_ref">object::generate_extend_ref</a>(constructor_ref),
});
}
Expand Down Expand Up @@ -304,7 +304,7 @@ Note: If the modules were deployed as immutable when calling <code>publish</code
Requires the publisher to be the owner of the <code>code_object</code>.


<pre><code><b>public</b> entry <b>fun</b> <a href="object_code_deployment.md#0x1_object_code_deployment_upgrade">upgrade</a>(publisher: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, metadata_serialized: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, <a href="code.md#0x1_code">code</a>: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;, code_object: <a href="object.md#0x1_object_Object">object::Object</a>&lt;<a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">object_code_deployment::PublisherRef</a>&gt;)
<pre><code><b>public</b> entry <b>fun</b> <a href="object_code_deployment.md#0x1_object_code_deployment_upgrade">upgrade</a>(publisher: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, metadata_serialized: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, <a href="code.md#0x1_code">code</a>: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;, code_object: <a href="object.md#0x1_object_Object">object::Object</a>&lt;<a href="code.md#0x1_code_PackageRegistry">code::PackageRegistry</a>&gt;)
</code></pre>


Expand All @@ -317,18 +317,18 @@ Requires the publisher to be the owner of the <code>code_object</code>.
publisher: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
metadata_serialized: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
<a href="code.md#0x1_code">code</a>: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;,
code_object: Object&lt;<a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a>&gt;,
) <b>acquires</b> <a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a> {
code_object: Object&lt;PackageRegistry&gt;,
) <b>acquires</b> <a href="object_code_deployment.md#0x1_object_code_deployment_ManagingRefs">ManagingRefs</a> {
<b>let</b> publisher_address = <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(publisher);
<b>assert</b>!(
<a href="object.md#0x1_object_is_owner">object::is_owner</a>(code_object, publisher_address),
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_permission_denied">error::permission_denied</a>(<a href="object_code_deployment.md#0x1_object_code_deployment_ENOT_CODE_OBJECT_OWNER">ENOT_CODE_OBJECT_OWNER</a>),
);

<b>let</b> code_object_address = <a href="object.md#0x1_object_object_address">object::object_address</a>(&code_object);
<b>assert</b>!(<b>exists</b>&lt;<a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a>&gt;(code_object_address), <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="object_code_deployment.md#0x1_object_code_deployment_ECODE_OBJECT_DOES_NOT_EXIST">ECODE_OBJECT_DOES_NOT_EXIST</a>));
<b>assert</b>!(<b>exists</b>&lt;<a href="object_code_deployment.md#0x1_object_code_deployment_ManagingRefs">ManagingRefs</a>&gt;(code_object_address), <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="object_code_deployment.md#0x1_object_code_deployment_ECODE_OBJECT_DOES_NOT_EXIST">ECODE_OBJECT_DOES_NOT_EXIST</a>));

<b>let</b> extend_ref = &<b>borrow_global</b>&lt;<a href="object_code_deployment.md#0x1_object_code_deployment_PublisherRef">PublisherRef</a>&gt;(code_object_address).extend_ref;
<b>let</b> extend_ref = &<b>borrow_global</b>&lt;<a href="object_code_deployment.md#0x1_object_code_deployment_ManagingRefs">ManagingRefs</a>&gt;(code_object_address).extend_ref;
<b>let</b> code_signer = &<a href="object.md#0x1_object_generate_signer_for_extending">object::generate_signer_for_extending</a>(extend_ref);
<a href="code.md#0x1_code_publish_package_txn">code::publish_package_txn</a>(code_signer, metadata_serialized, <a href="code.md#0x1_code">code</a>);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// 1. Create a new object with the address derived from the publisher address and the object seed.
/// 2. Publish the module passed in the function via `metadata_serialized` and `code` to the newly created object.
/// 3. Emits 'Publish' event with the address of the newly created object.
/// 4. Create a `PublisherRef` which stores the extend ref of the newly created object.
/// 4. Create a `ManagingRefs` which stores the extend ref of the newly created object.
/// Note: This is needed to upgrade the code as the signer must be generated to upgrade the existing code in an object.
///
/// Upgrading modules flow:
Expand Down Expand Up @@ -51,8 +51,8 @@ module aptos_framework::object_code_deployment {
const OBJECT_CODE_DEPLOYMENT_DOMAIN_SEPARATOR: vector<u8> = b"aptos_framework::object_code_deployment";

#[resource_group_member(group = aptos_framework::object::ObjectGroup)]
/// Object which contains the code deployed, along with the extend ref to upgrade the code.
struct PublisherRef has key {
/// Internal struct, attached to the object, that holds Refs we need to manage the code deployment (i.e. upgrades).
struct ManagingRefs has key {
/// We need to keep the extend ref to be able to generate the signer to upgrade existing code.
extend_ref: ExtendRef,
}
Expand Down Expand Up @@ -97,7 +97,7 @@ module aptos_framework::object_code_deployment {

event::emit(Publish { object_address: signer::address_of(code_signer), });

move_to(code_signer, PublisherRef {
move_to(code_signer, ManagingRefs {
extend_ref: object::generate_extend_ref(constructor_ref),
});
}
Expand All @@ -118,18 +118,18 @@ module aptos_framework::object_code_deployment {
publisher: &signer,
metadata_serialized: vector<u8>,
code: vector<vector<u8>>,
code_object: Object<PublisherRef>,
) acquires PublisherRef {
code_object: Object<PackageRegistry>,
) acquires ManagingRefs {
let publisher_address = signer::address_of(publisher);
assert!(
object::is_owner(code_object, publisher_address),
error::permission_denied(ENOT_CODE_OBJECT_OWNER),
);

let code_object_address = object::object_address(&code_object);
assert!(exists<PublisherRef>(code_object_address), error::not_found(ECODE_OBJECT_DOES_NOT_EXIST));
assert!(exists<ManagingRefs>(code_object_address), error::not_found(ECODE_OBJECT_DOES_NOT_EXIST));

let extend_ref = &borrow_global<PublisherRef>(code_object_address).extend_ref;
let extend_ref = &borrow_global<ManagingRefs>(code_object_address).extend_ref;
let code_signer = &object::generate_signer_for_extending(extend_ref);
code::publish_package_txn(code_signer, metadata_serialized, code);

Expand Down
4 changes: 2 additions & 2 deletions aptos-move/framework/cached-packages/src/aptos_stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn publish_module_source(module_name: &str, module_src: &str) -> Transaction
pub fn object_code_deployment_upgrade(
metadata_serialized: Vec<u8>,
code: Vec<Vec<u8>>,
publisher_ref: AccountAddress,
code_object: AccountAddress,
) -> TransactionPayload {
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
Expand All @@ -59,7 +59,7 @@ pub fn object_code_deployment_upgrade(
vec![
bcs::to_bytes(&metadata_serialized).unwrap(),
bcs::to_bytes(&code).unwrap(),
bcs::to_bytes(&publisher_ref).unwrap(),
bcs::to_bytes(&code_object).unwrap(),
],
))
}
Expand Down
6 changes: 3 additions & 3 deletions aptos-move/framework/src/natives/object_code_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ pub struct ExtendRef {
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct PublisherRef {
pub struct ManagingRefs {
pub extend_ref: ExtendRef,
}

impl PublisherRef {
impl ManagingRefs {
pub fn new(address: AccountAddress) -> Self {
PublisherRef {
ManagingRefs {
extend_ref: ExtendRef { address },
}
}
Expand Down
Loading