Skip to content

Commit

Permalink
chore: create append entries base types (#1076)
Browse files Browse the repository at this point in the history
* chore: create append entries base types

* doc: add details of log entries
  • Loading branch information
renancloudwalk authored Jun 12, 2024
1 parent 24fa599 commit 6b02de4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
65 changes: 65 additions & 0 deletions src/eth/consensus/log_entry.rs
Original file line number Diff line number Diff line change
@@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes = self.encode_to_vec();
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for BH {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes: Vec<u8> = 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes = self.encode_to_vec();
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for TE {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes: Vec<u8> = serde::Deserialize::deserialize(deserializer)?;
TE::decode(&*bytes).map_err(serde::de::Error::custom)
}
}
1 change: 1 addition & 0 deletions src/eth/consensus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod forward_to;
mod log_entry;

use std::collections::HashMap;
#[cfg(feature = "kubernetes")]
Expand Down

0 comments on commit 6b02de4

Please sign in to comment.