From 4db433293a1a17988fc0f91bf78e1072b9549f2d Mon Sep 17 00:00:00 2001 From: mikhailUshakoff Date: Wed, 11 Dec 2024 23:48:36 +0100 Subject: [PATCH] Update MEV Boost API --- Node/Cargo.lock | 49 +++++++++++++ Node/Cargo.toml | 2 +- Node/src/bls/mod.rs | 2 - Node/src/main.rs | 2 +- Node/src/mev_boost/constraints.rs | 76 ++++++++++++--------- Node/src/mev_boost/mod.rs | 13 ++-- Node/src/mev_boost/tests.rs | 2 +- Node/src/node/lookahead_updated_receiver.rs | 4 +- 8 files changed, 102 insertions(+), 48 deletions(-) diff --git a/Node/Cargo.lock b/Node/Cargo.lock index 8348c17e..050bb3ec 100644 --- a/Node/Cargo.lock +++ b/Node/Cargo.lock @@ -479,6 +479,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c31a3750b8f5a350d17354e46a52b0f2f19ec5f2006d816935af599dedc521" dependencies = [ + "alloy-rpc-types-beacon", "alloy-rpc-types-engine", "alloy-rpc-types-eth", "alloy-serde", @@ -496,6 +497,20 @@ dependencies = [ "serde", ] +[[package]] +name = "alloy-rpc-types-beacon" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8a24bcff4f9691d7a4971b43e5da46aa7b4ce22ed7789796612dc1eed220983" +dependencies = [ + "alloy-eips", + "alloy-primitives 0.7.7", + "alloy-rpc-types-engine", + "serde", + "serde_with", + "thiserror", +] + [[package]] name = "alloy-rpc-types-engine" version = "0.2.1" @@ -1706,6 +1721,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", + "serde", "wasm-bindgen", "windows-targets 0.52.6", ] @@ -2131,6 +2147,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", + "serde", ] [[package]] @@ -3555,6 +3572,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown 0.12.3", + "serde", ] [[package]] @@ -3565,6 +3583,7 @@ checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", "hashbrown 0.15.0", + "serde", ] [[package]] @@ -6763,6 +6782,36 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" +dependencies = [ + "base64 0.22.1", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.6.0", + "serde", + "serde_derive", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "serde_yaml" version = "0.8.26" diff --git a/Node/Cargo.toml b/Node/Cargo.toml index 3879b391..09e63930 100644 --- a/Node/Cargo.toml +++ b/Node/Cargo.toml @@ -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" diff --git a/Node/src/bls/mod.rs b/Node/src/bls/mod.rs index 37f02493..432b3399 100644 --- a/Node/src/bls/mod.rs +++ b/Node/src/bls/mod.rs @@ -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() } diff --git a/Node/src/main.rs b/Node/src/main.rs index eafe59f5..b4120736 100644 --- a/Node/src/main.rs +++ b/Node/src/main.rs @@ -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 = diff --git a/Node/src/mev_boost/constraints.rs b/Node/src/mev_boost/constraints.rs index 39472975..f4e98614 100644 --- a/Node/src/mev_boost/constraints.rs +++ b/Node/src/mev_boost/constraints.rs @@ -2,49 +2,57 @@ 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, - index: Option, -} - -#[derive(PartialEq, Debug, Encode, Decode, Serialize)] +#[derive(Debug, Clone, Serialize, Eq, PartialEq, Encode)] pub struct ConstraintsMessage { - validator_index: u64, - slot: u64, - constraints: Vec, + #[serde(serialize_with = "serialize_data_as_hex")] + pub pubkey: [u8; 48], + pub slot: u64, + pub top: bool, + pub transactions: Vec>, } impl ConstraintsMessage { - pub fn new(validator_index: u64, slot: u64, messages: Vec>) -> 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>) -> 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(data: &Vec, serializer: S) -> Result +fn serialize_data_as_hex(data: &[u8; 48], serializer: S) -> Result where S: Serializer, { @@ -52,7 +60,7 @@ where serializer.serialize_str(&hex_string) } -fn serialize_data_as_hex(data: &[u8; 96], serializer: S) -> Result +fn serialize_data1_as_hex(data: &[u8; 96], serializer: S) -> Result where S: Serializer, { @@ -62,17 +70,17 @@ where impl SignedConstraints { pub fn new(message: ConstraintsMessage, bls: Arc) -> 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 } } -} +} \ No newline at end of file diff --git a/Node/src/mev_boost/mod.rs b/Node/src/mev_boost/mod.rs index 5d313c6d..de3053e4 100644 --- a/Node/src/mev_boost/mod.rs +++ b/Node/src/mev_boost/mod.rs @@ -11,14 +11,12 @@ 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, } } @@ -26,7 +24,7 @@ impl MevBoost { 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(¶ms) .send() .await @@ -56,8 +54,11 @@ impl MevBoost { bls_service: Arc, ) -> 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); diff --git a/Node/src/mev_boost/tests.rs b/Node/src/mev_boost/tests.rs index 03632157..6869cf58 100644 --- a/Node/src/mev_boost/tests.rs +++ b/Node/src/mev_boost/tests.rs @@ -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); diff --git a/Node/src/node/lookahead_updated_receiver.rs b/Node/src/node/lookahead_updated_receiver.rs index 36a7ab1d..6b58a149 100644 --- a/Node/src/node/lookahead_updated_receiver.rs +++ b/Node/src/node/lookahead_updated_receiver.rs @@ -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), } }