Skip to content

Conversation

pugachAG
Copy link
Contributor

@pugachAG pugachAG commented May 16, 2025

This PR introduces host functions for deploying and using global contracts:

    fn promise_batch_action_deploy_global_contract(promise_index: u64, code_len: u64, code_ptr: u64);
    fn promise_batch_action_deploy_global_contract_by_account_id(promise_index: u64, code_len: u64, code_ptr: u64);
    fn promise_batch_action_use_global_contract(promise_index: u64, code_hash_len: u64, code_hash_ptr: u64);
    fn promise_batch_action_use_global_contract_by_account_id(promise_index: u64, account_id_len: u64, account_id_ptr: u64);

We use separate functions for deployments by hash and by account id to keep it consistent with DeployGlobalContract and DeployGlobalContractByAccountId action views. The same applies for using global contract by hash and by account_id.

Implementation is "monkey see, monkey do" based on other promise_batch_action_deploy_contract.

global_contract_host_fns runtime config param is added to ensure that new functions become available as part of protocol version 78.

Closes #13373.

@pugachAG pugachAG requested a review from nagisa May 16, 2025 19:46
@pugachAG pugachAG requested a review from a team as a code owner May 16, 2025 19:46
@pugachAG pugachAG force-pushed the deploy-global-contract-host-functions branch 2 times, most recently from 2c3aebb to 13df8d0 Compare May 16, 2025 19:57
@@ -76,6 +76,8 @@ extern "C" {
// #######################
fn promise_batch_action_create_account(promise_index: u64);
fn promise_batch_action_deploy_contract(promise_index: u64, code_len: u64, code_ptr: u64);
fn promise_batch_action_deploy_global_contract(promise_index: u64, code_len: u64, code_ptr: u64);
fn promise_batch_action_deploy_global_contract_by_account_id(promise_index: u64, code_len: u64, code_ptr: u64);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, why is deploying a global contract by an account id taking a full code to deploy and not the account id to reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when deploying global contract by account id we use account id of the signer (receipt's predecessor_id), also see DeployGlobalContractAction

Copy link
Contributor

@frol frol May 21, 2025

Choose a reason for hiding this comment

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

@pugachAG Do you mean current_account_id where the contract is executed at? It is totally odd to deploy the contract to the signer/predecessor account!!!

Will it actually replace the currently deployed contract on the account_id?

Would you be open to naming it promise_batch_action_deploy_global_contract_identifiable_by_account_id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean current_account_id where the contract is executed at?

Technically this corresponds to the receipt receiver account_id. DeployGlobalContract action requires predecessor_id to be equal to the receiver_id, so those should be the same.

Will it actually replace the currently deployed contract on the account_id?

No, deploying global contract does not replace anything, it just makes the contract available. UseGlobalContract does that.

Would you be open to naming it

I prefer to keep host function naming consistent with action naming. This name is derived from ActionView::DeployGlobalContractByAccountId

@nagisa
Copy link
Collaborator

nagisa commented May 19, 2025

Ah, I guess you may want to gate these host functions by some sort of a protocol feature/config in order to have these become available deterministically.

Comment on lines 173 to 174
promise_batch_action_deploy_global_contract<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,
promise_batch_action_deploy_global_contract_by_account_id<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
promise_batch_action_deploy_global_contract<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,
promise_batch_action_deploy_global_contract_by_account_id<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,
#[deploy_global_host_fns] promise_batch_action_deploy_global_contract<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,
#[deploy_global_host_fns] promise_batch_action_deploy_global_contract_by_account_id<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,

or something along these lines. This will require a new field in the Config.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added global_contract_host_fns parameter

@pugachAG pugachAG force-pushed the deploy-global-contract-host-functions branch 4 times, most recently from 41d9998 to b2336c4 Compare May 20, 2025 14:03
@pugachAG pugachAG changed the title feat: add host functions for deploying global contracts feat: add promise batch host functions for global contracts May 20, 2025
@pugachAG pugachAG force-pushed the deploy-global-contract-host-functions branch 2 times, most recently from 2d0e0f8 to ed0f375 Compare May 20, 2025 14:57
@pugachAG
Copy link
Contributor Author

@nagisa I've added functions for using global contracts, could you please take another look: ed0f375

@@ -76,6 +76,10 @@ extern "C" {
// #######################
fn promise_batch_action_create_account(promise_index: u64);
fn promise_batch_action_deploy_contract(promise_index: u64, code_len: u64, code_ptr: u64);
fn promise_batch_action_deploy_global_contract(promise_index: u64, code_len: u64, code_ptr: u64);
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume deploys the global contract that is identifiable by its hash, right?

What should be the receiver_account_id for the promise? Anything? Or MUST it be the current_account_id (like the regular deploy action)?

Will it replace the currently deployed contract on the signer, predecessor, or receiver account id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume deploys the global contract that is identifiable by its hash, right?

that is correct

What should be the receiver_account_id for the promise? Anything? Or MUST it be the current_account_id (like the regular deploy action)?

receiver should be the same as predecessor, see check_actor_permissions for DeployGlobalContract action

Will it replace the currently deployed contract on the signer, predecessor, or receiver account id?

promise_batch_action_deploy_global_contract doesn't replace anything, global contracts deployed by hash are not associated with any account. In case of promise_batch_action_deploy_global_contract_by_account_id it replaces current contract deployed under receiver account id (which is required to be the same as predecessor).

github-merge-queue bot pushed a commit that referenced this pull request May 22, 2025
Currently we copy `backwards_compatible_rs_contract.wasm` from `res`
directory. This is no longer necessary since we no longer support old
protocol versions.
This PR changes that to make it compiled from `test-contract-rs` without
`latest_protocol` feature enabled. This way we can make changes test
contract in backward compatible way using `#[cfg(feature =
"latest_protocol")]`.
`backwards_compatible_rs_contract.wasm` is renamed to
`legacy_backwards_compatible_rs_contract.wasm`, we keep it for the
`test_near_vm_artifact_output_stability` test.

We also change `resharding_v3` tests to use
`backwards_compatible_rs_contract` instead of `rs_contract` because that
test suite uses non-latest protocol version. So it imports host
functions that might not yet be exposed in that protocol version which
breaks it (#13565 is one example). Backward compatible contract should
be used in such cases.

---------

Co-authored-by: Simonas Kazlauskas <git@kazlauskas.me>
@pugachAG pugachAG force-pushed the deploy-global-contract-host-functions branch from 392d662 to 1513598 Compare May 23, 2025 13:42
Copy link

codecov bot commented May 23, 2025

Codecov Report

Attention: Patch coverage is 81.66667% with 33 lines in your changes missing coverage. Please review.

Project coverage is 69.43%. Comparing base (d74339f) to head (301b7c2).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
runtime/runtime/src/receipt_manager.rs 52.94% 16 Missing ⚠️
runtime/near-vm-runner/src/logic/logic.rs 93.26% 0 Missing and 7 partials ⚠️
runtime/runtime/src/ext.rs 53.33% 7 Missing ⚠️
core/parameters/src/parameter_table.rs 0.00% 0 Missing and 1 partial ⚠️
core/parameters/src/view.rs 50.00% 1 Missing ⚠️
runtime/near-vm-runner/src/logic/errors.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #13565       +/-   ##
===========================================
+ Coverage    1.62%   69.43%   +67.81%     
===========================================
  Files         661      856      +195     
  Lines      115296   168433    +53137     
  Branches   115296   168433    +53137     
===========================================
+ Hits         1873   116957   +115084     
+ Misses     113367    46687    -66680     
- Partials       56     4789     +4733     
Flag Coverage Δ
pytests 1.51% <0.00%> (?)
pytests-nightly 1.62% <0.00%> (-0.01%) ⬇️
unittests 69.06% <81.66%> (?)
unittests-nightly 68.98% <81.66%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pugachAG pugachAG added this pull request to the merge queue May 23, 2025
Merged via the queue into master with commit 0883a92 May 23, 2025
28 checks passed
@pugachAG pugachAG deleted the deploy-global-contract-host-functions branch May 23, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Runtime doesn't expose host functions for GlobalContracts actions
3 participants