From 9cb316c697b9677611cf49ba5107eb71472c31f0 Mon Sep 17 00:00:00 2001 From: Ekleog-NEAR <96595974+Ekleog-NEAR@users.noreply.github.com> Date: Sat, 1 Jul 2023 02:52:45 +0200 Subject: [PATCH] remove near-stable-hasher dep from near-vm-runner (#9270) Part of #8197 --- Cargo.lock | 1 - runtime/near-vm-runner/Cargo.toml | 1 - runtime/near-vm-runner/src/lib.rs | 1 + runtime/near-vm-runner/src/near_vm_runner.rs | 7 ++----- runtime/near-vm-runner/src/tests/cache.rs | 18 ++++-------------- runtime/near-vm-runner/src/utils.rs | 11 +++++++++++ runtime/near-vm-runner/src/wasmer2_runner.rs | 7 ++----- 7 files changed, 20 insertions(+), 26 deletions(-) create mode 100644 runtime/near-vm-runner/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 6d470df7f9a..af5b3b0c0ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4345,7 +4345,6 @@ dependencies = [ "near-primitives", "near-primitives-core", "near-rpc-error-macro", - "near-stable-hasher", "near-stdx", "near-test-contracts", "near-vm-compiler", diff --git a/runtime/near-vm-runner/Cargo.toml b/runtime/near-vm-runner/Cargo.toml index fd21b02977b..d62d8ffd9c8 100644 --- a/runtime/near-vm-runner/Cargo.toml +++ b/runtime/near-vm-runner/Cargo.toml @@ -40,7 +40,6 @@ near-crypto.workspace = true near-fmt.workspace = true near-primitives-core.workspace = true near-rpc-error-macro.workspace = true -near-stable-hasher.workspace = true # Old versions of pwasm-utils we need to preserve backwards compatibility under # old protocol versions. diff --git a/runtime/near-vm-runner/src/lib.rs b/runtime/near-vm-runner/src/lib.rs index 3dbe86d8633..d656008a3c4 100644 --- a/runtime/near-vm-runner/src/lib.rs +++ b/runtime/near-vm-runner/src/lib.rs @@ -14,6 +14,7 @@ pub mod prepare; mod runner; #[cfg(test)] mod tests; +mod utils; mod vm_kind; #[cfg(all(feature = "wasmer2_vm", target_arch = "x86_64"))] mod wasmer2_runner; diff --git a/runtime/near-vm-runner/src/near_vm_runner.rs b/runtime/near-vm-runner/src/near_vm_runner.rs index 1132a321d6b..8562c2dd5d8 100644 --- a/runtime/near-vm-runner/src/near_vm_runner.rs +++ b/runtime/near-vm-runner/src/near_vm_runner.rs @@ -16,7 +16,6 @@ use crate::{get_contract_cache_key, imports}; use memoffset::offset_of; use near_primitives_core::contract::ContractCode; use near_primitives_core::runtime::fees::RuntimeFeesConfig; -use near_stable_hasher::StableHasher; use near_vm_compiler_singlepass::Singlepass; use near_vm_engine::universal::{ LimitedMemoryPool, Universal, UniversalEngine, UniversalExecutable, UniversalExecutableRef, @@ -26,7 +25,7 @@ use near_vm_vm::{ Artifact, Instantiatable, LinearMemory, LinearTable, Memory, MemoryStyle, TrapCode, VMMemory, }; use std::borrow::Cow; -use std::hash::{Hash, Hasher}; +use std::hash::Hash; use std::mem::size_of; use std::sync::{Arc, OnceLock}; @@ -210,9 +209,7 @@ struct NearVmConfig { impl NearVmConfig { fn config_hash(self: Self) -> u64 { - let mut s = StableHasher::new(); - self.hash(&mut s); - s.finish() + crate::utils::stable_hash(&self) } } diff --git a/runtime/near-vm-runner/src/tests/cache.rs b/runtime/near-vm-runner/src/tests/cache.rs index ab560fdd205..b1894762a41 100644 --- a/runtime/near-vm-runner/src/tests/cache.rs +++ b/runtime/near-vm-runner/src/tests/cache.rs @@ -14,8 +14,6 @@ use assert_matches::assert_matches; use near_primitives_core::contract::ContractCode; use near_primitives_core::hash::CryptoHash; use near_primitives_core::runtime::fees::RuntimeFeesConfig; -use near_stable_hasher::StableHasher; -use std::hash::{Hash, Hasher}; use std::io; use std::sync::atomic::{AtomicBool, Ordering}; use wasmer_compiler::{CpuFeature, Target}; @@ -137,9 +135,7 @@ fn test_wasmer2_artifact_output_stability() { let config = VMConfig::test(); let prepared_code = prepare::prepare_contract(contract.code(), &config, VMKind::Wasmer2).unwrap(); - let mut hasher = StableHasher::new(); - (&contract.code(), &prepared_code).hash(&mut hasher); - got_prepared_hashes.push(hasher.finish()); + got_prepared_hashes.push(crate::utils::stable_hash((&contract.code(), &prepared_code))); let mut features = CpuFeature::set(); features.insert(CpuFeature::AVX); @@ -148,9 +144,7 @@ fn test_wasmer2_artifact_output_stability() { let vm = Wasmer2VM::new_for_target(config, target); let artifact = vm.compile_uncached(&contract).unwrap(); let serialized = artifact.serialize().unwrap(); - let mut hasher = StableHasher::new(); - serialized.hash(&mut hasher); - let this_hash = hasher.finish(); + let this_hash = crate::utils::stable_hash(&serialized); got_compiled_hashes.push(this_hash); std::fs::write(format!("/tmp/artifact{}", this_hash), serialized).unwrap(); @@ -212,9 +206,7 @@ fn test_near_vm_artifact_output_stability() { let config = VMConfig::test(); let prepared_code = prepare::prepare_contract(contract.code(), &config, VMKind::NearVm).unwrap(); - let mut hasher = StableHasher::new(); - (&contract.code(), &prepared_code).hash(&mut hasher); - got_prepared_hashes.push(hasher.finish()); + got_prepared_hashes.push(crate::utils::stable_hash((&contract.code(), &prepared_code))); let mut features = CpuFeature::set(); features.insert(CpuFeature::AVX); @@ -223,9 +215,7 @@ fn test_near_vm_artifact_output_stability() { let vm = NearVM::new_for_target(config, target); let artifact = vm.compile_uncached(&contract).unwrap(); let serialized = artifact.serialize().unwrap(); - let mut hasher = StableHasher::new(); - serialized.hash(&mut hasher); - let this_hash = hasher.finish(); + let this_hash = crate::utils::stable_hash(&serialized); got_compiled_hashes.push(this_hash); std::fs::write(format!("/tmp/artifact{}", this_hash), serialized).unwrap(); diff --git a/runtime/near-vm-runner/src/utils.rs b/runtime/near-vm-runner/src/utils.rs new file mode 100644 index 00000000000..8ab0b33d8e2 --- /dev/null +++ b/runtime/near-vm-runner/src/utils.rs @@ -0,0 +1,11 @@ +use std::hash::{Hash, Hasher}; + +pub(crate) fn stable_hash(value: T) -> u64 { + // This is ported over from the previous uses, that relied on near-stable-hasher. + // The need for stability here can certainly be discussed, and it could probably be replaced with DefaultHasher. + // Not doing it in this refactor as it’s not the core of the issue and using SipHasher is an easy alternative. + #[allow(deprecated)] + let mut hasher = std::hash::SipHasher::new(); + value.hash(&mut hasher); + hasher.finish() +} diff --git a/runtime/near-vm-runner/src/wasmer2_runner.rs b/runtime/near-vm-runner/src/wasmer2_runner.rs index 1f2b3d1ab0b..9bd44cffa70 100644 --- a/runtime/near-vm-runner/src/wasmer2_runner.rs +++ b/runtime/near-vm-runner/src/wasmer2_runner.rs @@ -16,9 +16,8 @@ use crate::{get_contract_cache_key, imports}; use memoffset::offset_of; use near_primitives_core::contract::ContractCode; use near_primitives_core::runtime::fees::RuntimeFeesConfig; -use near_stable_hasher::StableHasher; use std::borrow::Cow; -use std::hash::{Hash, Hasher}; +use std::hash::Hash; use std::mem::size_of; use std::sync::Arc; use wasmer_compiler_singlepass::Singlepass; @@ -211,9 +210,7 @@ struct Wasmer2Config { impl Wasmer2Config { fn config_hash(self: Self) -> u64 { - let mut s = StableHasher::new(); - self.hash(&mut s); - s.finish() + crate::utils::stable_hash(&self) } }