diff --git a/Cargo.lock b/Cargo.lock index 3812f38bf..6f529344b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5376,6 +5376,7 @@ dependencies = [ "phf_codegen", "pin-project", "prost", + "prost-types", "quote", "raft", "rand", diff --git a/Cargo.toml b/Cargo.toml index 6701ac09c..bb8fa7bbd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ dotenvy = "=0.15.7" bincode = { version = "=1.3.3", optional = true } display_json = "=0.2.1" prost = "=0.12.6" +prost-types = "=0.12.6" serde = "=1.0.203" serde_json = "=1.0.117" serde_urlencoded = "=0.7.1" diff --git a/src/eth/consensus/append_entries_storage.rs b/src/eth/consensus/append_entries_storage.rs new file mode 100644 index 000000000..8a8b5ced2 --- /dev/null +++ b/src/eth/consensus/append_entries_storage.rs @@ -0,0 +1,62 @@ +use prost::Message; +use serde::Deserialize; +use serde::Serialize; + +use super::append_entry::BlockHeader as BH; +use super::append_entry::TransactionExecution as TE; + +#[allow(clippy::large_enum_variant)] +#[derive(Serialize, Deserialize, Debug, Clone)] +enum LogEntryData { + BlockHeader(BH), + TransactionExecution(TE), +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +struct LogEntry { + index: u64, + term: u64, + data: LogEntryData, +} + +// Implement Serialize and Deserialize for BH +impl Serialize for BH { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let bytes = self.encode_to_vec(); + serializer.serialize_bytes(&bytes) + } +} + +impl<'de> Deserialize<'de> for BH { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let bytes: Vec = serde::Deserialize::deserialize(deserializer)?; + BH::decode(&*bytes).map_err(serde::de::Error::custom) + } +} + +// Implement Serialize and Deserialize for TE +impl Serialize for TE { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let bytes = self.encode_to_vec(); + serializer.serialize_bytes(&bytes) + } +} + +impl<'de> Deserialize<'de> for TE { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let bytes: Vec = serde::Deserialize::deserialize(deserializer)?; + TE::decode(&*bytes).map_err(serde::de::Error::custom) + } +} diff --git a/src/eth/consensus/mod.rs b/src/eth/consensus/mod.rs index 821475735..a94d92da5 100644 --- a/src/eth/consensus/mod.rs +++ b/src/eth/consensus/mod.rs @@ -1,3 +1,4 @@ +mod append_entries_storage; pub mod forward_to; use std::collections::HashMap;