Skip to content

Commit

Permalink
adding ExecutionResultsChecksum to data_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
EdHastingsCasperAssociation committed Feb 10, 2024
1 parent 2c5b146 commit 022e668
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 76 deletions.
64 changes: 39 additions & 25 deletions node/src/components/block_synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::{debug, error, info, trace, warn};

use casper_execution_engine::engine_state;
use casper_storage::data_access_layer::ExecutionResultsChecksumResult;
use casper_types::{
Block, BlockHash, BlockHeader, BlockSignatures, Chainspec, Digest, FinalitySignature,
FinalitySignatureId, Timestamp, Transaction,
Expand Down Expand Up @@ -1081,41 +1081,55 @@ impl BlockSynchronizer {
fn got_execution_results_checksum(
&mut self,
block_hash: BlockHash,
result: Result<Option<Digest>, engine_state::Error>,
result: ExecutionResultsChecksumResult,
) {
let builder = match &mut self.historical {
None => {
// execution results checksums are only relevant to historical blocks
debug!(%block_hash, "BlockSynchronizer: not currently synchronising block");
return;
}
Some(builder) => {
let current_block_hash = builder.block_hash();
if current_block_hash != block_hash {
debug!(%block_hash, %current_block_hash, "BlockSynchronizer: currently synchronising different block");
return;
}
builder
}
};

let execution_results_checksum = match result {
Ok(Some(digest)) => {
debug!(
"BlockSynchronizer: got execution_results_checksum for {}",
block_hash
);
ExecutionResultsChecksum::Checkable(digest)
ExecutionResultsChecksumResult::Failure(error) => {
error!(%block_hash, %error, "BlockSynchronizer: unexpected error getting checksum registry");
ExecutionResultsChecksum::Uncheckable
}
Err(engine_state::Error::MissingChecksumRegistry) => {
// The registry will not exist for legacy blocks.
ExecutionResultsChecksumResult::RootNotFound => {
error!(%block_hash, "BlockSynchronizer: unexpected error getting checksum registry (root not found)");
ExecutionResultsChecksum::Uncheckable
}
Ok(None) => {
warn!("BlockSynchronizer: the checksum registry should contain the execution results checksum");
ExecutionResultsChecksumResult::ChecksumNotFound => {
error!(%block_hash, "BlockSynchronizer: checksum not found (should exist)");
ExecutionResultsChecksum::Uncheckable
}
Err(error) => {
error!(%error, "BlockSynchronizer: unexpected error getting checksum registry");
ExecutionResultsChecksumResult::RegistryNotFound => {
// we didn't track this checksum pre-1.5
debug!(%block_hash, "BlockSynchronizer: checksum registry not found (legacy record)");
ExecutionResultsChecksum::Uncheckable
}
ExecutionResultsChecksumResult::Success { checksum } => {
debug!(
%block_hash, "BlockSynchronizer: got execution_results_checksum {}",
checksum
);
ExecutionResultsChecksum::Checkable(checksum)
}
};

if let Some(builder) = &mut self.historical {
if builder.block_hash() != block_hash {
debug!(%block_hash, "BlockSynchronizer: not currently synchronising block");
} else {
builder.latch_decrement();
if let Err(error) =
builder.register_execution_results_checksum(execution_results_checksum)
{
error!(%block_hash, %error, "BlockSynchronizer: failed to apply execution results checksum");
}
}
builder.latch_decrement();
if let Err(error) = builder.register_execution_results_checksum(execution_results_checksum)
{
error!(%block_hash, %error, "BlockSynchronizer: failed to apply execution results checksum");
}
}

Expand Down
8 changes: 4 additions & 4 deletions node/src/components/block_synchronizer/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use derive_more::From;
use either::Either;
use serde::Serialize;

use casper_execution_engine::engine_state;
use casper_types::{Block, BlockHash, BlockHeader, Digest, FinalitySignature, Transaction};
use casper_storage::data_access_layer::ExecutionResultsChecksumResult;
use casper_types::{Block, BlockHash, BlockHeader, FinalitySignature, Transaction};

use super::GlobalStateSynchronizerEvent;
use crate::{
Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) enum Event {
GotExecutionResultsChecksum {
block_hash: BlockHash,
#[serde(skip_serializing)]
result: Result<Option<Digest>, engine_state::Error>,
result: ExecutionResultsChecksumResult,
},
DeployFetched {
block_hash: BlockHash,
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Display for Event {
Event::GotExecutionResultsChecksum {
block_hash: _,
result,
} => match result {
} => match result.as_legacy() {
Ok(Some(digest)) => write!(f, "got exec results checksum {}", digest),
Ok(None) => write!(f, "got no exec results checksum"),
Err(error) => write!(f, "failed to get exec results checksum: {}", error),
Expand Down
25 changes: 15 additions & 10 deletions node/src/components/block_synchronizer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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_types::{
testing::TestRng, AccessRights, BlockV2, CLValue, Chainspec, Deploy, EraId, Key,
Expand Down Expand Up @@ -2513,7 +2514,7 @@ async fn historical_sync_no_legacy_block() {
state_root_hash,
responder,
},
)) => responder.respond(Ok(Some(state_root_hash))).await,
)) => responder.respond(ExecutionResultsChecksumResult::Success {checksum: state_root_hash}).await,
other => panic!("Event should be of type `ContractRuntimeRequest(ContractRuntimeRequest::GetExecutionResultsChecksum) but it is {:?}", other),
}

Expand All @@ -2522,7 +2523,9 @@ async fn historical_sync_no_legacy_block() {
rng,
Event::GotExecutionResultsChecksum {
block_hash: *block.hash(),
result: Ok(Some(Digest::SENTINEL_NONE)),
result: ExecutionResultsChecksumResult::Success {
checksum: Digest::SENTINEL_NONE,
},
},
);
let events = mock_reactor.process_effects(effects).await;
Expand Down Expand Up @@ -2737,7 +2740,7 @@ async fn historical_sync_legacy_block_strict_finality() {
state_root_hash,
responder,
},
)) => responder.respond(Ok(Some(state_root_hash))).await,
)) => responder.respond(ExecutionResultsChecksumResult::Success {checksum: state_root_hash}).await,
other => panic!("Event should be of type `ContractRuntimeRequest(ContractRuntimeRequest::GetExecutionResultsChecksum) but it is {:?}", other),
}

Expand All @@ -2746,7 +2749,7 @@ async fn historical_sync_legacy_block_strict_finality() {
rng,
Event::GotExecutionResultsChecksum {
block_hash: *block.hash(),
result: Ok(None), // No checksum because we want to test a legacy block
result: ExecutionResultsChecksumResult::RegistryNotFound, // test a legacy block
},
);
let events = mock_reactor.process_effects(effects).await;
Expand Down Expand Up @@ -2937,7 +2940,7 @@ async fn historical_sync_legacy_block_weak_finality() {
state_root_hash,
responder,
},
)) => responder.respond(Ok(Some(state_root_hash))).await,
)) => responder.respond(ExecutionResultsChecksumResult::Success {checksum: state_root_hash}).await,
other => panic!("Event should be of type `ContractRuntimeRequest(ContractRuntimeRequest::GetExecutionResultsChecksum) but it is {:?}", other),
}

