Skip to content

Commit

Permalink
Merge pull request #511 from chainbound/chore/sidecar/update-alloy
Browse files Browse the repository at this point in the history
chore(sidecar): bump alloy version to 0.7.2
  • Loading branch information
mempirate authored Dec 4, 2024
2 parents 4935878 + 6709b43 commit 5c1208c
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 361 deletions.
481 changes: 248 additions & 233 deletions bolt-sidecar/Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions bolt-sidecar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "ec3073e" }
ethereum_ssz = "0.5"

# alloy
alloy = { version = "0.6.4", features = [
alloy = { version = "0.7.2", features = [
"full",
"provider-trace-api",
"rpc-types-beacon",
"rpc-types-engine",
] }
alloy-rpc-types-engine = { version = "0.6.4", default-features = false, features = [
alloy-rpc-types-engine = { version = "0.7.2", default-features = false, features = [
"jwt",
] }

# reth
reth-primitives = { git = "https://github.com/paradigmxyz/reth", version = "1.1.1" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth", version = "1.1.2" }

reqwest = "0.12"
ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus", rev = "cf3c404" }
Expand Down Expand Up @@ -79,7 +79,7 @@ commit-boost = { git = "https://github.com/Commit-Boost/commit-boost-client", re
cb-common = { git = "https://github.com/Commit-Boost/commit-boost-client", rev = "45ce8f1" }

[dev-dependencies]
alloy-node-bindings = "0.6.4" # must match alloy version
alloy-node-bindings = "0.7.2" # must match alloy version
criterion = { version = "0.5", features = ["html_reports"] }

[package.metadata.cargo-machete]
Expand All @@ -96,6 +96,7 @@ harness = false
name = "bolt-sidecar"
path = "bin/sidecar.rs"


[lints.clippy]
explicit_iter_loop = "warn"
if_not_else = "warn"
Expand Down
7 changes: 4 additions & 3 deletions bolt-sidecar/src/builder/compat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloy::{
consensus::BlockHeader,
eips::{eip2718::Encodable2718, eip4895::Withdrawal},
primitives::{Address, Bloom, B256, U256},
rpc::types::{
Expand Down Expand Up @@ -99,15 +100,15 @@ pub(crate) fn to_alloy_execution_payload(
.unwrap_or_default();

AlloyExecutionPayload::V3(ExecutionPayloadV3 {
blob_gas_used: block.blob_gas_used(),
blob_gas_used: block.blob_gas_used().unwrap_or_default(),
excess_blob_gas: block.excess_blob_gas.unwrap_or_default(),
payload_inner: ExecutionPayloadV2 {
payload_inner: ExecutionPayloadV1 {
base_fee_per_gas: U256::from(block.base_fee_per_gas.unwrap_or_default()),
block_hash,
block_number: block.number,
extra_data: block.extra_data.clone(),
transactions: block.raw_transactions(),
transactions: block.encoded_2718_transactions(),
fee_recipient: block.header.beneficiary,
gas_limit: block.gas_limit,
gas_used: block.gas_used,
Expand Down Expand Up @@ -161,7 +162,7 @@ pub(crate) fn to_consensus_execution_payload(value: &SealedBlock) -> ConsensusEx
block_hash: to_bytes32(hash),
transactions: TryFrom::try_from(transactions).unwrap(),
withdrawals: TryFrom::try_from(withdrawals).unwrap(),
blob_gas_used: value.blob_gas_used(),
blob_gas_used: value.blob_gas_used().unwrap_or_default(),
excess_blob_gas: value.excess_blob_gas.unwrap_or_default(),
};
ConsensusExecutionPayload::Deneb(payload)
Expand Down
17 changes: 10 additions & 7 deletions bolt-sidecar/src/builder/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use alloy::{
consensus::{Header, EMPTY_OMMER_ROOT_HASH},
consensus::{Header, Transaction, EMPTY_OMMER_ROOT_HASH},
eips::{calc_excess_blob_gas, calc_next_block_base_fee, eip1559::BaseFeeParams},
primitives::{Address, Bloom, Bytes, B256, B64, U256},
rpc::types::{Block, Withdrawal, Withdrawals},
Expand Down Expand Up @@ -133,6 +133,7 @@ impl FallbackPayloadBuilder {
.iter()
.flat_map(|tx| tx.blob_versioned_hashes())
.flatten()
.copied()
.collect::<Vec<_>>();

let base_fee = calc_next_block_base_fee(
Expand Down Expand Up @@ -396,8 +397,10 @@ fn build_header_with_hints_and_context(
blob_gas_used: Some(context.blob_gas_used),
excess_blob_gas: Some(context.excess_blob_gas),
parent_beacon_block_root: Some(context.parent_beacon_block_root),
requests_hash: None,
extra_data: context.extra_data.clone(),
// TODO: handle the Pectra-related fields
requests_hash: None,
target_blobs_per_block: None,
}
}

Expand All @@ -409,8 +412,8 @@ pub fn secret_to_bearer_header(secret: &JwtSecret) -> HeaderValue {
"Bearer {}",
secret
.encode(&Claims {
iat: (SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
+ Duration::from_secs(60))
iat: (SystemTime::now().duration_since(UNIX_EPOCH).unwrap() +
Duration::from_secs(60))
.as_secs(),
exp: None,
})
Expand Down Expand Up @@ -474,9 +477,9 @@ mod tests {
let raw_encoded = tx_signed.encoded_2718();
let tx_signed_reth = TransactionSigned::decode_2718(&mut raw_encoded.as_slice())?;

let slot = genesis_time
+ (SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() / cfg.chain.slot_time())
+ 1;
let slot = genesis_time +
(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() / cfg.chain.slot_time()) +
1;

let block = builder.build_fallback_payload(slot, &[tx_signed_reth]).await?;
assert_eq!(block.body.transactions.len(), 1);
Expand Down
5 changes: 4 additions & 1 deletion bolt-sidecar/src/builder/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::HashMap;

use alloy::primitives::{Address, TxHash, U256};
use alloy::{
consensus::Transaction,
primitives::{Address, TxHash, U256},
};
use ethereum_consensus::{
crypto::{KzgCommitment, KzgProof},
deneb::mainnet::{Blob, BlobsBundle},
Expand Down
7 changes: 2 additions & 5 deletions bolt-sidecar/src/common/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use alloy::primitives::U256;
use alloy::{consensus::Transaction, primitives::U256};
use reth_primitives::PooledTransactionsElement;

use crate::{
primitives::{AccountState, TransactionExt},
state::ValidationError,
};
use crate::{primitives::AccountState, state::ValidationError};

/// Calculates the max_basefee `slot_diff` blocks in the future given a current basefee (in wei).
/// Returns None if an overflow would occur.
Expand Down
10 changes: 7 additions & 3 deletions bolt-sidecar/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{fmt, sync::Arc, time::Instant};

use alloy::{rpc::types::beacon::events::HeadEvent, signers::local::PrivateKeySigner};
use alloy::{
consensus::{Transaction, TxType},
rpc::types::beacon::events::HeadEvent,
signers::local::PrivateKeySigner,
};
use beacon_api_client::mainnet::Client as BeaconClient;
use ethereum_consensus::{
clock::{self, SlotStream, SystemTimeProvider},
Expand Down Expand Up @@ -28,7 +32,7 @@ use crate::{
crypto::{SignableBLS, SignerECDSA},
primitives::{
commitment::SignedCommitment, read_signed_delegations_from_file, CommitmentRequest,
ConstraintsMessage, FetchPayloadRequest, SignedConstraints, TransactionExt,
ConstraintsMessage, FetchPayloadRequest, SignedConstraints,
},
signer::{keystore::KeystoreSigner, local::LocalSigner, CommitBoostSigner, SignerBLS},
state::{fetcher::StateFetcher, ConsensusState, ExecutionState, HeadTracker, StateClient},
Expand Down Expand Up @@ -340,7 +344,7 @@ impl<C: StateFetcher, ECDSA: SignerECDSA> SidecarDriver<C, ECDSA> {
// For more information, check out the constraints API docs:
// https://docs.boltprotocol.xyz/technical-docs/api/builder#constraints
for tx in &inclusion_request.txs {
let tx_type = tx.tx_type();
let tx_type = TxType::try_from(tx.ty()).expect("valid tx type");
let message =
ConstraintsMessage::from_tx(signing_pubkey.clone(), target_slot, tx.clone());
let digest = message.digest();
Expand Down
4 changes: 3 additions & 1 deletion bolt-sidecar/src/primitives/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::str::FromStr;

use alloy::{
consensus::Transaction,
hex,
primitives::{keccak256, Address, Signature, B256},
};
Expand Down Expand Up @@ -102,6 +103,7 @@ impl InclusionRequest {
) -> eyre::Result<InclusionCommitment> {
let digest = self.digest();
let signature = signer.sign_hash(&digest).await?;
let signature = Signature::try_from(signature.as_bytes().as_ref())?;
Ok(InclusionCommitment { request: self, signature })
}

Expand Down Expand Up @@ -147,7 +149,7 @@ impl InclusionRequest {
/// Validates the init code limit.
pub fn validate_init_code_limit(&self, limit: usize) -> bool {
for tx in &self.txs {
if tx.tx_kind().is_create() && tx.input().len() > limit {
if tx.kind().is_create() && tx.input().len() > limit {
return false;
}
}
Expand Down
107 changes: 18 additions & 89 deletions bolt-sidecar/src/primitives/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
use std::{borrow::Cow, fmt};

use alloy::{
consensus::BlobTransactionSidecar,
consensus::{BlobTransactionSidecar, Transaction, TxType},
eips::eip2718::{Decodable2718, Encodable2718},
hex,
primitives::{Address, Bytes, TxKind, U256},
primitives::{Address, U256},
};
use reth_primitives::{PooledTransactionsElement, TxType};
use reth_primitives::PooledTransactionsElement;
use serde::{de, ser::SerializeSeq};

/// Trait that exposes additional information on transaction types that don't already do it
/// by themselves (e.g. [`PooledTransactionsElement`]).
pub trait TransactionExt {
/// Returns the gas limit of the transaction.
fn gas_limit(&self) -> u64;

/// Returns the value of the transaction.
fn value(&self) -> U256;

/// Returns the type of the transaction.
fn tx_type(&self) -> TxType;

/// Returns the kind of the transaction.
fn tx_kind(&self) -> TxKind;

/// Returns the input data of the transaction.
fn input(&self) -> &Bytes;

/// Returns the chain ID of the transaction.
fn chain_id(&self) -> Option<u64>;

/// Returns the blob sidecar of the transaction, if any.
fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>;

Expand All @@ -38,85 +23,29 @@ pub trait TransactionExt {
}

impl TransactionExt for PooledTransactionsElement {
fn gas_limit(&self) -> u64 {
match self {
Self::Legacy { transaction, .. } => transaction.gas_limit,
Self::Eip2930 { transaction, .. } => transaction.gas_limit,
Self::Eip1559 { transaction, .. } => transaction.gas_limit,
Self::BlobTransaction(blob_tx) => blob_tx.transaction.tx.gas_limit,
_ => unimplemented!(),
}
}

fn value(&self) -> U256 {
match self {
Self::Legacy { transaction, .. } => transaction.value,
Self::Eip2930 { transaction, .. } => transaction.value,
Self::Eip1559 { transaction, .. } => transaction.value,
Self::BlobTransaction(blob_tx) => blob_tx.transaction.tx.value,
_ => unimplemented!(),
}
}

fn tx_type(&self) -> TxType {
match self {
Self::Legacy { .. } => TxType::Legacy,
Self::Eip2930 { .. } => TxType::Eip2930,
Self::Eip1559 { .. } => TxType::Eip1559,
Self::BlobTransaction(_) => TxType::Eip4844,
_ => unimplemented!(),
}
}

fn tx_kind(&self) -> TxKind {
match self {
Self::Legacy { transaction, .. } => transaction.to,
Self::Eip2930 { transaction, .. } => transaction.to,
Self::Eip1559 { transaction, .. } => transaction.to,
Self::BlobTransaction(blob_tx) => {
TxKind::Call(blob_tx.transaction.tx.to)
}
_ => unimplemented!(),
}
}

fn input(&self) -> &Bytes {
match self {
Self::Legacy { transaction, .. } => &transaction.input,
Self::Eip2930 { transaction, .. } => &transaction.input,
Self::Eip1559 { transaction, .. } => &transaction.input,
Self::BlobTransaction(blob_tx) => &blob_tx.transaction.tx.input,
_ => unimplemented!(),
}
}

fn chain_id(&self) -> Option<u64> {
match self {
Self::Legacy { transaction, .. } => transaction.chain_id,
Self::Eip2930 { transaction, .. } => Some(transaction.chain_id),
Self::Eip1559 { transaction, .. } => Some(transaction.chain_id),
Self::BlobTransaction(blob_tx) => {
Some(blob_tx.transaction.tx.chain_id)
}
Self::Legacy(transaction) => transaction.tx().value,
Self::Eip2930(transaction) => transaction.tx().value,
Self::Eip1559(transaction) => transaction.tx().value,
Self::BlobTransaction(blob_tx) => blob_tx.tx().tx.value,
_ => unimplemented!(),
}
}

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

fn size(&self) -> usize {
match self {
Self::Legacy { transaction, .. } => transaction.size(),
Self::Eip2930 { transaction, .. } => transaction.size(),
Self::Eip1559 { transaction, .. } => transaction.size(),
Self::BlobTransaction(blob_tx) => blob_tx.transaction.tx.size(),
Self::Legacy(transaction) => transaction.tx().size(),
Self::Eip2930(transaction) => transaction.tx().size(),
Self::Eip1559(transaction) => transaction.tx().size(),
Self::BlobTransaction(blob_tx) => blob_tx.tx().tx.size(),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -156,16 +85,16 @@ impl fmt::Debug for FullTransaction {
PooledTransactionsElement::BlobTransaction(blob_tx) => {
let shortened_blobs: Vec<String> =
// Use alternative `Display` to print trimmed blob
blob_tx.transaction.sidecar.blobs.iter().map(|blob| format!("{blob:#}")).collect();
blob_tx.tx().sidecar.blobs.iter().map(|blob| format!("{blob:#}")).collect();

debug_struct.field("tx", &"BlobTransaction");
debug_struct.field("hash", &blob_tx.hash);
debug_struct.field("transaction", &blob_tx.transaction);
debug_struct.field("signature", &blob_tx.signature);
debug_struct.field("hash", &blob_tx.hash());
debug_struct.field("transaction", &blob_tx.tx());
debug_struct.field("signature", &blob_tx.signature());

debug_struct.field("sidecar_blobs", &shortened_blobs);
debug_struct.field("sidecar_commitments", &blob_tx.transaction.sidecar.commitments);
debug_struct.field("sidecar_proofs", &blob_tx.transaction.sidecar.proofs);
debug_struct.field("sidecar_commitments", &blob_tx.tx().sidecar().commitments);
debug_struct.field("sidecar_proofs", &blob_tx.tx().sidecar.proofs);
}
other => {
debug_struct.field("tx", other);
Expand Down
8 changes: 3 additions & 5 deletions bolt-sidecar/src/signer/commit_boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use ssz::Decode;
use thiserror::Error;
use tracing::{debug, error, info};

use crate::{
crypto::{bls::BLS_DST_PREFIX, ecdsa::SignerECDSA},
primitives::commitment::ECDSASignatureExt,
};
use crate::crypto::{bls::BLS_DST_PREFIX, ecdsa::SignerECDSA};

use super::SignerResult;

Expand Down Expand Up @@ -105,7 +102,8 @@ impl CommitBoostSigner {

/// Verify the ECDSA signature of the object with the given public key.
pub fn verify_ecdsa(&self, data: &[u8; 32], sig: &Signature, pubkey: &EcdsaPublicKey) -> bool {
let sig = secp256k1::ecdsa::Signature::from_str(&sig.to_hex()).expect("signature is valid");
let sig_hex = hex::encode(sig.as_bytes());
let sig = secp256k1::ecdsa::Signature::from_str(&sig_hex).expect("signature is valid");
let pubkey =
secp256k1::PublicKey::from_slice(pubkey.as_ref()).expect("public key is valid");
secp256k1::Secp256k1::new()
Expand Down
Loading

0 comments on commit 5c1208c

Please sign in to comment.