Skip to content

Commit

Permalink
Replace integers with block::Height, vote::Power
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalence committed Nov 15, 2021
1 parent 1acc5b3 commit 243452d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 46 deletions.
8 changes: 4 additions & 4 deletions tendermint/src/abci/request/init_chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bytes::Bytes;
use chrono::{DateTime, Utc};

use crate::{consensus, prelude::*};
use crate::{block, consensus, prelude::*};

use super::super::types::ValidatorUpdate;

Expand All @@ -21,7 +21,7 @@ pub struct InitChain {
/// Serialized JSON bytes containing the initial application state.
pub app_state_bytes: Bytes,
/// Height of the initial block (typically `1`).
pub initial_height: i64,
pub initial_height: block::Height,
}

// =============================================================================
Expand All @@ -43,7 +43,7 @@ impl From<InitChain> for pb::RequestInitChain {
consensus_params: Some(init_chain.consensus_params.into()),
validators: init_chain.validators.into_iter().map(Into::into).collect(),
app_state_bytes: init_chain.app_state_bytes,
initial_height: init_chain.initial_height,
initial_height: init_chain.initial_height.into(),
}
}
}
Expand All @@ -65,7 +65,7 @@ impl TryFrom<pb::RequestInitChain> for InitChain {
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
app_state_bytes: init_chain.app_state_bytes,
initial_height: init_chain.initial_height,
initial_height: init_chain.initial_height.try_into()?,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions tendermint/src/abci/request/load_snapshot_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::prelude::*;
use crate::{block, prelude::*};

#[doc = include_str!("../doc/request-loadsnapshotchunk.md")]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct LoadSnapshotChunk {
/// The height of the snapshot the chunks belong to.
pub height: u64,
pub height: block::Height,
/// An application-specific identifier of the format of the snapshot chunk.
pub format: u32,
/// The chunk index, starting from `0` for the initial chunk.
Expand All @@ -25,7 +25,7 @@ use tendermint_proto::Protobuf;
impl From<LoadSnapshotChunk> for pb::RequestLoadSnapshotChunk {
fn from(load_snapshot_chunk: LoadSnapshotChunk) -> Self {
Self {
height: load_snapshot_chunk.height,
height: load_snapshot_chunk.height.into(),
format: load_snapshot_chunk.format,
chunk: load_snapshot_chunk.chunk,
}
Expand All @@ -37,7 +37,7 @@ impl TryFrom<pb::RequestLoadSnapshotChunk> for LoadSnapshotChunk {

fn try_from(load_snapshot_chunk: pb::RequestLoadSnapshotChunk) -> Result<Self, Self::Error> {
Ok(Self {
height: load_snapshot_chunk.height,
height: load_snapshot_chunk.height.try_into()?,
format: load_snapshot_chunk.format,
chunk: load_snapshot_chunk.chunk,
})
Expand Down
10 changes: 5 additions & 5 deletions tendermint/src/abci/request/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;

use bytes::Bytes;

use crate::{block, prelude::*};

#[doc = include_str!("../doc/request-query.md")]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Query {
Expand All @@ -23,7 +23,7 @@ pub struct Query {
/// this is the height of the block containing the application's Merkle root
/// hash, which represents the state as it was after committing the block at
/// `height - 1`.
pub height: i64,
pub height: block::Height,
/// Whether to return a Merkle proof with the response, if possible.
pub prove: bool,
}
Expand All @@ -44,7 +44,7 @@ impl From<Query> for pb::RequestQuery {
Self {
data: query.data,
path: query.path,
height: query.height,
height: query.height.into(),
prove: query.prove,
}
}
Expand All @@ -57,7 +57,7 @@ impl TryFrom<pb::RequestQuery> for Query {
Ok(Self {
data: query.data,
path: query.path,
height: query.height,
height: query.height.try_into()?,
prove: query.prove,
})
}
Expand Down
8 changes: 4 additions & 4 deletions tendermint/src/abci/response/commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{block, prelude::*};

use bytes::Bytes;

Expand All @@ -11,7 +11,7 @@ pub struct Commit {
/// XXX(hdevalence) - rename to app_hash ?
pub data: Bytes,
/// Blocks below this height may be removed.
pub retain_height: i64,
pub retain_height: block::Height,
}

// =============================================================================
Expand All @@ -29,7 +29,7 @@ impl From<Commit> for pb::ResponseCommit {
fn from(commit: Commit) -> Self {
Self {
data: commit.data,
retain_height: commit.retain_height,
retain_height: commit.retain_height.into(),
}
}
}
Expand All @@ -40,7 +40,7 @@ impl TryFrom<pb::ResponseCommit> for Commit {
fn try_from(commit: pb::ResponseCommit) -> Result<Self, Self::Error> {
Ok(Self {
data: commit.data,
retain_height: commit.retain_height,
retain_height: commit.retain_height.try_into()?,
})
}
}
Expand Down
12 changes: 6 additions & 6 deletions tendermint/src/abci/response/info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::{block, prelude::*, Error};

use bytes::Bytes;

#[doc = include_str!("../doc/response-info.md")]
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Info {
/// Some arbitrary information.
pub data: String,
Expand All @@ -12,7 +12,7 @@ pub struct Info {
/// The application protocol version.
pub app_version: u64,
/// The latest block for which the app has called [`Commit`](super::super::Request::Commit).
pub last_block_height: i64,
pub last_block_height: block::Height,
/// The latest result of [`Commit`](super::super::Request::Commit).
// XXX(hdevalence): fix this, should be apphash?
pub last_block_app_hash: Bytes,
Expand All @@ -35,21 +35,21 @@ impl From<Info> for pb::ResponseInfo {
data: info.data,
version: info.version,
app_version: info.app_version,
last_block_height: info.last_block_height,
last_block_height: info.last_block_height.into(),
last_block_app_hash: info.last_block_app_hash,
}
}
}

impl TryFrom<pb::ResponseInfo> for Info {
type Error = &'static str;
type Error = Error;

fn try_from(info: pb::ResponseInfo) -> Result<Self, Self::Error> {
Ok(Self {
data: info.data,
version: info.version,
app_version: info.app_version,
last_block_height: info.last_block_height,
last_block_height: info.last_block_height.try_into()?,
last_block_app_hash: info.last_block_app_hash,
})
}
Expand Down
9 changes: 4 additions & 5 deletions tendermint/src/abci/response/query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::prelude::*;

use bytes::Bytes;

/// XXX(hdevalence): hide merkle::proof and re-export its contents from merkle?
use crate::merkle::proof as merkle;
use crate::{block, prelude::*};

#[doc = include_str!("../doc/response-query.md")]
#[derive(Clone, PartialEq, Eq, Debug, Default)]
Expand Down Expand Up @@ -32,7 +31,7 @@ pub struct Query {
/// Note that this is the height of the block containing the application's
/// Merkle root hash, which represents the state as it was after committing
/// the block at `height - 1`.
pub height: i64,
pub height: block::Height,
/// The namespace for the `code`.
pub codespace: String,
}
Expand All @@ -58,7 +57,7 @@ impl From<Query> for pb::ResponseQuery {
key: query.key,
value: query.value,
proof_ops: query.proof.map(Into::into),
height: query.height,
height: query.height.into(),
codespace: query.codespace,
}
}
Expand All @@ -76,7 +75,7 @@ impl TryFrom<pb::ResponseQuery> for Query {
key: query.key,
value: query.value,
proof: query.proof_ops.map(TryInto::try_into).transpose()?,
height: query.height,
height: query.height.try_into()?,
codespace: query.codespace,
})
}
Expand Down
34 changes: 16 additions & 18 deletions tendermint/src/abci/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
//!
//! [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#data-types)

use crate::prelude::*;

use core::convert::{TryFrom, TryInto};

use bytes::Bytes;
use chrono::{DateTime, Utc};

use crate::PublicKey;
use crate::{block, prelude::*, vote, PublicKey};

/// A validator address with voting power.
///
Expand All @@ -22,7 +20,7 @@ pub struct Validator {
/// The validator's address (the first 20 bytes of `SHA256(public_key)`).
pub address: [u8; 20],
/// The voting power of the validator.
pub power: i64,
pub power: vote::Power,
}

/// A change to the validator set.
Expand All @@ -35,7 +33,7 @@ pub struct ValidatorUpdate {
/// The validator's public key.
pub pub_key: PublicKey,
/// The validator's voting power.
pub power: i64,
pub power: vote::Power,
}

/// Information about a whether a validator signed the last block.
Expand Down Expand Up @@ -80,15 +78,15 @@ pub struct Evidence {
/// The offending validator.
pub validator: Validator,
/// The height when the offense occurred.
pub height: i64,
pub height: block::Height,
/// The corresponding time when the offense occurred.
pub time: DateTime<Utc>,
/// Total voting power of the validator set at `height`.
///
/// This is included in case the ABCI application does not store historical
/// validators, cf.
/// [#4581](https://github.com/tendermint/tendermint/issues/4581)
pub total_voting_power: i64,
pub total_voting_power: vote::Power,
}

/// Information on the last block commit.
Expand All @@ -114,7 +112,7 @@ pub struct LastCommitInfo {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Snapshot {
/// The height at which the snapshot was taken
pub height: u64,
pub height: block::Height,
/// The application-specific snapshot format identifier.
///
/// This allows applications to version their snapshot data format and make
Expand Down Expand Up @@ -146,7 +144,7 @@ impl From<Validator> for pb::Validator {
fn from(v: Validator) -> Self {
Self {
address: Bytes::copy_from_slice(&v.address[..]),
power: v.power,
power: v.power.into(),
}
}
}
Expand All @@ -165,7 +163,7 @@ impl TryFrom<pb::Validator> for Validator {

Ok(Self {
address,
power: vu.power,
power: vu.power.try_into()?,
})
}
}
Expand All @@ -176,7 +174,7 @@ impl From<ValidatorUpdate> for pb::ValidatorUpdate {
fn from(vu: ValidatorUpdate) -> Self {
Self {
pub_key: Some(vu.pub_key.into()),
power: vu.power,
power: vu.power.into(),
}
}
}
Expand All @@ -187,7 +185,7 @@ impl TryFrom<pb::ValidatorUpdate> for ValidatorUpdate {
fn try_from(vu: pb::ValidatorUpdate) -> Result<Self, Self::Error> {
Ok(Self {
pub_key: vu.pub_key.ok_or("missing public key")?.try_into()?,
power: vu.power,
power: vu.power.try_into()?,
})
}
}
Expand Down Expand Up @@ -221,9 +219,9 @@ impl From<Evidence> for pb::Evidence {
Self {
r#type: evidence.kind as i32,
validator: Some(evidence.validator.into()),
height: evidence.height,
height: evidence.height.into(),
time: Some(evidence.time.into()),
total_voting_power: evidence.total_voting_power,
total_voting_power: evidence.total_voting_power.into(),
}
}
}
Expand All @@ -242,9 +240,9 @@ impl TryFrom<pb::Evidence> for Evidence {
Ok(Self {
kind,
validator: evidence.validator.ok_or("missing validator")?.try_into()?,
height: evidence.height,
height: evidence.height.try_into()?,
time: evidence.time.ok_or("missing time")?.into(),
total_voting_power: evidence.total_voting_power,
total_voting_power: evidence.total_voting_power.try_into()?,
})
}
}
Expand Down Expand Up @@ -280,7 +278,7 @@ impl Protobuf<pb::LastCommitInfo> for LastCommitInfo {}
impl From<Snapshot> for pb::Snapshot {
fn from(snapshot: Snapshot) -> Self {
Self {
height: snapshot.height,
height: snapshot.height.into(),
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
Expand All @@ -294,7 +292,7 @@ impl TryFrom<pb::Snapshot> for Snapshot {

fn try_from(snapshot: pb::Snapshot) -> Result<Self, Self::Error> {
Ok(Self {
height: snapshot.height,
height: snapshot.height.try_into()?,
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
Expand Down
3 changes: 3 additions & 0 deletions tendermint/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ pub use alloc::vec::Vec;

pub use alloc::format;
pub use alloc::vec;

// will be included in 2021 edition.
pub use core::convert::{TryFrom, TryInto};

0 comments on commit 243452d

Please sign in to comment.