diff --git a/Cargo.lock b/Cargo.lock index fd9f2fd90..83ae72c78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5480,6 +5480,7 @@ dependencies = [ "phf_codegen", "pin-project", "prost", + "prost-types", "quote", "raft", "rand", diff --git a/Cargo.toml b/Cargo.toml index a1f620e70..d7843e7d5 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/log_entry.rs b/src/eth/consensus/log_entry.rs new file mode 100644 index 000000000..db5d83665 --- /dev/null +++ b/src/eth/consensus/log_entry.rs @@ -0,0 +1,65 @@ +/// Holds the log entries that are stored in the Raft log. +/// The log entries are either a BlockHeader or a TransactionExecution. +/// The LogEntry struct is used to store the index and term of the log entry. +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..20ee9dd3c 100644 --- a/src/eth/consensus/mod.rs +++ b/src/eth/consensus/mod.rs @@ -1,4 +1,5 @@ pub mod forward_to; +mod log_entry; use std::collections::HashMap; #[cfg(feature = "kubernetes")]