Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Notifications and pubsub transports #10

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/json-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ exclude.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy-primitives.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json = { version = "1.0.103", features = ["raw_value"] }
serde_json = { version = "1.0.103", features = ["raw_value"] }
24 changes: 24 additions & 0 deletions crates/json-rpc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ pub enum Id {
None,
}

impl PartialOrd for Id {
Copy link
Member Author

@prestwich prestwich Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allows use as key in btreemaps. implementation is arbitrary, but totally ordered

fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
// numbers < strings
// strings < null
// null == null
match (self, other) {
(Id::Number(a), Id::Number(b)) => a.partial_cmp(b),
(Id::Number(_), _) => Some(std::cmp::Ordering::Less),

(Id::String(_), Id::Number(_)) => Some(std::cmp::Ordering::Greater),
(Id::String(a), Id::String(b)) => a.partial_cmp(b),
(Id::String(_), Id::None) => Some(std::cmp::Ordering::Less),

(Id::None, Id::None) => Some(std::cmp::Ordering::Equal),
(Id::None, _) => Some(std::cmp::Ordering::Greater),
}
}
}
impl Ord for Id {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap()
}
}

impl Id {
/// Returns `true` if the ID is a number.
pub fn is_number(&self) -> bool {
Expand Down
3 changes: 3 additions & 0 deletions crates/json-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use serde::{de::DeserializeOwned, Serialize};

mod notification;
pub use notification::EthNotification;

mod request;
pub use request::Request;

Expand Down
23 changes: 23 additions & 0 deletions crates/json-rpc/src/notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use alloy_primitives::U256;
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};

#[derive(Debug, Deserialize)]
pub struct EthNotification<T = Box<serde_json::value::RawValue>> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are distinct from JSON-PRC notifications (which are ID-less requests)

pub subscription: U256,
pub result: T,
}

impl<T> Serialize for EthNotification<T>
prestwich marked this conversation as resolved.
Show resolved Hide resolved
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_map(Some(2))?;
state.serialize_entry("subscription", &self.subscription)?;
state.serialize_entry("result", &self.result)?;
state.end()
}
}
6 changes: 1 addition & 5 deletions crates/json-rpc/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where
map.serialize_entry("method", self.method)?;

// Params may be omitted if it is 0-sized
if !is_zst::<Params>() {
if std::mem::size_of::<Params>() != 0 {
prestwich marked this conversation as resolved.
Show resolved Hide resolved
map.serialize_entry("params", &self.params)?;
}

Expand All @@ -41,7 +41,3 @@ where
map.end()
}
}

fn is_zst<T>() -> bool {
std::mem::size_of::<T>() == 0
}
Loading