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

Partial state update with ssz rs #3

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ opt-level = 3
[profile.release]
debug = 1
lto = true

[workspace.dependencies]
alloy-primitives = "0.8"
ethereum_ssz = "0.8"
ream-consensus = { git = "https://github.com/ReamLabs/ream.git", package = "ream-consensus" }
ream-bls = { git = "https://github.com/ReamLabs/ream.git", package = "ream-bls" }
serde = { version = '1.0', features = ['derive', "rc"] }
snap = "1.1"
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs.git", package = "ssz_rs"}
ssz_rs_derive = { git = "https://github.com/ralexstokes/ssz-rs.git", package = "ssz_rs_derive"}
tree_hash = "0.9"
typenum = "1.12.0"
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.DEFAULT_GOAL := help

# Cargo features for builds.
FEATURES ?=

# Cargo profile for builds.
PROFILE ?= release

# Extra flags for Cargo.
CARGO_INSTALL_EXTRA_FLAGS ?=

##@ Help
.PHONY: help
help: # Display this help.
@awk 'BEGIN {FS = ":.*#"; printf "Usage:\n make \033[34m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?#/ { printf " \033[34m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) }' $(MAKEFILE_LIST)

##@ Build
.PHONY: build
build: # Build the Ream binary into `target` directory.
cargo build --bin ream --features "$(FEATURES)" --profile "$(PROFILE)"

##@ Fmt
.PHONY: fmt
fmt:
cargo fmt
(cd methods/guest && cargo fmt)

##@ Dev
.PHONY: dev
dev: # Run in dev mode
RUST_LOG="[executor]=info" RISC0_DEV_MODE=1 RUST_BACKTRACE=0 cargo run --release

##@ Prove
.PHONY: prove
prod: # Run with real proof generation
RUST_LOG="[executor]=info" RISC0_DEV_MODE=0 RUST_BACKTRACE=0 cargo run --release
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ To build all methods and execute the method within the zkVM, run the following
command:

```bash
cargo run -- --slot-number <SLOT_NUMBER>
cargo run -- --local
```

