Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify block hash when responding with SYNCING #24

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/new_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
},
};
use eth2::types::{EthSpec, ExecutionBlockHash, ExecutionPayload, ForkName, Slot};
use execution_layer::http::ENGINE_NEW_PAYLOAD_V1;
use execution_layer::{http::ENGINE_NEW_PAYLOAD_V1, ExecutionLayer};
use std::time::{Duration, Instant};

impl<E: EthSpec> Multiplexer<E> {
Expand All @@ -18,7 +18,6 @@ impl<E: EthSpec> Multiplexer<E> {
tracing::info!("processing payload from controller");
let (id, json_execution_payload) = self.decode_execution_payload(request)?;

// TODO: verify block hash
let block_hash = *json_execution_payload.block_hash();
let block_number = *json_execution_payload.block_number();

Expand Down Expand Up @@ -65,8 +64,7 @@ impl<E: EthSpec> Multiplexer<E> {
tracing::info!("processing new payload from client");
let (id, execution_payload) = self.decode_execution_payload(request)?;

// TODO: verify block hash
let block_hash = execution_payload.block_hash();
let block_hash = *execution_payload.block_hash();
let block_number = *execution_payload.block_number();

// If this is a *recent* payload, wait a short time for a definite response from the EL.
Expand All @@ -75,20 +73,37 @@ impl<E: EthSpec> Multiplexer<E> {
if is_recent {
let start = Instant::now();
while start.elapsed().as_millis() < self.config.new_payload_wait_millis {
if let Some(status) = self.get_cached_payload_status(block_hash, true).await {
if let Some(status) = self.get_cached_payload_status(&block_hash, true).await {
return Response::new(id, status);
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
}

// Try again to get any status from the cache, or fall back on a SYNCING response.
let status = if let Some(status) = self.get_cached_payload_status(block_hash, false).await {
let status = if let Some(status) = self.get_cached_payload_status(&block_hash, false).await
{
if !Self::is_definite(&status) {
tracing::info!("sending indefinite status on newPayload");
}
status
} else {
// Before sending a synthetic SYNCING response, check the block hash.
let execution_payload = ExecutionPayload::from(execution_payload);
let (calculated_block_hash, _) =
ExecutionLayer::<E>::calculate_execution_block_hash(execution_payload.to_ref());

if calculated_block_hash != block_hash {
tracing::warn!(
expected = ?block_hash,
computed = ?calculated_block_hash,
"mismatched block hash"
);
return Err(ErrorResponse::invalid_request(
id,
format!("mismatched block hash {calculated_block_hash:?} vs {block_hash:?}"),
));
}
if is_recent {
tracing::info!("sending SYNCING response on recent newPayload");
} else {
Expand Down