-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/ssz_test' into online
- Loading branch information
Showing
10 changed files
with
205 additions
and
7 deletions.
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.
Binary file not shown.
This file contains 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 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,27 @@ | ||
use super::MemorizerError; | ||
use crate::memorizer::keys::BeaconHeaderKey; | ||
use cfg_if::cfg_if; | ||
use ssz_rs::prelude::*; | ||
|
||
#[derive( | ||
PartialEq, Eq, Debug, Default, SimpleSerialize, serde::Serialize, serde::Deserialize, Clone, | ||
)] | ||
pub struct BeaconHeader { | ||
pub slot: u64, | ||
pub proposer_index: u64, | ||
pub parent_root: Vector<u8, 32>, | ||
pub state_root: Vector<u8, 32>, | ||
pub body_root: Vector<u8, 32>, | ||
} | ||
|
||
pub trait ClHeaderMemorizer { | ||
fn get_cl_header(&mut self, key: BeaconHeaderKey) -> Result<BeaconHeader, MemorizerError>; | ||
} | ||
|
||
cfg_if! { | ||
if #[cfg(target_os = "zkvm")] { | ||
mod zkvm; | ||
} else { | ||
mod online; | ||
} | ||
} |
This file contains 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,93 @@ | ||
use super::BeaconHeader; | ||
use super::ClHeaderMemorizer; | ||
use crate::memorizer::values::BeaconHeaderMemorizerValue; | ||
use crate::memorizer::values::MemorizerValue; | ||
use crate::memorizer::MemorizerError; | ||
use crate::memorizer::{keys::BeaconHeaderKey, Memorizer}; | ||
use alloy_primitives::hex; | ||
use ssz_rs::Vector; | ||
use tokio::runtime::Runtime; | ||
|
||
use reqwest::Client; | ||
use serde::Deserialize; | ||
|
||
impl ClHeaderMemorizer for Memorizer { | ||
fn get_cl_header(&mut self, key: BeaconHeaderKey) -> Result<BeaconHeader, MemorizerError> { | ||
let rt = Runtime::new().unwrap(); | ||
let header: BeaconHeader = rt.block_on(async { | ||
let client = Client::new(); | ||
|
||
println!("slot: {:?}", key.slot); | ||
let url = format!( | ||
"{}/eth/v1/beacon/headers?slot={}", | ||
self.rpc_url.clone().unwrap(), | ||
key.slot | ||
); | ||
|
||
println!("url: {:?}", url); | ||
|
||
// Sending GET request to the specified URL | ||
let response = client | ||
.get(&url) | ||
.header("accept", "application/json") | ||
.send() | ||
.await | ||
.expect("Failed to send request") | ||
.json::<BeaconHeaderApiResponse>() | ||
.await | ||
.expect("Failed to parse response"); | ||
|
||
// Extracting the first header in the `data` array | ||
let header_data = &response.data[0].header.message; | ||
|
||
let parent_root = | ||
Vector::<u8, 32>::try_from(hex::decode(&header_data.parent_root).unwrap()).unwrap(); | ||
let state_root = | ||
Vector::<u8, 32>::try_from(hex::decode(&header_data.state_root).unwrap()).unwrap(); | ||
let body_root = | ||
Vector::<u8, 32>::try_from(hex::decode(&header_data.body_root).unwrap()).unwrap(); | ||
// Converting response data to BeaconHeader struct | ||
BeaconHeader { | ||
slot: header_data.slot.parse().unwrap(), | ||
proposer_index: header_data.proposer_index.parse().unwrap(), | ||
parent_root, | ||
state_root, | ||
body_root, | ||
} | ||
}); | ||
|
||
self.map.insert( | ||
key.into(), | ||
MemorizerValue::BeaconHeader(BeaconHeaderMemorizerValue { | ||
header: header.clone(), | ||
proof: vec![], | ||
}), | ||
); | ||
|
||
Ok(header) | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct BeaconHeaderApiResponse { | ||
data: Vec<BeaconHeaderData>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct BeaconHeaderData { | ||
header: BeaconHeaderDetail, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct BeaconHeaderDetail { | ||
message: BeaconHeaderMessage, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct BeaconHeaderMessage { | ||
slot: String, | ||
proposer_index: String, | ||
parent_root: String, | ||
state_root: String, | ||
body_root: String, | ||
} |
This file contains 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,9 @@ | ||
use super::{BeaconHeader, ClHeaderMemorizer}; | ||
use crate::memorizer::{keys::BeaconHeaderKey, Memorizer, MemorizerError}; | ||
|
||
impl ClHeaderMemorizer for Memorizer { | ||
fn get_cl_header(&mut self, key: BeaconHeaderKey) -> Result<BeaconHeader, MemorizerError> { | ||
println!("zkvm run"); | ||
Ok(BeaconHeader::default()) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod account; | ||
pub mod cl_header; | ||
pub mod header; | ||
pub mod keys; | ||
pub mod storage; | ||
|
This file contains 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 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