Skip to content

Commit

Permalink
Merge pull request #132 from chainbound/feat/blobs-bundle-in-local-pa…
Browse files Browse the repository at this point in the history
…yload

feat(sidecar): populate blobs bundle when self-building
  • Loading branch information
merklefruit authored Jul 16, 2024
2 parents c24ebe2 + 58dc11a commit 87cbfa5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async fn main() -> eyre::Result<()> {
}


if let Err(e) = local_builder.build_new_local_payload(template.as_signed_transactions()).await {
if let Err(e) = local_builder.build_new_local_payload(&template).await {
tracing::error!(err = ?e, "CRITICAL: Error while building local payload at slot deadline for {slot}");
};
},
Expand Down
9 changes: 5 additions & 4 deletions bolt-sidecar/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ethereum_consensus::{
ssz::prelude::{List, MerkleizationError},
};
use payload_builder::FallbackPayloadBuilder;
use reth_primitives::TransactionSigned;
use signature::sign_builder_message;

use crate::{
Expand Down Expand Up @@ -99,8 +98,11 @@ impl LocalBuilder {
///
pub async fn build_new_local_payload(
&mut self,
transactions: Vec<TransactionSigned>,
template: &BlockTemplate,
) -> Result<(), BuilderError> {
let transactions = template.as_signed_transactions();
let blobs_bundle = template.as_blobs_bundle();

// 1. build a fallback payload with the given transactions, on top of
// the current head of the chain
let sealed_block = self
Expand All @@ -119,8 +121,7 @@ impl LocalBuilder {
let eth_payload = compat::to_consensus_execution_payload(&sealed_block);
let payload_and_blobs = PayloadAndBlobs {
execution_payload: eth_payload,
// TODO: add included blobs here
blobs_bundle: Default::default(),
blobs_bundle,
};

// 2. create a signed builder bid with the sealed block header we just created
Expand Down
44 changes: 43 additions & 1 deletion bolt-sidecar/src/builder/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
use std::collections::HashMap;

use alloy_primitives::{Address, U256};
use ethereum_consensus::{
crypto::{KzgCommitment, KzgProof},
deneb::mainnet::{Blob, BlobsBundle},
};
use reth_primitives::{PooledTransactionsElement, TransactionSigned};

use crate::{
common::max_transaction_cost,
primitives::{constraint::Constraint, AccountState, SignedConstraints},
primitives::{constraint::Constraint, AccountState, SignedConstraints, TransactionExt},
};

/// A block template that serves as a fallback block, but is also used
Expand Down Expand Up @@ -61,6 +65,44 @@ impl BlockTemplate {
.collect()
}

/// Converts the list of signed constraints into a list of all blobs in all transactions
/// in the constraints. Use this when building a local execution payload.
#[inline]
pub fn as_blobs_bundle(&self) -> BlobsBundle {
let (commitments, proofs, blobs) = self
.signed_constraints_list
.iter()
.flat_map(|sc| sc.message.constraints.iter())
.filter_map(|c| c.transaction.blob_sidecar())
.fold(
(Vec::new(), Vec::new(), Vec::new()),
|(mut commitments, mut proofs, mut blobs), bs| {
commitments.extend(
bs.commitments
.iter()
.map(|c| KzgCommitment::try_from(c.as_slice()).unwrap()),
);
proofs.extend(
bs.proofs
.iter()
.map(|p| KzgProof::try_from(p.as_slice()).unwrap()),
);
blobs.extend(
bs.blobs
.iter()
.map(|b| Blob::try_from(b.as_slice()).unwrap()),
);
(commitments, proofs, blobs)
},
);

BlobsBundle {
commitments,
proofs,
blobs,
}
}

/// Returns the length of the transactions in the block template.
#[inline]
pub fn transactions_len(&self) -> usize {
Expand Down
10 changes: 9 additions & 1 deletion bolt-sidecar/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ethereum_consensus::{
types::mainnet::ExecutionPayload,
Fork,
};
use reth_primitives::{PooledTransactionsElement, TxType};
use reth_primitives::{BlobTransactionSidecar, PooledTransactionsElement, TxType};
use tokio::sync::{mpsc, oneshot};

/// Commitment types, received by users wishing to receive preconfirmations.
Expand Down Expand Up @@ -228,6 +228,7 @@ pub trait TransactionExt {
fn value(&self) -> U256;
fn tx_type(&self) -> TxType;
fn chain_id(&self) -> Option<u64>;
fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>;
}

impl TransactionExt for PooledTransactionsElement {
Expand Down Expand Up @@ -268,4 +269,11 @@ impl TransactionExt for PooledTransactionsElement {
}
}
}

fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
match self {
PooledTransactionsElement::BlobTransaction(blob_tx) => Some(&blob_tx.sidecar),
_ => None,
}
}
}

0 comments on commit 87cbfa5

Please sign in to comment.