Skip to content

Commit

Permalink
Fix regressions on hive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ventura committed Jan 15, 2025
1 parent 91c840c commit 40d9014
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 40 deletions.
86 changes: 46 additions & 40 deletions crates/networking/rpc/engine/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ethrex_blockchain::{
latest_canonical_block_hash,
payload::{create_payload, BuildPayloadArgs},
};
use ethrex_core::types::BlockHeader;
use ethrex_core::types::{BlockHeader, Fork};
use serde_json::Value;
use tracing::{info, warn};

Expand Down Expand Up @@ -37,7 +37,7 @@ impl RpcHandler for ForkChoiceUpdatedV1 {
let (head_block_opt, mut response) =
handle_forkchoice(&self.fork_choice_state, context.clone(), 1)?;
if let (Some(head_block), Some(attributes)) = (head_block_opt, &self.payload_attributes) {
validate_v1(attributes, head_block)?;
validate_attributes_v1_v2(attributes, &head_block, &context)?;
let payload_id = build_payload(attributes, context, &self.fork_choice_state, 1)?;
response.set_id(payload_id);
}
Expand Down Expand Up @@ -66,14 +66,7 @@ impl RpcHandler for ForkChoiceUpdatedV2 {
let (head_block_opt, mut response) =
handle_forkchoice(&self.fork_choice_state, context.clone(), 2)?;
if let (Some(head_block), Some(attributes)) = (head_block_opt, &self.payload_attributes) {
let chain_config = context.storage.get_chain_config()?;

if chain_config.is_shanghai_activated(attributes.timestamp) {
validate_v2(attributes, head_block)?;
} else {
validate_v1(attributes, head_block)?;
}

validate_attributes_v1_v2(attributes, &head_block, &context)?;
let payload_id = build_payload(attributes, context, &self.fork_choice_state, 2)?;
response.set_id(payload_id);
}
Expand All @@ -82,6 +75,35 @@ impl RpcHandler for ForkChoiceUpdatedV2 {
}
}

fn validate_attributes_v1_v2(
attributes: &PayloadAttributesV3,
head_block: &BlockHeader,
context: &RpcApiContext,
) -> Result<(), RpcErr> {
let chain_config = context.storage.get_chain_config()?;
if attributes.parent_beacon_block_root.is_some() {
return Err(RpcErr::InvalidPayloadAttributes(
"Attribute parent_beacon_block_root is non-null".to_string(),
));
}

match chain_config.get_fork(attributes.timestamp) {
Fork::Paris => {
if attributes.withdrawals.is_some() {
return Err(RpcErr::WrongParam("withdrawals".to_string()));
}
}
Fork::Shanghai => {
if attributes.withdrawals.is_none() {
return Err(RpcErr::WrongParam("withdrawals".to_string()));
}
}
Fork::Cancun => return Err(RpcErr::UnsuportedFork("{current_fork:?}".to_string())),
}

validate_timestamp(attributes, head_block)
}

#[derive(Debug)]
pub struct ForkChoiceUpdatedV3 {
pub fork_choice_state: ForkChoiceState,
Expand Down Expand Up @@ -115,7 +137,7 @@ impl RpcHandler for ForkChoiceUpdatedV3 {
let (head_block_opt, mut response) =
handle_forkchoice(&self.fork_choice_state, context.clone(), 3)?;
if let (Some(head_block), Some(attributes)) = (head_block_opt, &self.payload_attributes) {
validate_v3(attributes, head_block, &context)?;
validate_attributes_v3(attributes, &head_block, &context)?;
let payload_id = build_payload(attributes, context, &self.fork_choice_state, 3)?;
response.set_id(payload_id);
}
Expand Down Expand Up @@ -211,49 +233,33 @@ fn handle_forkchoice(
}
}

fn validate_v1(attributes: &PayloadAttributesV3, head_block: BlockHeader) -> Result<(), RpcErr> {
if attributes.withdrawals.is_some() {
return Err(RpcErr::WrongParam("withdrawals".to_string()));
}
if attributes.parent_beacon_block_root.is_some() {
return Err(RpcErr::WrongParam("parent_beacon_block_root".to_string()));
}
validate_timestamp(attributes, head_block)
}

fn validate_v2(attributes: &PayloadAttributesV3, head_block: BlockHeader) -> Result<(), RpcErr> {
if attributes.withdrawals.is_none() {
return Err(RpcErr::WrongParam("withdrawals".to_string()));
}
if attributes.parent_beacon_block_root.is_some() {
return Err(RpcErr::WrongParam("parent_beacon_block_root".to_string()));
}
validate_timestamp(attributes, head_block)
}

fn validate_v3(
fn validate_attributes_v3(
attributes: &PayloadAttributesV3,
head_block: BlockHeader,
head_block: &BlockHeader,
context: &RpcApiContext,
) -> Result<(), RpcErr> {
let chain_config = context.storage.get_chain_config()?;
// Specification indicates this order of validations:
// https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md#specification-1
if attributes.withdrawals.is_none() {
return Err(RpcErr::InvalidPayloadAttributes("withdrawals".to_string()));
}
if attributes.parent_beacon_block_root.is_none() {
return Err(RpcErr::InvalidPayloadAttributes(
"Attribute parent_beacon_block_root is null".to_string(),
));
}
if !chain_config.is_cancun_activated(attributes.timestamp) {
return Err(RpcErr::UnsuportedFork(
"forkChoiceV3 used to build pre-Cancun payload".to_string(),
));
}
if attributes.withdrawals.is_none() {
return Err(RpcErr::WrongParam("withdrawals".to_string()));
}
if attributes.parent_beacon_block_root.is_none() {
return Err(RpcErr::WrongParam("parent_beacon_block_root".to_string()));
}
validate_timestamp(attributes, head_block)
}

fn validate_timestamp(
attributes: &PayloadAttributesV3,
head_block: BlockHeader,
head_block: &BlockHeader,
) -> Result<(), RpcErr> {
if attributes.timestamp <= head_block.timestamp {
return Err(RpcErr::InvalidPayloadAttributes(
Expand Down
9 changes: 9 additions & 0 deletions crates/networking/rpc/engine/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl RpcHandler for GetPayloadV2Request {

fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
let payload = get_payload(self.payload_id, &context)?;
validate_payload_v1_v2(&payload.0, &context)?;
let execution_payload_response =
build_execution_payload_response(self.payload_id, payload, None, context)?;
serde_json::to_value(execution_payload_response)
Expand Down Expand Up @@ -241,6 +242,14 @@ fn validate_execution_payload_v3(payload: &ExecutionPayload) -> Result<(), RpcEr
Ok(())
}

fn validate_payload_v1_v2(block: &Block, context: &RpcApiContext) -> Result<(), RpcErr> {
let chain_config = &context.storage.get_chain_config()?;
if chain_config.is_cancun_activated(block.header.timestamp) {
return Err(RpcErr::UnsuportedFork(format!("Cancun")));
}
Ok(())
}

fn handle_new_payload_v1_v2(
payload: &ExecutionPayload,
context: RpcApiContext,
Expand Down

0 comments on commit 40d9014

Please sign in to comment.