From 0eeaff86fcf7a459735ced2b5d6fd9e3f74d28ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gaspard?= Date: Thu, 15 Jun 2023 15:04:17 +0000 Subject: [PATCH] move TrieNodesCount to near-vm-errors See #9176 for the rationale, near-vm-errors will be merged into near-vm-runner as soon as circular dependencies are done for. --- core/primitives/src/types.rs | 20 +----------------- runtime/near-vm-errors/src/lib.rs | 21 +++++++++++++++++++ runtime/near-vm-logic/src/dependencies.rs | 4 ++-- runtime/near-vm-logic/src/gas_counter.rs | 2 +- .../near-vm-logic/src/mocks/mock_external.rs | 4 ++-- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index 385d8191757..395f087d45d 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -9,6 +9,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use near_crypto::PublicKey; /// Reexport primitive types pub use near_primitives_core::types::*; +pub use near_vm_errors::TrieNodesCount; use once_cell::sync::Lazy; use serde_with::base64::Base64; use serde_with::serde_as; @@ -971,25 +972,6 @@ pub enum TrieCacheMode { CachingChunk, } -/// Counts trie nodes reads during tx/receipt execution for proper storage costs charging. -#[derive(Debug, PartialEq)] -pub struct TrieNodesCount { - /// Potentially expensive trie node reads which are served from disk in the worst case. - pub db_reads: u64, - /// Cheap trie node reads which are guaranteed to be served from RAM. - pub mem_reads: u64, -} - -impl TrieNodesCount { - /// Used to determine the number of trie nodes charged during some operation. - pub fn checked_sub(self, other: &Self) -> Option { - Some(Self { - db_reads: self.db_reads.checked_sub(other.db_reads)?, - mem_reads: self.mem_reads.checked_sub(other.mem_reads)?, - }) - } -} - /// State changes for a range of blocks. /// Expects that a block is present at most once in the list. #[derive(borsh::BorshDeserialize, borsh::BorshSerialize)] diff --git a/runtime/near-vm-errors/src/lib.rs b/runtime/near-vm-errors/src/lib.rs index 77676983084..62c7bc96530 100644 --- a/runtime/near-vm-errors/src/lib.rs +++ b/runtime/near-vm-errors/src/lib.rs @@ -7,6 +7,7 @@ use std::any::Any; use std::fmt::{self, Error, Formatter}; use std::io; +// ----------8<---------- // TODO: this does not belong in near-vm-errors but near-vm-runner. // See #9176 and #9180 for more context. #[derive(Debug, Clone, PartialEq, BorshDeserialize, BorshSerialize)] @@ -30,6 +31,26 @@ impl fmt::Debug for dyn CompiledContractCache { } } +/// Counts trie nodes reads during tx/receipt execution for proper storage costs charging. +#[derive(Debug, PartialEq)] +pub struct TrieNodesCount { + /// Potentially expensive trie node reads which are served from disk in the worst case. + pub db_reads: u64, + /// Cheap trie node reads which are guaranteed to be served from RAM. + pub mem_reads: u64, +} + +impl TrieNodesCount { + /// Used to determine the number of trie nodes charged during some operation. + pub fn checked_sub(self, other: &Self) -> Option { + Some(Self { + db_reads: self.db_reads.checked_sub(other.db_reads)?, + mem_reads: self.mem_reads.checked_sub(other.mem_reads)?, + }) + } +} +// ---------->8---------- + /// For bugs in the runtime itself, crash and die is the usual response. /// /// See the doc comment on `VMResult` for an explanation what the difference diff --git a/runtime/near-vm-logic/src/dependencies.rs b/runtime/near-vm-logic/src/dependencies.rs index d38b2330988..27659831aca 100644 --- a/runtime/near-vm-logic/src/dependencies.rs +++ b/runtime/near-vm-logic/src/dependencies.rs @@ -1,8 +1,8 @@ //! External dependencies of the near-vm-logic. -use near_primitives::hash::CryptoHash; -use near_primitives::types::TrieNodesCount; +use near_primitives_core::hash::CryptoHash; use near_primitives_core::types::{AccountId, Balance}; +use near_vm_errors::TrieNodesCount; use near_vm_errors::VMLogicError; use std::borrow::Cow; diff --git a/runtime/near-vm-logic/src/gas_counter.rs b/runtime/near-vm-logic/src/gas_counter.rs index a4f42756416..511e6e8c48b 100644 --- a/runtime/near-vm-logic/src/gas_counter.rs +++ b/runtime/near-vm-logic/src/gas_counter.rs @@ -1,5 +1,4 @@ use crate::{HostError, VMLogicError}; -use near_primitives::types::TrieNodesCount; use near_primitives_core::config::ExtCosts::read_cached_trie_node; use near_primitives_core::config::ExtCosts::touching_trie_node; use near_primitives_core::{ @@ -7,6 +6,7 @@ use near_primitives_core::{ profile::ProfileDataV3, types::Gas, }; +use near_vm_errors::TrieNodesCount; use std::collections::HashMap; #[inline] diff --git a/runtime/near-vm-logic/src/mocks/mock_external.rs b/runtime/near-vm-logic/src/mocks/mock_external.rs index fb6c0c0e464..87850dd3ce2 100644 --- a/runtime/near-vm-logic/src/mocks/mock_external.rs +++ b/runtime/near-vm-logic/src/mocks/mock_external.rs @@ -1,7 +1,7 @@ use crate::{External, StorageGetMode, ValuePtr}; -use near_primitives::hash::{hash, CryptoHash}; -use near_primitives::types::TrieNodesCount; +use near_primitives_core::hash::{hash, CryptoHash}; use near_primitives_core::types::{AccountId, Balance}; +use near_vm_errors::TrieNodesCount; use std::collections::HashMap; #[derive(Default, Clone)]