Skip to content

Commit

Permalink
test: use backwards_compatible_rs_contract where appropriate (#9175)
Browse files Browse the repository at this point in the history
`rs_contract` can be used by tests that run only on  the latest
 protocol version, as defined by the const PROTOCOL_VERSION.

`backwards_compatible_rs_contract` must be used in backwards 
compatibility tests that rely on specific protocol versions.

This is a pre-requisite to land the rustc 1.70 toolchain upgrade.
(See also #9140)
  • Loading branch information
jakmeier committed Jun 12, 2023
1 parent 733c1d9 commit a7aed55
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn compare_node_counts() {
deploy_test_contract(
&mut env,
"test0".parse().unwrap(),
near_test_contracts::base_rs_contract(),
near_test_contracts::backwards_compatible_rs_contract(),
num_blocks,
1,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn test_flat_storage_upgrade() {
deploy_test_contract_with_protocol_version(
&mut env,
"test0".parse().unwrap(),
near_test_contracts::base_rs_contract(),
near_test_contracts::backwards_compatible_rs_contract(),
blocks_to_process_txn,
1,
old_protocol_version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn assert_compute_limit_reached(
// setup: deploy the contract
{
// This contract has a bunch of methods to invoke storage operations.
let code = near_test_contracts::rs_contract().to_vec();
let code = near_test_contracts::backwards_compatible_rs_contract().to_vec();
let actions = vec![Action::DeployContract(DeployContractAction { code })];

let signer = InMemorySigner::from_seed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn protocol_upgrade() {
deploy_test_contract_with_protocol_version(
&mut env,
"test0".parse().unwrap(),
near_test_contracts::base_rs_contract(),
near_test_contracts::backwards_compatible_rs_contract(),
epoch_length,
1,
old_protocol_version,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/tests/client/features/nearvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_nearvm_upgrade() {
deploy_test_contract(
&mut env,
"test0".parse().unwrap(),
near_test_contracts::rs_contract(),
near_test_contracts::backwards_compatible_rs_contract(),
epoch_length,
1,
);
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/tests/client/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub(crate) fn prepare_env_with_congestion(
"test0".parse().unwrap(),
&signer,
vec![Action::DeployContract(DeployContractAction {
code: near_test_contracts::base_rs_contract().to_vec(),
code: near_test_contracts::backwards_compatible_rs_contract().to_vec(),
})],
*genesis_block.hash(),
);
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/tests/client/sharding_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ fn setup_test_env_with_cross_contract_txs(
account_id.clone(),
&signer,
vec![Action::DeployContract(DeployContractAction {
code: near_test_contracts::base_rs_contract().to_vec(),
code: near_test_contracts::backwards_compatible_rs_contract().to_vec(),
})],
genesis_hash,
)
Expand Down
2 changes: 1 addition & 1 deletion pytest/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def load_binary_file(filepath):


def load_test_contract(
filename: str = 'base_test_contract_rs.wasm') -> bytearray:
filename: str = 'backwards_compatible_rs_contract.wasm') -> bytearray:
"""Loads a WASM file from near-test-contracts package.
This is just a convenience function around load_binary_file which loads
Expand Down
10 changes: 5 additions & 5 deletions runtime/near-test-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn sized_contract(size: usize) -> Vec<u8> {
///
/// Note: the contract relies on the latest stable protocol version, and might
/// not work for tests using an older version. In particular, if a test depends
/// on a specific protocol version, it should use [`base_rs_contract`].
/// on a specific protocol version, it should use [`backwards_compatible_rs_contract`].
pub fn rs_contract() -> &'static [u8] {
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/res/", "test_contract_rs.wasm"))
}
Expand All @@ -65,9 +65,9 @@ pub fn rs_contract() -> &'static [u8] {
/// enabled. So we have to build it with Rustc <= 1.69. If we need to update the
/// contracts content, we can build it manually with an older compiler and check
/// in the new WASM.
pub fn base_rs_contract() -> &'static [u8] {
pub fn backwards_compatible_rs_contract() -> &'static [u8] {
static CONTRACT: OnceCell<Vec<u8>> = OnceCell::new();
CONTRACT.get_or_init(|| read_contract("base_test_contract_rs.wasm")).as_slice()
CONTRACT.get_or_init(|| read_contract("backwards_compatible_rs_contract.wasm")).as_slice()
}

/// Standard test contract which additionally includes all host functions from
Expand Down Expand Up @@ -154,7 +154,7 @@ fn smoke_test() {
assert!(!ts_contract().is_empty());
assert!(!trivial_contract().is_empty());
assert!(!fuzzing_contract().is_empty());
assert!(!base_rs_contract().is_empty());
assert!(!backwards_compatible_rs_contract().is_empty());
assert!(!ft_contract().is_empty());
}

Expand Down Expand Up @@ -238,7 +238,7 @@ pub fn arbitrary_contract(seed: u64) -> Vec<u8> {
config.exceptions_enabled = false;
config.saturating_float_to_int_enabled = false;
config.sign_extension_enabled = false;
config.available_imports = Some(base_rs_contract().to_vec());
config.available_imports = Some(backwards_compatible_rs_contract().to_vec());
let module = wasm_smith::Module::new(config, &mut arbitrary).expect("generate module");
module.to_bytes()
}
17 changes: 12 additions & 5 deletions runtime/near-vm-runner/src/tests/rs_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ use crate::tests::{
};
use crate::vm_kind::VMKind;

fn test_contract() -> ContractCode {
let code = near_test_contracts::rs_contract();
fn test_contract(vm_kind: VMKind) -> ContractCode {
let code = match vm_kind {
// testing backwards-compatibility, use an old WASM
VMKind::Wasmer0 | VMKind::Wasmer2 => {
near_test_contracts::backwards_compatible_rs_contract()
}
// production and developer environment, use a cutting-edge WASM
VMKind::Wasmtime | VMKind::NearVm => near_test_contracts::rs_contract(),
};
ContractCode::new(code.to_vec(), None)
}

Expand All @@ -39,7 +46,7 @@ fn assert_run_result(result: VMResult, expected_value: u64) {
pub fn test_read_write() {
let config = VMConfig::test();
with_vm_variants(&config, |vm_kind: VMKind| {
let code = test_contract();
let code = test_contract(vm_kind);
let mut fake_external = MockedExternal::new();

let context = create_context(encode(&[10u64, 20u64]));
Expand Down Expand Up @@ -112,7 +119,7 @@ fn run_test_ext(
validators: Vec<(&str, Balance)>,
vm_kind: VMKind,
) {
let code = test_contract();
let code = test_contract(vm_kind);
let mut fake_external = MockedExternal::new();
fake_external.validators =
validators.into_iter().map(|(s, b)| (s.parse().unwrap(), b)).collect();
Expand Down Expand Up @@ -218,7 +225,7 @@ pub fn test_out_of_memory() {
_ => {}
}

let code = test_contract();
let code = test_contract(vm_kind);
let mut fake_external = MockedExternal::new();

let context = create_context(Vec::new());
Expand Down
2 changes: 1 addition & 1 deletion tools/state-viewer/src/state_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ mod test {
"test0".parse().unwrap(),
&signer0,
vec![Action::DeployContract(DeployContractAction {
code: near_test_contracts::base_rs_contract().to_vec(),
code: near_test_contracts::backwards_compatible_rs_contract().to_vec(),
})],
genesis_hash,
);
Expand Down

0 comments on commit a7aed55

Please sign in to comment.