Skip to content

Commit 4f0b63d

Browse files
committed
More debug
1 parent 3f56981 commit 4f0b63d

File tree

4 files changed

+134
-45
lines changed

4 files changed

+134
-45
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ members = [
1414

1515
[workspace.dependencies]
1616
async-trait = "0.1"
17-
axum = { version = "0.7", features = ["ws"] }
17+
axum = { version = "0.8", features = ["ws"] }
1818
bytes = "1.6"
19-
eth2 = { git = "https://github.com/sigp/lighthouse.git", rev = "df131b2a6c58609d3f772cd1ac89487a0d4247d0" }
19+
eth2 = { git = "https://github.com/sigp/lighthouse.git", rev = "897cab02a071ca8f35ce0b55aefc17617da2f14e" }
2020
ethereum_serde_utils = "0.7"
2121
ethereum_ssz = "0.7"
2222
ethereum_ssz_derive = "0.7"
2323
flate2 = "1.0"
2424
futures = "0.3.30"
25-
http = "1"
25+
http = "1.2"
2626
mediatype = "0.19.13"
27-
reqwest = { version = "0.12.5", features = ["json"] }
27+
reqwest = { version = "0.12.12", features = ["json"] }
2828
serde = { version = "1.0", features = ["derive"] }
2929
serde_json = { version = "1", features = ["raw_value"] }
3030
superstruct = "0.8"
3131
tokio = { version = "1", default-features = false, features = ["signal", "rt-multi-thread"] }
3232
tokio-tungstenite = "0.24.0"
3333
tracing = { version = "0.1", features = ["attributes"] }
34-
types = { git = "https://github.com/sigp/lighthouse.git", rev = "df131b2a6c58609d3f772cd1ac89487a0d4247d0" }
34+
types = { git = "https://github.com/sigp/lighthouse.git", rev = "897cab02a071ca8f35ce0b55aefc17617da2f14e" }
3535
rand = "0.8"

builder-server/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ serde.workspace = true
1717
serde_json.workspace = true
1818
tokio.workspace = true
1919
tracing.workspace = true
20+
21+
[dev-dependencies]
22+
tower = "0.5.2"

builder-server/src/server.rs

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use axum::{
99
Json, Router,
1010
};
1111
use builder_api_types::{
12-
eth_spec::EthSpec, ExecutionBlockHash, PublicKeyBytes, SignedBlindedBeaconBlock,
12+
eth_spec::EthSpec, fork_versioned_response::ForkVersionDecode, ExecutionBlockHash, ForkName,
13+
FullPayloadContents, MainnetEthSpec, PublicKeyBytes, SignedBlindedBeaconBlock,
1314
SignedValidatorRegistrationData, Slot,
1415
};
1516
use ethereum_apis_common::{
@@ -40,7 +41,7 @@ where
4041
)
4142
.route("/eth/v1/builder/status", get(get_status))
4243
.route(
43-
"/eth/v1/builder/header/:slot/:parent_hash/:pubkey",
44+
"/eth/v1/builder/header/{slot}/{parent_hash}/{pubkey}",
4445
get(get_header::<I, A, E>),
4546
)
4647
.with_state(api_impl)
@@ -69,20 +70,22 @@ where
6970
I: AsRef<A> + Send + Sync,
7071
A: Builder<E>,
7172
{
73+
dbg!(&headers);
7274
let content_type_header = headers.get(CONTENT_TYPE);
7375
let content_type = content_type_header.and_then(|value| value.to_str().ok());
7476
let content_type = match content_type {
7577
Some("application/octet-stream") => ContentType::Ssz,
7678
_ => ContentType::Json,
7779
};
7880
let slot = block.slot();
81+
7982
let res = api_impl.as_ref().submit_blinded_block(block).await;
8083

81-
dbg!("in submit_blinded_block");
84+
println!("in submit_blinded_block");
8285
let response =
8386
build_response_with_headers(res, content_type, api_impl.as_ref().fork_name_at_slot(slot))
8487
.await;
85-
dbg!(&response);
88+
println!("Got response ok {}", response.is_ok());
8689
response
8790
}
8891

@@ -122,3 +125,110 @@ where
122125
tracing::info!("Got response from builder, constructing response");
123126
build_response_with_headers(res, content_type, api_impl.as_ref().fork_name_at_slot(slot)).await
124127
}
128+
129+
#[cfg(test)]
130+
mod tests {
131+
use super::*;
132+
use async_trait::async_trait;
133+
use axum::{body::Body, http::Request};
134+
use builder_api_types::{
135+
builder_bid::SignedBuilderBid, BeaconBlock, BeaconBlockDeneb, Blob, BlobsBundle,
136+
EmptyBlock, ExecutionPayload, ExecutionPayloadAndBlobs, ExecutionPayloadDeneb, ForkName,
137+
ForkVersionDecode, FullPayloadContents, KzgCommitment, KzgProof, MainnetEthSpec, Signature,
138+
};
139+
use ethereum_apis_common::{ErrorResponse, CONSENSUS_VERSION_HEADER};
140+
use http::HeaderValue;
141+
use ssz::Encode;
142+
use std::{marker::PhantomData, usize};
143+
use tower::ServiceExt;
144+
145+
#[derive(Clone)]
146+
struct DummyBuilder<E: EthSpec> {
147+
_phantom: PhantomData<E>,
148+
}
149+
150+
impl<E: EthSpec> AsRef<DummyBuilder<E>> for DummyBuilder<E> {
151+
fn as_ref(&self) -> &DummyBuilder<E> {
152+
self
153+
}
154+
}
155+
156+
#[async_trait]
157+
impl<E: EthSpec> Builder<E> for DummyBuilder<E> {
158+
fn fork_name_at_slot(&self, _slot: Slot) -> builder_api_types::ForkName {
159+
ForkName::Deneb
160+
}
161+
162+
async fn get_header(
163+
&self,
164+
_slot: Slot,
165+
_parent_hash: ExecutionBlockHash,
166+
_pubkey: PublicKeyBytes,
167+
) -> Result<SignedBuilderBid<E>, ErrorResponse> {
168+
todo!()
169+
}
170+
171+
async fn register_validators(
172+
&self,
173+
_registrations: Vec<SignedValidatorRegistrationData>,
174+
) -> Result<(), ErrorResponse> {
175+
Ok(())
176+
}
177+
178+
async fn submit_blinded_block(
179+
&self,
180+
_block: SignedBlindedBeaconBlock<E>,
181+
) -> Result<FullPayloadContents<E>, ErrorResponse> {
182+
let payload_and_blobs: ExecutionPayloadAndBlobs<E> = ExecutionPayloadAndBlobs {
183+
blobs_bundle: BlobsBundle {
184+
commitments: vec![KzgCommitment::empty_for_testing()].into(),
185+
proofs: vec![KzgProof::empty()].into(),
186+
blobs: vec![Blob::<E>::new(vec![42; E::bytes_per_blob()]).unwrap()].into(),
187+
},
188+
execution_payload: ExecutionPayload::Deneb(ExecutionPayloadDeneb {
189+
..Default::default()
190+
}),
191+
};
192+
let full_payload = FullPayloadContents::PayloadAndBlobs(payload_and_blobs);
193+
Ok(full_payload)
194+
}
195+
}
196+
197+
#[tokio::test]
198+
async fn test_api() {
199+
let app = new(DummyBuilder::<MainnetEthSpec> {
200+
_phantom: PhantomData,
201+
});
202+
203+
let spec = MainnetEthSpec::default_spec();
204+
let dummy_block = SignedBlindedBeaconBlock::<MainnetEthSpec>::from_block(
205+
BeaconBlock::Deneb(BeaconBlockDeneb::empty(&spec)),
206+
Signature::empty(),
207+
);
208+
let request = Request::builder()
209+
.uri("/eth/v1/builder/blinded_blocks")
210+
.method("POST")
211+
.header(
212+
CONTENT_TYPE,
213+
HeaderValue::from_static("application/octet-stream"),
214+
)
215+
.header(CONSENSUS_VERSION_HEADER, HeaderValue::from_static("deneb"))
216+
.body(Body::from(dummy_block.as_ssz_bytes()))
217+
.unwrap();
218+
219+
let response = app.oneshot(request).await.unwrap();
220+
221+
// Assert status code
222+
// assert_eq!(response.status(), StatusCode::ACCEPTED);
223+
224+
// Get response body as bytes
225+
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
226+
.await
227+
.unwrap();
228+
229+
dbg!(
230+
FullPayloadContents::<MainnetEthSpec>::from_ssz_bytes_by_fork(&body, ForkName::Deneb)
231+
.unwrap()
232+
);
233+
}
234+
}

