aptos-core icon indicating copy to clipboard operation
aptos-core copied to clipboard

Add test-only code publish function

Open alnoki opened this issue 1 year ago • 17 comments

Current problem

Currently the code module lacks test-only functions, making it difficult for devs to mock out or stub behaviors that depend on autonomous publication during runtime.

In particular, when a package is published via code::publish_package_txn(), an inner call to check_dependencies verifies that each of its dependencies have a code::PackageRegistry resource under their accounts:

https://github.com/aptos-labs/aptos-core/blob/66d9efb7d30b7c6916f79b6ef65f0bdcbe9c6acb/aptos-move/framework/aptos-framework/sources/code.move#L273

In the general case, the only way to ensure that a PackageRegistry exists under an account is to call code::publish_package_txn():

https://github.com/aptos-labs/aptos-core/blob/66d9efb7d30b7c6916f79b6ef65f0bdcbe9c6acb/aptos-move/framework/aptos-framework/sources/code.move#L155-L157

Hence, to run a test that calls code::publish_package_txn(), there first needs to be setup that calls the same function for each of the expected dependencies. Problematically, however, this function relies on an inner call to the native_request_publish native function in code.rs, which errors out with EALREADY_REQUESTED when one attempts to publish two packages in the same txn:

https://github.com/aptos-labs/aptos-core/blob/66d9efb7d30b7c6916f79b6ef65f0bdcbe9c6acb/aptos-move/framework/aptos-framework/sources/code.move#L199-L205

https://github.com/aptos-labs/aptos-core/blob/66d9efb7d30b7c6916f79b6ef65f0bdcbe9c6acb/aptos-move/framework/src/natives/code.rs#L319-L323

Hence it is impossible to write unit tests for code that publishes a package with even a single dependency (because it is impossible to publish both the dependency and the relevant package during the same unit test, since native_request_publish throws EALREADY_REQUESTED).

For packages that have more than one dependency, it is impossible to publish even the dependencies (e.g. to ensure that a code::PackageRegistry exists under each relevant account) for the same reason.

Solution

  • This PR adds a test-only code publish function, code::publish_package_txn_for_testing() which circumvents the above hassle by simply ignoring the blocking native function call (which appears to mainly serve metering purposes and occurs after all other extensive validation)
  • Abstract out the publish func inner logic so that identical code (except for the offending native function call) is used both in tests and during runtime

This approach will allow developers to write unit tests for code that calls code::publish_package_txn()

Housekeeping

The following was run from repository root to ensure CI passes:

cargo build -p aptos-cached-packages
pre-commit run --all-files
scripts/rust_lint.sh
aptos move test --package-dir aptos-move/framework/aptos-framework/ --dev

alnoki avatar May 02 '24 04:05 alnoki

⏱️ 8s total CI duration on this PR

Job Cumulative Duration Recent Runs
permission-check 2s 🟥
permission-check 2s 🟥
permission-check 2s 🟥
permission-check 2s 🟥

settingsfeedbackdocs ⋅ learn more about trunk.io

trunk-io[bot] avatar May 02 '24 04:05 trunk-io[bot]

This issue is stale because it has been open 45 days with no activity. Remove the stale label, comment or push a commit - otherwise this will be closed in 15 days.

github-actions[bot] avatar Jun 17 '24 01:06 github-actions[bot]

Bumping this as the PR eases DevEx

alnoki avatar Jun 17 '24 01:06 alnoki

This issue is stale because it has been open 45 days with no activity. Remove the stale label, comment or push a commit - otherwise this will be closed in 15 days.

github-actions[bot] avatar Aug 02 '24 01:08 github-actions[bot]

Bumping again as the PR eases DevEx

alnoki avatar Aug 02 '24 17:08 alnoki

This issue is stale because it has been open 45 days with no activity. Remove the stale label, comment or push a commit - otherwise this will be closed in 15 days.

github-actions[bot] avatar Sep 18 '24 01:09 github-actions[bot]

Bumping per stale label

alnoki avatar Sep 18 '24 16:09 alnoki