This is an empty template, and so there is no expected output (until you modify
Expand All @@ -61,15 +61,15 @@ During development, faster iteration upon code changes can be achieved by levera
Put together, the command to run your project in development mode while getting execution statistics is:

```bash
RUST_LOG="[executor]=info" RISC0_DEV_MODE=1 cargo run -- --slot-number <SLOT_NUMBER>
RUST_LOG="[executor]=info" RISC0_DEV_MODE=1 cargo run -- --local
```

### Real Proof Generation

Once you've reached a point where you're ready to generate real proofs, you can do so by setting RISC0_DEV_MODE=0. Generating proofs locally would be achieved by running the following:

```bash
RISC0_DEV_MODE=0 cargo run --release -- --slot-number <SLOT_NUMBER>
RISC0_DEV_MODE=0 cargo run --release -- --local
```

### Executor Statistics
Expand All @@ -79,7 +79,7 @@ To gain insights into your application's performance, you can obtain executor st
Setting this filter will print statistics about the execution before proof generation, so you can understand how computationally expensive your application is. Since the statistics concern only the executor phase, it is recommended to run your application in dev-mode to avoid the overhead of proof generation:

```bash
RISC0_DEV_MODE=1 RUST_LOG=info cargo run --release -- --slot-number <SLOT_NUMBER>
RISC0_DEV_MODE=1 RUST_LOG=info cargo run --release -- --local
```

The statistics include:
Expand Down
16 changes: 16 additions & 0 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "consensus"
version = "0.1.0"
edition = "2021"

[dependencies]
alloy-primitives.workspace = true
ethereum_ssz.workspace = true
ream-consensus.workspace = true
ream-bls.workspace = true
tree_hash.workspace = true
anyhow = "1.0"
ssz_rs.workspace = true
ssz_rs_derive.workspace = true
typenum.workspace = true
serde.workspace = true
27 changes: 27 additions & 0 deletions consensus/src/beacon_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use alloy_primitives::FixedBytes;
use ssz_rs::prelude::*;

use ream_consensus::deneb::beacon_block::BeaconBlock as ReamBeaconBlock;

use crate::beacon_block_body::BeaconBlockBody;

#[derive(Debug, SimpleSerialize, serde::Serialize, serde::Deserialize)]
pub struct BeaconBlock {
pub slot: u64,
pub proposer_index: u64,
pub parent_root: FixedBytes<32>,
pub state_root: FixedBytes<32>,
pub body: BeaconBlockBody,
}

impl From<ReamBeaconBlock> for BeaconBlock {
fn from(block: ReamBeaconBlock) -> Self {
BeaconBlock {
slot: block.slot,
proposer_index: block.proposer_index,
parent_root: block.parent_root.as_slice().try_into().unwrap(),
state_root: block.state_root.as_slice().try_into().unwrap(),
body: block.body.into(),
}
}
}
41 changes: 41 additions & 0 deletions consensus/src/beacon_block_body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use ssz_rs::prelude::*;

use ream_consensus::deneb::beacon_block_body::BeaconBlockBody as ReamBeaconBlockBody;

use crate::bls::BLSSignature;
use crate::eth_1_data::Eth1Data;

#[derive(Debug, SimpleSerialize, serde::Serialize, serde::Deserialize)]
pub struct BeaconBlockBody {
pub randao_reveal: BLSSignature,
pub eth1_data: Eth1Data,
pub graffiti: Vector<u8, 32>,
// pub proposer_slashings: VariableList<ProposerSlashing, U16>,
// pub attester_slashings: VariableList<AttesterSlashing, U2>,
// pub attestations: VariableList<Attestation, U128>,
// pub deposits: VariableList<Deposit, U16>,
// pub voluntary_exits: VariableList<SignedVoluntaryExit, U16>,
// pub sync_aggregate: SyncAggregate,
// pub execution_payload: ExecutionPayload,
// pub bls_to_execution_changes: VariableList<SignedBLSToExecutionChange, U16>,
// pub blob_kzg_commitments: VariableList<KZGCommitment, U4096>,
}

impl From<ReamBeaconBlockBody> for BeaconBlockBody {
fn from(body: ReamBeaconBlockBody) -> Self {
BeaconBlockBody {
randao_reveal: body.randao_reveal.into(),
eth1_data: body.eth1_data.into(),
graffiti: body.graffiti.as_slice().try_into().unwrap(),
// pub proposer_slashings: VariableList<ProposerSlashing, U16>,
// pub attester_slashings: VariableList<AttesterSlashing, U2>,
// pub attestations: VariableList<Attestation, U128>,
// pub deposits: VariableList<Deposit, U16>,
// pub voluntary_exits: VariableList<SignedVoluntaryExit, U16>,
// pub sync_aggregate: SyncAggregate,
// pub execution_payload: ExecutionPayload,
// pub bls_to_execution_changes: VariableList<SignedBLSToExecutionChange, U16>,
// pub blob_kzg_commitments: VariableList<KZGCommitment, U4096>,
}
}
}
25 changes: 25 additions & 0 deletions consensus/src/beacon_block_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use alloy_primitives::FixedBytes;
use ssz_rs::prelude::*;

use ream_consensus::beacon_block_header::BeaconBlockHeader as ReamBeaconBlockHeader;

#[derive(Debug, SimpleSerialize, serde::Serialize, serde::Deserialize)]
pub struct BeaconBlockHeader {
pub slot: u64,
pub proposer_index: u64,
pub parent_root: FixedBytes<32>,
pub state_root: FixedBytes<32>,
pub body_root: FixedBytes<32>,
}

impl From<ReamBeaconBlockHeader> for BeaconBlockHeader {
fn from(header: ReamBeaconBlockHeader) -> Self {
BeaconBlockHeader {
slot: header.slot,
proposer_index: header.proposer_index,
parent_root: header.parent_root.as_slice().try_into().unwrap(),
state_root: header.state_root.as_slice().try_into().unwrap(),
body_root: header.body_root.as_slice().try_into().unwrap(),
}
}
}
189 changes: 189 additions & 0 deletions consensus/src/beacon_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
use alloy_primitives::FixedBytes;
use anyhow::ensure;
use ssz_rs::prelude::*;

use ream_consensus::deneb::beacon_state::BeaconState as ReamBeaconState;

use crate::fork::Fork;
use crate::beacon_block_header::BeaconBlockHeader;
use crate::beacon_block::BeaconBlock;
use crate::eth_1_data::Eth1Data;
use crate::validator::Validator;

#[derive(Debug, SimpleSerialize)]
pub struct BeaconState {
// Versioning
pub genesis_time: u64,
pub genesis_validators_root: Vector<u8, 32>,
pub slot: u64,
pub fork: Fork,

// // History
pub latest_block_header: BeaconBlockHeader,
// pub block_roots: FixedVector<B256, U8192>,
// pub state_roots: FixedVector<B256, U8192>,
// /// Frozen in Capella, replaced by historical_summaries
// pub historical_roots: VariableList<B256, U16777216>,

// // Eth1
pub eth1_data: Eth1Data,
// pub eth1_data_votes: VariableList<Eth1Data, U2048>,
// pub eth1_deposit_index: u64,

// // Registry
pub validators: List<Validator, 10000000>,
// pub balances: VariableList<u64, U1099511627776>,

// // Randomness
// pub randao_mixes: FixedVector<B256, U65536>,

// // Slashings
// pub slashings: FixedVector<u64, U8192>,

// // Participation
// pub previous_epoch_participation: VariableList<u8, U1099511627776>,
// pub current_epoch_participation: VariableList<u8, U1099511627776>,

// // Finality
// pub justification_bits: BitVector<U4>,
// pub previous_justified_checkpoint: Checkpoint,
// pub current_justified_checkpoint: Checkpoint,
// pub finalized_checkpoint: Checkpoint,

// // Inactivity
// pub inactivity_scores: VariableList<u64, U1099511627776>,

// // Sync
// pub current_sync_committee: Arc<SyncCommittee>,
// pub next_sync_committee: Arc<SyncCommittee>,

// // Execution
// pub latest_execution_payload_header: ExecutionPayloadHeader,

// // Withdrawals
// pub next_withdrawal_index: u64,
// pub next_withdrawal_validator_index: u64,

// // Deep history valid from Capella onwards.
// pub historical_summaries: VariableList<HistoricalSummary, U16777216>,
}

impl From<ReamBeaconState> for BeaconState {
fn from(state: ReamBeaconState) -> Self {
let mut validators = List::<Validator, 10000000>::default();

for v in state.validators.iter() {
validators.push(v.clone().into());
}


BeaconState {
// Versioning
genesis_time: state.genesis_time,
genesis_validators_root: state.genesis_validators_root.as_slice().try_into().unwrap(),
slot: state.slot,
fork: state.fork.into(),

// // History
latest_block_header: state.latest_block_header.into(),
// pub block_roots: FixedVector<B256, U8192>,
// pub state_roots: FixedVector<B256, U8192>,
// /// Frozen in Capella, replaced by historical_summaries
// pub historical_roots: VariableList<B256, U16777216>,

// // Eth1
eth1_data: state.eth1_data.into(),
// pub eth1_data_votes: VariableList<Eth1Data, U2048>,
// eth1_deposit_index: state.eth1_deposit_index,

// // Registry
validators: validators,
// pub balances: VariableList<u64, U1099511627776>,

// // Randomness
// pub randao_mixes: FixedVector<B256, U65536>,

// // Slashings
// pub slashings: FixedVector<u64, U8192>,

// // Participation
// pub previous_epoch_participation: VariableList<u8, U1099511627776>,
// pub current_epoch_participation: VariableList<u8, U1099511627776>,

// // Finality
// pub justification_bits: BitVector<U4>,
// pub previous_justified_checkpoint: Checkpoint,
// pub current_justified_checkpoint: Checkpoint,
// pub finalized_checkpoint: Checkpoint,

// // Inactivity
// pub inactivity_scores: VariableList<u64, U1099511627776>,

// // Sync
// pub current_sync_committee: Arc<SyncCommittee>,
// pub next_sync_committee: Arc<SyncCommittee>,

// // Execution
// pub latest_execution_payload_header: ExecutionPayloadHeader,

// // Withdrawals
// next_withdrawal_index: state.next_withdrawal_index,
// next_withdrawal_validator_index: state.next_withdrawal_validator_index,

// // Deep history valid from Capella onwards.
// pub historical_summaries: VariableList<HistoricalSummary, U16777216>,
}
}
}

pub fn process_block_header(slot: u64, latest_block_header: &BeaconBlockHeader, proposer_index: u64, block: &BeaconBlock) -> anyhow::Result<BeaconBlockHeader> {
// Authenticates the arguments against the provided root


// Verify that the slots match
ensure!(
slot == block.slot,
"State slot must be equal to block slot"
);
// Verify that the block is newer than latest block header
ensure!(
block.slot > latest_block_header.slot,
"Block slot must be greater than latest block header slot of state"
);
// Verify that proposer index is the correct index
ensure!(
block.proposer_index == proposer_index,
"Block proposer index must be equal to beacon proposer index"
);
// Verify that the parent matches
ensure!(
block.parent_root == latest_block_header.hash_tree_root()?,
"Block Parent Root must be equal root of latest block header"
);

// Cache current block as the new latest block
let new_latest_block_header = BeaconBlockHeader {
slot: block.slot,
proposer_index: block.proposer_index,
parent_root: block.parent_root,
state_root: FixedBytes::<32>::ZERO, // Overwritten in the next process_slot call
body_root: block.body.hash_tree_root()?,
};

// // Verify proposer is not slashed
// let proposer_slashed = Self::not_slashed(block.proposer_index as usize, "validators_merkle_root", "merkle_path");
// ensure!(!proposer_slashed, "Block proposer must not be slashed");

Ok(new_latest_block_header)
}

// fn not_slashed(proposer_index: usize, block_state_root: &Vector<u8, 32>, merkle_path: &str) -> bool {
// true
// }

impl BeaconState {
// Mock function for consenzero PoC
pub fn get_beacon_proposer_index(&self) -> anyhow::Result<u64> {
Ok(42)
}
}
Loading