Skip to content

Commit

Permalink
Fix rpc response deserialization (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jul 11, 2020
1 parent 624e997 commit 337b052
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
18 changes: 9 additions & 9 deletions rpc/tests/support/block_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"code": "0",
"log": "[{\"msg_index\":\"0\",\"success\":true,\"log\":\"\"}]",
"info": "",
"gas_wanted": "200000",
"gas_used": "105662",
"data": "",
"gasWanted": "200000",
"gasUsed": "105662",
"data": null,
"events": [
{
"type": "someevent1",
Expand All @@ -36,9 +36,9 @@
"code": "0",
"log": "[{\"msg_index\":\"0\",\"success\":true,\"log\":\"\"}]",
"info": "",
"gas_wanted": "99164",
"gas_used": "99164",
"data": "",
"gasWanted": "99164",
"gasUsed": "99164",
"data": null,
"events": [
{
"type": "someevent2",
Expand All @@ -64,9 +64,9 @@
"code": "0",
"log": "[{\"msg_index\":\"0\",\"success\":true,\"log\":\"\"}]",
"info": "",
"gas_wanted": "200000",
"gas_used": "106515",
"data": "",
"gasWanted": "200000",
"gasUsed": "106515",
"data": null,
"events": [
{
"type": "someeventtype1",
Expand Down
5 changes: 4 additions & 1 deletion tendermint/src/abci/responses.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! ABCI response types used by the `/block_results` RPC endpoint.

use super::{code::Code, data::Data, gas::Gas, info::Info, log::Log, tag::Tag};
use crate::{consensus, validator};
use crate::{consensus, serializers, validator};
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt::{self, Display};

Expand Down Expand Up @@ -50,6 +50,7 @@ pub struct DeliverTx {
pub code: Code,

/// ABCI application data
#[serde(deserialize_with = "serializers::null_as_default")]
pub data: Data,

/// ABCI log data (nondeterministic)
Expand All @@ -59,9 +60,11 @@ pub struct DeliverTx {
pub info: Info,

/// Amount of gas wanted
#[serde(rename = "gasWanted")]
pub gas_wanted: Gas,

/// Amount of gas used
#[serde(rename = "gasUsed")]
pub gas_used: Gas,

/// Events
Expand Down
1 change: 1 addition & 0 deletions tendermint/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ pub(crate) use raw_commit_sig::RawCommitSig;
mod tests;

mod custom;
pub use custom::null_as_default;
pub use custom::parse_non_empty_block_id;
pub use custom::parse_non_empty_hash;
10 changes: 10 additions & 0 deletions tendermint/src/serializers/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ where
Ok(None)
}
}

/// Parse null as default
pub fn null_as_default<'de, D, T: Default + Deserialize<'de>>(
deserializer: D,
) -> Result<T, D::Error>
where
D: Deserializer<'de>,
{
Ok(<Option<T>>::deserialize(deserializer)?.unwrap_or_default())
}

0 comments on commit 337b052

Please sign in to comment.