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

abci: fix rpc response deserialization #432

Merged
merged 1 commit into from
Jul 11, 2020
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
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")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid all of these field attribute by putting a #[serde(rename_all = "camelCase")] on the struct. See the section in the container attribute documentation.

Copy link
Contributor Author

@yihuang yihuang Jul 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the camelCase here is a special case, because other places tendermint actually use a_b format.

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())
}