Skip to content

Commit

Permalink
remove near-stable-hasher dep from near-vm-runner (#9270)
Browse files Browse the repository at this point in the history
Part of #8197
  • Loading branch information
Ekleog-NEAR authored Jul 1, 2023
1 parent b5921f2 commit 9cb316c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion runtime/near-vm-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions runtime/near-vm-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions runtime/near-vm-runner/src/near_vm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};

Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 4 additions & 14 deletions runtime/near-vm-runner/src/tests/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions runtime/near-vm-runner/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::hash::{Hash, Hasher};

pub(crate) fn stable_hash<T: 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()
}
7 changes: 2 additions & 5 deletions runtime/near-vm-runner/src/wasmer2_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 9cb316c

Please sign in to comment.