1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
use crate::{common::Id, RpcParam};
use alloy_primitives::{keccak256, B256};
use serde::{de::DeserializeOwned, ser::SerializeMap, Deserialize, Serialize};
use serde_json::value::RawValue;
use std::borrow::Cow;
/// `RequestMeta` contains the [`Id`] and method name of a request.
#[derive(Clone, Debug)]
pub struct RequestMeta {
/// The method name.
pub method: Cow<'static, str>,
/// The request ID.
pub id: Id,
/// Whether the request is a subscription, other than `eth_subscribe`.
is_subscription: bool,
}
impl RequestMeta {
/// Create a new `RequestMeta`.
pub const fn new(method: Cow<'static, str>, id: Id) -> Self {
Self { method, id, is_subscription: false }
}
/// Returns `true` if the request is a subscription.
pub fn is_subscription(&self) -> bool {
self.is_subscription || self.method == "eth_subscribe"
}
/// Indicates that the request is a non-standard subscription (i.e. not
/// "eth_subscribe").
pub fn set_is_subscription(&mut self) {
self.set_subscription_status(true);
}
/// Setter for `is_subscription`. Indicates to RPC clients that the request
/// triggers a stream of notifications.
pub fn set_subscription_status(&mut self, sub: bool) {
self.is_subscription = sub;
}
}
/// 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` should be known at compile time.
#[derive(Clone, Debug)]
pub struct Request<Params> {
/// The request metadata (ID and method).
pub meta: RequestMeta,
/// The request parameters.
pub params: Params,
}
impl<Params> Request<Params> {
/// Create a new `Request`.
pub fn new(method: impl Into<Cow<'static, str>>, id: Id, params: Params) -> Self {
Self { meta: RequestMeta::new(method.into(), id), params }
}
/// Returns `true` if the request is a subscription.
pub fn is_subscription(&self) -> bool {
self.meta.is_subscription()
}
/// Indicates that the request is a non-standard subscription (i.e. not
/// "eth_subscribe").
pub fn set_is_subscription(&mut self) {
self.meta.set_is_subscription()
}
/// Setter for `is_subscription`. Indicates to RPC clients that the request
/// triggers a stream of notifications.
pub fn set_subscription_status(&mut self, sub: bool) {
self.meta.set_subscription_status(sub);
}
}
/// A [`Request`] that has been partially serialized. The request parameters
/// have been serialized, and are represented as a boxed [`RawValue`]. This is
/// useful for collections containing many requests, as it erases the `Param`
/// type. It can be created with [`Request::box_params()`].
///
/// See the [top-level docs] for more info.
///
/// [top-level docs]: crate
pub type PartiallySerializedRequest = Request<Box<RawValue>>;
impl<Params> Request<Params>
where
Params: RpcParam,
{
/// Serialize the request parameters as a boxed [`RawValue`].
///
/// # Panics
///
/// If serialization of the params fails.
pub fn box_params(self) -> PartiallySerializedRequest {
Request { meta: self.meta, params: serde_json::value::to_raw_value(&self.params).unwrap() }
}
/// Serialize the request, including the request parameters.
pub fn serialize(self) -> serde_json::Result<SerializedRequest> {
let request = serde_json::value::to_raw_value(&self)?;
Ok(SerializedRequest { meta: self.meta, request })
}
}
impl<Params> Request<&Params>
where
Params: Clone,
{
/// Clone the request, including the request parameters.
pub fn into_owned_params(self) -> Request<Params> {
Request { meta: self.meta, params: self.params.clone() }
}
}
impl<'a, Params> Request<Params>
where
Params: AsRef<RawValue> + 'a,
{
/// Attempt to deserialize the params.
///
/// To borrow from the params via the deserializer, use
/// [`Request::try_borrow_params_as`].
///
/// # Returns
/// - `Ok(T)` if the params can be deserialized as `T`
/// - `Err(e)` if the params cannot be deserialized as `T`
pub fn try_params_as<T: DeserializeOwned>(&self) -> serde_json::Result<T> {
serde_json::from_str(self.params.as_ref().get())
}
/// Attempt to deserialize the params, borrowing from the params
///
/// # Returns
/// - `Ok(T)` if the params can be deserialized as `T`
/// - `Err(e)` if the params cannot be deserialized as `T`
pub fn try_borrow_params_as<T: Deserialize<'a>>(&'a self) -> serde_json::Result<T> {
serde_json::from_str(self.params.as_ref().get())
}
}
// manually implemented to avoid adding a type for the protocol-required
// `jsonrpc` field
impl<Params> Serialize for Request<Params>
where
Params: RpcParam,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let sized_params = std::mem::size_of::<Params>() != 0;
let mut map = serializer.serialize_map(Some(3 + sized_params as usize))?;
map.serialize_entry("method", &self.meta.method[..])?;
// Params may be omitted if it is 0-sized
if sized_params {
map.serialize_entry("params", &self.params)?;
}
map.serialize_entry("id", &self.meta.id)?;
map.serialize_entry("jsonrpc", "2.0")?;
map.end()
}
}
/// A JSON-RPC 2.0 request object that has been serialized, with its [`Id`] and
/// method preserved.
///
/// This struct is used to represent a request that has been serialized, but
/// not yet sent. It is used by RPC clients to build batch requests and manage
/// in-flight requests.
#[derive(Clone, Debug)]
pub struct SerializedRequest {
meta: RequestMeta,
request: Box<RawValue>,
}
impl<Params> std::convert::TryFrom<Request<Params>> for SerializedRequest
where
Params: RpcParam,
{
type Error = serde_json::Error;
fn try_from(value: Request<Params>) -> Result<Self, Self::Error> {
value.serialize()
}
}
impl SerializedRequest {
/// Returns the request metadata (ID and Method).
pub const fn meta(&self) -> &RequestMeta {
&self.meta
}
/// Returns the request ID.
pub const fn id(&self) -> &Id {
&self.meta.id
}
/// Returns the request method.
pub fn method(&self) -> &str {
&self.meta.method
}
/// Mark the request as a non-standard subscription (i.e. not
/// `eth_subscribe`)
pub fn set_is_subscription(&mut self) {
self.meta.set_is_subscription();
}
/// Returns `true` if the request is a subscription.
pub fn is_subscription(&self) -> bool {
self.meta.is_subscription()
}
/// Returns the serialized request.
pub const fn serialized(&self) -> &RawValue {
&self.request
}
/// Consume the serialized request, returning the underlying [`RawValue`].
pub fn into_serialized(self) -> Box<RawValue> {
self.request
}
/// Consumes the serialized request, returning the underlying
/// [`RequestMeta`] and the [`RawValue`].
pub fn decompose(self) -> (RequestMeta, Box<RawValue>) {
(self.meta, self.request)
}
/// Take the serialized request, consuming the [`SerializedRequest`].
pub fn take_request(self) -> Box<RawValue> {
self.request
}
/// Get a reference to the serialized request's params.
///
/// This partially deserializes the request, and should be avoided if
/// possible.
pub fn params(&self) -> Option<&RawValue> {
#[derive(Deserialize)]
struct Req<'a> {
#[serde(borrow)]
params: Option<&'a RawValue>,
}
let req: Req<'_> = serde_json::from_str(self.request.get()).unwrap();
req.params
}
/// Get the hash of the serialized request's params.
///
/// This partially deserializes the request, and should be avoided if
/// possible.
pub fn params_hash(&self) -> B256 {
self.params().map_or_else(|| keccak256(""), |params| keccak256(params.get()))
}
}
impl Serialize for SerializedRequest {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.request.serialize(serializer)
}
}