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

tendermint-testgen: Add app_hash field to Header #1344

Merged
merged 10 commits into from
Sep 15, 2023
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))
4 changes: 3 additions & 1 deletion p2p/src/secret_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl Handshake<AwaitingEphKey> {
&mut self,
remote_eph_pubkey: EphemeralPublic,
) -> Result<Handshake<AwaitingAuthSig>, Error> {
let Some(local_eph_privkey) = self.state.local_eph_privkey.take() else { return Err(Error::missing_secret()) };
let Some(local_eph_privkey) = self.state.local_eph_privkey.take() else {
return Err(Error::missing_secret());
};
Farhad-Shabani marked this conversation as resolved.
Show resolved Hide resolved
let local_eph_pubkey = EphemeralPublic::from(&local_eph_privkey);

// Compute common shared secret.
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub mod allow_empty {
}

/// AppHash is usually a SHA256 hash, but in reality it can be any kind of data
#[derive(Clone, PartialEq, Eq, Default)]
#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
Farhad-Shabani marked this conversation as resolved.
Show resolved Hide resolved
pub struct AppHash(Vec<u8>);

impl Protobuf<Vec<u8>> for AppHash {}
Expand Down
13 changes: 12 additions & 1 deletion testgen/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ 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 = "last commit hash (default: AppHash(vec![])")]
pub app_hash: Option<AppHash>,
}

// Serialize and deserialize time only up to second precision for integration with MBT.
Expand Down Expand Up @@ -74,6 +76,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 +90,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 +112,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 +138,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 @@ -171,6 +177,11 @@ impl Generator<block::Header> for Header {
part_set_header: Default::default(),
});

let app_hash = self
.app_hash
.clone()
.unwrap_or(AppHash::from_hex_upper("").map_err(|e| SimpleError::new(e.to_string()))?);
Farhad-Shabani marked this conversation as resolved.
Show resolved Hide resolved

let header = block::Header {
// block version in Tendermint-go is hardcoded with value 11
// so we do the same with MBT for now for compatibility
Expand All @@ -185,7 +196,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,
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 {
mzabaluev marked this conversation as resolved.
Show resolved Hide resolved
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