Skip to content

Commit

Permalink
Retrieve genesis_fork_version from network parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailUshakoff committed Dec 30, 2024
1 parent ecb980a commit 0989af4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
9 changes: 8 additions & 1 deletion Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ async fn main() -> Result<(), Error> {
config.taiko_chain_id,
));

let mev_boost = mev_boost::MevBoost::new(&config.mev_boost_url);
let ethereum_l1 = Arc::new(ethereum_l1);
let mev_boost = mev_boost::MevBoost::new(
&config.mev_boost_url,
ethereum_l1
.consensus_layer
.get_genesis_details()
.await?
.genesis_fork_version,
);

let block_proposed_event_checker =
BlockProposedEventReceiver::new(ethereum_l1.clone(), block_proposed_tx);
Expand Down
22 changes: 13 additions & 9 deletions Node/src/mev_boost/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::bls::BLSService;
use alloy::signers::k256::sha2::{Digest, Sha256};
use anyhow::Error;
use ethereum_consensus::crypto::PublicKey;
use ethereum_consensus::primitives::BlsSignature;
use ethereum_consensus::state_transition::Context;
Expand Down Expand Up @@ -30,20 +31,19 @@ impl ConstraintsMessage {
}
}

fn digest(&self) -> [u8; 32] {
fn digest(&self) -> Result<[u8; 32], Error> {
let mut hasher = Sha256::new();
hasher.update(self.pubkey.to_vec());
hasher.update(self.slot.to_le_bytes());
hasher.update((self.top as u8).to_le_bytes());
for tx in self.transactions.iter() {
// Convert the opaque bytes to a EIP-2718 envelope and obtain the tx hash.
// this is needed to handle type 3 transactions.
// FIXME: don't unwrap here and handle the error properly
let tx = PooledTransactionsElement::decode_enveloped(tx.to_vec().into()).unwrap();
let tx = PooledTransactionsElement::decode_enveloped(tx.to_vec().into())?;
hasher.update(tx.hash().as_slice());
}

hasher.finalize().into()
Ok(hasher.finalize().into())
}
}

Expand Down Expand Up @@ -93,21 +93,25 @@ impl SignedConstraints {
signing_data.tree_hash_root().0
}

pub fn new(message: ConstraintsMessage, bls: Arc<BLSService>) -> Self {
let genesis_fork_version = [16, 0, 0, 56]; // TODO get frpm eth/v1/beacon/genesis. Replace with the correct value for your chain
pub fn new(
message: ConstraintsMessage,
bls: Arc<BLSService>,
genesis_fork_version: [u8; 4],
) -> Result<Self, Error> {
// Prepare data
let mut context = Context::for_minimal();
context.genesis_fork_version = genesis_fork_version;

let digest = message.digest();
let digest = message.digest()?;

let domain = Self::compute_domain_custom(&context, COMMIT_BOOST_DOMAIN);
let signing_root = Self::compute_signing_root_custom(digest.tree_hash_root().0, domain);

// Sign message
let signature = bls
.sign_with_ethereum_secret_key(&signing_root)
.expect("sign_with_domain error");
.map_err(|e| anyhow::anyhow!("Sign_with_domain error: {}", e))?;

Self { message, signature }
Ok(Self { message, signature })
}
}
6 changes: 4 additions & 2 deletions Node/src/mev_boost/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use constraints::{ConstraintsMessage, SignedConstraints};

pub struct MevBoost {
url: String,
genesis_fork_version: [u8; 4],
}

impl MevBoost {
pub fn new(url: &str) -> Self {
pub fn new(url: &str, genesis_fork_version: [u8; 4]) -> Self {
Self {
url: url.to_string(),
genesis_fork_version,
}
}

Expand Down Expand Up @@ -49,7 +51,7 @@ impl MevBoost {
let pubkey = bls_service.get_ethereum_public_key();
let message = ConstraintsMessage::new(pubkey, slot_id, constraints);

let signed = SignedConstraints::new(message, bls_service);
let signed = SignedConstraints::new(message, bls_service, self.genesis_fork_version)?;

let json_data = serde_json::to_value([&signed])?;

Expand Down

0 comments on commit 0989af4

Please sign in to comment.