Expand All @@ -2946,7 +2949,7 @@ async fn historical_sync_legacy_block_weak_finality() {
rng,
Event::GotExecutionResultsChecksum {
block_hash: *block.hash(),
result: Ok(None), // No checksum because we want to test a legacy block
result: ExecutionResultsChecksumResult::RegistryNotFound, // test a legacy block
},
);
let events = mock_reactor.process_effects(effects).await;
Expand Down Expand Up @@ -3148,7 +3151,7 @@ async fn historical_sync_legacy_block_any_finality() {
state_root_hash,
responder,
},
)) => responder.respond(Ok(Some(state_root_hash))).await,
)) => responder.respond(ExecutionResultsChecksumResult::Success {checksum: state_root_hash}).await,
other => panic!("Event should be of type `ContractRuntimeRequest(ContractRuntimeRequest::GetExecutionResultsChecksum) but it is {:?}", other),
}

Expand All @@ -3157,7 +3160,7 @@ async fn historical_sync_legacy_block_any_finality() {
rng,
Event::GotExecutionResultsChecksum {
block_hash: *block.hash(),
result: Ok(None), // No checksum because we want to test a legacy block
result: ExecutionResultsChecksumResult::RegistryNotFound, // test a legacy block
},
);
let events = mock_reactor.process_effects(effects).await;
Expand Down Expand Up @@ -3796,7 +3799,9 @@ async fn historical_sync_latch_should_not_decrement_for_old_deploy_fetch_respons
rng,
Event::GotExecutionResultsChecksum {
block_hash: *block.hash(),
result: Ok(Some(Digest::SENTINEL_NONE)),
result: ExecutionResultsChecksumResult::Success {
checksum: Digest::SENTINEL_NONE,
},
},
);

