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

rpc: Allow empty JSON object value for consensus_param_updates in /block_results response #1441

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[tendermint-rpc]` Deserialize an empty JSON object as `None` for the `consensus_param_updates`
field in the `/block_results` response.
([\#1440](https://github.com/informalsystems/tendermint-rs/issues/1440))
2 changes: 2 additions & 0 deletions rpc/src/endpoint/block_results.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! `/block_results` endpoint JSON-RPC wrapper

use serde::{Deserialize, Serialize};
use tendermint::deserializers::allow_empty_object::allow_empty_object;
use tendermint::{abci, block, consensus, serializers, validator, AppHash};

use crate::dialect::{self, Dialect};
Expand Down Expand Up @@ -78,6 +79,7 @@ pub struct Response {
pub validator_updates: Vec<validator::Update>,

/// New consensus params (might be explicit null)
#[serde(deserialize_with = "allow_empty_object")]
pub consensus_param_updates: Option<consensus::Params>,

/// Merkle hash of the application state
Expand Down
8 changes: 5 additions & 3 deletions tendermint/src/consensus/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use serde::{Deserialize, Serialize};

use crate::{block, evidence, prelude::*, public_key};
use crate::{
block, deserializers::allow_empty_object::allow_empty_object, evidence, prelude::*, public_key,
};

/// All consensus-relevant parameters that can be adjusted by the ABCI app.
///
Expand All @@ -15,8 +17,8 @@ pub struct Params {
pub evidence: evidence::Params,
/// Parameters limiting the types of public keys validators can use.
pub validator: ValidatorParams,
/// The ABCI application version.
#[serde(skip)] // FIXME: kvstore /genesis returns '{}' instead of '{app_version: "0"}'
/// The ABCI application version. Will default to None if not present.
#[serde(default, deserialize_with = "allow_empty_object")]
pub version: Option<VersionParams>,
/// Parameters specific to the Application Blockchain Interface.
///
Expand Down
26 changes: 26 additions & 0 deletions tendermint/src/deserializers/allow_empty_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::{Deserialize, Deserializer};

/// Deserialize `null` or an empty object `{}` as `None`.
pub fn allow_empty_object<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
#[derive(Deserialize)]
#[serde(
untagged,
deny_unknown_fields,
expecting = "object, empty object or null"
)]
enum Helper<T> {
Data(T),
Empty {},
Null,
}

match Helper::deserialize(deserializer) {
Ok(Helper::Data(data)) => Ok(Some(data)),
Ok(Helper::Empty {} | Helper::Null) => Ok(None),
Err(e) => Err(e),
}
}
1 change: 1 addition & 0 deletions tendermint/src/deserializers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod allow_empty_object;
1 change: 1 addition & 0 deletions tendermint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod chain;
pub mod channel;
pub mod consensus;
pub mod crypto;
pub mod deserializers;
Copy link
Member

Choose a reason for hiding this comment

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

Let's add it to the serializers instead, to help discovery.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 568f843

pub mod evidence;
pub mod genesis;
pub mod hash;
Expand Down