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

Add a transports crate & initial Network abstraction #2

Merged
merged 53 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
83a5c8a
feature: transports crate
prestwich Jul 10, 2023
0203bad
review: address comments
prestwich Jul 12, 2023
7528c5e
refactor: RpcObject trait
prestwich Jul 12, 2023
fe6142d
refactor: RpcResult type
prestwich Jul 12, 2023
b2e3705
refactor: small code quality
prestwich Jul 12, 2023
f1010c6
deps: bump Cargo.toml
prestwich Jul 12, 2023
04f3c49
chore: delete unused src
prestwich Jul 12, 2023
366a5ac
refactor: more stuff
prestwich Jul 12, 2023
4de4a8e
refactor: remove Params type from RpcCall
prestwich Jul 12, 2023
27a6c0f
fix: remove extra to_json_raw_value
prestwich Jul 12, 2023
e2703df
refactor: minor legibility
prestwich Jul 12, 2023
53128fd
feat: unwrap variants
prestwich Jul 12, 2023
76bef7f
fix: add debug bounds
prestwich Jul 12, 2023
d654407
feature: some cool combinators on rpccall
prestwich Jul 12, 2023
1d691ec
fix: hide __ENFORCE_ZST
prestwich Jul 12, 2023
0073a12
feature: DummyNetwork compile check
prestwich Jul 12, 2023
9d736b5
test: dummynet compile checks
prestwich Jul 13, 2023
268412b
doc: fix comment
prestwich Jul 13, 2023
3cf3f2d
wip: mware and combinator stuff
prestwich Jul 14, 2023
5c53aa8
fuck jsonrpsee
prestwich Jul 16, 2023
efb5cdc
feature: blanket
prestwich Jul 16, 2023
cf00429
test: http impls transport
prestwich Jul 16, 2023
ed0a5e8
feature: send batch request
prestwich Jul 16, 2023
1e9e873
feature: separate rpc type crate
prestwich Jul 16, 2023
21fda6e
feat: RpcObject
prestwich Jul 17, 2023
12f78fa
refactor: transport future aliases
prestwich Jul 22, 2023
2be245a
refactor: transport requires type-erased futures. improved batch ergo
prestwich Jul 25, 2023
38a63ab
wip: some middleware noodling
prestwich Jul 30, 2023
dcdb4cf
feature: manual future for json rpc to avoid higher-ranked lifetime
prestwich Aug 1, 2023
d45a19d
cleanup: some clippy and stuff
prestwich Aug 2, 2023
b8dbb9a
feature: client builder
prestwich Aug 2, 2023
763a5c6
refactor: move is_local to transport
prestwich Aug 2, 2023
9d1f491
chore: clippy
prestwich Aug 2, 2023
0f3fb8f
feature: generic request
prestwich Aug 2, 2023
843009b
feature: allow type-erased rpc client
prestwich Aug 7, 2023
2b83171
chore: cleanup in transports mod
prestwich Aug 7, 2023
562e539
chore: misc cleanup
prestwich Aug 7, 2023
ceda9f7
refactor: more crate
prestwich Aug 9, 2023
b7a8d1e
chore: clippy cleanup
prestwich Aug 9, 2023
36513b2
fix: lifetimes for rpc calls
prestwich Aug 9, 2023
e204269
feature: lifetime on rpccall
prestwich Aug 9, 2023
7a9726e
feature: BoxTransport
prestwich Aug 12, 2023
ba8fd02
chore: clippy
prestwich Aug 12, 2023
5cd4826
refactor: cow for jsonrpc params
prestwich Aug 14, 2023
7f8923b
fix: lint
prestwich Aug 19, 2023
047ba1d
refactor: rename to boxed
prestwich Aug 19, 2023
c5ce15b
docs and misc convenience
prestwich Aug 21, 2023
b7fbff4
refactor: docs and cleanup
prestwich Aug 21, 2023
7173fcc
docs: more of em
prestwich Aug 21, 2023
ee69008
refactor: seal transport
prestwich Aug 21, 2023
5770e67
feature: seal transport
prestwich Aug 21, 2023
0c6b2c9
rename middleware to provider
prestwich Aug 21, 2023
8a6a838
chore: remove dead code
prestwich Aug 24, 2023
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
29 changes: 26 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,35 @@ edition = "2021"
rust-version = "1.65"
authors = ["Alloy Contributors"]
license = "MIT OR Apache-2.0"
homepage = "https://github.com/ethers-rs/next"
repository = "https://github.com/ethers-rs/next"
homepage = "https://github.com/alloy-rs/next"
repository = "https://github.com/alloy-rs/next"
exclude = ["benches/", "tests/"]

