Skip to content

Commit

Permalink
feat(verkle): boilerplate implementation of verkle network
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev committed May 18, 2024
1 parent 109816a commit 5ac2cc0
Show file tree
Hide file tree
Showing 11 changed files with 816 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

75 changes: 74 additions & 1 deletion ethportal-api/src/types/content_key/verkle.rs
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
pub type VerkleContentKey = Vec<u8>;
use std::{fmt, hash::Hash};

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{ContentKeyError, OverlayContentKey};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum VerkleContentKey {
InnerRootNode,
InnerSubNode,
LeafRootNode,
LeafSubNode,
}

impl OverlayContentKey for VerkleContentKey {
fn content_id(&self) -> [u8; 32] {
todo!()
}

fn to_bytes(&self) -> Vec<u8> {
todo!()
}
}

impl TryFrom<Vec<u8>> for VerkleContentKey {
type Error = ContentKeyError;

fn try_from(_value: Vec<u8>) -> Result<Self, Self::Error> {
todo!()
}
}

impl From<VerkleContentKey> for Vec<u8> {
fn from(val: VerkleContentKey) -> Self {
val.to_bytes()
}
}

impl From<&VerkleContentKey> for Vec<u8> {
fn from(val: &VerkleContentKey) -> Self {
val.to_bytes()
}
}

impl Hash for VerkleContentKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write(&self.to_bytes());
}
}

impl Serialize for VerkleContentKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_hex())
}
}

impl<'de> Deserialize<'de> for VerkleContentKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::from_hex(&s).map_err(serde::de::Error::custom)
}
}

impl fmt::Display for VerkleContentKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "VerkleContentKey {{ }}")
}
}
37 changes: 36 additions & 1 deletion ethportal-api/src/types/content_value/verkle.rs
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
pub type VerkleContentValue = Vec<u8>;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::ContentValue;

/// Verkle content value.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum VerkleContentValue {}

impl ContentValue for VerkleContentValue {
fn encode(&self) -> Vec<u8> {
todo!()
}

fn decode(_buf: &[u8]) -> Result<Self, crate::ContentValueError> {
todo!()
}
}

impl Serialize for VerkleContentValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_hex())
}
}

impl<'de> Deserialize<'de> for VerkleContentValue {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::from_hex(&s).map_err(serde::de::Error::custom)
}
}
1 change: 1 addition & 0 deletions trin-storage/src/versioned/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use utils::create_store;
pub enum ContentType {
History,
State,
Verkle,
}

/// The version of the store. There should be exactly one implementation of the
Expand Down
4 changes: 4 additions & 0 deletions trin-verkle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ authors = ["https://github.com/ethereum/trin/graphs/contributors"]

[dependencies]
anyhow = "1.0.68"
discv5 = { version = "0.4.1", features = ["serde"] }
ethportal-api = { path = "../ethportal-api" }
parking_lot = "0.11.2"
portalnet = { path = "../portalnet" }
serde_json = "1.0.89"
tokio = {version = "1.14.0", features = ["full"]}
tracing = "0.1.36"
trin-storage = { path = "../trin-storage" }
trin-validation = { path = "../trin-validation" }
utp-rs = { git = "https://github.com/ethereum/utp", tag = "v0.1.0-alpha.12" }
67 changes: 67 additions & 0 deletions trin-verkle/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use ethportal_api::types::portal_wire::Message;
use portalnet::events::OverlayRequest;
use std::sync::Arc;
use tokio::sync::mpsc::UnboundedReceiver;
use tracing::{error, warn, Instrument};

use crate::network::VerkleNetwork;

pub struct VerkleEvents {
pub network: Arc<VerkleNetwork>,
pub message_rx: UnboundedReceiver<OverlayRequest>,
}

impl VerkleEvents {
pub async fn start(mut self) {
loop {
tokio::select! {
Some(msg) = self.message_rx.recv() => {
self.handle_verkle_message(msg);
} else => {
error!("Verkle event channel closed, shutting down");
break;
}
}
}
}

/// Handle verkle network OverlayRequest.
fn handle_verkle_message(&self, msg: OverlayRequest) {
let network = Arc::clone(&self.network);
tokio::spawn(async move {
match msg {
OverlayRequest::Talk(talk_request) => {
Self::handle_talk_request(talk_request, network).await;
}
OverlayRequest::Event(event) => {
let _ = network.overlay.process_one_event(event).await;
}
}
});
}

/// Handle verkle network TALKREQ message.
async fn handle_talk_request(request: discv5::TalkRequest, network: Arc<VerkleNetwork>) {
let talk_request_id = request.id().clone();
let reply = match network
.overlay
.process_one_request(&request)
.instrument(tracing::info_span!("verkle_network", req = %talk_request_id))
.await
{
Ok(response) => Message::from(response).into(),
Err(error) => {
error!(
error = %error,
request.discv5.id = %talk_request_id,
"Error processing portal verkle request, responding with empty TALKRESP"
);
// Return an empty TALKRESP if there was an error executing the request
"".into()
}
};
if let Err(error) = request.respond(reply) {
warn!(error = %error, request.discv5.id = %talk_request_id, "Error responding to TALKREQ");
}
}
}
Loading

0 comments on commit 5ac2cc0

Please sign in to comment.