fix: all request ids start at 1 (#1265)

Co-authored-by: Oliver Giersch <oliver.giersch@gmail.com>
This commit is contained in:
oliver-giersch 2022-05-15 01:31:40 +02:00 committed by GitHub
parent f3699d08bf
commit 3df1527cef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -212,6 +212,8 @@ impl fmt::Display for Authorization {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ethers_core::types::U64;
use super::*; use super::*;
#[test] #[test]
@ -247,10 +249,28 @@ mod tests {
} }
_ => panic!("expected `Error` response"), _ => panic!("expected `Error` response"),
} }
let response: Response<'_> =
serde_json::from_str(r#"{"jsonrpc":"2.0","result":"0xfa","id":0}"#).unwrap();
match response {
Response::Success { id, result } => {
assert_eq!(id, 0);
let result: U64 = serde_json::from_str(result.get()).unwrap();
assert_eq!(result.as_u64(), 250);
}
_ => panic!("expected `Success` response"),
}
} }
#[test] #[test]
fn ser_request() { fn ser_request() {
let request: Request<()> = Request::new(0, "eth_chainId", ());
assert_eq!(
&serde_json::to_string(&request).unwrap(),
r#"{"id":0,"jsonrpc":"2.0","method":"eth_chainId"}"#
);
let request: Request<()> = Request::new(300, "method_name", ()); let request: Request<()> = Request::new(300, "method_name", ());
assert_eq!( assert_eq!(
&serde_json::to_string(&request).unwrap(), &serde_json::to_string(&request).unwrap(),

View File

@ -149,7 +149,7 @@ impl Provider {
/// let provider = Http::new_with_client(url, client); /// let provider = Http::new_with_client(url, client);
/// ``` /// ```
pub fn new_with_client(url: impl Into<Url>, client: reqwest::Client) -> Self { pub fn new_with_client(url: impl Into<Url>, client: reqwest::Client) -> Self {
Self { id: AtomicU64::new(0), client, url: url.into() } Self { id: AtomicU64::new(1), client, url: url.into() }
} }
} }
@ -164,7 +164,7 @@ impl FromStr for Provider {
impl Clone for Provider { impl Clone for Provider {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { id: AtomicU64::new(0), client: self.client.clone(), url: self.url.clone() } Self { id: AtomicU64::new(1), client: self.client.clone(), url: self.url.clone() }
} }
} }

View File

@ -114,7 +114,7 @@ impl Ws {
// Spawn the server // Spawn the server
WsServer::new(ws, stream).spawn(); WsServer::new(ws, stream).spawn();
Self { id: Arc::new(AtomicU64::new(0)), instructions: sink } Self { id: Arc::new(AtomicU64::new(1)), instructions: sink }
} }
/// Returns true if the WS connection is active, false otherwise /// Returns true if the WS connection is active, false otherwise