Skip to content

Commit e7ea61d

Browse files
authored
feat(engine): Optional Serde (#1283)
* feat: optional serde * fix: beacon types * fix: provider * fix: eip7547 * fix: run ci
1 parent 8c5aff5 commit e7ea61d

File tree

10 files changed

+121
-76
lines changed

10 files changed

+121
-76
lines changed

crates/eip7547/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ workspace = true
2020

2121
[dependencies]
2222
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
23-
alloy-rpc-types-engine = { workspace = true }
23+
alloy-rpc-types-engine = { workspace = true, features = ["serde"] }
2424
alloy-serde = { workspace = true }
2525

2626
# serde

crates/provider/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ alloy-rpc-types-anvil = { workspace = true, optional = true }
3232
alloy-rpc-types-eth = { workspace = true, features = ["serde"] }
3333
alloy-rpc-types-trace = { workspace = true, optional = true }
3434
alloy-rpc-types-txpool = { workspace = true, optional = true }
35-
alloy-rpc-types-engine = { workspace = true, optional = true }
35+
alloy-rpc-types-engine = { workspace = true, optional = true, features = ["serde"] }
3636
alloy-rpc-types = { workspace = true, optional = true }
3737
alloy-transport-http = { workspace = true, optional = true }
3838
alloy-transport-ipc = { workspace = true, optional = true }

crates/rpc-types-beacon/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ workspace = true
2020
[dependencies]
2121
# ethereum
2222
alloy-eips = { workspace = true, features = ["serde"] }
23-
alloy-rpc-types-engine.workspace = true
23+
alloy-rpc-types-engine = { workspace = true, features = ["serde"] }
2424
alloy-primitives.workspace = true
2525

2626
# ssz

crates/rpc-types-engine/Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ workspace = true
2020

2121
[dependencies]
2222
# ethereum
23-
alloy-serde.workspace = true
2423
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
2524
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
2625
alloy-consensus = { workspace = true, features = ["serde"] }
2726
alloy-eips = { workspace = true, features = ["serde"] }
2827

2928
# misc
30-
serde = { workspace = true, features = ["derive"] }
3129
derive_more = { workspace = true, features = ["display"] }
3230

31+
# serde
32+
alloy-serde = { workspace = true, optional = true }
33+
serde = { workspace = true, features = ["derive"], optional = true }
34+
3335
# ssz
3436
ethereum_ssz_derive = { workspace = true, optional = true }
3537
ethereum_ssz = { workspace = true, optional = true }
@@ -42,8 +44,9 @@ rand = { workspace = true, optional = true }
4244
jsonwebtoken = { version = "9.3.0", optional = true }
4345

4446
[features]
45-
default = ["jwt", "std"]
47+
default = ["jwt", "std", "serde"]
4648
std = ["alloy-consensus/std", "derive_more/std"]
49+
serde = ["dep:serde", "dep:alloy-serde"]
4750
jwt = ["dep:jsonwebtoken", "dep:rand"]
4851
jsonrpsee-types = ["dep:jsonrpsee-types"]
4952
ssz = ["std", "dep:ethereum_ssz", "dep:ethereum_ssz_derive", "alloy-eips/ssz"]

crates/rpc-types-engine/src/cancun.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use alloy_primitives::B256;
1010
///
1111
/// See also:
1212
/// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#request>
13-
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
13+
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
14+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1415
pub struct CancunPayloadFields {
1516
/// The parent beacon block root.
1617
pub parent_beacon_block_root: B256,

crates/rpc-types-engine/src/forkchoice.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::{PayloadStatus, PayloadStatusEnum};
22
use crate::PayloadId;
33
use alloy_primitives::B256;
4-
use serde::{Deserialize, Serialize};
54

65
/// invalid forkchoice state error code.
76
pub const INVALID_FORK_CHOICE_STATE_ERROR: i32 = -38002;
@@ -19,8 +18,9 @@ pub const INVALID_PAYLOAD_ATTRIBUTES_ERROR_MSG: &str = "Invalid payload attribut
1918
pub type ForkChoiceUpdateResult = Result<ForkchoiceUpdated, ForkchoiceUpdateError>;
2019

2120
/// This structure encapsulates the fork choice state
22-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
23-
#[serde(rename_all = "camelCase")]
21+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
22+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23+
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
2424
pub struct ForkchoiceState {
2525
/// Hash of the head block.
2626
pub head_block_hash: B256,
@@ -116,8 +116,9 @@ impl From<ForkchoiceUpdateError> for jsonrpsee_types::error::ErrorObject<'static
116116
/// Represents a successfully _processed_ forkchoice state update.
117117
///
118118
/// Note: this can still be INVALID if the provided payload was invalid.
119-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
120-
#[serde(rename_all = "camelCase")]
119+
#[derive(Clone, Debug, PartialEq, Eq)]
120+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
121+
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
121122
pub struct ForkchoiceUpdated {
122123
/// Represents the outcome of the validation of the payload, independently of the payload being
123124
/// valid or not.

crates/rpc-types-engine/src/identification.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
33
use alloc::string::{String, ToString};
44
use core::str::FromStr;
5-
use serde::{Deserialize, Serialize};
65

76
/// This enum defines a standard for specifying a client with just two letters. Clients teams which
87
/// have a code reserved in this list MUST use this code when identifying themselves.
9-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
8+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1010
pub enum ClientCode {
1111
/// Besu
1212
BU,
@@ -101,8 +101,9 @@ impl core::fmt::Display for ClientCode {
101101
}
102102

103103
/// Contains information which identifies a client implementation.
104-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
105-
#[serde(rename_all = "camelCase")]
104+
#[derive(Clone, Debug, PartialEq, Eq)]
105+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106+
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
106107
pub struct ClientVersionV1 {
107108
/// Client code, e.g. GE for Geth
108109
pub code: ClientCode,
@@ -120,6 +121,7 @@ mod tests {
120121
use super::*;
121122

122123
#[test]
124+
#[cfg(feature = "serde")]
123125
fn client_id_serde() {
124126
let s = r#"{"code":"RH","name":"Reth","version":"v1.10.8","commit":"fa4ff922"}"#;
125127
let v: ClientVersionV1 = serde_json::from_str(s).unwrap();

crates/rpc-types-engine/src/jwt.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use jsonwebtoken::{
77
decode, errors::ErrorKind, get_current_timestamp, Algorithm, DecodingKey, Validation,
88
};
99
use rand::Rng;
10-
use serde::{Deserialize, Serialize};
1110
#[cfg(feature = "std")]
1211
use std::{
1312
fs, io,
@@ -117,7 +116,8 @@ const JWT_SIGNATURE_ALGO: Algorithm = Algorithm::HS256;
117116
///
118117
/// The Engine API spec requires that just the `iat` (issued-at) claim is provided.
119118
/// It ignores claims that are optional or additional for this specification.
120-
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
119+
#[derive(Copy, Clone, Debug)]
120+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
121121
pub struct Claims {
122122
/// The "iat" value MUST be a number containing a NumericDate value.
123123
/// According to the RFC A NumericDate represents the number of seconds since
@@ -215,6 +215,7 @@ impl JwtSecret {
215215
/// - The JWT `exp` (expiration time) claim is validated by default if defined.
216216
///
217217
/// See also: [JWT Claims - Engine API specs](https://github.com/ethereum/execution-apis/blob/main/src/engine/authentication.md#jwt-claims)
218+
#[cfg(feature = "serde")]
218219
pub fn validate(&self, jwt: &str) -> Result<(), JwtError> {
219220
// Create a new validation object with the required signature algorithm
220221
// and ensure that the `iat` claim is present. The `exp` claim is validated if defined.
@@ -250,6 +251,7 @@ impl JwtSecret {
250251

251252
/// Encode the header and claims given and sign the payload using the algorithm from the header
252253
/// and the key.
254+
#[cfg(feature = "serde")]
253255
pub fn encode(&self, claims: &Claims) -> Result<String, jsonwebtoken::errors::Error> {
254256
let bytes = &self.0;
255257
let key = jsonwebtoken::EncodingKey::from_secret(bytes);
@@ -331,6 +333,7 @@ mod tests {
331333
}
332334

333335
#[test]
336+
#[cfg(feature = "serde")]
334337
fn validation_ok() {
335338
let secret = JwtSecret::random();
336339
let claims = Claims { iat: get_current_timestamp(), exp: Some(10000000000) };
@@ -342,6 +345,7 @@ mod tests {
342345
}
343346

344347
#[test]
348+
#[cfg(feature = "serde")]
345349
fn validation_with_current_time_ok() {
346350
let secret = JwtSecret::random();
347351
let claims = Claims::default();
@@ -353,7 +357,7 @@ mod tests {
353357
}
354358

355359
#[test]
356-
#[cfg(feature = "std")]
360+
#[cfg(all(feature = "std", feature = "serde"))]
357361
fn validation_error_iat_out_of_window() {
358362
let secret = JwtSecret::random();
359363

@@ -379,6 +383,7 @@ mod tests {
379383
}
380384

381385
#[test]
386+
#[cfg(feature = "serde")]
382387
fn validation_error_exp_expired() {
383388
let secret = JwtSecret::random();
384389
let claims = Claims { iat: get_current_timestamp(), exp: Some(1) };
@@ -390,6 +395,7 @@ mod tests {
390395
}
391396

392397
#[test]
398+
#[cfg(feature = "serde")]
393399
fn validation_error_wrong_signature() {
394400
let secret_1 = JwtSecret::random();
395401
let claims = Claims { iat: get_current_timestamp(), exp: Some(10000000000) };
@@ -402,6 +408,7 @@ mod tests {
402408
}
403409

404410
#[test]
411+
#[cfg(feature = "serde")]
405412
fn validation_error_unsupported_algorithm() {
406413
let secret = JwtSecret::random();
407414
let bytes = &secret.0;
@@ -417,6 +424,7 @@ mod tests {
417424
}
418425

419426
#[test]
427+
#[cfg(feature = "serde")]
420428
fn valid_without_exp_claim() {
421429
let secret = JwtSecret::random();
422430

0 commit comments

Comments
 (0)