[workspace.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[workspace.dependencies]
[workspace.dependencies]

alloy-json-rpc = { path = "crates/json-rpc" }
alloy-transports = { path = "crates/transports" }
alloy-networks = { path = "crates/networks" }

alloy-primitives = { version = "0.2.0", features = ["serde"] }
alloy-rlp = "0.3.0"

# futures
futures-channel = "0.3"
futures-util = "0.3"

# serde
serde = { version = "1.0", default-features = false, features = ["alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

# thiserror
thiserror = "1.0"

# transports
url = "2.4.0"
pin-project = "1.1.2"
tower = { version = "0.4.13", features = ["util"] }
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# alloy-next

### Layout

- alloy-json-rpc
- Core data types for JSON-RPC 2.0
- alloy-transports
- Transports and RPC call futures.
- alloy-networks
- Network abstraction for RPC types. Allows capturing different RPC param and response types on a per-network basis.
- alloy-provider
- Based on ethers::middleware::Middleware, but abstract over <N>, and object-safe.
16 changes: 16 additions & 0 deletions crates/json-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "alloy-json-rpc"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
exclude.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json = { version = "1.0.103", features = ["raw_value"] }
43 changes: 43 additions & 0 deletions crates/json-rpc/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};

/// A JSON-RPC 2.0 ID object. This may be a number, string, or null.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum Id {
Number(u64),
String(String),
None,
}

impl Id {
/// Returns `true` if the ID is a number.
pub fn is_number(&self) -> bool {
matches!(self, Id::Number(_))
}

/// Returns `true` if the ID is a string.
pub fn is_string(&self) -> bool {
matches!(self, Id::String(_))
}

/// Returns `true` if the ID is `None`.
pub fn is_none(&self) -> bool {
matches!(self, Id::None)
}

/// Returns the ID as a number, if it is one.
pub fn as_number(&self) -> Option<u64> {
match self {
Id::Number(n) => Some(*n),
_ => None,
}
}

/// Returns the ID as a string, if it is one.
pub fn as_string(&self) -> Option<&str> {
match self {
Id::String(s) => Some(s),
_ => None,
}
}
}
38 changes: 38 additions & 0 deletions crates/json-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Alloy JSON-RPC data types.
//!
//! This crate provides data types for use with the JSON-RPC 2.0 protocol. It
//! does not provide any functionality for actually sending or receiving
//! JSON-RPC data.
//!
//! This crate is aimed at simplifying client implementations. It is not
//! well-suited to in-server applications. We do not support borrowing data from
//! deserializers, for example. This choice prevents complex lifetime
//! propagation in user code, at the expense of copying data

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

mod request;
pub use request::JsonRpcRequest;

mod response;
pub use response::{ErrorPayload, JsonRpcResponse, ResponsePayload};

mod common;
pub use common::Id;

mod result;
pub use result::RpcResult;

/// An object that can be used as a JSON-RPC parameter.
pub trait RpcParam: Serialize + Clone + Send + Sync + Unpin {}
impl<T> RpcParam for T where T: Serialize + Clone + Send + Sync + Unpin {}

/// An object that can be used as a JSON-RPC return value.
// Note: we add `'static` here to indicate that the Resp is wholly owned. It
// may not borrow.
pub trait RpcReturn: DeserializeOwned + Send + Sync + Unpin + 'static {}
impl<T> RpcReturn for T where T: DeserializeOwned + Send + Sync + Unpin + 'static {}

/// An object that can be used as a JSON-RPC parameter and return value.
pub trait RpcObject: RpcParam + RpcReturn {}
impl<T> RpcObject for T where T: RpcParam + RpcReturn {}
36 changes: 36 additions & 0 deletions crates/json-rpc/src/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::{common::Id, RpcParam};

use serde::{ser::SerializeMap, Deserialize, Serialize};

/// A JSON-RPC 2.0 request object.
///
/// This is a generic type that can be used to represent any JSON-RPC request.
/// The `Params` type parameter is used to represent the parameters of the
/// request, and the `method` field is used to represent the method name.
///
/// ### Note
///
/// The value of `method` must be known at compile time.
#[derive(Debug, Deserialize, Clone)]
pub struct JsonRpcRequest<Params> {
pub method: &'static str,
pub params: Params,
pub id: Id,
}

impl<Params> Serialize for JsonRpcRequest<Params>
where
Params: RpcParam,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut map = serializer.serialize_map(Some(4))?;
map.serialize_entry("method", self.method)?;
map.serialize_entry("params", &self.params)?;
map.serialize_entry("id", &self.id)?;
map.serialize_entry("jsonrpc", "2.0")?;
map.end()
}
}
Loading