diff --git a/execution_engine/src/engine_state/mod.rs b/execution_engine/src/engine_state/mod.rs index b0a84e0539..510ea387c0 100644 --- a/execution_engine/src/engine_state/mod.rs +++ b/execution_engine/src/engine_state/mod.rs @@ -26,12 +26,10 @@ use num_traits::Zero; use once_cell::sync::Lazy; use tracing::{debug, error, trace, warn}; -pub use casper_storage::data_access_layer::get_bids::{ - GetBidsError, GetBidsRequest, GetBidsResult, -}; use casper_storage::{ data_access_layer::{ balance::BalanceResult, + get_bids::{BidsRequest, BidsResult}, query::{QueryRequest, QueryResult}, DataAccessLayer, EraValidatorsRequest, EraValidatorsResult, }, @@ -2241,7 +2239,7 @@ where } /// Gets current bids from the auction system. - pub fn get_bids(&self, get_bids_request: GetBidsRequest) -> GetBidsResult { + pub fn get_bids(&self, get_bids_request: BidsRequest) -> BidsResult { let state_root_hash = get_bids_request.state_hash(); let tracking_copy = match self.state.checkout(state_root_hash) { Ok(ret) => match ret { @@ -2249,16 +2247,16 @@ where tracking_copy, self.config.max_query_depth, ))), - None => return GetBidsResult::RootNotFound, + None => return BidsResult::RootNotFound, }, - Err(err) => return GetBidsResult::Failure(GetBidsError::GlobalState(err)), + Err(err) => return BidsResult::Failure(TrackingCopyError::Storage(err)), }; let mut tc = tracking_copy.borrow_mut(); let bid_keys = match tc.get_keys(&KeyTag::BidAddr) { Ok(ret) => ret, - Err(err) => return GetBidsResult::Failure(GetBidsError::TrackingCopyError(err)), + Err(err) => return BidsResult::Failure(err), }; let mut bids = vec![]; @@ -2269,18 +2267,14 @@ where bids.push(bid_kind); } Some(_) => { - return GetBidsResult::Failure(GetBidsError::InvalidStoredValueVariant( - *key, - )) + return BidsResult::Failure(TrackingCopyError::UnexpectedStoredValueVariant) } - None => return GetBidsResult::Failure(GetBidsError::MissingBid(*key)), + None => return BidsResult::Failure(TrackingCopyError::MissingBid(*key)), }, - Err(error) => { - return GetBidsResult::Failure(GetBidsError::TrackingCopyError(error)) - } + Err(error) => return BidsResult::Failure(error), } } - GetBidsResult::Success { bids } + BidsResult::Success { bids } } /// Distribute block rewards. diff --git a/execution_engine_testing/test_support/src/wasm_test_builder.rs b/execution_engine_testing/test_support/src/wasm_test_builder.rs index 13bb76cc2f..cc9ac6b12d 100644 --- a/execution_engine_testing/test_support/src/wasm_test_builder.rs +++ b/execution_engine_testing/test_support/src/wasm_test_builder.rs @@ -19,9 +19,10 @@ use casper_execution_engine::engine_state::{ execution_result::ExecutionResult, run_genesis_request::RunGenesisRequest, step::{StepRequest, StepSuccess}, - EngineConfig, EngineConfigBuilder, EngineState, Error, GenesisSuccess, GetBidsRequest, - PruneConfig, PruneResult, StepError, UpgradeSuccess, DEFAULT_MAX_QUERY_DEPTH, + EngineConfig, EngineConfigBuilder, EngineState, Error, GenesisSuccess, PruneConfig, + PruneResult, StepError, UpgradeSuccess, DEFAULT_MAX_QUERY_DEPTH, }; +use casper_storage::data_access_layer::BidsRequest; use casper_storage::{ data_access_layer::{ BalanceResult, BlockStore, DataAccessLayer, EraValidatorsRequest, EraValidatorsResult, @@ -1254,7 +1255,7 @@ where /// Gets [`Vec`]. pub fn get_bids(&mut self) -> Vec { - let get_bids_request = GetBidsRequest::new(self.get_post_state_hash()); + let get_bids_request = BidsRequest::new(self.get_post_state_hash()); let get_bids_result = self.engine_state.get_bids(get_bids_request); diff --git a/node/src/components/contract_runtime.rs b/node/src/components/contract_runtime.rs index 389900ea6c..0df610be5f 100644 --- a/node/src/components/contract_runtime.rs +++ b/node/src/components/contract_runtime.rs @@ -30,7 +30,7 @@ use thiserror::Error; use tracing::{debug, error, info, trace}; #[cfg(test)] -use casper_execution_engine::engine_state::{GetBidsRequest, GetBidsResult}; +use casper_execution_engine::engine_state::{BidsRequest, BidsResult}; use casper_execution_engine::engine_state::{ self, genesis::GenesisError, DeployItem, EngineConfigBuilder, EngineState, GenesisSuccess, @@ -732,9 +732,9 @@ impl ContractRuntime { /// Returns auction state, for testing only. #[cfg(test)] - pub(crate) fn auction_state(&self, root_hash: Digest) -> GetBidsResult { + pub(crate) fn auction_state(&self, root_hash: Digest) -> BidsResult { let engine_state = Arc::clone(&self.engine_state); - let get_bids_request = GetBidsRequest::new(root_hash); + let get_bids_request = BidsRequest::new(root_hash); engine_state.get_bids(get_bids_request) } @@ -861,6 +861,22 @@ impl ContractRuntime { } .ignore() } + ContractRuntimeRequest::GetBids { + request: bids_request, + responder, + } => { + trace!(?bids_request, "get bids request"); + let metrics = Arc::clone(&self.metrics); + let data_access_layer = Arc::clone(&self.data_access_layer); + async move { + let start = Instant::now(); + let result = data_access_layer.bids(bids_request); + metrics.get_bids.observe(start.elapsed().as_secs_f64()); + trace!(?result, "balance result"); + responder.respond(result).await + } + .ignore() + } ContractRuntimeRequest::GetTrie { trie_or_chunk_id, responder, @@ -990,22 +1006,6 @@ impl ContractRuntime { .set(self.exec_queue.len().try_into().unwrap_or(i64::MIN)); effects } - ContractRuntimeRequest::GetBids { - get_bids_request, - responder, - } => { - trace!(?get_bids_request, "get bids request"); - let engine_state = Arc::clone(&self.engine_state); - let metrics = Arc::clone(&self.metrics); - async move { - let start = Instant::now(); - let result = engine_state.get_bids(get_bids_request); - metrics.get_bids.observe(start.elapsed().as_secs_f64()); - trace!(?result, "get bids result"); - responder.respond(result).await - } - .ignore() - } ContractRuntimeRequest::GetExecutionResultsChecksum { state_root_hash, responder, diff --git a/node/src/components/rpc_server.rs b/node/src/components/rpc_server.rs index 970cfca498..051d37181f 100644 --- a/node/src/components/rpc_server.rs +++ b/node/src/components/rpc_server.rs @@ -25,7 +25,7 @@ use futures::join; use tracing::{error, info, warn}; use casper_storage::data_access_layer::{ - BalanceRequest, BalanceResult, EraValidatorsResult, GetBidsRequest, QueryRequest, QueryResult, + BalanceRequest, BalanceResult, BidsRequest, EraValidatorsResult, QueryRequest, QueryResult, }; use casper_types::{Digest, Key, ProtocolVersion, URef}; @@ -273,7 +273,7 @@ where state_root_hash, responder, }) => { - let get_bids_request = GetBidsRequest::new(state_root_hash); + let get_bids_request = BidsRequest::new(state_root_hash); effect_builder .get_bids(get_bids_request) .event(move |result| Event::GetBidsResult { diff --git a/node/src/components/rpc_server/event.rs b/node/src/components/rpc_server/event.rs index a12165bf5e..d8f6434db0 100644 --- a/node/src/components/rpc_server/event.rs +++ b/node/src/components/rpc_server/event.rs @@ -6,7 +6,7 @@ use std::{ use derive_more::From; use casper_storage::data_access_layer::{ - BalanceResult, EraValidatorsResult, GetBidsResult, QueryResult, + BalanceResult, BidsResult, EraValidatorsResult, QueryResult, }; use casper_types::{BlockHash, Transfer}; @@ -34,8 +34,8 @@ pub(crate) enum Event { main_responder: Responder, }, GetBidsResult { - result: GetBidsResult, - main_responder: Responder, + result: BidsResult, + main_responder: Responder, }, GetPeersResult { peers: BTreeMap, diff --git a/node/src/components/rpc_server/rpcs/state.rs b/node/src/components/rpc_server/rpcs/state.rs index c0d4b2a3bd..93befdb58c 100644 --- a/node/src/components/rpc_server/rpcs/state.rs +++ b/node/src/components/rpc_server/rpcs/state.rs @@ -10,7 +10,7 @@ use tracing::{debug, error, info, warn}; use casper_json_rpc::ReservedErrorCode; use casper_storage::{ - data_access_layer::{get_bids::GetBidsResult, BalanceResult, EraValidatorsResult, QueryResult}, + data_access_layer::{get_bids::BidsResult, BalanceResult, EraValidatorsResult, QueryResult}, global_state::trie::merkle_proof::TrieMerkleProof, }; use casper_types::{ @@ -373,8 +373,8 @@ impl RpcWithOptionalParams for GetAuctionInfo { .await; let bids = match get_bids_result { - GetBidsResult::Success { bids } => bids, - GetBidsResult::RootNotFound => { + BidsResult::Success { bids } => bids, + BidsResult::RootNotFound => { error!( block_hash=?block.hash(), ?state_root_hash, @@ -388,7 +388,7 @@ impl RpcWithOptionalParams for GetAuctionInfo { ), )); } - GetBidsResult::Failure(error) => { + BidsResult::Failure(error) => { error!( block_hash=?block.hash(), ?state_root_hash, diff --git a/node/src/effect.rs b/node/src/effect.rs index 49eccc2d29..238e9b4893 100644 --- a/node/src/effect.rs +++ b/node/src/effect.rs @@ -118,7 +118,7 @@ use tracing::{debug, error, warn}; use casper_execution_engine::engine_state::{self}; use casper_storage::{ data_access_layer::{ - BalanceRequest, BalanceResult, GetBidsRequest, GetBidsResult, QueryRequest, QueryResult, + BalanceRequest, BalanceResult, BidsRequest, BidsResult, QueryRequest, QueryResult, }, global_state::trie::TrieRaw, }; @@ -2072,13 +2072,13 @@ impl EffectBuilder { } /// Requests a query be executed on the Contract Runtime component. - pub(crate) async fn get_bids(self, get_bids_request: GetBidsRequest) -> GetBidsResult + pub(crate) async fn get_bids(self, get_bids_request: BidsRequest) -> BidsResult where REv: From, { self.make_request( |responder| ContractRuntimeRequest::GetBids { - get_bids_request, + request: get_bids_request, responder, }, QueueKind::ContractRuntime, diff --git a/node/src/effect/requests.rs b/node/src/effect/requests.rs index 5f64a62acd..3c7b4d3773 100644 --- a/node/src/effect/requests.rs +++ b/node/src/effect/requests.rs @@ -19,7 +19,7 @@ use static_assertions::const_assert; use casper_execution_engine::engine_state::{self}; use casper_storage::{ data_access_layer::{ - get_bids::{GetBidsRequest, GetBidsResult}, + get_bids::{BidsRequest, BidsResult}, BalanceRequest, BalanceResult, EraValidatorsRequest, EraValidatorsResult, QueryRequest, QueryResult, }, @@ -767,7 +767,7 @@ pub(crate) enum RpcRequest { /// The global state hash. state_root_hash: Digest, /// Responder to call with the result. - responder: Responder, + responder: Responder, }, /// Query the global state at the given root hash. @@ -925,9 +925,9 @@ pub(crate) enum ContractRuntimeRequest { GetBids { /// Get bids request. #[serde(skip_serializing)] - get_bids_request: GetBidsRequest, + request: BidsRequest, /// Responder to call with the result. - responder: Responder, + responder: Responder, }, /// Returns the value of the execution results checksum stored in the ChecksumRegistry for the /// given state root hash. @@ -1008,7 +1008,8 @@ impl Display for ContractRuntimeRequest { write!(formatter, "get era validators: {:?}", request) } ContractRuntimeRequest::GetBids { - get_bids_request, .. + request: get_bids_request, + .. } => { write!(formatter, "get bids request: {:?}", get_bids_request) } diff --git a/node/src/reactor/main_reactor/tests.rs b/node/src/reactor/main_reactor/tests.rs index 4152579702..3fc9978e9d 100644 --- a/node/src/reactor/main_reactor/tests.rs +++ b/node/src/reactor/main_reactor/tests.rs @@ -11,7 +11,7 @@ use tempfile::TempDir; use tokio::time::{self, error::Elapsed}; use tracing::{error, info}; -use casper_execution_engine::engine_state::{GetBidsRequest, GetBidsResult}; +use casper_execution_engine::engine_state::{BidsRequest, BidsResult}; use casper_storage::global_state::state::{StateProvider, StateReader}; use casper_types::{ execution::{ExecutionResult, ExecutionResultV2, Transform, TransformKind}, @@ -527,7 +527,7 @@ impl TestFixture { .contract_runtime .auction_state(*highest_block.state_root_hash()); - if let GetBidsResult::Success { bids } = bids_result { + if let BidsResult::Success { bids } = bids_result { match bids.iter().find(|bid_kind| { &bid_kind.validator_public_key() == validator_public_key && bid_kind.delegator_public_key().as_ref() == delegator_public_key @@ -739,9 +739,9 @@ impl SwitchBlocks { fn bids(&self, nodes: &Nodes, era_number: u64) -> Vec { let state_root_hash = *self.headers[era_number as usize].state_root_hash(); for runner in nodes.values() { - let request = GetBidsRequest::new(state_root_hash); + let request = BidsRequest::new(state_root_hash); let engine_state = runner.main_reactor().contract_runtime().engine_state(); - if let GetBidsResult::Success { bids } = engine_state.get_bids(request) { + if let BidsResult::Success { bids } = engine_state.get_bids(request) { return bids; } } diff --git a/storage/src/data_access_layer.rs b/storage/src/data_access_layer.rs index d7a2490b50..c6a7949d2d 100644 --- a/storage/src/data_access_layer.rs +++ b/storage/src/data_access_layer.rs @@ -16,7 +16,7 @@ pub mod query; pub use balance::{BalanceRequest, BalanceResult}; pub use era_validators::{EraValidatorsRequest, EraValidatorsResult}; -pub use get_bids::{GetBidsError, GetBidsRequest, GetBidsResult}; +pub use get_bids::{BidsRequest, BidsResult}; pub use query::{QueryRequest, QueryResult}; pub struct Block { diff --git a/storage/src/data_access_layer/get_bids.rs b/storage/src/data_access_layer/get_bids.rs index d6b1a32cfb..b9d72f239b 100644 --- a/storage/src/data_access_layer/get_bids.rs +++ b/storage/src/data_access_layer/get_bids.rs @@ -1,18 +1,18 @@ //! Support for obtaining current bids from the auction system. -use crate::{global_state::error::Error as GlobalStateError, tracking_copy::TrackingCopyError}; +use crate::tracking_copy::TrackingCopyError; -use casper_types::{system::auction::BidKind, Digest, Key}; +use casper_types::{system::auction::BidKind, Digest}; /// Represents a request to obtain current bids in the auction system. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct GetBidsRequest { +pub struct BidsRequest { state_hash: Digest, } -impl GetBidsRequest { +impl BidsRequest { /// Creates new request. pub fn new(state_hash: Digest) -> Self { - GetBidsRequest { state_hash } + BidsRequest { state_hash } } /// Returns state root hash. @@ -21,17 +21,9 @@ impl GetBidsRequest { } } -#[derive(Debug)] -pub enum GetBidsError { - GlobalState(GlobalStateError), - TrackingCopyError(TrackingCopyError), - MissingBid(Key), - InvalidStoredValueVariant(Key), -} - /// Represents a result of a `get_bids` request. #[derive(Debug)] -pub enum GetBidsResult { +pub enum BidsResult { /// Invalid state root hash. RootNotFound, /// Contains current bids returned from the global state. @@ -39,10 +31,10 @@ pub enum GetBidsResult { /// Current bids. bids: Vec, }, - Failure(GetBidsError), + Failure(TrackingCopyError), } -impl GetBidsResult { +impl BidsResult { /// Returns wrapped [`Vec`] if this represents a successful query result. pub fn into_success(self) -> Option> { if let Self::Success { bids } = self { diff --git a/storage/src/global_state/state/mod.rs b/storage/src/global_state/state/mod.rs index b8c38a7c61..fbb45d73d7 100644 --- a/storage/src/global_state/state/mod.rs +++ b/storage/src/global_state/state/mod.rs @@ -15,7 +15,7 @@ use casper_types::{ execution::{Effects, Transform, TransformError, TransformInstruction, TransformKind}, package::PackageKindTag, system::{auction::SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY, AUCTION}, - Digest, Key, StoredValue, + Digest, Key, KeyTag, StoredValue, }; #[cfg(test)] @@ -23,8 +23,8 @@ pub use self::lmdb::make_temporary_global_state; use crate::{ data_access_layer::{ - era_validators::EraValidatorsResult, BalanceRequest, BalanceResult, EraValidatorsRequest, - QueryRequest, QueryResult, + era_validators::EraValidatorsResult, BalanceRequest, BalanceResult, BidsRequest, + BidsResult, EraValidatorsRequest, QueryRequest, QueryResult, }, global_state::{ error::Error as GlobalStateError, @@ -199,6 +199,37 @@ pub trait StateProvider { EraValidatorsResult::Success { era_validators } } + fn bids(&self, bids_request: BidsRequest) -> BidsResult { + let state_root_hash = bids_request.state_hash(); + let mut tc = match self.tracking_copy(state_root_hash) { + Ok(Some(tc)) => tc, + Ok(None) => return BidsResult::RootNotFound, + Err(err) => return BidsResult::Failure(TrackingCopyError::Storage(err)), + }; + + let bid_keys = match tc.get_keys(&KeyTag::BidAddr) { + Ok(ret) => ret, + Err(err) => return BidsResult::Failure(err), + }; + + let mut bids = vec![]; + for key in bid_keys.iter() { + match tc.get(key) { + Ok(ret) => match ret { + Some(StoredValue::BidKind(bid_kind)) => { + bids.push(bid_kind); + } + Some(_) => { + return BidsResult::Failure(TrackingCopyError::UnexpectedStoredValueVariant) + } + None => return BidsResult::Failure(TrackingCopyError::MissingBid(*key)), + }, + Err(error) => return BidsResult::Failure(error), + } + } + BidsResult::Success { bids } + } + /// Returns an empty root hash. fn empty_root(&self) -> Digest; diff --git a/storage/src/tracking_copy/error.rs b/storage/src/tracking_copy/error.rs index 4647064ebe..c85ea052c8 100644 --- a/storage/src/tracking_copy/error.rs +++ b/storage/src/tracking_copy/error.rs @@ -188,14 +188,17 @@ pub enum Error { #[error("Invalid UTF-8 string encoding: {0}")] InvalidUtf8Encoding(Utf8Error), /// Circular reference error. - #[error("Query attempted a circular reference : {0}")] + #[error("Query attempted a circular reference: {0}")] CircularReference(String), /// Depth limit reached. - #[error("Query exceeded depth limit : {depth}")] + #[error("Query exceeded depth limit: {depth}")] QueryDepthLimit { /// Current depth limit. depth: u64, }, + /// Missing bid. + #[error("Missing bid: {0}")] + MissingBid(Key), } impl Error {