Skip to content

Commit

Permalink
tendermint-testgen: Add app_hash field to Header (#1344)
Browse files Browse the repository at this point in the history
* fix: use default app_hash value instead of empty

* imp: add app_hash field to testgen Header

* fix: cargo fmt

* fix: impl custom serde for Option<AppHash>

* fix: use serde default for empty app_hash

* imp: use unwrap_or_default for None variant of AppHash

Co-authored-by: Mikhail Zabaluev <mikhail@informal.systems>

* fix: skip serializing if app_hash is None

* fix: move away from mix serialization for app_hash

* imp: better ser/de impls for Option<AppHash>

* Update testgen/src/header.rs

Co-authored-by: Mikhail Zabaluev <mikhail@informal.systems>

---------

Co-authored-by: Mikhail Zabaluev <mikhail@informal.systems>
  • Loading branch information
Farhad-Shabani and mzabaluev committed Sep 15, 2023
1 parent 1236a93 commit ff7a864
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[tendermint-testgen]` Add `app_hash` field to testgen `Header` and implement
convenient method for default `LightBlock` construction from `Header`
([\#1343](https://github.com/informalsystems/tendermint-rs/issues/1343))
29 changes: 28 additions & 1 deletion testgen/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub struct Header {
pub proposer: Option<usize>,
#[options(help = "last block id hash (default: Hash::None)")]
pub last_block_id_hash: Option<Hash>,
#[options(help = "application hash (default: AppHash(vec![])")]
#[serde(default, with = "app_hash_serde")]
#[serde(skip_serializing_if = "Option::is_none")]
pub app_hash: Option<AppHash>,
}

// Serialize and deserialize time only up to second precision for integration with MBT.
Expand Down Expand Up @@ -64,6 +68,25 @@ where
m_secs.serialize(serializer)
}

// Serialize and deserialize the `Option<AppHash>`, delegating to the `AppHash`
// serialization/deserialization into/from hexstring.
mod app_hash_serde {
use super::*;
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<AppHash>, D::Error>
where
D: Deserializer<'de>,
{
tendermint::serializers::apphash::deserialize(deserializer).map(Some)
}

pub fn serialize<S>(value: &Option<AppHash>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
tendermint::serializers::apphash::serialize(value.as_ref().unwrap(), serializer)
}
}

impl Header {
pub fn new(validators: &[Validator]) -> Self {
Header {
Expand All @@ -74,6 +97,7 @@ impl Header {
time: None,
proposer: None,
last_block_id_hash: None,
app_hash: None,
}
}
set_option!(validators, &[Validator], Some(validators.to_vec()));
Expand All @@ -87,6 +111,7 @@ impl Header {
set_option!(time, Time);
set_option!(proposer, usize);
set_option!(last_block_id_hash, Hash);
set_option!(app_hash, AppHash);

pub fn next(&self) -> Self {
let height = self.height.expect("Missing previous header's height");
Expand All @@ -108,6 +133,7 @@ impl Header {
time: Some((time + Duration::from_secs(1)).unwrap()),
proposer: self.proposer, // TODO: proposer must be incremented
last_block_id_hash: Some(last_block_id_hash),
app_hash: self.app_hash.clone(),
}
}
}
Expand All @@ -133,6 +159,7 @@ impl Generator<block::Header> for Header {
time: self.time.or(default.time),
proposer: self.proposer.or(default.proposer),
last_block_id_hash: self.last_block_id_hash.or(default.last_block_id_hash),
app_hash: self.app_hash.or(default.app_hash),
}
}

Expand Down Expand Up @@ -185,7 +212,7 @@ impl Generator<block::Header> for Header {
validators_hash,
next_validators_hash: next_valset.hash(),
consensus_hash: validators_hash, // TODO: currently not clear how to produce a valid hash
app_hash: AppHash::from_hex_upper("").unwrap(),
app_hash: self.app_hash.clone().unwrap_or_default(),
last_results_hash: None,
evidence_hash: None,
proposer_address,
Expand Down
22 changes: 8 additions & 14 deletions testgen/src/light_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,7 @@ impl LightBlock {
.next_validators(&validators)
.time(Time::from_unix_timestamp(height as i64, 0).unwrap()); // just wanted to initialize time with some value

let commit = Commit::new(header.clone(), 1);

Self {
header: Some(header),
commit: Some(commit),
validators: Some(validators.to_vec()),
next_validators: Some(validators.to_vec()),
provider: Some(default_peer_id()),
}
Self::new_default_with_header(header)
}

pub fn new_default_with_time_and_chain_id(chain_id: String, time: Time, height: u64) -> Self {
Expand All @@ -117,13 +109,15 @@ impl LightBlock {
.next_validators(&validators)
.time(time);

let commit = Commit::new(header.clone(), 1);
Self::new_default_with_header(header)
}

pub fn new_default_with_header(header: Header) -> Self {
Self {
header: Some(header),
commit: Some(commit),
validators: Some(validators.to_vec()),
next_validators: Some(validators.to_vec()),
header: Some(header.clone()),
commit: Some(Commit::new(header.clone(), 1)),
validators: header.validators.clone(),
next_validators: header.validators,
provider: Some(default_peer_id()),
}
}
Expand Down

0 comments on commit ff7a864

Please sign in to comment.