Skip to content

Commit

Permalink
Update MEV Boost API
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailUshakoff committed Dec 11, 2024
1 parent 9327928 commit 4db4332
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 48 deletions.
49 changes: 49 additions & 0 deletions Node/Cargo.lock

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

2 changes: 1 addition & 1 deletion Node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy = { version = "0.2", features = ["full", "node-bindings", "rlp"] }
alloy = { version = "0.2", features = ["full", "node-bindings", "rlp", "rpc-types-beacon"] }
alloy-rlp = "0.3"
tokio = { version = "1.38", features = ["full"] }
tracing = "0.1.40"
Expand Down
2 changes: 0 additions & 2 deletions Node/src/bls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ impl BLSService {
[res1, res2]
}

#[cfg(test)]
#[cfg(not(feature = "use_mock"))]
pub fn get_public_key_compressed(&self) -> PublicKey {
self.pk.clone()
}
Expand Down
2 changes: 1 addition & 1 deletion Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn main() -> Result<(), Error> {
config.taiko_chain_id,
));

let mev_boost = mev_boost::MevBoost::new(&config.mev_boost_url, config.validator_index);
let mev_boost = mev_boost::MevBoost::new(&config.mev_boost_url);
let ethereum_l1 = Arc::new(ethereum_l1);

let block_proposed_event_checker =
Expand Down
76 changes: 42 additions & 34 deletions Node/src/mev_boost/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,65 @@ use crate::bls::BLSService;
use alloy::hex::encode;
use serde::ser::Serializer;
use serde::Serialize;
use ssz::Encode;
use ssz_derive::{Decode, Encode};
use ssz_derive::Encode;
use std::sync::Arc;
//use ssz_derive::{Decode, Encode};
use alloy::{
consensus::TxEnvelope,
eips::eip2718::{Decodable2718, Eip2718Result},
signers::k256::sha2::{Digest, Sha256},
};

#[derive(PartialEq, Debug, Encode, Decode, Serialize)]
pub struct Constraint {
#[serde(serialize_with = "serialize_vec_as_hex")]
tx: Vec<u8>,
index: Option<u64>,
}

#[derive(PartialEq, Debug, Encode, Decode, Serialize)]
#[derive(Debug, Clone, Serialize, Eq, PartialEq, Encode)]
pub struct ConstraintsMessage {
validator_index: u64,
slot: u64,
constraints: Vec<Constraint>,
#[serde(serialize_with = "serialize_data_as_hex")]
pub pubkey: [u8; 48],
pub slot: u64,
pub top: bool,
pub transactions: Vec<Vec<u8>>,
}

impl ConstraintsMessage {
pub fn new(validator_index: u64, slot: u64, messages: Vec<Vec<u8>>) -> Self {
let constraints = messages
.iter()
.map(|message| Constraint {
tx: message.clone(),
index: None,
})
.collect();
Self {
validator_index,
pub fn new(pubkey: [u8; 48], slot: u64, transactions: Vec<Vec<u8>>) -> Self {
ConstraintsMessage {
pubkey,
slot,
constraints,
top: true,
transactions,
}
}
/// Returns the digest of this message.
pub fn digest(&self) -> Eip2718Result<[u8; 32]> {
let mut hasher = Sha256::new();
hasher.update(self.pubkey);
hasher.update(self.slot.to_le_bytes());
hasher.update((self.top as u8).to_le_bytes());

for bytes in &self.transactions {
let tx = TxEnvelope::decode_2718(&mut bytes.as_ref())?;
hasher.update(tx.tx_hash());
}

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

#[derive(Serialize)]
#[derive(Debug, Clone, Serialize)]
pub struct SignedConstraints {
message: ConstraintsMessage,
#[serde(serialize_with = "serialize_data_as_hex")]
signature: [u8; 96],
pub message: ConstraintsMessage,
#[serde(serialize_with = "serialize_data1_as_hex")]
pub signature: [u8; 96],
}

fn serialize_vec_as_hex<S>(data: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
fn serialize_data_as_hex<S>(data: &[u8; 48], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let hex_string = format!("0x{}", encode(data));
serializer.serialize_str(&hex_string)
}

fn serialize_data_as_hex<S>(data: &[u8; 96], serializer: S) -> Result<S::Ok, S::Error>
fn serialize_data1_as_hex<S>(data: &[u8; 96], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Expand All @@ -62,17 +70,17 @@ where

impl SignedConstraints {
pub fn new(message: ConstraintsMessage, bls: Arc<BLSService>) -> Self {
// Encode message;
let data = message.as_ssz_bytes();
// TODO fix data calculation
let digest = message.digest().expect("Could not compute digest");
// Use propper DST
let dst = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"
.as_bytes()
.to_vec();
// Sign message
let signature: [u8; 96] = bls
.sign(&data, &dst)
.sign(&digest.to_vec(), &dst)
.try_into()
.expect("Vec should have exactly 96 elements");
Self { message, signature }
}
}
}
13 changes: 7 additions & 6 deletions Node/src/mev_boost/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ mod tests;

pub struct MevBoost {
url: String,
validator_index: u64,
}

impl MevBoost {
pub fn new(url: &str, validator_index: u64) -> Self {
pub fn new(url: &str) -> Self {
Self {
url: url.to_string(),
validator_index,
}
}

async fn post_constraints(&self, params: Value) -> Result<Value, Error> {
let client = Client::new();
// Send the POST request to the MEV Boost
let response = client
.post(self.url.clone() + "/eth/v1/builder/constraints")
.post(self.url.clone() + "/constraints/v1/builder/constraints")
.json(&params)
.send()
.await
Expand Down Expand Up @@ -56,8 +54,11 @@ impl MevBoost {
bls_service: Arc<BLSService>,
) -> Result<(), Error> {
// Prepare the message

let message = ConstraintsMessage::new(self.validator_index, slot_id, constraints);
let pubkey: [u8; 48] = bls_service
.get_public_key_compressed()
.try_into()
.map_err(|e| anyhow::anyhow!("BLS service failed to get public key: {:?}", e))?;
let message = ConstraintsMessage::new(pubkey, slot_id, constraints);

let signed = SignedConstraints::new(message, bls_service);

Expand Down
2 changes: 1 addition & 1 deletion Node/src/mev_boost/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod tests {
.unwrap(),
);
// Create mev-boost
let mev_boost = MevBoost::new(" http://localhost:8080", 123);
let mev_boost = MevBoost::new(" http://localhost:8080");
// Some random constraints
let constraint1 = generate_random_vec_with_random_size(50, 200);
let constraint2 = generate_random_vec_with_random_size(50, 200);
Expand Down
4 changes: 1 addition & 3 deletions Node/src/node/lookahead_updated_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ impl LookaheadUpdatedEventHandler {
&lookahead_updated_event_params[lookahead_params.len()];
Ok(Some(first_additional_wrong_param.timestamp.try_into()?))
}
std::cmp::Ordering::Equal => {
Ok(None)
}
std::cmp::Ordering::Equal => Ok(None),
}
}

Expand Down

0 comments on commit 4db4332

Please sign in to comment.