Skip to content
Draft
Show file tree
Hide file tree
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
91 changes: 90 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ redb = "3.1.0"
reqwest = { version = "0.12.24", features = ["native-tls-vendored", "json"] }
rstest = "0.26.1"
rust-kzg-blst = { git = 'https://github.com/grandinetech/rust-kzg.git' }
rust_eth_kzg = { git = "https://github.com/crate-crypto/rust-eth-kzg.git", tag = "v0.5.4" }
serde = { version = '1.0', features = ['derive', "rc"] }
serde_json = "1.0.145"
serde_yaml = "0.9"
Expand Down
2 changes: 2 additions & 0 deletions crates/common/consensus/beacon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ alloy-primitives.workspace = true
alloy-rlp.workspace = true
anyhow.workspace = true
async-trait.workspace = true
discv5.workspace = true
ethereum_hashing.workspace = true
ethereum_serde_utils.workspace = true
ethereum_ssz.workspace = true
ethereum_ssz_derive.workspace = true
itertools.workspace = true
rust_eth_kzg.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
Expand Down
62 changes: 62 additions & 0 deletions crates/common/consensus/beacon/src/custody_group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use anyhow::{Ok, Result, anyhow};
use discv5::enr::NodeId;
use ream_consensus_misc::constants::beacon::NUM_CUSTODY_GROUPS;
use sha2::{Digest, Sha256};

use crate::data_column_sidecar::NUMBER_OF_COLUMNS;

pub fn get_custody_group_indices(node_id: NodeId, custody_group_count: u64) -> Result<Vec<u64>> {
if custody_group_count > NUM_CUSTODY_GROUPS {
return Err(anyhow!(
"Custody group count more than number of custody groups",
));
}

if custody_group_count == NUM_CUSTODY_GROUPS {
return Ok((0..NUM_CUSTODY_GROUPS).collect());
}

let mut custody_indices: Vec<u64> = Vec::new();
let mut current_id = node_id.raw();

while custody_indices.len() < custody_group_count as usize {
let hash = Sha256::digest(current_id);

let mut array = [0u8; 8];
array.copy_from_slice(&hash[0..8]);
let index = u64::from_le_bytes(array) % NUM_CUSTODY_GROUPS;

if !custody_indices.contains(&index) {
custody_indices.push(index);
}

let mut carry = true;
for byte in current_id.iter_mut().rev() {
if carry {
let (new_byte, overflow) = byte.overflowing_add(1);
*byte = new_byte;
carry = overflow;
}
}
}

Ok(custody_indices)
}

pub fn compute_columns_for_custody_group(custody_group_index: u64) -> Result<Vec<u64>> {
if custody_group_index >= NUM_CUSTODY_GROUPS {
return Err(anyhow!(
"Custody group index is greater than total custody groups"
));
}

let columns_per_group = NUMBER_OF_COLUMNS.saturating_div(NUM_CUSTODY_GROUPS);

let mut column_indices: Vec<u64> = Vec::new();
for i in 0..columns_per_group {
column_indices
.push((NUM_CUSTODY_GROUPS.saturating_mul(i)).saturating_add(custody_group_index));
}

Ok(column_indices)
}
51 changes: 50 additions & 1 deletion crates/common/consensus/beacon/src/data_column_sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ream_consensus_misc::{
use ream_merkle::is_valid_merkle_branch;
use ream_network_spec::networks::beacon_network_spec;
use serde::{Deserialize, Serialize};
use ssz::DecodeError;
use ssz_derive::{Decode, Encode};
use ssz_types::{FixedVector, VariableList, typenum};
use tree_hash::TreeHash;
Expand All @@ -19,7 +20,7 @@ pub const DATA_COLUMN_SIDECAR_SUBNET_COUNT: u64 = 128;

pub type MaxBlobCommitmentsPerBlock = typenum::U6;

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Encode, Decode, TreeHash)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Decode, Encode, TreeHash)]
pub struct DataColumnSidecar {
pub index: u64,
pub column: VariableList<Cell, MaxBlobCommitmentsPerBlock>,
Expand Down Expand Up @@ -102,6 +103,54 @@ impl DataColumnSidecar {
}
}

pub fn get_column_data_sidecars(
signed_block_header: SignedBeaconBlockHeader,
kzg_commitments: VariableList<KZGCommitment, MaxBlobCommitmentsPerBlock>,
kzg_commitments_inclusion_proof: FixedVector<B256, typenum::U4>,
cells_and_kzg_proofs: Vec<(Vec<Cell>, Vec<KZGProof>)>,
) -> Result<Vec<DataColumnSidecar>, DecodeError> {
if cells_and_kzg_proofs.len() != kzg_commitments.len() {
return Err(DecodeError::InvalidByteLength {
len: kzg_commitments.len(),
expected: cells_and_kzg_proofs.len(),
});
}

let mut sidecars: Vec<DataColumnSidecar> = Vec::new();
for column_index in 0..NUMBER_OF_COLUMNS {
let mut column_cells: Vec<Cell> = Vec::new();
let mut column_proofs: Vec<KZGProof> = Vec::new();
for (cells, proofs) in &cells_and_kzg_proofs {
if column_index as usize >= cells.len() || column_index as usize >= proofs.len() {
return Err(DecodeError::OffsetOutOfBounds(column_index as usize));
}
column_cells.push(cells[column_index as usize].clone());
column_proofs.push(proofs[column_index as usize]);
}
let column_vl = VariableList::try_from(column_cells).map_err(|err| {
DecodeError::BytesInvalid(format!(
"Creating column VariableList for index {column_index} failed: {err}",
))
})?;

let proofs_vl = VariableList::try_from(column_proofs).map_err(|err| {
DecodeError::BytesInvalid(format!(
"Creating proofs VariableList for index {column_index} failed: {err}",
))
})?;

sidecars.push(DataColumnSidecar {
index: column_index,
column: column_vl,
kzg_commitments: kzg_commitments.clone(),
kzg_proofs: proofs_vl,
signed_block_header: signed_block_header.clone(),
kzg_commitments_inclusion_proof: kzg_commitments_inclusion_proof.clone(),
});
}
Ok(sidecars)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions crates/common/consensus/beacon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ pub mod attester_slashing;
pub mod beacon_committee_selection;
pub mod blob_sidecar;
pub mod bls_to_execution_change;
pub mod custody_group;
pub mod data_column_sidecar;
pub mod electra;
pub mod eth_1_block;
pub mod fork_choice;
pub mod genesis;
pub mod helpers;
pub mod historical_summary;
pub mod matrix_entry;
pub mod pending_consolidation;
pub mod pending_deposit;
pub mod pending_partial_withdrawal;
Expand Down
Loading