Skip to content

Commit

Permalink
Generic AppState for genesis() (informalsystems#1107)
Browse files Browse the repository at this point in the history
* Allow users to supply generic param AppState in genesis()

* Add changelog entry

* Fix tests

* Fix clippy errors
  • Loading branch information
hu55a1n1 authored and james-chf committed Sep 30, 2022
1 parent b4ffeed commit a64e802
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/1106-genesis-app-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[tendermint-rpc]` Allow users to specify the `AppState` type in the `Client::genesis()` function.
([#1106](https://github.com/informalsystems/tendermint-rs/issues/1106))
10 changes: 8 additions & 2 deletions rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use crate::prelude::*;
use crate::query::Query;
use crate::{Error, Order, SimpleRequest};
use async_trait::async_trait;
use core::fmt;
use core::time::Duration;
use serde::de::DeserializeOwned;
use serde::Serialize;
use tendermint::abci::{self, Transaction};
use tendermint::block::Height;
use tendermint::evidence::Evidence;
Expand Down Expand Up @@ -229,8 +232,11 @@ pub trait Client {
}

/// `/genesis`: get genesis file.
async fn genesis(&self) -> Result<Genesis, Error> {
Ok(self.perform(genesis::Request).await?.genesis)
async fn genesis<AppState>(&self) -> Result<Genesis<AppState>, Error>
where
AppState: fmt::Debug + Serialize + DeserializeOwned + Send,
{
Ok(self.perform(genesis::Request::default()).await?.genesis)
}

/// `/net_info`: obtain information about P2P and other network connections.
Expand Down
3 changes: 2 additions & 1 deletion rpc/src/client/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ where
serde_json::to_string_pretty(&client.consensus_state().await?).map_err(Error::serde)?
}
ClientRequest::Genesis => {
serde_json::to_string_pretty(&client.genesis().await?).map_err(Error::serde)?
serde_json::to_string_pretty(&client.genesis::<serde_json::Value>().await?)
.map_err(Error::serde)?
}
ClientRequest::Health => {
serde_json::to_string_pretty(&client.health().await?).map_err(Error::serde)?
Expand Down
29 changes: 21 additions & 8 deletions rpc/src/endpoint/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
//! `/genesis` endpoint JSON-RPC wrapper

use serde::{Deserialize, Serialize};
use core::{fmt, marker::PhantomData};

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tendermint::Genesis;

/// Get the genesis state for the current chain
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Request;
pub struct Request<AppState>(#[serde(skip)] PhantomData<AppState>);

impl crate::Request for Request {
type Response = Response;
impl<AppState> Default for Request<AppState> {
fn default() -> Self {
Self(PhantomData)
}
}

impl<AppState> crate::Request for Request<AppState>
where
AppState: fmt::Debug + Serialize + DeserializeOwned + Send,
{
type Response = Response<AppState>;

fn method(&self) -> crate::Method {
crate::Method::Genesis
}
}

impl crate::SimpleRequest for Request {}
impl<AppState> crate::SimpleRequest for Request<AppState> where
AppState: fmt::Debug + Serialize + DeserializeOwned + Send
{
}

/// Block responses
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
pub struct Response<AppState> {
/// Genesis data
pub genesis: Genesis,
pub genesis: Genesis<AppState>,
}

impl crate::Response for Response {}
impl<AppState> crate::Response for Response<AppState> where AppState: Serialize + DeserializeOwned {}
9 changes: 7 additions & 2 deletions rpc/tests/gaia_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ fn incoming_fixtures() {
assert!(endpoint::consensus_state::Response::from_string(content).is_ok())
}
"genesis" => {
assert!(endpoint::genesis::Response::from_string(content).is_ok())
assert!(
endpoint::genesis::Response::<serde_json::Value>::from_string(content).is_ok()
)
}
"net_info" => {
assert!(endpoint::net_info::Response::from_string(content).is_ok())
Expand Down Expand Up @@ -145,7 +147,10 @@ fn outgoing_fixtures() {
assert!(endpoint::consensus_state::Request::from_string(content).is_ok())
}
"genesis" => {
assert!(endpoint::genesis::Request::from_string(content).is_ok())
assert!(
endpoint::genesis::Request::<Option<serde_json::Value>>::from_string(content)
.is_ok()
)
}
"net_info" => {
assert!(endpoint::net_info::Request::from_string(content).is_ok())
Expand Down
12 changes: 7 additions & 5 deletions rpc/tests/kvstore_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ fn outgoing_fixtures() {
RequestWrapper<endpoint::consensus_state::Request>,
>(&content)
.is_ok()),
"genesis" => assert!(
serde_json::from_str::<RequestWrapper<endpoint::genesis::Request>>(&content)
.is_ok()
),
"genesis" => assert!(serde_json::from_str::<
RequestWrapper<endpoint::genesis::Request::<serde_json::Value>>,
>(&content)
.is_ok()),
"net_info" => assert!(serde_json::from_str::<
RequestWrapper<endpoint::net_info::Request>,
>(&content)
Expand Down Expand Up @@ -726,7 +726,9 @@ fn incoming_fixtures() {
assert!(endpoint::consensus_state::Response::from_string(content).is_ok());
}
"genesis" => {
let result = endpoint::genesis::Response::from_string(content).unwrap();
let result =
endpoint::genesis::Response::<Option<serde_json::Value>>::from_string(content)
.unwrap();
assert!(result.genesis.app_hash.is_empty());
assert_eq!(result.genesis.chain_id.as_str(), CHAIN_ID);
assert_eq!(result.genesis.consensus_params.block.max_bytes, 22020096);
Expand Down
1 change: 0 additions & 1 deletion tendermint/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ pub struct Genesis<AppState = serde_json::Value> {
pub app_hash: Vec<u8>,

/// App state
#[serde(default)]
pub app_state: AppState,
}
1 change: 1 addition & 0 deletions tools/kvstore-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tokio = { version = "1.0", features = [ "rt-multi-thread", "macros" ] }
tracing = "0.1"
tracing-subscriber = "0.2"
contracts = "0.4.0"
serde_json = "1"
2 changes: 1 addition & 1 deletion tools/kvstore-test/tests/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod rpc {
/// `/genesis` endpoint
#[tokio::test]
async fn genesis() {
let genesis = localhost_http_client().genesis().await.unwrap(); // https://github.com/tendermint/tendermint/issues/5549
let genesis = localhost_http_client().genesis::<Option<serde_json::Value>>().await.unwrap(); // https://github.com/tendermint/tendermint/issues/5549

assert_eq!(
genesis.consensus_params.validator.pub_key_types[0].to_string(),
Expand Down

0 comments on commit a64e802

Please sign in to comment.