Skip to content

Commit

Permalink
refactor: move CompiledContractCache to vm_runner::cache module (near…
Browse files Browse the repository at this point in the history
…#10817)

Based on top of near#10791
  • Loading branch information
nagisa authored Mar 18, 2024
1 parent b74caf3 commit 7cb9087
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 144 deletions.
3 changes: 1 addition & 2 deletions chain/chain/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ use near_store::{
ApplyStatePartResult, DBCol, ShardTries, StateSnapshotConfig, Store, Trie, TrieConfig,
TrieUpdate, WrappedTrieChanges, COLD_HEAD_KEY,
};
use near_vm_runner::logic::CompiledContractCache;
use near_vm_runner::ContractCode;
use near_vm_runner::{precompile_contract, FilesystemCompiledContractCache};
use near_vm_runner::{precompile_contract, CompiledContractCache, FilesystemCompiledContractCache};
use node_runtime::adapter::ViewRuntimeAdapter;
use node_runtime::state_viewer::TrieViewer;
use node_runtime::{
Expand Down
3 changes: 1 addition & 2 deletions chain/client/src/test_utils/test_env_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use near_primitives::types::{AccountId, NumShards};
use near_store::config::StateSnapshotType;
use near_store::test_utils::create_test_store;
use near_store::{NodeStorage, ShardUId, Store, StoreConfig, TrieConfig};
use near_vm_runner::logic::CompiledContractCache;
use near_vm_runner::FilesystemCompiledContractCache;
use near_vm_runner::{CompiledContractCache, FilesystemCompiledContractCache};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub struct ViewApplyState {
/// Current Protocol version when we apply the state transition
pub current_protocol_version: ProtocolVersion,
/// Cache for compiled contracts.
pub cache: Option<Box<dyn near_vm_runner::logic::CompiledContractCache>>,
pub cache: Option<Box<dyn near_vm_runner::CompiledContractCache>>,
}

impl From<&Account> for AccountView {
Expand Down
44 changes: 20 additions & 24 deletions core/store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
extern crate core;

use std::fs::File;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use std::{fmt, io};

use crate::db::{refcount, DBIterator, DBOp, DBSlice, DBTransaction, Database, StoreStatistics};
pub use crate::trie::iterator::{TrieIterator, TrieTraversalItem};
pub use crate::trie::update::{TrieUpdate, TrieUpdateIterator, TrieUpdateValuePtr};
pub use crate::trie::{
estimator, resharding, ApplyStatePartResult, KeyForStateChanges, KeyLookupMode, NibbleSlice,
PartialStorage, PrefetchApi, PrefetchError, RawTrieNode, RawTrieNodeWithSize, ShardTries,
StateSnapshot, StateSnapshotConfig, Trie, TrieAccess, TrieCache, TrieCachingStorage,
TrieChanges, TrieConfig, TrieDBStorage, TrieStorage, WrappedTrieChanges,
STATE_SNAPSHOT_COLUMNS,
};
use borsh::{BorshDeserialize, BorshSerialize};
use metadata::{DbKind, DbVersion, KIND_KEY, VERSION_KEY};
use once_cell::sync::Lazy;
use strum;

pub use columns::DBCol;
pub use db::{
CHUNK_TAIL_KEY, COLD_HEAD_KEY, FINAL_HEAD_KEY, FORK_TAIL_KEY, GENESIS_JSON_HASH_KEY,
GENESIS_STATE_ROOTS_KEY, HEADER_HEAD_KEY, HEAD_KEY, LARGEST_TARGET_HEIGHT_KEY,
LATEST_KNOWN_KEY, STATE_SNAPSHOT_KEY, STATE_SYNC_DUMP_KEY, TAIL_KEY,
};
use metadata::{DbKind, DbVersion, KIND_KEY, VERSION_KEY};
use near_crypto::PublicKey;
use near_fmt::{AbbrBytes, StorageKey};
use near_primitives::account::{AccessKey, Account};
Expand All @@ -29,19 +30,14 @@ use near_primitives::receipt::{
pub use near_primitives::shard_layout::ShardUId;
use near_primitives::trie_key::{trie_key_parsers, TrieKey};
use near_primitives::types::{AccountId, BlockHeight, StateRoot};
use near_vm_runner::logic::{CompiledContract, CompiledContractCache};
use near_vm_runner::ContractCode;

use crate::db::{refcount, DBIterator, DBOp, DBSlice, DBTransaction, Database, StoreStatistics};
pub use crate::trie::iterator::{TrieIterator, TrieTraversalItem};
pub use crate::trie::update::{TrieUpdate, TrieUpdateIterator, TrieUpdateValuePtr};
pub use crate::trie::{
estimator, resharding, ApplyStatePartResult, KeyForStateChanges, KeyLookupMode, NibbleSlice,
PartialStorage, PrefetchApi, PrefetchError, RawTrieNode, RawTrieNodeWithSize, ShardTries,
StateSnapshot, StateSnapshotConfig, Trie, TrieAccess, TrieCache, TrieCachingStorage,
TrieChanges, TrieConfig, TrieDBStorage, TrieStorage, WrappedTrieChanges,
STATE_SNAPSHOT_COLUMNS,
};
use near_vm_runner::{CompiledContract, CompiledContractCache, ContractCode};
use once_cell::sync::Lazy;
use std::fs::File;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use std::{fmt, io};
use strum;

pub mod cold_storage;
mod columns;
Expand Down Expand Up @@ -1133,7 +1129,7 @@ mod tests {
/// Check StoreCompiledContractCache implementation.
#[test]
fn test_store_compiled_contract_cache() {
use near_vm_runner::logic::{CompiledContract, CompiledContractCache};
use near_vm_runner::{CompiledContract, CompiledContractCache};
use std::str::FromStr;

let store = crate::test_utils::create_test_store();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use near_store::config::StateSnapshotType;
use near_store::genesis::initialize_genesis_state;
use near_store::test_utils::create_test_store;
use near_store::TrieConfig;
use near_vm_runner::logic::CompiledContractCache;
use near_vm_runner::CompiledContractCache;
use near_vm_runner::FilesystemCompiledContractCache;
use nearcore::NightshadeRuntime;
use std::collections::HashMap;
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 @@ -3381,7 +3381,7 @@ mod contract_precompilation_tests {
use near_primitives::test_utils::MockEpochInfoProvider;
use near_primitives::views::ViewApplyState;
use near_store::TrieUpdate;
use near_vm_runner::logic::CompiledContractCache;
use near_vm_runner::CompiledContractCache;
use near_vm_runner::{get_contract_cache_key, ContractCode, FilesystemCompiledContractCache};
use node_runtime::state_viewer::TrieViewer;

Expand Down
4 changes: 2 additions & 2 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use near_rosetta_rpc::RosettaRpcConfig;
use near_store::config::StateSnapshotType;
use near_store::{StateSnapshotConfig, Store, TrieConfig};
use near_telemetry::TelemetryConfig;
use near_vm_runner::FilesystemCompiledContractCache;
use near_vm_runner::{CompiledContractCache, FilesystemCompiledContractCache};
use num_rational::Rational32;
use std::fs;
use std::fs::File;
Expand Down Expand Up @@ -640,7 +640,7 @@ impl NightshadeRuntime {
FilesystemCompiledContractCache::new(home_dir, config.config.store.path.as_ref())?;
Ok(NightshadeRuntime::new(
store,
near_vm_runner::logic::CompiledContractCache::handle(&contract_cache),
CompiledContractCache::handle(&contract_cache),
&config.genesis.config,
epoch_manager,
config.client_config.trie_viewer_state_size_limit,
Expand Down
2 changes: 1 addition & 1 deletion nearcore/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use near_epoch_manager::EpochManagerHandle;
use near_parameters::RuntimeConfigStore;
use near_store::genesis::initialize_genesis_state;
use near_store::{Store, TrieConfig};
use near_vm_runner::logic::CompiledContractCache;
use near_vm_runner::CompiledContractCache;
use std::path::PathBuf;
use std::sync::Arc;

Expand Down
74 changes: 72 additions & 2 deletions runtime/near-vm-runner/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::errors::ContractPrecompilatonResult;
use crate::logic::errors::{CacheError, CompilationError};
use crate::logic::{CompiledContract, CompiledContractCache, Config};
use crate::logic::Config;
use crate::runner::VMKindExt;
use crate::ContractCode;
use borsh::BorshSerialize;
use borsh::{BorshDeserialize, BorshSerialize};
use near_parameters::vm::VMKind;
use near_primitives_core::hash::CryptoHash;
use std::collections::HashMap;
Expand Down Expand Up @@ -56,6 +56,76 @@ pub fn get_contract_cache_key(code: &ContractCode, config: &Config) -> CryptoHas
CryptoHash::hash_borsh(key)
}

#[derive(Debug, Clone, PartialEq, BorshDeserialize, BorshSerialize)]
pub enum CompiledContract {
CompileModuleError(crate::logic::errors::CompilationError),
Code(Vec<u8>),
}

impl CompiledContract {
/// Return the length of the compiled contract data.
///
/// If the `CompiledContract` represents a compilation failure, returns `0`.
pub fn debug_len(&self) -> usize {
match self {
CompiledContract::CompileModuleError(_) => 0,
CompiledContract::Code(c) => c.len(),
}
}
}

/// Cache for compiled modules
pub trait CompiledContractCache: Send + Sync {
fn handle(&self) -> Box<dyn CompiledContractCache>;
fn put(&self, key: &CryptoHash, value: CompiledContract) -> std::io::Result<()>;
fn get(&self, key: &CryptoHash) -> std::io::Result<Option<CompiledContract>>;
fn has(&self, key: &CryptoHash) -> std::io::Result<bool> {
self.get(key).map(|entry| entry.is_some())
}
}

impl fmt::Debug for dyn CompiledContractCache {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Compiled contracts cache")
}
}

impl CompiledContractCache for Box<dyn CompiledContractCache> {
fn handle(&self) -> Box<dyn CompiledContractCache> {
<dyn CompiledContractCache>::handle(&**self)
}

fn put(&self, key: &CryptoHash, value: CompiledContract) -> std::io::Result<()> {
<dyn CompiledContractCache>::put(&**self, key, value)
}

fn get(&self, key: &CryptoHash) -> std::io::Result<Option<CompiledContract>> {
<dyn CompiledContractCache>::get(&**self, key)
}

fn has(&self, key: &CryptoHash) -> std::io::Result<bool> {
<dyn CompiledContractCache>::has(&**self, key)
}
}

impl<C: CompiledContractCache> CompiledContractCache for &C {
fn handle(&self) -> Box<dyn CompiledContractCache> {
<C as CompiledContractCache>::handle(self)
}

fn put(&self, key: &CryptoHash, value: CompiledContract) -> std::io::Result<()> {
<C as CompiledContractCache>::put(self, key, value)
}

fn get(&self, key: &CryptoHash) -> std::io::Result<Option<CompiledContract>> {
<C as CompiledContractCache>::get(self, key)
}

fn has(&self, key: &CryptoHash) -> std::io::Result<bool> {
<C as CompiledContractCache>::has(self, key)
}
}

#[derive(Default, Clone)]
pub struct MockCompiledContractCache {
store: Arc<Mutex<HashMap<CryptoHash, CompiledContract>>>,
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ mod wasmtime_runner;

pub use crate::logic::with_ext_cost_counter;
pub use cache::{
get_contract_cache_key, precompile_contract, FilesystemCompiledContractCache,
MockCompiledContractCache,
get_contract_cache_key, precompile_contract, CompiledContract, CompiledContractCache,
FilesystemCompiledContractCache, MockCompiledContractCache,
};
pub use code::ContractCode;
pub use profile::ProfileDataV3;
Expand Down
74 changes: 0 additions & 74 deletions runtime/near-vm-runner/src/logic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_primitives_core::hash::CryptoHash;
use std::fmt;

mod alt_bn128;
mod context;
mod dependencies;
Expand All @@ -24,73 +20,3 @@ pub use logic::{VMLogic, VMOutcome};
pub use near_parameters::vm::{Config, ContractPrepareVersion, LimitConfig, StorageGetMode};
pub use near_primitives_core::types::ProtocolVersion;
pub use types::ReturnData;

#[derive(Debug, Clone, PartialEq, BorshDeserialize, BorshSerialize)]
pub enum CompiledContract {
CompileModuleError(errors::CompilationError),
Code(Vec<u8>),
}

impl CompiledContract {
/// Return the length of the compiled contract data.
///
/// If the `CompiledContract` represents a compilation failure, returns `0`.
pub fn debug_len(&self) -> usize {
match self {
CompiledContract::CompileModuleError(_) => 0,
CompiledContract::Code(c) => c.len(),
}
}
}

/// Cache for compiled modules
pub trait CompiledContractCache: Send + Sync {
fn handle(&self) -> Box<dyn CompiledContractCache>;
fn put(&self, key: &CryptoHash, value: CompiledContract) -> std::io::Result<()>;
fn get(&self, key: &CryptoHash) -> std::io::Result<Option<CompiledContract>>;
fn has(&self, key: &CryptoHash) -> std::io::Result<bool> {
self.get(key).map(|entry| entry.is_some())
}
}

impl fmt::Debug for dyn CompiledContractCache {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Compiled contracts cache")
}
}

impl CompiledContractCache for Box<dyn CompiledContractCache> {
fn handle(&self) -> Box<dyn CompiledContractCache> {
<dyn CompiledContractCache>::handle(&**self)
}

fn put(&self, key: &CryptoHash, value: CompiledContract) -> std::io::Result<()> {
<dyn CompiledContractCache>::put(&**self, key, value)
}

fn get(&self, key: &CryptoHash) -> std::io::Result<Option<CompiledContract>> {
<dyn CompiledContractCache>::get(&**self, key)
}

fn has(&self, key: &CryptoHash) -> std::io::Result<bool> {
<dyn CompiledContractCache>::has(&**self, key)
}
}

impl<C: CompiledContractCache> CompiledContractCache for &C {
fn handle(&self) -> Box<dyn CompiledContractCache> {
<C as CompiledContractCache>::handle(self)
}

fn put(&self, key: &CryptoHash, value: CompiledContract) -> std::io::Result<()> {
<C as CompiledContractCache>::put(self, key, value)
}

fn get(&self, key: &CryptoHash) -> std::io::Result<Option<CompiledContract>> {
<C as CompiledContractCache>::get(self, key)
}

fn has(&self, key: &CryptoHash) -> std::io::Result<bool> {
<C as CompiledContractCache>::has(self, key)
}
}
9 changes: 4 additions & 5 deletions runtime/near-vm-runner/src/near_vm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use crate::logic::errors::{
};
use crate::logic::gas_counter::FastGasCounter;
use crate::logic::types::PromiseResult;
use crate::logic::{
CompiledContract, CompiledContractCache, Config, External, MemSlice, MemoryLike, VMContext,
VMLogic, VMOutcome,
};
use crate::logic::{Config, External, MemSlice, MemoryLike, VMContext, VMLogic, VMOutcome};
use crate::prepare;
use crate::runner::VMResult;
use crate::{get_contract_cache_key, imports, ContractCode};
use crate::{
get_contract_cache_key, imports, CompiledContract, CompiledContractCache, ContractCode,
};
use memoffset::offset_of;
use near_parameters::vm::VMKind;
use near_parameters::RuntimeFeesConfig;
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::errors::ContractPrecompilatonResult;
use crate::logic::errors::{CacheError, CompilationError, VMRunnerError};
use crate::logic::types::PromiseResult;
use crate::logic::{CompiledContractCache, External, VMContext, VMOutcome};
use crate::ContractCode;
use crate::logic::{External, VMContext, VMOutcome};
use crate::{CompiledContractCache, ContractCode};
use near_parameters::vm::{Config, VMKind};
use near_parameters::RuntimeFeesConfig;

Expand Down
4 changes: 1 addition & 3 deletions runtime/near-vm-runner/src/tests/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use super::{create_context, test_vm_config, with_vm_variants};
use crate::logic::errors::VMRunnerError;
use crate::logic::mocks::mock_external::MockedExternal;
use crate::logic::Config;
use crate::logic::{CompiledContract, CompiledContractCache};
use crate::runner::VMKindExt;
use crate::runner::VMResult;
use crate::ContractCode;
use crate::MockCompiledContractCache;
use crate::{CompiledContract, CompiledContractCache, ContractCode, MockCompiledContractCache};
use assert_matches::assert_matches;
use near_parameters::vm::VMKind;
use near_parameters::RuntimeFeesConfig;
Expand Down
9 changes: 4 additions & 5 deletions runtime/near-vm-runner/src/wasmer2_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use crate::logic::errors::{
};
use crate::logic::gas_counter::FastGasCounter;
use crate::logic::types::PromiseResult;
use crate::logic::{
CompiledContract, CompiledContractCache, Config, External, MemSlice, MemoryLike, VMContext,
VMLogic, VMOutcome,
};
use crate::logic::{Config, External, MemSlice, MemoryLike, VMContext, VMLogic, VMOutcome};
use crate::prepare;
use crate::runner::VMResult;
use crate::{get_contract_cache_key, imports, ContractCode};
use crate::{
get_contract_cache_key, imports, CompiledContract, CompiledContractCache, ContractCode,
};
use memoffset::offset_of;
use near_parameters::vm::VMKind;
use near_parameters::RuntimeFeesConfig;
Expand Down
Loading

0 comments on commit 7cb9087

Please sign in to comment.