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)]
mod tests {
use ethers_core::types::U64;
use super::*;
#[test]
@ -247,10 +249,28 @@ mod tests {
}
_ => 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]
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", ());
assert_eq!(
&serde_json::to_string(&request).unwrap(),

View File

@ -149,7 +149,7 @@ impl Provider {
/// let provider = Http::new_with_client(url, client);
/// ```
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 {
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
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