Skip to content

Commit

Permalink
Merge pull request #285 from chainbound/nico/devnet-fix
Browse files Browse the repository at this point in the history
fix: updated devnet
  • Loading branch information
thedevbirb authored Oct 14, 2024
2 parents 410c1db + 7c7d677 commit 4634ff9
Show file tree
Hide file tree
Showing 48 changed files with 3,756 additions and 928 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
.git
Dockerfile
.dockerignore
1 change: 1 addition & 0 deletions bolt-boost/Cargo.lock

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

1 change: 1 addition & 0 deletions bolt-boost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ alloy = { version = "0.3.6", features = [
"rpc-types-beacon",
"rpc-types-engine",
] }
alloy-rlp = "0.3.8"

# commit-boost
cb-common = { git = "https://github.com/commit-boost/commit-boost-client", tag = "v0.3.0" }
Expand Down
4 changes: 2 additions & 2 deletions bolt-boost/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub fn verify_multiproofs(
// Get all the leaves from the saved constraints
let mut leaves = Vec::with_capacity(proofs.total_leaves());

// NOTE: Get the leaves from the constraints cache by matching the saved hashes. We need the leaves
// in order to verify the multiproof.
// NOTE: Get the leaves from the constraints cache by matching the saved hashes. We need the
// leaves in order to verify the multiproof.
for hash in &proofs.transaction_hashes {
let mut found = false;
for constraint in constraints {
Expand Down
8 changes: 2 additions & 6 deletions bolt-boost/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,9 @@ where
Ok(response) => {
let url = response.url().clone();
let status = response.status();
let body = response.text().await.ok();
if status != StatusCode::OK {
error!(
%status,
%url,
"Failed to POST to relay: {body:?}"
)
let body = response.text().await.ok();
error!(%status, %url, "Failed to POST to relay: {body:?}");
} else {
debug!(%url, "Successfully sent POST request to relay");
success = true;
Expand Down
120 changes: 106 additions & 14 deletions bolt-boost/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use alloy::{
consensus::TxEnvelope,
eips::eip2718::{Decodable2718, Eip2718Error},
consensus::{TxEip4844Variant, TxEnvelope},
eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result},
primitives::{Bytes, TxHash, B256},
rpc::types::beacon::{BlsPublicKey, BlsSignature},
signers::k256::sha2::{Digest, Sha256},
};
use alloy_rlp::{BufMut, Encodable};
use axum::http::HeaderMap;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use std::ops::Deref;
use tracing::error;

use cb_common::{
constants::COMMIT_BOOST_DOMAIN,
Expand Down Expand Up @@ -44,8 +46,15 @@ impl SignedConstraints {
#[allow(unused)]
pub fn verify_signature(&self, chain: Chain, pubkey: &BlsPublicKey) -> bool {
let domain = compute_domain(chain, COMMIT_BOOST_DOMAIN);
let signing_root = compute_signing_root(self.message.digest(), domain);
let digest = match self.message.digest() {
Ok(digest) => digest,
Err(e) => {
error!(err = ?e, "Failed to compute digest");
return false;
}
};

let signing_root = compute_signing_root(digest, domain);
verify_bls_signature(pubkey, &signing_root, &self.signature).is_ok()
}
}
Expand All @@ -60,19 +69,18 @@ pub struct ConstraintsMessage {

impl ConstraintsMessage {
/// Returns the digest of this message.
pub fn digest(&self) -> [u8; 32] {
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()).expect("valid transaction");

let tx = TxEnvelope::decode_2718(&mut bytes.as_ref())?;
hasher.update(tx.tx_hash());
}

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

Expand All @@ -92,21 +100,53 @@ impl TryFrom<ConstraintsMessage> for ConstraintsWithProofData {
.transactions
.iter()
.map(|tx| {
let tx_hash = *TxEnvelope::decode_2718(&mut tx.as_ref())?.tx_hash();

let tx_root =
tree_hash::TreeHash::tree_hash_root(&Transaction::<
<DenebSpec as EthSpec>::MaxBytesPerTransaction,
>::from(tx.to_vec()));
let envelope = TxEnvelope::decode_2718(&mut tx.as_ref())?;
let tx_hash_tree_root = calculate_tx_hash_tree_root(&envelope, tx)?;

Ok((tx_hash, tx_root))
Ok((*envelope.tx_hash(), tx_hash_tree_root))
})
.collect::<Result<Vec<_>, Eip2718Error>>()?;

Ok(Self { message: value, proof_data: transactions })
}
}

/// Calculate the SSZ hash tree root of a transaction, starting from its enveloped form.
/// For type 3 transactions, the hash tree root of the inner transaction is taken (without blobs).
fn calculate_tx_hash_tree_root(
envelope: &TxEnvelope,
raw_tx: &Bytes,
) -> Result<B256, Eip2718Error> {
match envelope {
// For type 3 txs, take the hash tree root of the inner tx (EIP-4844)
TxEnvelope::Eip4844(tx) => match tx.tx() {
TxEip4844Variant::TxEip4844(tx) => {
let mut out = Vec::new();
out.put_u8(0x03);
tx.encode(&mut out);

Ok(tree_hash::TreeHash::tree_hash_root(&Transaction::<
<DenebSpec as EthSpec>::MaxBytesPerTransaction,
>::from(out)))
}
TxEip4844Variant::TxEip4844WithSidecar(tx) => {
use alloy_rlp::Encodable;
let mut out = Vec::new();
out.put_u8(0x03);
tx.tx.encode(&mut out);

Ok(tree_hash::TreeHash::tree_hash_root(&Transaction::<
<DenebSpec as EthSpec>::MaxBytesPerTransaction,
>::from(out)))
}
},
// For other transaction types, take the hash tree root of the whole tx
_ => Ok(tree_hash::TreeHash::tree_hash_root(&Transaction::<
<DenebSpec as EthSpec>::MaxBytesPerTransaction,
>::from(raw_tx.to_vec()))),
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct SignedDelegation {
pub message: DelegationMessage,
Expand All @@ -115,6 +155,7 @@ pub struct SignedDelegation {

#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct DelegationMessage {
action: u8,
pub validator_pubkey: BlsPublicKey,
pub delegatee_pubkey: BlsPublicKey,
}
Expand All @@ -127,6 +168,7 @@ pub struct SignedRevocation {

#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct RevocationMessage {
action: u8,
pub validator_pubkey: BlsPublicKey,
pub delegatee_pubkey: BlsPublicKey,
}
Expand Down Expand Up @@ -173,3 +215,53 @@ pub struct RequestConfig {
pub timeout_ms: u64,
pub headers: HeaderMap,
}

#[cfg(test)]
mod tests {
use alloy::{hex::FromHex, primitives::Bytes};

use super::ConstraintsWithProofData;
use crate::types::SignedConstraints;

#[test]
fn decode_constraints_test() {
let raw = r#"{
"message": {
"pubkey": "0xa695ad325dfc7e1191fbc9f186f58eff42a634029731b18380ff89bf42c464a42cb8ca55b200f051f57f1e1893c68759",
"slot": 32,
"top": true,
"transactions": [
"0x02f86c870c72dd9d5e883e4d0183408f2382520894d2e2adf7177b7a8afddbc12d1634cf23ea1a71020180c001a08556dcfea479b34675db3fe08e29486fe719c2b22f6b0c1741ecbbdce4575cc6a01cd48009ccafd6b9f1290bbe2ceea268f94101d1d322c787018423ebcbc87ab4"
]
},
"signature": "0xb8d50ee0d4b269db3d4658c1dac784d273a4160d769e16dce723a9684c390afe5865348416b3bf0f1a4f47098bec9024135d0d95f08bed18eb577a3d8a67f5dc78b13cc62515e280786a73fb267d35dfb7ab46a25ac29bf5bc2fa5b07b3e07a6"
}"#;

let mut c = serde_json::from_str::<SignedConstraints>(raw).unwrap();
let pd = ConstraintsWithProofData::try_from(c.message.clone()).unwrap().proof_data[0];

assert_eq!(
pd.0.to_string(),
"0x385b9f1ba5dbbe419dcbbbbf0840b76b941f3c216d383ec9deb9b1a323ee0cea".to_string()
);

assert_eq!(
pd.1.to_string(),
"0x02e383af0c34516ef38e13391d917d5b61b6f69e17d5234f77cb8cc3a1ae932e".to_string()
);

c.message.transactions[0] = Bytes::from_hex("0x03f9029c01830299f184b2d05e008507aef40a00832dc6c09468d30f47f19c07bccef4ac7fae2dc12fca3e0dc980b90204ef16e845000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000633b68f5d8d3a86593ebb815b4663bcbe0302e31382e302d64657600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004109de8da2a97e37f2e6dc9f7d50a408f9344d7aa1a925ae53daf7fbef43491a571960d76c0cb926190a9da10df7209fb1ba93cd98b1565a3a2368749d505f90c81c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0843b9aca00e1a00141e3a338e30c49ed0501e315bcc45e4edefebed43ab1368a1505461d9cf64901a01e8511e06b17683d89eb57b9869b96b8b611f969f7f56cbc0adc2df7c88a2a07a00910deacf91bba0d74e368d285d311dc5884e7cfe219d85aea5741b2b6e3a2fe").unwrap();

let pd = ConstraintsWithProofData::try_from(c.message).unwrap().proof_data[0];

assert_eq!(
pd.0.to_string(),
"0x15bd881daa1408b33f67fa4bdeb8acfb0a2289d9b4c6f81eef9bb2bb2e52e780".to_string()
);

assert_eq!(
pd.1.to_string(),
"0x0a637924b9f9b28a413b01cb543bcd688850b8964f77576fc71219448f7b4ab9".to_string()
);
}
}
9 changes: 7 additions & 2 deletions bolt-kurtosis-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async fn main() -> Result<()> {
);

info!("Transaction hash: {}", tx_hash);
info!("body: {}", serde_json::to_string(&request)?);
info!("body: {}", trim_zeroes(serde_json::to_string(&request)?));

let client = reqwest::Client::new();
let response = client
Expand All @@ -95,8 +95,13 @@ async fn main() -> Result<()> {
.send()
.await?;

info!("Response: {:?}", response.text().await?);
let res = trim_zeroes(response.text().await?);
info!("Response: {:?}", res);
}

Ok(())
}

fn trim_zeroes(s: impl Into<String>) -> String {
s.into().replace(&"0".repeat(32), ".").replace(&".".repeat(4), "")
}
2 changes: 1 addition & 1 deletion bolt-kurtosis-client/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn generate_random_blob_tx() -> TransactionRequest {
.with_value(U256::from(100))
.with_max_fee_per_blob_gas(100u128)
.max_fee_per_gas(NOICE_GAS_PRICE)
.max_priority_fee_per_gas(NOICE_GAS_PRICE / 10)
.max_priority_fee_per_gas(NOICE_GAS_PRICE)
.with_gas_limit(42_000u128)
.with_blob_sidecar(sidecar)
.with_input(random_bytes)
Expand Down
Loading

0 comments on commit 4634ff9

Please sign in to comment.