-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add cast command to convert beacon payload to execution payload b2e-payload
#11629
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
Merged
mattsse
merged 22 commits into
foundry-rs:master
from
lean-apple:cast-convert-beacon-to-el-block
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ca2a51d
feat: add v1 cast commande b2epayload
lean-apple a9de85e
feat: add v1 cast commande b2epayload
lean-apple 14342de
chore: fmt
lean-apple 819199d
Merge branch 'foundry-rs:master' into master
lean-apple 65b924e
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple 920096b
chore: use pathbuf
lean-apple a5fa112
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple ade3eda
fix: restore long name
lean-apple 7a94011
chore: fmt
lean-apple 713be9c
Merge branch 'foundry-rs:master' into master
lean-apple bd462ec
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple f43b27b
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple eb11d6b
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple 86e5092
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple 9cd99e0
chore: update lock
lean-apple 6cf5318
refactor: use ex payload alloy helper
lean-apple 8507478
Merge branch 'master' into cast-convert-beacon-to-el-block
lean-apple 4185dd3
refactor: use raw input source str
lean-apple 3b89ca9
refactor: remove right now json rpc format output
lean-apple 35f5304
test: add test for malformated input
lean-apple 27b308c
chore: remove unused dep
lean-apple fc52cb8
chore: fmt
lean-apple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//! Command Line handler to convert Beacon block's execution payload to Execution format. | ||
|
||
use std::path::PathBuf; | ||
|
||
use alloy_rpc_types_beacon::payload::BeaconBlockData; | ||
use clap::{Parser, builder::ValueParser}; | ||
use eyre::{Result, eyre}; | ||
use foundry_common::{fs, sh_print}; | ||
|
||
/// CLI arguments for `cast b2e-payload`, convert Beacon block's execution payload to Execution | ||
/// format. | ||
#[derive(Parser)] | ||
pub struct B2EPayloadArgs { | ||
/// Input data, it can be either a file path to JSON file or raw JSON string containing the | ||
/// beacon block | ||
#[arg(value_name = "INPUT", value_parser=ValueParser::new(parse_input_source), help = "File path to JSON file or raw JSON string containing the beacon block")] | ||
pub input: InputSource, | ||
} | ||
|
||
impl B2EPayloadArgs { | ||
pub async fn run(self) -> Result<()> { | ||
let beacon_block_json = match self.input { | ||
InputSource::Json(json) => json, | ||
InputSource::File(path) => fs::read_to_string(&path) | ||
.map_err(|e| eyre!("Failed to read JSON file '{}': {}", path.display(), e))?, | ||
}; | ||
|
||
let beacon_block_data: BeaconBlockData = serde_json::from_str(&beacon_block_json) | ||
.map_err(|e| eyre!("Failed to parse beacon block JSON: {}", e))?; | ||
|
||
let execution_payload = beacon_block_data.execution_payload(); | ||
|
||
// Output raw execution payload | ||
let output = serde_json::to_string(&execution_payload) | ||
.map_err(|e| eyre!("Failed to serialize execution payload: {}", e))?; | ||
sh_print!("{}", output)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Represents the different input sources for beacon block data | ||
#[derive(Debug, Clone)] | ||
pub enum InputSource { | ||
/// Path to a JSON file containing beacon block data | ||
File(PathBuf), | ||
/// Raw JSON string containing beacon block data | ||
Json(String), | ||
} | ||
|
||
fn parse_input_source(s: &str) -> Result<InputSource, String> { | ||
// Try parsing as JSON first | ||
if serde_json::from_str::<serde_json::Value>(s).is_ok() { | ||
return Ok(InputSource::Json(s.to_string())); | ||
} | ||
|
||
// Otherwise treat as file path | ||
Ok(InputSource::File(PathBuf::from(s))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_input_source_json_object() { | ||
let json_input = r#"{"execution_payload": {"block_hash": "0x123"}}"#; | ||
let result = parse_input_source(json_input).unwrap(); | ||
|
||
match result { | ||
InputSource::Json(json) => assert_eq!(json, json_input), | ||
InputSource::File(_) => panic!("Expected JSON input, got File"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse_input_source_json_array() { | ||
let json_input = r#"[{"block": "data"}]"#; | ||
let result = parse_input_source(json_input).unwrap(); | ||
|
||
match result { | ||
InputSource::Json(json) => assert_eq!(json, json_input), | ||
InputSource::File(_) => panic!("Expected JSON input, got File"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse_input_source_file_path() { | ||
let file_path = | ||
"block-12225729-6ceadbf2a6adbbd64cbec33fdebbc582f25171cd30ac43f641cbe76ac7313ddf.json"; | ||
let result = parse_input_source(file_path).unwrap(); | ||
|
||
match result { | ||
InputSource::File(path) => assert_eq!(path, PathBuf::from(file_path)), | ||
InputSource::Json(_) => panic!("Expected File input, got JSON"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse_input_source_malformed_but_not_json() { | ||
let malformed = "not-json-{"; | ||
let result = parse_input_source(malformed).unwrap(); | ||
|
||
// Should be treated as file path since it's not valid JSON | ||
match result { | ||
InputSource::File(path) => assert_eq!(path, PathBuf::from(malformed)), | ||
InputSource::Json(_) => panic!("Expected File input, got File"), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.