Offloading KZG Proof Computation from the beacon node (#7117)

Addresses #7108

- Add EL integration for `getPayloadV5` and `getBlobsV2`
- Offload proof computation and use proofs from EL RPC APIs
This commit is contained in:
Jimmy Chen
2025-04-08 17:37:16 +10:00
committed by GitHub
parent e924264e17
commit 759b0612b3
31 changed files with 721 additions and 476 deletions

View File

@@ -20,13 +20,14 @@ use tree_hash_derive::TreeHash;
use types::{
Blob, ChainSpec, EthSpec, ExecutionBlockHash, ExecutionPayload, ExecutionPayloadBellatrix,
ExecutionPayloadCapella, ExecutionPayloadDeneb, ExecutionPayloadElectra, ExecutionPayloadFulu,
ExecutionPayloadHeader, FixedBytesExtended, ForkName, Hash256, Transaction, Transactions,
Uint256,
ExecutionPayloadHeader, FixedBytesExtended, ForkName, Hash256, KzgProofs, Transaction,
Transactions, Uint256,
};
use super::DEFAULT_TERMINAL_BLOCK;
const TEST_BLOB_BUNDLE: &[u8] = include_bytes!("fixtures/mainnet/test_blobs_bundle.ssz");
const TEST_BLOB_BUNDLE_V2: &[u8] = include_bytes!("fixtures/mainnet/test_blobs_bundle_v2.ssz");
pub const DEFAULT_GAS_LIMIT: u64 = 30_000_000;
const GAS_USED: u64 = DEFAULT_GAS_LIMIT - 1;
@@ -697,15 +698,13 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
},
};
if execution_payload.fork_name().deneb_enabled() {
let fork_name = execution_payload.fork_name();
if fork_name.deneb_enabled() {
// get random number between 0 and Max Blobs
let mut rng = self.rng.lock();
let max_blobs = self
.spec
.max_blobs_per_block_by_fork(execution_payload.fork_name())
as usize;
let max_blobs = self.spec.max_blobs_per_block_by_fork(fork_name) as usize;
let num_blobs = rng.gen::<usize>() % (max_blobs + 1);
let (bundle, transactions) = generate_blobs(num_blobs)?;
let (bundle, transactions) = generate_blobs(num_blobs, fork_name)?;
for tx in Vec::from(transactions) {
execution_payload
.transactions_mut()
@@ -721,7 +720,8 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
}
}
pub fn load_test_blobs_bundle<E: EthSpec>() -> Result<(KzgCommitment, KzgProof, Blob<E>), String> {
pub fn load_test_blobs_bundle_v1<E: EthSpec>() -> Result<(KzgCommitment, KzgProof, Blob<E>), String>
{
let BlobsBundle::<E> {
commitments,
proofs,
@@ -745,32 +745,56 @@ pub fn load_test_blobs_bundle<E: EthSpec>() -> Result<(KzgCommitment, KzgProof,
))
}
pub fn load_test_blobs_bundle_v2<E: EthSpec>(
) -> Result<(KzgCommitment, KzgProofs<E>, Blob<E>), String> {
let BlobsBundle::<E> {
commitments,
proofs,
blobs,
} = BlobsBundle::from_ssz_bytes(TEST_BLOB_BUNDLE_V2)
.map_err(|e| format!("Unable to decode ssz: {:?}", e))?;
Ok((
commitments
.first()
.cloned()
.ok_or("commitment missing in test bundle")?,
// there's only one blob in the test bundle, hence we take all the cell proofs here.
proofs,
blobs
.first()
.cloned()
.ok_or("blob missing in test bundle")?,
))
}
pub fn generate_blobs<E: EthSpec>(
n_blobs: usize,
fork_name: ForkName,
) -> Result<(BlobsBundle<E>, Transactions<E>), String> {
let (kzg_commitment, kzg_proof, blob) = load_test_blobs_bundle::<E>()?;
let tx = static_valid_tx::<E>()
.map_err(|e| format!("error creating valid tx SSZ bytes: {:?}", e))?;
let transactions = vec![tx; n_blobs];
let mut bundle = BlobsBundle::<E>::default();
let mut transactions = vec![];
for blob_index in 0..n_blobs {
let tx = static_valid_tx::<E>()
.map_err(|e| format!("error creating valid tx SSZ bytes: {:?}", e))?;
transactions.push(tx);
bundle
.blobs
.push(blob.clone())
.map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?;
bundle
.commitments
.push(kzg_commitment)
.map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?;
bundle
.proofs
.push(kzg_proof)
.map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?;
}
let bundle = if fork_name.fulu_enabled() {
let (kzg_commitment, kzg_proofs, blob) = load_test_blobs_bundle_v2::<E>()?;
BlobsBundle {
commitments: vec![kzg_commitment; n_blobs].into(),
proofs: vec![kzg_proofs.to_vec(); n_blobs]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.into(),
blobs: vec![blob; n_blobs].into(),
}
} else {
let (kzg_commitment, kzg_proof, blob) = load_test_blobs_bundle_v1::<E>()?;
BlobsBundle {
commitments: vec![kzg_commitment; n_blobs].into(),
proofs: vec![kzg_proof; n_blobs].into(),
blobs: vec![blob; n_blobs].into(),
}
};
Ok((bundle, transactions.into()))
}
@@ -905,7 +929,7 @@ pub fn generate_pow_block(
#[cfg(test)]
mod test {
use super::*;
use kzg::{trusted_setup::get_trusted_setup, TrustedSetup};
use kzg::{trusted_setup::get_trusted_setup, Bytes48, CellRef, KzgBlobRef, TrustedSetup};
use types::{MainnetEthSpec, MinimalEthSpec};
#[test]
@@ -974,20 +998,28 @@ mod test {
}
#[test]
fn valid_test_blobs() {
fn valid_test_blobs_bundle_v1() {
assert!(
validate_blob::<MainnetEthSpec>().is_ok(),
validate_blob_bundle_v1::<MainnetEthSpec>().is_ok(),
"Mainnet preset test blobs bundle should contain valid proofs"
);
assert!(
validate_blob::<MinimalEthSpec>().is_ok(),
validate_blob_bundle_v1::<MinimalEthSpec>().is_ok(),
"Minimal preset test blobs bundle should contain valid proofs"
);
}
fn validate_blob<E: EthSpec>() -> Result<(), String> {
#[test]
fn valid_test_blobs_bundle_v2() {
validate_blob_bundle_v2::<MainnetEthSpec>()
.expect("Mainnet preset test blobs bundle v2 should contain valid proofs");
validate_blob_bundle_v2::<MinimalEthSpec>()
.expect("Minimal preset test blobs bundle v2 should contain valid proofs");
}
fn validate_blob_bundle_v1<E: EthSpec>() -> Result<(), String> {
let kzg = load_kzg()?;
let (kzg_commitment, kzg_proof, blob) = load_test_blobs_bundle::<E>()?;
let (kzg_commitment, kzg_proof, blob) = load_test_blobs_bundle_v1::<E>()?;
let kzg_blob = kzg::Blob::from_bytes(blob.as_ref())
.map(Box::new)
.map_err(|e| format!("Error converting blob to kzg blob: {e:?}"))?;
@@ -995,6 +1027,26 @@ mod test {
.map_err(|e| format!("Invalid blobs bundle: {e:?}"))
}
fn validate_blob_bundle_v2<E: EthSpec>() -> Result<(), String> {
let kzg = load_kzg()?;
let (kzg_commitments, kzg_proofs, cells) =
load_test_blobs_bundle_v2::<E>().map(|(commitment, proofs, blob)| {
let kzg_blob: KzgBlobRef = blob.as_ref().try_into().unwrap();
(
vec![Bytes48::from(commitment); proofs.len()],
proofs.into_iter().map(|p| p.into()).collect::<Vec<_>>(),
kzg.compute_cells(kzg_blob).unwrap(),
)
})?;
let (cell_indices, cell_refs): (Vec<u64>, Vec<CellRef>) = cells
.iter()
.enumerate()
.map(|(cell_idx, cell)| (cell_idx as u64, CellRef::try_from(cell.as_ref()).unwrap()))
.unzip();
kzg.verify_cell_proof_batch(&cell_refs, &kzg_proofs, cell_indices, &kzg_commitments)
.map_err(|e| format!("Invalid blobs bundle: {e:?}"))
}
fn load_kzg() -> Result<Kzg, String> {
let trusted_setup: TrustedSetup =
serde_json::from_reader(get_trusted_setup().as_slice())

View File

@@ -383,9 +383,8 @@ pub async fn handle_rpc<E: EthSpec>(
== ForkName::Fulu
&& (method == ENGINE_GET_PAYLOAD_V1
|| method == ENGINE_GET_PAYLOAD_V2
|| method == ENGINE_GET_PAYLOAD_V3)
// TODO(fulu): Uncomment this once v5 method is ready for Fulu
// || method == ENGINE_GET_PAYLOAD_V4)
|| method == ENGINE_GET_PAYLOAD_V3
|| method == ENGINE_GET_PAYLOAD_V4)
{
return Err((
format!("{} called after Fulu fork!", method),
@@ -451,22 +450,6 @@ pub async fn handle_rpc<E: EthSpec>(
})
.unwrap()
}
// TODO(fulu): remove this once we switch to v5 method
JsonExecutionPayload::V5(execution_payload) => {
serde_json::to_value(JsonGetPayloadResponseV5 {
execution_payload,
block_value: Uint256::from(DEFAULT_MOCK_EL_PAYLOAD_VALUE_WEI),
blobs_bundle: maybe_blobs
.ok_or((
"No blobs returned despite V5 Payload".to_string(),
GENERIC_ERROR_CODE,
))?
.into(),
should_override_builder: false,
execution_requests: Default::default(),
})
.unwrap()
}
_ => unreachable!(),
}),
ENGINE_GET_PAYLOAD_V5 => Ok(match JsonExecutionPayload::from(response) {

View File

@@ -546,7 +546,7 @@ impl<E: EthSpec> MockBuilder<E> {
.map_err(|_| "incorrect payload variant".to_string())?
.into(),
blob_kzg_commitments: maybe_blobs_bundle
.map(|b| b.commitments)
.map(|b| b.commitments.clone())
.unwrap_or_default(),
value: self.get_bid_value(value),
pubkey: self.builder_sk.public_key().compress(),
@@ -558,7 +558,7 @@ impl<E: EthSpec> MockBuilder<E> {
.map_err(|_| "incorrect payload variant".to_string())?
.into(),
blob_kzg_commitments: maybe_blobs_bundle
.map(|b| b.commitments)
.map(|b| b.commitments.clone())
.unwrap_or_default(),
value: self.get_bid_value(value),
pubkey: self.builder_sk.public_key().compress(),
@@ -570,7 +570,7 @@ impl<E: EthSpec> MockBuilder<E> {
.map_err(|_| "incorrect payload variant".to_string())?
.into(),
blob_kzg_commitments: maybe_blobs_bundle
.map(|b| b.commitments)
.map(|b| b.commitments.clone())
.unwrap_or_default(),
value: self.get_bid_value(value),
pubkey: self.builder_sk.public_key().compress(),

View File

@@ -58,6 +58,7 @@ pub const DEFAULT_ENGINE_CAPABILITIES: EngineCapabilities = EngineCapabilities {
get_payload_v5: true,
get_client_version_v1: true,
get_blobs_v1: true,
get_blobs_v2: true,
};
pub static DEFAULT_CLIENT_VERSION: LazyLock<JsonClientVersionV1> =