common/src/lib.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use axum::{
2-
async_trait,
32
body::Body,
43
extract::{FromRequest, Request},
54
response::{IntoResponse, Response},
@@ -32,10 +31,6 @@ where
3231

3332
let resp = match result {
3433
Ok(body) => {
35-
tracing::info!(
36-
"Got a valid response from builder, content-type {:?}",
37-
content_type
38-
);
3934
println!(
4035
"Got a valid response from builder, content-type {:?}",
4136
content_type
@@ -59,40 +54,26 @@ where
5954
})?,
6055
);
6156
}
62-
57+
dbg!(&content_type);
6358
let body_content = match content_type {
6459
ContentType::Json => {
6560
let body = ForkVersionedResponse {
6661
version: Some(fork_name),
6762
metadata: EmptyMetadata {},
6863
data: body,
6964
};
70-
tokio::task::spawn_blocking(move || {
71-
serde_json::to_vec(&body).map_err(|e| {
72-
error!(error = ?e);
73-
StatusCode::INTERNAL_SERVER_ERROR
74-
})
75-
})
76-
.await
77-
.map_err(|e| {
65+
serde_json::to_vec(&body).map_err(|e| {
7866
error!(error = ?e);
7967
StatusCode::INTERNAL_SERVER_ERROR
80-
})??
68+
})?
8169
}
82-
ContentType::Ssz => tokio::task::spawn_blocking(move || T::as_ssz_bytes(&body))
83-
.await
84-
.map_err(|e| {
85-
error!(error = ?e);
86-
StatusCode::INTERNAL_SERVER_ERROR
87-
})?,
70+
71+
ContentType::Ssz => T::as_ssz_bytes(&body),
8872
};
89-
dbg!(&body_content.len());
9073
let resp = response.body(Body::from(body_content)).map_err(|e| {
9174
error!(error = ?e);
92-
dbg!(&e);
9375
StatusCode::INTERNAL_SERVER_ERROR
9476
});
95-
dbg!(&resp);
9677
resp
9778
}
9879
Err(body) => {
@@ -208,7 +189,6 @@ where
208189
#[derive(Debug, Clone, Copy, Default)]
209190
pub struct Ssz<T>(pub T);
210191

211-
#[async_trait]
212192
impl<T, S> FromRequest<S> for Ssz<T>
213193
where
214194
T: ssz::Decode,
@@ -239,7 +219,6 @@ where
239219
#[derive(Debug, Clone, Copy, Default)]
240220
pub struct JsonOrSszWithFork<T>(pub T);
241221

242-
#[async_trait]
243222
impl<T, S> FromRequest<S> for JsonOrSszWithFork<T>
244223
where
245224
T: serde::de::DeserializeOwned + ForkVersionDecode + 'static,
@@ -252,7 +231,6 @@ where
252231
let content_type = headers
253232
.get(CONTENT_TYPE)
254233
.and_then(|value| value.to_str().ok());
255-
dbg!(&headers);
256234
let fork_name = headers
257235
.get(CONSENSUS_VERSION_HEADER)
258236
.and_then(|value| ForkName::from_str(value.to_str().unwrap()).ok());
@@ -283,7 +261,6 @@ where
283261
#[derive(Debug, Clone, Copy, Default)]
284262
pub struct JsonOrSsz<T>(pub T);
285263

286-
#[async_trait]
287264
impl<T, S> FromRequest<S> for JsonOrSsz<T>
288265
where
289266
T: serde::de::DeserializeOwned + ssz::Decode + 'static,
@@ -323,7 +300,6 @@ where
323300
#[derive(Debug, Clone, Copy, Default)]
324301
pub struct JsonOrSszMaybeGzipped<T>(pub T);
325302

326-
#[async_trait]
327303
impl<T, S> FromRequest<S> for JsonOrSszMaybeGzipped<T>
328304
where
329305
T: serde::de::DeserializeOwned + ssz::Decode + 'static,
@@ -447,7 +423,6 @@ pub fn custom_internal_err(message: String) -> ErrorResponse {
447423
#[derive(Debug, Clone, Copy, Default)]
448424
pub struct JsonConsensusVersionHeader<T>(pub T);
449425

450-
#[async_trait]
451426
impl<T, S> FromRequest<S> for JsonConsensusVersionHeader<T>
452427
where
453428
T: ForkVersionDeserialize + 'static,
@@ -554,10 +529,10 @@ impl FromStr for Accept {
554529

555530
#[cfg(test)]
556531
mod tests {
557-
use std::usize;
532+
// use std::usize;
558533

559534
use super::*;
560-
use axum::body::to_bytes;
535+
// use axum::body::to_bytes;
561536
use beacon_api_types::{
562537
Blob, BlobsBundle, EthSpec, ExecutionPayload, ExecutionPayloadAndBlobs,
563538
ExecutionPayloadDeneb, FullPayloadContents, KzgCommitment, KzgProof, MainnetEthSpec,
@@ -585,9 +560,10 @@ mod tests {
585560
let resp = build_response_with_headers(Ok(full_payload), ContentType::Ssz, ForkName::Deneb)
586561
.await
587562
.unwrap();
588-
let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
589-
dbg!(&body.len());
590-
FullPayloadContents::<MainnetEthSpec>::from_ssz_bytes_by_fork(&body, ForkName::Deneb)
591-
.unwrap();
563+
dbg!(&resp);
564+
// let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
565+
// dbg!(&body.len());
566+
// FullPayloadContents::<MainnetEthSpec>::from_ssz_bytes_by_fork(&body, ForkName::Deneb)
567+
// .unwrap();
592568
}
593569
}

0 commit comments

Comments
 (0)