Skip to content

Commit 834caf0

Browse files
authored
test: add global contract update test (#12992)
This PR introduces test loop test for updating global contract deployed by account id. Part of #12718.
1 parent c2b095c commit 834caf0

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

test-loop-tests/Cargo.toml

+39
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,42 @@ near-test-contracts.workspace = true
4242
near-vm-runner.workspace = true
4343
nearcore.workspace = true
4444
testlib.workspace = true
45+
46+
[features]
47+
nightly = [
48+
"near-async/nightly",
49+
"near-chain-configs/nightly",
50+
"near-chain/nightly",
51+
"near-chunks/nightly",
52+
"near-client/nightly",
53+
"near-epoch-manager/nightly",
54+
"near-jsonrpc/nightly",
55+
"near-network/nightly",
56+
"near-o11y/nightly",
57+
"near-parameters/nightly",
58+
"near-primitives-core/nightly",
59+
"near-primitives/nightly",
60+
"near-store/nightly",
61+
"near-vm-runner/nightly",
62+
"nearcore/nightly",
63+
"nightly_protocol",
64+
"testlib/nightly",
65+
]
66+
nightly_protocol = [
67+
"near-async/nightly_protocol",
68+
"near-chain-configs/nightly_protocol",
69+
"near-chain/nightly_protocol",
70+
"near-chunks/nightly_protocol",
71+
"near-client/nightly_protocol",
72+
"near-epoch-manager/nightly_protocol",
73+
"near-jsonrpc/nightly_protocol",
74+
"near-network/nightly_protocol",
75+
"near-o11y/nightly_protocol",
76+
"near-parameters/nightly_protocol",
77+
"near-primitives-core/nightly_protocol",
78+
"near-primitives/nightly_protocol",
79+
"near-store/nightly_protocol",
80+
"near-vm-runner/nightly_protocol",
81+
"nearcore/nightly_protocol",
82+
"testlib/nightly_protocol",
83+
]

test-loop-tests/src/tests/global_contracts.rs

+68-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use near_o11y::testonly::init_test_logger;
1212
use near_parameters::{ActionCosts, RuntimeConfigStore, RuntimeFeesConfig};
1313
use near_primitives::action::{GlobalContractDeployMode, GlobalContractIdentifier};
1414
use near_primitives::epoch_manager::EpochConfigStore;
15-
use near_primitives::errors::{ActionError, ActionErrorKind, TxExecutionError};
15+
use near_primitives::errors::{
16+
ActionError, ActionErrorKind, FunctionCallError, MethodResolveError, TxExecutionError,
17+
};
1618
use near_primitives::hash::CryptoHash;
1719
use near_primitives::shard_layout::ShardLayout;
1820
use near_primitives::test_utils::create_user_test_signer;
@@ -80,6 +82,46 @@ fn test_use_non_existent_global_contract() {
8082
env.shutdown();
8183
}
8284

85+
#[cfg_attr(not(feature = "nightly_protocol"), ignore)]
86+
#[test]
87+
fn test_global_contract_update() {
88+
let mut env = GlobalContractsTestEnv::setup(1000 * ONE_NEAR);
89+
let use_accounts = [env.account_shard_0.clone(), env.account_shard_1.clone()];
90+
91+
env.deploy_trivial_global_contract(GlobalContractDeployMode::AccountId);
92+
93+
for account in &use_accounts {
94+
env.use_global_contract(
95+
account,
96+
GlobalContractIdentifier::AccountId(env.deploy_account.clone()),
97+
);
98+
99+
// Currently deployed trivial contract doesn't have any methods,
100+
// so we expect any function call to fail with MethodNotFound error
101+
let call_tx = env.call_global_contract_tx(account);
102+
let call_outcome = env.execute_tx(call_tx);
103+
assert_matches!(
104+
call_outcome.status,
105+
FinalExecutionStatus::Failure(TxExecutionError::ActionError(ActionError {
106+
kind: ActionErrorKind::FunctionCallError(FunctionCallError::MethodResolveError(
107+
MethodResolveError::MethodNotFound
108+
)),
109+
index: _
110+
}))
111+
);
112+
}
113+
114+
env.deploy_global_contract(GlobalContractDeployMode::AccountId);
115+
116+
for account in &use_accounts {
117+
// Function call should be successful after deploying rs contract
118+
// containing the function we call here
119+
env.call_global_contract(account);
120+
}
121+
122+
env.shutdown();
123+
}
124+
83125
fn test_deploy_and_call_global_contract(deploy_mode: GlobalContractDeployMode) {
84126
const INITIAL_BALANCE: Balance = 1000 * ONE_NEAR;
85127
let mut env = GlobalContractsTestEnv::setup(INITIAL_BALANCE);
@@ -185,25 +227,41 @@ impl GlobalContractsTestEnv {
185227
}
186228
}
187229

188-
fn deploy_global_contract_tx(
230+
fn deploy_global_contract_custom_tx(
189231
&mut self,
190232
deploy_mode: GlobalContractDeployMode,
233+
contract_code: Vec<u8>,
191234
) -> SignedTransaction {
192235
SignedTransaction::deploy_global_contract(
193236
self.next_nonce(),
194237
self.deploy_account.clone(),
195-
self.contract.code().to_vec(),
238+
contract_code,
196239
&create_user_test_signer(&self.deploy_account),
197240
self.get_tx_block_hash(),
198241
deploy_mode,
199242
)
200243
}
201244

245+
fn deploy_global_contract_tx(
246+
&mut self,
247+
deploy_mode: GlobalContractDeployMode,
248+
) -> SignedTransaction {
249+
self.deploy_global_contract_custom_tx(deploy_mode, self.contract.code().to_vec())
250+
}
251+
202252
fn deploy_global_contract(&mut self, deploy_mode: GlobalContractDeployMode) {
203253
let tx = self.deploy_global_contract_tx(deploy_mode);
204254
self.run_tx(tx);
205255
}
206256

257+
fn deploy_trivial_global_contract(&mut self, deploy_mode: GlobalContractDeployMode) {
258+
let tx = self.deploy_global_contract_custom_tx(
259+
deploy_mode,
260+
near_test_contracts::trivial_contract().to_vec(),
261+
);
262+
self.run_tx(tx);
263+
}
264+
207265
fn deploy_regular_contract(&mut self, account: &AccountId) {
208266
let tx = SignedTransaction::deploy_contract(
209267
self.next_nonce(),
@@ -234,8 +292,8 @@ impl GlobalContractsTestEnv {
234292
self.run_tx(tx);
235293
}
236294

237-
fn call_global_contract(&mut self, account: &AccountId) {
238-
let tx = SignedTransaction::call(
295+
fn call_global_contract_tx(&mut self, account: &AccountId) -> SignedTransaction {
296+
SignedTransaction::call(
239297
self.next_nonce(),
240298
account.clone(),
241299
account.clone(),
@@ -245,7 +303,11 @@ impl GlobalContractsTestEnv {
245303
vec![],
246304
300 * TGAS,
247305
self.get_tx_block_hash(),
248-
);
306+
)
307+
}
308+
309+
fn call_global_contract(&mut self, account: &AccountId) {
310+
let tx = self.call_global_contract_tx(account);
249311
self.run_tx(tx);
250312
}
251313

0 commit comments

Comments
 (0)