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

workaround for issue in serde_json with arbitrary_precision feature #462

Merged
merged 3 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ jobs:
command: test
toolchain: stable
args: --all --verbose
- name: Testing rust-stable with arbitrary_precision
uses: actions-rs/cargo@master
with:
command: test
toolchain: stable
args: --all --verbose --features arbitrary_precision

## Build Stage
- name: Building rust-stable
Expand Down Expand Up @@ -145,4 +151,4 @@ jobs:
with:
command: check
toolchain: stable
args: --no-default-features --features ipc-tokio
args: --no-default-features --features ipc-tokio
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ ws-async-std = ["soketto", "url", "async-std"]
ws-tls-tokio = ["async-native-tls", "async-native-tls/runtime-tokio", "ws-tokio"]
ws-tls-async-std = ["async-native-tls", "async-native-tls/runtime-async-std", "ws-async-std"]
ipc-tokio = ["tokio"]
arbitrary_precision = ["serde_json/arbitrary_precision", "jsonrpc-core/arbitrary_precision"]
test = []

[workspace]
11 changes: 9 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Web3 helpers.

use crate::{error, rpc};
use crate::{error, rpc, Error};
use futures::{
task::{Context, Poll},
Future,
Expand Down Expand Up @@ -68,8 +68,15 @@ pub fn build_request(id: usize, method: &str, params: Vec<rpc::Value>) -> rpc::C
}

/// Parse bytes slice into JSON-RPC response.
/// It looks for arbitrary_precision feature as a temporary workaround for https://github.com/tomusdrw/rust-web3/issues/460.
pub fn to_response_from_slice(response: &[u8]) -> error::Result<rpc::Response> {
serde_json::from_slice(response).map_err(|e| error::Error::InvalidResponse(format!("{:?}", e)))
if cfg!(feature = "arbitrary_precision") {
let val: serde_json::Value =
serde_json::from_slice(response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))?;
serde_json::from_value(val).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))
} else {
serde_json::from_slice(response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))
}
}

/// Parse bytes slice into JSON-RPC notification.
Expand Down
8 changes: 4 additions & 4 deletions src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ impl BatchTransport for Http {

/// Parse bytes RPC response into `Result`.
fn single_response<T: Deref<Target = [u8]>>(response: T) -> error::Result<rpc::Value> {
let response = serde_json::from_slice(&*response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))?;

let response =
helpers::to_response_from_slice(&*response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))?;
match response {
rpc::Response::Single(output) => helpers::to_result_from_output(output),
_ => Err(Error::InvalidResponse("Expected single, got batch.".into())),
Expand All @@ -200,8 +200,8 @@ fn single_response<T: Deref<Target = [u8]>>(response: T) -> error::Result<rpc::V

/// Parse bytes RPC batch response into `Result`.
fn batch_response<T: Deref<Target = [u8]>>(response: T) -> error::Result<Vec<error::Result<rpc::Value>>> {
let response = serde_json::from_slice(&*response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))?;

let response =
helpers::to_response_from_slice(&*response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))?;
match response {
rpc::Response::Batch(outputs) => Ok(outputs.into_iter().map(helpers::to_result_from_output).collect()),
_ => Err(Error::InvalidResponse("Expected batch, got single.".into())),
Expand Down
2 changes: 1 addition & 1 deletion src/transports/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ mod test {
while let Some(Ok(bytes)) = rx.next().await {
buf.extend(bytes);

let requests: std::result::Result<Vec<rpc::Call>, serde_json::Error> =
let requests: std::result::Result<Vec<serde_json::Value>, serde_json::Error> =
serde_json::Deserializer::from_slice(&buf).into_iter().collect();

if let Ok(requests) = requests {
Expand Down