Skip to content

Commit

Permalink
adding AddressableEntity to data_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
EdHastingsCasperAssociation committed Feb 10, 2024
1 parent 022e668 commit 10ae2a4
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ where

let get_bids_result = self.engine_state.get_bids(get_bids_request);

get_bids_result.into_success().unwrap()
get_bids_result.into_option().unwrap()
}

/// Returns named keys for an account entity by its account hash.
Expand Down
6 changes: 4 additions & 2 deletions node/src/components/block_synchronizer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ use derive_more::From;
use num_rational::Ratio;
use rand::{seq::IteratorRandom, Rng};

use casper_storage::data_access_layer::ExecutionResultsChecksumResult;
use casper_storage::global_state::trie::merkle_proof::TrieMerkleProof;
use casper_storage::{
data_access_layer::ExecutionResultsChecksumResult,
global_state::trie::merkle_proof::TrieMerkleProof,
};
use casper_types::{
testing::TestRng, AccessRights, BlockV2, CLValue, Chainspec, Deploy, EraId, Key,
LegacyRequiredFinality, ProtocolVersion, PublicKey, SecretKey, StoredValue, TestBlockBuilder,
Expand Down
36 changes: 22 additions & 14 deletions node/src/components/contract_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ use casper_storage::data_access_layer::{BidsRequest, BidsResult};

use casper_storage::{
data_access_layer::{
BlockStore, DataAccessLayer, ExecutionResultsChecksumRequest, QueryRequest, QueryResult,
AddressableEntityRequest, BlockStore, DataAccessLayer, ExecutionResultsChecksumRequest,
QueryRequest, QueryResult,
},
global_state::{
state::{lmdb::LmdbGlobalState, StateProvider},
Expand Down Expand Up @@ -899,6 +900,26 @@ impl ContractRuntime {
}
.ignore()
}
ContractRuntimeRequest::GetAddressableEntity {
state_root_hash,
key,
responder,
} => {
trace!(?state_root_hash, "get addressable entity");
let metrics = Arc::clone(&self.metrics);
let data_access_layer = Arc::clone(&self.data_access_layer);
async move {
let start = Instant::now();
let request = AddressableEntityRequest::new(state_root_hash, key);
let result = data_access_layer.addressable_entity(request);
metrics
.addressable_entity
.observe(start.elapsed().as_secs_f64());
trace!(?result, "get addressable entity");
responder.respond(result).await
}
.ignore()
}
ContractRuntimeRequest::GetTrie {
trie_or_chunk_id,
responder,
Expand Down Expand Up @@ -1028,19 +1049,6 @@ impl ContractRuntime {
.set(self.exec_queue.len().try_into().unwrap_or(i64::MIN));
effects
}
ContractRuntimeRequest::GetAddressableEntity {
state_root_hash,
key,
responder,
} => {
let engine_state = Arc::clone(&self.engine_state);
async move {
let result =
operations::get_addressable_entity(&engine_state, state_root_hash, key);
responder.respond(result).await
}
.ignore()
}
ContractRuntimeRequest::SpeculativelyExecute {
execution_prestate,
transaction,
Expand Down
10 changes: 10 additions & 0 deletions node/src/components/contract_runtime/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const GET_BIDS_HELP: &str = "time in seconds to get bids from global state";
const EXECUTION_RESULTS_CHECKSUM_NAME: &str = "contract_runtime_execution_results_checksum";
const EXECUTION_RESULTS_CHECKSUM_HELP: &str = "contract_runtime_execution_results_checksum";

const ADDRESSABLE_ENTITY_NAME: &str = "contract_runtime_addressable_entity";
const ADDRESSABLE_ENTITY_HELP: &str = "contract_runtime_addressable_entity";

const PUT_TRIE_NAME: &str = "contract_runtime_put_trie";
const PUT_TRIE_HELP: &str = "time in seconds to put a trie";

Expand Down Expand Up @@ -79,6 +82,7 @@ pub struct Metrics {
pub(super) get_era_validators: Histogram,
pub(super) get_bids: Histogram,
pub(super) execution_results_checksum: Histogram,
pub(super) addressable_entity: Histogram,
pub(super) put_trie: Histogram,
pub(super) get_trie: Histogram,
pub(super) exec_block: Histogram,
Expand Down Expand Up @@ -175,6 +179,12 @@ impl Metrics {
EXECUTION_RESULTS_CHECKSUM_HELP,
common_buckets.clone(),
)?,
addressable_entity: utils::register_histogram_metric(
registry,
ADDRESSABLE_ENTITY_NAME,
ADDRESSABLE_ENTITY_HELP,
common_buckets.clone(),
)?,
get_trie: utils::register_histogram_metric(
registry,
GET_TRIE_NAME,
Expand Down
169 changes: 3 additions & 166 deletions node/src/components/contract_runtime/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ use casper_execution_engine::engine_state::{
PruneResult, StepError, StepRequest, StepSuccess,
};
use casper_storage::{
data_access_layer::{
DataAccessLayer, EraValidatorsRequest, EraValidatorsResult, QueryRequest, QueryResult,
},
data_access_layer::{DataAccessLayer, EraValidatorsRequest, EraValidatorsResult},
global_state::state::{lmdb::LmdbGlobalState, CommitProvider, StateProvider},
};
use casper_types::{
account::AccountHash,
bytesrepr::{self, ToBytes, U32_SERIALIZED_LENGTH},
contract_messages::Messages,
execution::{Effects, ExecutionResult, ExecutionResultV2, Transform, TransformKind},
AddressableEntity, BlockV2, CLValue, ChecksumRegistry, DeployHash, Digest, EntityAddr,
EraEndV2, EraId, HashAddr, Key, ProtocolVersion, PublicKey, StoredValue, Transaction, U512,
BlockV2, CLValue, ChecksumRegistry, DeployHash, Digest, EraEndV2, EraId, Key, ProtocolVersion,
PublicKey, Transaction, U512,
};

use crate::{
Expand Down Expand Up @@ -620,166 +617,6 @@ pub(crate) fn compute_execution_results_checksum<'a>(
})
}

pub(super) fn get_addressable_entity<S>(
engine_state: &EngineState<S>,
state_root_hash: Digest,
key: Key,
) -> Option<AddressableEntity>
where
S: StateProvider + CommitProvider,
{
match key {
Key::AddressableEntity(entity_addr) => {
get_addressable_entity_under_entity_hash(engine_state, state_root_hash, entity_addr)
}
Key::Account(account_hash) => {
get_addressable_entity_under_account_hash(engine_state, state_root_hash, account_hash)
}
Key::Hash(contract_hash) => {
get_addressable_entity_under_contract_hash(engine_state, state_root_hash, contract_hash)
}
_ => {
warn!(%key, "expected a Key::AddressableEntity, Key::Account, Key::Hash");
None
}
}
}

fn get_addressable_entity_under_entity_hash<S>(
engine_state: &EngineState<S>,
state_root_hash: Digest,
entity_addr: EntityAddr,
) -> Option<AddressableEntity>
where
S: StateProvider + CommitProvider,
{
let key = Key::AddressableEntity(entity_addr);
let query_request = QueryRequest::new(state_root_hash, key, vec![]);
let value = match engine_state.run_query(query_request) {
QueryResult::RootNotFound => {
error!(%state_root_hash, "state root not found");
return None;
}
QueryResult::ValueNotFound(msg) => {
debug!(%key, "expected to find addressable entity: {}", msg);
return None;
}
QueryResult::Failure(tce) => {
warn!(%tce, %key, "failed querying for addressable entity");
return None;
}
QueryResult::Success { value, .. } => *value,
};
match value {
StoredValue::AddressableEntity(addressable_entity) => Some(addressable_entity),
_ => {
debug!(type_name = %value.type_name(), %key, "expected an AddressableEntity");
None
}
}
}

fn get_addressable_entity_under_account_hash<S>(
engine_state: &EngineState<S>,
state_root_hash: Digest,
account_hash: AccountHash,
) -> Option<AddressableEntity>
where
S: StateProvider + CommitProvider,
{
let account_key = Key::Account(account_hash);
let query_request = QueryRequest::new(state_root_hash, account_key, vec![]);
let value = match engine_state.run_query(query_request) {
QueryResult::RootNotFound => {
error!(%state_root_hash, "state root not found");
return None;
}
QueryResult::ValueNotFound(msg) => {
debug!(%account_key, "expected to find account: {}", msg);
return None;
}
QueryResult::Failure(tce) => {
warn!(%tce, %account_key, "failed querying for account");
return None;
}
QueryResult::Success { value, .. } => *value,
};
match value {
StoredValue::CLValue(cl_value) => match cl_value.into_t::<Key>() {
Ok(Key::AddressableEntity(entity_addr)) => {
get_addressable_entity_under_entity_hash(engine_state, state_root_hash, entity_addr)
}
Ok(invalid_key) => {
warn!(
%account_key,
%invalid_key,
"expected a Key::AddressableEntity to be stored under account hash"
);
None
}
Err(error) => {
warn!(%account_key, %error, "expected a Key to be stored under account hash");
None
}
},
StoredValue::Account(account) => Some(AddressableEntity::from(account)),
_ => {
warn!(
type_name = %value.type_name(),
%account_key,
"expected a CLValue or Account to be stored under account hash"
);
None
}
}
}

fn get_addressable_entity_under_contract_hash<S>(
engine_state: &EngineState<S>,
state_root_hash: Digest,
contract_hash: HashAddr,
) -> Option<AddressableEntity>
where
S: StateProvider + CommitProvider,
{
// First try with an `AddressableEntityHash` derived from the contract hash.
get_addressable_entity_under_entity_hash(
engine_state,
state_root_hash,
EntityAddr::SmartContract(contract_hash),
)
.or_else(|| {
// Didn't work; the contract was either not migrated yet or the `AddressableEntity`
// record was not available at this state root hash. Try to query with a
// contract key next.
let contract_key = Key::Hash(contract_hash);
let query_request = QueryRequest::new(state_root_hash, contract_key, vec![]);
let value = match engine_state.run_query(query_request) {
QueryResult::RootNotFound => {
error!(%state_root_hash, "state root not found");
return None;
}
QueryResult::ValueNotFound(msg) => {
debug!(%contract_key, "expected to find contract: {}", msg);
return None;
}
QueryResult::Failure(tce) => {
warn!(%tce, %contract_key, "failed querying for contract");
return None;
}
QueryResult::Success { value, .. } => *value,
};

match value {
StoredValue::Contract(contract) => Some(contract.into()),
_ => {
debug!(type_name = %value.type_name(), ?contract_hash, "expected a Contract");
None
}
}
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 10ae2a4

Please sign in to comment.