Skip to content

Commit

Permalink
Merge branch 'feat/ssz_test' into online
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Oct 10, 2024
2 parents 1eca70a + a218e6f commit f02dd7a
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 7 deletions.
53 changes: 52 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
1 change: 1 addition & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sp1-zkvm = { version = "2.0.0", features = ["verify"] }
thiserror-no-std = { workspace = true }
url = { workspace = true, features = ["serde"] }
alloy-trie = { workspace = true }
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs.git", features = ["serde"] }

[target.'cfg(not(target_os = "zkvm"))'.dependencies]
alloy-rlp = { workspace = true }
Expand Down
27 changes: 27 additions & 0 deletions program/src/memorizer/cl_header/mod.rs
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;
}
}
93 changes: 93 additions & 0 deletions program/src/memorizer/cl_header/online.rs
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,
}
9 changes: 9 additions & 0 deletions program/src/memorizer/cl_header/zkvm.rs
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())
}
}
11 changes: 11 additions & 0 deletions program/src/memorizer/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub struct TransactionKey {
pub transaction_index: u64,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct BeaconHeaderKey {
pub slot: u64,
}

impl From<HeaderKey> for MemorizerKey {
fn from(value: HeaderKey) -> Self {
Self(*keccak256(bincode::serialize(&value).unwrap()))
Expand All @@ -54,3 +59,9 @@ impl From<TransactionKey> for MemorizerKey {
Self(*keccak256(bincode::serialize(&value).unwrap()))
}
}

impl From<BeaconHeaderKey> for MemorizerKey {
fn from(value: BeaconHeaderKey) -> Self {
Self(*keccak256(bincode::serialize(&value).unwrap()))
}
}
1 change: 1 addition & 0 deletions program/src/memorizer/mod.rs
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;
Expand Down
16 changes: 11 additions & 5 deletions program/src/memorizer/values.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
use alloy_consensus::serde_bincode_compat;
use crate::memorizer::cl_header::BeaconHeader;
use alloy_consensus::{Account, Header};
use alloy_primitives::{Bytes, B256, U256};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

#[serde_as]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct HeaderMemorizerValue {
#[serde_as(as = "serde_bincode_compat::Header")]
pub header: Header,
pub element_index: u128,
pub element_hash: B256,
pub rlp: String,
pub proof: Vec<B256>,
}

#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct AccountMemorizerValue {
pub account: Account,
pub proof: Vec<Bytes>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct BeaconHeaderMemorizerValue {
pub header: BeaconHeader,
pub proof: Vec<Bytes>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct StorageMemorizerValue {
pub key: B256,
pub value: U256,
pub proof: Vec<Bytes>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct TransactionMemorizerValue {
pub transaction_encoded: Bytes,
pub tx_index: u64,
Expand All @@ -41,4 +46,5 @@ pub enum MemorizerValue {
Account(AccountMemorizerValue),
Storage(StorageMemorizerValue),
Transaction(TransactionMemorizerValue),
BeaconHeader(BeaconHeaderMemorizerValue),
}
1 change: 0 additions & 1 deletion script/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ fn main() {

let manifest_dir: String = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
let path = Path::new(&manifest_dir).join("../memorizer.bin");
println!("Memorizer saved to {path:?}");
stdin.write(&bincode::deserialize::<Memorizer>(&fs::read(path).unwrap()).unwrap());

if args.execute {
Expand Down

0 comments on commit f02dd7a

Please sign in to comment.