diff --git a/tendermint/src/abci/request/init_chain.rs b/tendermint/src/abci/request/init_chain.rs index 1e943ef33..8632444a8 100644 --- a/tendermint/src/abci/request/init_chain.rs +++ b/tendermint/src/abci/request/init_chain.rs @@ -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; @@ -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, } // ============================================================================= @@ -43,7 +43,7 @@ impl From 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(), } } } @@ -65,7 +65,7 @@ impl TryFrom for InitChain { .map(TryInto::try_into) .collect::>()?, app_state_bytes: init_chain.app_state_bytes, - initial_height: init_chain.initial_height, + initial_height: init_chain.initial_height.try_into()?, }) } } diff --git a/tendermint/src/abci/request/load_snapshot_chunk.rs b/tendermint/src/abci/request/load_snapshot_chunk.rs index 25229f600..b2379d4c7 100644 --- a/tendermint/src/abci/request/load_snapshot_chunk.rs +++ b/tendermint/src/abci/request/load_snapshot_chunk.rs @@ -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. @@ -25,7 +25,7 @@ use tendermint_proto::Protobuf; impl From 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, } @@ -37,7 +37,7 @@ impl TryFrom for LoadSnapshotChunk { fn try_from(load_snapshot_chunk: pb::RequestLoadSnapshotChunk) -> Result { Ok(Self { - height: load_snapshot_chunk.height, + height: load_snapshot_chunk.height.try_into()?, format: load_snapshot_chunk.format, chunk: load_snapshot_chunk.chunk, }) diff --git a/tendermint/src/abci/request/query.rs b/tendermint/src/abci/request/query.rs index e567c5865..98edb2f17 100644 --- a/tendermint/src/abci/request/query.rs +++ b/tendermint/src/abci/request/query.rs @@ -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 { @@ -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, } @@ -44,7 +44,7 @@ impl From for pb::RequestQuery { Self { data: query.data, path: query.path, - height: query.height, + height: query.height.into(), prove: query.prove, } } @@ -57,7 +57,7 @@ impl TryFrom for Query { Ok(Self { data: query.data, path: query.path, - height: query.height, + height: query.height.try_into()?, prove: query.prove, }) } diff --git a/tendermint/src/abci/response/commit.rs b/tendermint/src/abci/response/commit.rs index cfdfa3036..c5c4e9ac7 100644 --- a/tendermint/src/abci/response/commit.rs +++ b/tendermint/src/abci/response/commit.rs @@ -1,4 +1,4 @@ -use crate::prelude::*; +use crate::{block, prelude::*}; use bytes::Bytes; @@ -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, } // ============================================================================= @@ -29,7 +29,7 @@ impl From for pb::ResponseCommit { fn from(commit: Commit) -> Self { Self { data: commit.data, - retain_height: commit.retain_height, + retain_height: commit.retain_height.into(), } } } @@ -40,7 +40,7 @@ impl TryFrom for Commit { fn try_from(commit: pb::ResponseCommit) -> Result { Ok(Self { data: commit.data, - retain_height: commit.retain_height, + retain_height: commit.retain_height.try_into()?, }) } } diff --git a/tendermint/src/abci/response/info.rs b/tendermint/src/abci/response/info.rs index aa3f798e7..90fb5fe00 100644 --- a/tendermint/src/abci/response/info.rs +++ b/tendermint/src/abci/response/info.rs @@ -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, @@ -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, @@ -35,21 +35,21 @@ impl From 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 for Info { - type Error = &'static str; + type Error = Error; fn try_from(info: pb::ResponseInfo) -> Result { 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, }) } diff --git a/tendermint/src/abci/response/query.rs b/tendermint/src/abci/response/query.rs index 9185f205e..9e8f86c76 100644 --- a/tendermint/src/abci/response/query.rs +++ b/tendermint/src/abci/response/query.rs @@ -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)] @@ -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, } @@ -58,7 +57,7 @@ impl From 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, } } @@ -76,7 +75,7 @@ impl TryFrom 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, }) } diff --git a/tendermint/src/abci/types.rs b/tendermint/src/abci/types.rs index f77a36a51..5d3550630 100644 --- a/tendermint/src/abci/types.rs +++ b/tendermint/src/abci/types.rs @@ -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. /// @@ -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. @@ -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. @@ -80,7 +78,7 @@ 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, /// Total voting power of the validator set at `height`. @@ -88,7 +86,7 @@ pub struct Evidence { /// 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. @@ -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 @@ -146,7 +144,7 @@ impl From for pb::Validator { fn from(v: Validator) -> Self { Self { address: Bytes::copy_from_slice(&v.address[..]), - power: v.power, + power: v.power.into(), } } } @@ -165,7 +163,7 @@ impl TryFrom for Validator { Ok(Self { address, - power: vu.power, + power: vu.power.try_into()?, }) } } @@ -176,7 +174,7 @@ impl From for pb::ValidatorUpdate { fn from(vu: ValidatorUpdate) -> Self { Self { pub_key: Some(vu.pub_key.into()), - power: vu.power, + power: vu.power.into(), } } } @@ -187,7 +185,7 @@ impl TryFrom for ValidatorUpdate { fn try_from(vu: pb::ValidatorUpdate) -> Result { Ok(Self { pub_key: vu.pub_key.ok_or("missing public key")?.try_into()?, - power: vu.power, + power: vu.power.try_into()?, }) } } @@ -221,9 +219,9 @@ impl From 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(), } } } @@ -242,9 +240,9 @@ impl TryFrom 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()?, }) } } @@ -280,7 +278,7 @@ impl Protobuf for LastCommitInfo {} impl From 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, @@ -294,7 +292,7 @@ impl TryFrom for Snapshot { fn try_from(snapshot: pb::Snapshot) -> Result { Ok(Self { - height: snapshot.height, + height: snapshot.height.try_into()?, format: snapshot.format, chunks: snapshot.chunks, hash: snapshot.hash, diff --git a/tendermint/src/prelude.rs b/tendermint/src/prelude.rs index 9ff42a989..3aee8bcda 100644 --- a/tendermint/src/prelude.rs +++ b/tendermint/src/prelude.rs @@ -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};