Expand Down Expand Up @@ -4081,7 +4086,7 @@ async fn historical_sync_latch_should_not_decrement_for_old_execution_results()
rng,
Event::GotExecutionResultsChecksum {
block_hash: *block.hash(),
result: Ok(Some(checksum)),
result: ExecutionResultsChecksumResult::Success { checksum },
},
);

Expand Down
41 changes: 24 additions & 17 deletions node/src/components/contract_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ use casper_execution_engine::engine_state::{
use casper_storage::data_access_layer::{BidsRequest, BidsResult};

use casper_storage::{
data_access_layer::{BlockStore, DataAccessLayer, QueryRequest, QueryResult},
data_access_layer::{
BlockStore, DataAccessLayer, ExecutionResultsChecksumRequest, QueryRequest, QueryResult,
},
global_state::{
state::{lmdb::LmdbGlobalState, StateProvider},
transaction_source::lmdb::LmdbEnvironment,
Expand Down Expand Up @@ -857,7 +859,7 @@ impl ContractRuntime {
metrics
.get_era_validators
.observe(start.elapsed().as_secs_f64());
trace!(?result, "balance result");
trace!(?result, "era validators result");
responder.respond(result).await
}
.ignore()
Expand All @@ -873,7 +875,26 @@ impl ContractRuntime {
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");
trace!(?result, "bids result");
responder.respond(result).await
}
.ignore()
}
ContractRuntimeRequest::GetExecutionResultsChecksum {
state_root_hash,
responder,
} => {
trace!(?state_root_hash, "get exection results checksum request");
let metrics = Arc::clone(&self.metrics);
let data_access_layer = Arc::clone(&self.data_access_layer);
async move {
let start = Instant::now();
let request = ExecutionResultsChecksumRequest::new(state_root_hash);
let result = data_access_layer.execution_result_checksum(request);
metrics
.execution_results_checksum
.observe(start.elapsed().as_secs_f64());
trace!(?result, "execution result checksum");
responder.respond(result).await
}
.ignore()
Expand Down Expand Up @@ -1007,20 +1028,6 @@ impl ContractRuntime {
.set(self.exec_queue.len().try_into().unwrap_or(i64::MIN));
effects
}
ContractRuntimeRequest::GetExecutionResultsChecksum {
state_root_hash,
responder,
} => {
let result = self
.engine_state
.get_checksum_registry(state_root_hash)
.map(|maybe_registry| {
maybe_registry.and_then(|registry| {
registry.get(EXECUTION_RESULTS_CHECKSUM_NAME).copied()
})
});
responder.respond(result).ignore()
}
ContractRuntimeRequest::GetAddressableEntity {
state_root_hash,
key,
Expand Down
11 changes: 11 additions & 0 deletions node/src/components/contract_runtime/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const GET_ERA_VALIDATORS_HELP: &str =
const GET_BIDS_NAME: &str = "contract_runtime_get_bids";
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 PUT_TRIE_NAME: &str = "contract_runtime_put_trie";
const PUT_TRIE_HELP: &str = "time in seconds to put a trie";

Expand Down Expand Up @@ -75,6 +78,7 @@ pub struct Metrics {
pub(super) get_round_seigniorage_rate: Histogram,
pub(super) get_era_validators: Histogram,
pub(super) get_bids: Histogram,
pub(super) execution_results_checksum: Histogram,
pub(super) put_trie: Histogram,
pub(super) get_trie: Histogram,
pub(super) exec_block: Histogram,
Expand Down Expand Up @@ -165,6 +169,12 @@ impl Metrics {
GET_BIDS_HELP,
common_buckets.clone(),
)?,
execution_results_checksum: utils::register_histogram_metric(
registry,
EXECUTION_RESULTS_CHECKSUM_NAME,
EXECUTION_RESULTS_CHECKSUM_HELP,
common_buckets.clone(),
)?,
get_trie: utils::register_histogram_metric(
registry,
GET_TRIE_NAME,
Expand Down Expand Up @@ -200,6 +210,7 @@ impl Drop for Metrics {
unregister_metric!(self.registry, self.get_balance);
unregister_metric!(self.registry, self.get_era_validators);
unregister_metric!(self.registry, self.get_bids);
unregister_metric!(self.registry, self.execution_results_checksum);
unregister_metric!(self.registry, self.put_trie);
unregister_metric!(self.registry, self.get_trie);
unregister_metric!(self.registry, self.exec_block);
Expand Down
6 changes: 4 additions & 2 deletions node/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ use casper_storage::{
global_state::trie::TrieRaw,
};

use casper_storage::data_access_layer::{EraValidatorsRequest, EraValidatorsResult};
use casper_storage::data_access_layer::{
EraValidatorsRequest, EraValidatorsResult, ExecutionResultsChecksumResult,
};
use casper_types::{
bytesrepr::Bytes,
contract_messages::Messages,
Expand Down Expand Up @@ -2091,7 +2093,7 @@ impl<REv> EffectBuilder<REv> {
pub(crate) async fn get_execution_results_checksum(
self,
state_root_hash: Digest,
) -> Result<Option<Digest>, engine_state::Error>
) -> ExecutionResultsChecksumResult
where
REv: From<ContractRuntimeRequest>,
{
Expand Down
6 changes: 3 additions & 3 deletions node/src/effect/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use casper_execution_engine::engine_state::{self};
use casper_storage::{
data_access_layer::{
get_bids::{BidsRequest, BidsResult},
BalanceRequest, BalanceResult, EraValidatorsRequest, EraValidatorsResult, QueryRequest,
QueryResult,
BalanceRequest, BalanceResult, EraValidatorsRequest, EraValidatorsResult,
ExecutionResultsChecksumResult, QueryRequest, QueryResult,
},
global_state::trie::TrieRaw,
};
Expand Down Expand Up @@ -933,7 +933,7 @@ pub(crate) enum ContractRuntimeRequest {
/// given state root hash.
GetExecutionResultsChecksum {
state_root_hash: Digest,
responder: Responder<Result<Option<Digest>, engine_state::Error>>,
responder: Responder<ExecutionResultsChecksumResult>,
},
/// Returns an `AddressableEntity` if found under the given key. If a legacy `Account` exists
/// under the given key, it will be converted to an `AddressableEntity` and returned.
Expand Down
5 changes: 5 additions & 0 deletions storage/src/data_access_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ use crate::tracking_copy::TrackingCopy;

pub mod balance;
pub mod era_validators;
mod execution_results_checksum;
pub mod get_bids;
pub mod query;

pub use balance::{BalanceRequest, BalanceResult};
pub use era_validators::{EraValidatorsRequest, EraValidatorsResult};
pub use execution_results_checksum::{
ExecutionResultsChecksumRequest, ExecutionResultsChecksumResult,
EXECUTION_RESULTS_CHECKSUM_NAME,
};
pub use get_bids::{BidsRequest, BidsResult};
pub use query::{QueryRequest, QueryResult};

Expand Down
Loading

0 comments on commit 022e668

Please sign in to comment.