Skip to content

Commit

Permalink
update Tendermint genesis (#917)
Browse files Browse the repository at this point in the history
* update genesis for 0.34.x

* remove dead code

* fixup! update genesis for 0.34.x

* add .changelog entry

* fixup! fixup! update genesis for 0.34.x

* fixup! update genesis for 0.34.x
  • Loading branch information
tzemanovic authored Jun 25, 2021
1 parent d9668d3 commit c8b97aa
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `[tendermint]` Update Genesis for Tendermint v.0.34.x ([#917](https://github.com/informalsystems/tendermint-rs/pull/917))
4 changes: 1 addition & 3 deletions light-client/src/operations/voting_power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ pub trait VotingPowerCalculator: Send + Sync {
validator_set
.validators()
.iter()
.fold(0u64, |total, val_info| {
total + val_info.voting_power.value()
})
.fold(0u64, |total, val_info| total + val_info.power.value())
}

/// Check against the given threshold that there is enough trust
Expand Down
2 changes: 1 addition & 1 deletion rpc/tests/kvstore_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ fn incoming_fixtures() {
> 0
);
assert!(result.validator_info.pub_key.ed25519().is_some());
assert_eq!(result.validator_info.voting_power.value(), 10);
assert_eq!(result.validator_info.power.value(), 10);
}
"subscribe_malformed" => {
let result = endpoint::subscribe::Response::from_string(content)
Expand Down
2 changes: 1 addition & 1 deletion rpc/tests/parse_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ fn status() {

assert_eq!(response.node_info.network.as_str(), EXAMPLE_CHAIN);
assert_eq!(response.sync_info.latest_block_height.value(), 410_744);
assert_eq!(response.validator_info.voting_power.value(), 0);
assert_eq!(response.validator_info.power.value(), 0);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions rpc/tests/support/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"genesis": {
"genesis_time": "2019-03-13T23:00:00Z",
"chain_id": "cosmoshub-2",
"initial_height": "0",
"consensus_params": {
"block": {
"max_bytes": "200000",
Expand Down
5 changes: 5 additions & 0 deletions tendermint/src/block/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct Size {
/// Maximum amount of gas which can be spent on a block
#[serde(with = "serializers::from_str")]
pub max_gas: i64,

/// This parameter has no value anymore in Tendermint-core
#[serde(with = "serializers::from_str")]
pub time_iota_ms: i64,
}

impl Protobuf<RawSize> for Size {}
Expand All @@ -33,6 +37,7 @@ impl TryFrom<RawSize> for Size {
.try_into()
.map_err(|_| Self::Error::from(Kind::IntegerOverflow))?,
max_gas: value.max_gas,
time_iota_ms: 1000,
})
}
}
Expand Down
28 changes: 7 additions & 21 deletions tendermint/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Genesis data
use crate::serializers;
use crate::{chain, consensus, validator, Time};
use chrono::DateTime;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
use std::convert::TryFrom;
use tendermint_proto::google::protobuf::Timestamp;
use serde::{Deserialize, Serialize};

/// Genesis data
#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -16,6 +13,10 @@ pub struct Genesis<AppState = serde_json::Value> {
/// Chain ID
pub chain_id: chain::Id,

/// Starting height of the blockchain
#[serde(with = "serializers::from_str")]
pub initial_height: i64,

/// Consensus parameters
pub consensus_params: consensus::Params,

Expand All @@ -24,25 +25,10 @@ pub struct Genesis<AppState = serde_json::Value> {
pub validators: Vec<validator::Info>,

/// App hash
#[serde(skip_serializing_if = "Vec::is_empty", with = "serde_bytes")]
#[serde(with = "serializers::bytes::hexstring")]
pub app_hash: Vec<u8>,

/// App state
#[serde(default)]
pub app_state: AppState,
}

/// Deserialize string into Time through Timestamp
pub fn deserialize_time<'de, D>(deserializer: D) -> Result<Time, D::Error>
where
D: Deserializer<'de>,
{
let value_string = String::deserialize(deserializer)?;
let value_datetime = DateTime::parse_from_rfc3339(value_string.as_str())
.map_err(|e| D::Error::custom(format!("{}", e)))?;
Time::try_from(Timestamp {
seconds: value_datetime.timestamp(),
nanos: value_datetime.timestamp_subsec_nanos() as i32,
})
.map_err(|e| D::Error::custom(format!("{}", e)))
}
27 changes: 16 additions & 11 deletions tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Set {
// Compute the total voting power
let total_voting_power = validators
.iter()
.map(|v| v.voting_power.value())
.map(|v| v.power.value())
.sum::<u64>()
.try_into()
.unwrap();
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Set {
/// Sort the validators according to the current Tendermint requirements
/// (v. 0.34 -> first by validator power, descending, then by address, ascending)
fn sort_validators(vals: &mut Vec<Info>) {
vals.sort_by_key(|v| (std::cmp::Reverse(v.voting_power), v.address));
vals.sort_by_key(|v| (std::cmp::Reverse(v.power), v.address));
}

/// Returns the validator with the given Id if its in the Set.
Expand All @@ -143,7 +143,7 @@ impl Set {

/// Validator information
// Todo: Remove address and make it into a function that generates it on the fly from pub_key.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Eq)]
pub struct Info {
/// Validator account address
pub address: account::Id,
Expand All @@ -153,8 +153,11 @@ pub struct Info {

/// Validator voting power
// Compatibility with genesis.json https://github.com/tendermint/tendermint/issues/5549
#[serde(alias = "power", alias = "total_voting_power")]
pub voting_power: vote::Power,
#[serde(alias = "voting_power", alias = "total_voting_power")]
pub power: vote::Power,

/// Validator name
pub name: Option<String>,

/// Validator proposer priority
#[serde(skip)]
Expand All @@ -168,7 +171,8 @@ impl TryFrom<RawValidator> for Info {
Ok(Info {
address: value.address.try_into()?,
pub_key: value.pub_key.ok_or(Kind::MissingPublicKey)?.try_into()?,
voting_power: value.voting_power.try_into()?,
power: value.voting_power.try_into()?,
name: None,
proposer_priority: value.proposer_priority.try_into()?,
})
}
Expand All @@ -179,7 +183,7 @@ impl From<Info> for RawValidator {
RawValidator {
address: value.address.into(),
pub_key: Some(value.pub_key.into()),
voting_power: value.voting_power.into(),
voting_power: value.power.into(),
proposer_priority: value.proposer_priority.into(),
}
}
Expand All @@ -188,7 +192,7 @@ impl From<Info> for RawValidator {
impl Info {
/// Return the voting power of the validator.
pub fn power(&self) -> u64 {
self.voting_power.value()
self.power.value()
}

/// Verify the given signature against the given sign_bytes using the validators
Expand All @@ -214,7 +218,8 @@ impl Info {
Info {
address: account::Id::from(pk),
pub_key: pk,
voting_power: vp,
power: vp,
name: None,
proposer_priority: ProposerPriority::default(),
}
}
Expand Down Expand Up @@ -269,7 +274,7 @@ impl From<&Info> for SimpleValidator {
};
SimpleValidator {
pub_key: Some(tendermint_proto::crypto::PublicKey { sum }),
voting_power: info.voting_power,
voting_power: info.power,
}
}
}
Expand Down Expand Up @@ -409,7 +414,7 @@ mod tests {
22, 57, 84, 71, 122, 200, 169, 192, 252, 41, 148, 223, 180,
];

let val_set = Set::without_proposer(vec![v1, v2, v3]);
let val_set = Set::without_proposer(vec![v1.clone(), v2.clone(), v3.clone()]);
let hash = val_set.hash();
assert_eq!(hash_expect, hash.as_bytes().to_vec());

Expand Down
1 change: 1 addition & 0 deletions testgen/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn default_consensus_params() -> consensus::Params {
block: block::Size {
max_bytes: 22020096,
max_gas: -1, // Tendetmint-go also has TimeIotaMs: 1000, // 1s
time_iota_ms: 1000,
},
evidence: evidence::Params {
max_age_num_blocks: 100000,
Expand Down
7 changes: 4 additions & 3 deletions testgen/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ impl Generator<validator::Info> for Validator {
let info = validator::Info {
address: account::Id::from(keypair.public),
pub_key: PublicKey::from(keypair.public),
voting_power: vote::Power::try_from(self.voting_power.unwrap_or(0)).unwrap(),
power: vote::Power::try_from(self.voting_power.unwrap_or(0)).unwrap(),
name: None,
proposer_priority: validator::ProposerPriority::from(
self.proposer_priority.unwrap_or_default(),
),
Expand All @@ -130,7 +131,7 @@ pub fn sort_validators(vals: &[Validator]) -> Vec<Validator> {
let mut sorted = vals.to_owned();
sorted.sort_by_key(|v| {
let v = v.generate().unwrap();
(std::cmp::Reverse(v.voting_power), v.address)
(std::cmp::Reverse(v.power), v.address)
});
sorted
}
Expand Down Expand Up @@ -186,7 +187,7 @@ mod tests {

let mut block_val = val.generate().unwrap();

block_val.voting_power = vote::Power::from(30_u32);
block_val.power = vote::Power::from(30_u32);
assert_ne!(val.generate().unwrap(), block_val);

let val = val.voting_power(30);
Expand Down
2 changes: 1 addition & 1 deletion tools/kvstore-test/tests/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ mod rpc {
let status = localhost_http_client().status().await.unwrap();

// For lack of better things to test
assert_eq!(status.validator_info.voting_power.value(), 10);
assert_eq!(status.validator_info.power.value(), 10);
}

#[tokio::test]
Expand Down

0 comments on commit c8b97aa

Please sign in to comment.