Skip to content

Commit

Permalink
adding bids support to data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
EdHastingsCasperAssociation committed Feb 8, 2024
1 parent f9fdf9a commit 8435966
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 80 deletions.
24 changes: 9 additions & 15 deletions execution_engine/src/engine_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -2241,24 +2239,24 @@ 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 {
Some(tracking_copy) => Rc::new(RefCell::new(TrackingCopy::new(
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![];
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1254,7 +1255,7 @@ where

/// Gets [`Vec<BidKind>`].
pub fn get_bids(&mut self) -> Vec<BidKind> {
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);

Expand Down
38 changes: 19 additions & 19 deletions node/src/components/contract_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions node/src/components/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions node/src/components/rpc_server/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -34,8 +34,8 @@ pub(crate) enum Event {
main_responder: Responder<EraValidatorsResult>,
},
GetBidsResult {
result: GetBidsResult,
main_responder: Responder<GetBidsResult>,
result: BidsResult,
main_responder: Responder<BidsResult>,
},
GetPeersResult {
peers: BTreeMap<NodeId, String>,
Expand Down
8 changes: 4 additions & 4 deletions node/src/components/rpc_server/rpcs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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,
Expand All @@ -388,7 +388,7 @@ impl RpcWithOptionalParams for GetAuctionInfo {
),
));
}
GetBidsResult::Failure(error) => {
BidsResult::Failure(error) => {
error!(
block_hash=?block.hash(),
?state_root_hash,
Expand Down
6 changes: 3 additions & 3 deletions node/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -2072,13 +2072,13 @@ impl<REv> EffectBuilder<REv> {
}

/// 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<ContractRuntimeRequest>,
{
self.make_request(
|responder| ContractRuntimeRequest::GetBids {
get_bids_request,
request: get_bids_request,
responder,
},
QueueKind::ContractRuntime,
Expand Down
11 changes: 6 additions & 5 deletions node/src/effect/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -767,7 +767,7 @@ pub(crate) enum RpcRequest {
/// The global state hash.
state_root_hash: Digest,
/// Responder to call with the result.
responder: Responder<GetBidsResult>,
responder: Responder<BidsResult>,
},

/// Query the global state at the given root hash.
Expand Down Expand Up @@ -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<GetBidsResult>,
responder: Responder<BidsResult>,
},
/// Returns the value of the execution results checksum stored in the ChecksumRegistry for the
/// given state root hash.
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions node/src/reactor/main_reactor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -739,9 +739,9 @@ impl SwitchBlocks {
fn bids(&self, nodes: &Nodes, era_number: u64) -> Vec<BidKind> {
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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion storage/src/data_access_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 8 additions & 16 deletions storage/src/data_access_layer/get_bids.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,28 +21,20 @@ 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.
Success {
/// Current bids.
bids: Vec<BidKind>,
},
Failure(GetBidsError),
Failure(TrackingCopyError),
}

impl GetBidsResult {
impl BidsResult {
/// Returns wrapped [`Vec<BidKind>`] if this represents a successful query result.
pub fn into_success(self) -> Option<Vec<BidKind>> {
if let Self::Success { bids } = self {
Expand Down
Loading

0 comments on commit 8435966

Please sign in to comment.