Skip to content

Commit

Permalink
Use compile_and_publish_move_package method in `large_package_publi…
Browse files Browse the repository at this point in the history
…sher` example
  • Loading branch information
0xjunha committed Oct 25, 2024
1 parent 123255f commit 74e91ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 86 deletions.
12 changes: 7 additions & 5 deletions aptos_sdk/package_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ async def upgrade_package_object(
)
return await self.client.submit_bcs_transaction(signed_transaction)

async def publish_move_package(
async def compile_and_publish_move_package(
self,
sender: Account,
package_dir: str,
Expand All @@ -339,25 +339,25 @@ async def publish_move_package(
Note: This method requires the local Aptos CLI for compilation and will not work without it.
"""

if not AptosCLIWrapper.does_cli_exist():
raise Exception("AptosCLI does not exist.")
AptosCLIWrapper.assert_cli_exists()

# Determine the account or object address for publishing the package.
if publish_mode == PublishMode.ACCOUNT_DEPLOY:
deploy_address = sender.address()

elif publish_mode == PublishMode.OBJECT_DEPLOY:
# Calculate the number of transactions needed for the chunked publish to predict the code object address.
# Start by assuming a single transaction for a preliminary build to estimate the artifact size.
deploy_address = await CompileHelper.derive_object_address(
self.client, sender.address()
)

# Compile the package as a preliminary build to check if chunking is required.
AptosCLIWrapper.compile_package(package_dir, {module_name: deploy_address})
metadata, modules = PublishHelper.load_package_artifacts(package_dir)

# If the package size requires chunked publishing, recalculate the deploy address.
if PublishHelper.is_large_package(metadata, modules):
# Number of transactions required for the chunked publish.
required_txns = len(
PublishHelper.prepare_chunked_payloads(
metadata,
Expand Down Expand Up @@ -385,6 +385,8 @@ async def publish_move_package(
# Compile the package with the correct deployment address.
AptosCLIWrapper.compile_package(package_dir, {module_name: deploy_address})

print(f"Deploying {module_name} to {deploy_address}...")

return await self.publish_package_in_path(
sender,
package_dir,
Expand Down
101 changes: 20 additions & 81 deletions examples/large_package_publisher.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# Copyright © Aptos Foundation
# SPDX-License-Identifier: Apache-2.0

"""
This example demonstrates publishing large Move packages which cannot fit in a single transaction, using the most
abstract method `compile_and_publish_move_package` from the `PackagePublisher` class. This method handles all necessary
steps for compiling and publishing both regular and large packages.
Note: This method requires the presence of the Aptos CLI in `APTOS_CLI_PATH`. As an alternative, if you want finer
control over the process or do not want to rely on the CLI, you may use `publish_package_in_path`, which is
demonstrated in the `object_code_deployment.py` example.
"""

import asyncio
import os
import sys

import aptos_sdk.cli as aptos_sdk_cli
from aptos_sdk.account import Account
from aptos_sdk.account_address import AccountAddress
from aptos_sdk.aptos_cli_wrapper import AptosCLIWrapper
from aptos_sdk.async_client import ClientConfig, FaucetClient, RestClient
from aptos_sdk.package_publisher import (
MODULE_ADDRESS,
CompileHelper,
PackagePublisher,
PublishHelper,
PublishMode,
)
from aptos_sdk.package_publisher import MODULE_ADDRESS, PackagePublisher, PublishMode

from .common import APTOS_CORE_PATH, FAUCET_URL, NODE_URL

Expand Down Expand Up @@ -58,97 +62,32 @@ async def main(
# Name of the move module for the package to be published, containing artifacts larger than the MAX_CHUNK_SIZE
module_name = "large_package_example"

# Example 1. Account deployment
# -- Example 1. Account deployment
print("=== Publishing large package to account ===")

if AptosCLIWrapper.does_cli_exist():
AptosCLIWrapper.compile_package(
large_package_example_dir, {module_name: alice.address()}
)
else:
input("\nUpdate the module with Alice's address, compile, and press Enter.")

account_deploy_txn_hash = await publisher.publish_package_in_path(
alice, large_package_example_dir, large_packages_module_address
account_deploy_txn_hash = await publisher.compile_and_publish_move_package(
alice, large_package_example_dir, module_name, large_packages_module_address
)

print(f"Tx submitted: {account_deploy_txn_hash[0]}")
await rest_client.wait_for_transaction(account_deploy_txn_hash[0])
print(f"Package deployed to account {alice.address()}")
print("Transaction completed.")

# Example 2. Object code deployment
# Note: Here we assume that we already know we should use the chunked publish mode, so we run a preliminary build.
# ----- Example 2. Object code deployment
print("=== Publishing large package to object ===")

# Calculate the number of transactions needed for the chunked publish to predict the code object address.
# Start by deriving the address assuming a single transaction for a preliminary build to estimate artifact size.
code_object_address = await CompileHelper.derive_object_address(
rest_client, alice.address()
)

print("\nCompiling package as a preliminary build...")
if AptosCLIWrapper.does_cli_exist():
AptosCLIWrapper.compile_package(
large_package_example_dir, {module_name: code_object_address}
)
else:
print(f"Address of the object to be created: {code_object_address}")
input(
"\nUpdate the module with the derived code object address, compile, and press enter."
)

metadata, modules = PublishHelper.load_package_artifacts(large_package_example_dir)

# Number of transactions required for the chunked publish.
required_txns = len(
PublishHelper.prepare_chunked_payloads(
metadata,
modules,
large_packages_module_address,
PublishMode.OBJECT_DEPLOY,
)
)

if required_txns > 1:
code_object_address = await CompileHelper.derive_object_address(
rest_client, alice.address(), required_txns
)
print("\nCompiling the package with updated object address...")
if AptosCLIWrapper.does_cli_exist():
AptosCLIWrapper.compile_package(
large_package_example_dir, {module_name: code_object_address}
)
else:
print(f"Address of the object to be created: {code_object_address}")
input(
"\nUpdate the module with the derived code object address, compile, and press enter."
)

object_deploy_txn_hash = await publisher.publish_package_in_path(
object_deploy_txn_hash = await publisher.compile_and_publish_move_package(
alice,
large_package_example_dir,
module_name,
large_packages_module_address,
PublishMode.OBJECT_DEPLOY,
)

print(f"The last tx submitted: {object_deploy_txn_hash[-1]}")
await rest_client.wait_for_transaction(object_deploy_txn_hash[-1])
print(f"Package deployed to object {code_object_address}")

# Example 3. Object code upgrade
print("=== Upgrading large package object ===")

object_upgrade_txn_hash = await publisher.publish_package_in_path(
alice,
large_package_example_dir,
large_packages_module_address,
PublishMode.OBJECT_UPGRADE,
code_object_address,
)
print("Transaction completed.")

print(f"The last tx submitted: {object_upgrade_txn_hash[-1]}")
await rest_client.wait_for_transaction(object_upgrade_txn_hash[-1])
print(f"Package in object {code_object_address} upgraded")
await rest_client.close()


Expand Down
9 changes: 9 additions & 0 deletions examples/object_code_deployment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Copyright © Aptos Foundation
# SPDX-License-Identifier: Apache-2.0

"""
This example demonstrates publishing Move packages using the `publish_package_in_path` method from the
`PackagePublisher` class. This method provides more control over the package publishing process, directly loading
artifacts from a pre-compiled package directory and handling both Account and Object deployment.
Note: For a higher-level abstraction that handles compilation and deployment automatically, you may use
`compile_and_publish_move_package`, as demonstrated in the `large_package_publisher.py` example.
"""

import asyncio
import os
import sys
Expand Down

0 comments on commit 74e91ab

Please sign in to comment.