mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
fix: always serialize validationError (#2139)
This commit is contained in:
@ -5,7 +5,7 @@ use reth_primitives::{
|
|||||||
H256, U256, U64,
|
H256, U256, U64,
|
||||||
};
|
};
|
||||||
use reth_rlp::{Decodable, Encodable};
|
use reth_rlp::{Decodable, Encodable};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
|
||||||
|
|
||||||
/// This structure maps on the ExecutionPayload structure of the beacon chain spec.
|
/// This structure maps on the ExecutionPayload structure of the beacon chain spec.
|
||||||
///
|
///
|
||||||
@ -199,7 +199,7 @@ pub struct PayloadAttributes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This structure contains the result of processing a payload
|
/// This structure contains the result of processing a payload
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PayloadStatus {
|
pub struct PayloadStatus {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
@ -223,6 +223,19 @@ impl PayloadStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Serialize for PayloadStatus {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let mut map = serializer.serialize_map(Some(3))?;
|
||||||
|
map.serialize_entry("status", self.status.as_str())?;
|
||||||
|
map.serialize_entry("latestValidHash", &self.latest_valid_hash)?;
|
||||||
|
map.serialize_entry("validationError", &self.status.validation_error())?;
|
||||||
|
map.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
|
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
pub enum PayloadStatusEnum {
|
pub enum PayloadStatusEnum {
|
||||||
@ -253,6 +266,28 @@ pub enum PayloadStatusEnum {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PayloadStatusEnum {
|
||||||
|
/// Returns the string representation of the payload status.
|
||||||
|
pub fn as_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
PayloadStatusEnum::Valid => "VALID",
|
||||||
|
PayloadStatusEnum::Invalid { .. } => "INVALID",
|
||||||
|
PayloadStatusEnum::Syncing => "SYNCING",
|
||||||
|
PayloadStatusEnum::Accepted => "ACCEPTED",
|
||||||
|
PayloadStatusEnum::InvalidBlockHash { .. } => "INVALID_BLOCK_HASH",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the validation error if the payload status is invalid.
|
||||||
|
pub fn validation_error(&self) -> Option<&str> {
|
||||||
|
match self {
|
||||||
|
PayloadStatusEnum::InvalidBlockHash { validation_error } |
|
||||||
|
PayloadStatusEnum::Invalid { validation_error } => Some(validation_error),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -378,4 +413,22 @@ mod tests {
|
|||||||
let valid_block = block;
|
let valid_block = block;
|
||||||
assert_matches!(TryInto::<SealedBlock>::try_into(valid_block), Ok(_));
|
assert_matches!(TryInto::<SealedBlock>::try_into(valid_block), Ok(_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serde_payload_status() {
|
||||||
|
let s = r#"{"status":"SYNCING","latestValidHash":null,"validationError":null}"#;
|
||||||
|
let status: PayloadStatus = serde_json::from_str(s).unwrap();
|
||||||
|
assert_eq!(status.status, PayloadStatusEnum::Syncing);
|
||||||
|
assert!(status.latest_valid_hash.is_none());
|
||||||
|
assert!(status.status.validation_error().is_none());
|
||||||
|
assert_eq!(serde_json::to_string(&status).unwrap(), s);
|
||||||
|
|
||||||
|
let full = s;
|
||||||
|
let s = r#"{"status":"SYNCING","latestValidHash":null}"#;
|
||||||
|
let status: PayloadStatus = serde_json::from_str(s).unwrap();
|
||||||
|
assert_eq!(status.status, PayloadStatusEnum::Syncing);
|
||||||
|
assert!(status.latest_valid_hash.is_none());
|
||||||
|
assert!(status.status.validation_error().is_none());
|
||||||
|
assert_eq!(serde_json::to_string(&status).unwrap(), full);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user