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

feat: add response type for execution header #5342

Merged
merged 1 commit into from
Nov 7, 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
75 changes: 73 additions & 2 deletions crates/rpc/rpc-types/src/beacon/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,64 @@
//! See also <https://github.com/ethereum/consensus-specs/blob/master/specs/deneb/beacon-chain.md#executionpayload>

use crate::{
beacon::withdrawals::BeaconWithdrawal, engine::ExecutionPayloadV3, ExecutionPayload,
ExecutionPayloadV1, ExecutionPayloadV2, Withdrawal,
beacon::{withdrawals::BeaconWithdrawal, BlsPublicKey},
engine::ExecutionPayloadV3,
ExecutionPayload, ExecutionPayloadV1, ExecutionPayloadV2, Withdrawal,
};
use alloy_primitives::{Address, Bloom, Bytes, B256, U256};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{serde_as, DeserializeAs, DisplayFromStr, SerializeAs};
use std::borrow::Cow;

/// Response object of GET `/eth/v1/builder/header/{slot}/{parent_hash}/{pubkey}`
///
/// See also <https://ethereum.github.io/builder-specs/#/Builder/getHeader>
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GetExecutionPayloadHeaderResponse {
pub version: String,
pub data: ExecutionPayloadHeaderData,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExecutionPayloadHeaderData {
pub message: ExecutionPayloadHeaderMessage,
pub signature: String,
}

#[serde_as]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExecutionPayloadHeaderMessage {
pub header: ExecutionPayloadHeader,
#[serde_as(as = "DisplayFromStr")]
pub value: U256,
pub pubkey: BlsPublicKey,
}

/// The header of the execution payload.
#[serde_as]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExecutionPayloadHeader {
pub parent_hash: B256,
pub fee_recipient: Address,
pub state_root: B256,
pub receipts_root: B256,
pub logs_bloom: Bloom,
pub prev_randao: B256,
#[serde_as(as = "DisplayFromStr")]
pub block_number: String,
#[serde_as(as = "DisplayFromStr")]
pub gas_limit: u64,
#[serde_as(as = "DisplayFromStr")]
pub gas_used: u64,
#[serde_as(as = "DisplayFromStr")]
pub timestamp: u64,
pub extra_data: Bytes,
#[serde_as(as = "DisplayFromStr")]
pub base_fee_per_gas: U256,
pub block_hash: B256,
pub transactions_root: B256,
}

#[serde_as]
#[derive(Serialize, Deserialize)]
struct BeaconPayloadAttributes {
Expand Down Expand Up @@ -450,3 +500,24 @@ pub mod beacon_payload {
BeaconExecutionPayload::deserialize(deserializer).map(Into::into)
}
}

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

#[test]
fn serde_get_payload_header_response() {
let s = r#"{"version":"bellatrix","data":{"message":{"header":{"parent_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","fee_recipient":"0xabcf8e0d4e9587369b2301d0790347320302cc09","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","receipts_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","logs_bloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prev_randao":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","block_number":"1","gas_limit":"1","gas_used":"1","timestamp":"1","extra_data":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","base_fee_per_gas":"1","block_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","transactions_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"value":"1","pubkey":"0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}"#;
let resp: GetExecutionPayloadHeaderResponse = serde_json::from_str(s).unwrap();
let json: serde_json::Value = serde_json::from_str(s).unwrap();
assert_eq!(json, serde_json::to_value(resp).unwrap());
}

#[test]
fn serde_payload_header() {
let s = r#"{"parent_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","fee_recipient":"0xabcf8e0d4e9587369b2301d0790347320302cc09","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","receipts_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","logs_bloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prev_randao":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","block_number":"1","gas_limit":"1","gas_used":"1","timestamp":"1","extra_data":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","base_fee_per_gas":"1","block_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","transactions_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}"#;
let header: ExecutionPayloadHeader = serde_json::from_str(s).unwrap();
let json: serde_json::Value = serde_json::from_str(s).unwrap();
assert_eq!(json, serde_json::to_value(header).unwrap());
}
}