forked from ethereum/trin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(verkle): boilerplate implementation of verkle network
- Loading branch information
Showing
11 changed files
with
816 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {{ }}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
Oops, something went wrong.