Skip to content

Commit

Permalink
fix: all request ids start at 1 (#1265)
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Giersch <oliver.giersch@gmail.com>
  • Loading branch information
oliver-giersch and Oliver Giersch committed May 14, 2022
1 parent f3699d0 commit 3df1527
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
20 changes: 20 additions & 0 deletions ethers-providers/src/transports/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ impl fmt::Display for Authorization {

#[cfg(test)]
mod tests {
use ethers_core::types::U64;

use super::*;

#[test]
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions ethers-providers/src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
}

Expand All @@ -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() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion ethers-providers/src/transports/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3df1527

Please sign in to comment.