Skip to content

Commit

Permalink
Remove redundant use of num-traits::ToPrimitive for BlockIDFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Mar 19, 2024
1 parent 68a7763 commit 5d23bea
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[tendermint-proto]` Remove redundant impl of `num_traits::ToPrimitive` for `BlockIDFlag`
([\#1389](https://github.com/informalsystems/tendermint-rs/pull/1389))
2 changes: 0 additions & 2 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ bytes = { version = "1.0", default-features = false, features = ["serde"]}
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_bytes = { version = "0.11", default-features = false, features = ["alloc"] }
subtle-encoding = { version = "0.5", default-features = false, features = ["hex", "base64", "alloc"] }
num-traits = { version = "0.2", default-features = false }
num-derive = { version = "0.4", default-features = false }
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }
flex-error = { version = "0.4.4", default-features = false }
tonic = { version = "0.10", optional = true }
Expand Down
1 change: 0 additions & 1 deletion proto/src/prost/v0_34/tendermint.types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ pub struct TxProof {
pub proof: ::core::option::Option<super::crypto::Proof>,
}
/// BlockIdFlag indicates which BlcokID the signature is for
#[derive(::num_derive::FromPrimitive, ::num_derive::ToPrimitive)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum BlockIdFlag {
Expand Down
1 change: 0 additions & 1 deletion proto/src/prost/v0_37/tendermint.types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ pub struct TxProof {
pub proof: ::core::option::Option<super::crypto::Proof>,
}
/// BlockIdFlag indicates which BlcokID the signature is for
#[derive(::num_derive::FromPrimitive, ::num_derive::ToPrimitive)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum BlockIdFlag {
Expand Down
1 change: 0 additions & 1 deletion proto/src/prost/v0_38/tendermint.types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub struct SimpleValidator {
pub voting_power: i64,
}
/// BlockIdFlag indicates which BlockID the signature is for
#[derive(::num_derive::FromPrimitive, ::num_derive::ToPrimitive)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum BlockIdFlag {
Expand Down
13 changes: 6 additions & 7 deletions tendermint/src/block/commit_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ tendermint_pb_modules! {
use super::{CommitSig, ZERO_TIMESTAMP};
use crate::{error::Error, prelude::*, Signature};

use num_traits::ToPrimitive;
use pb::types::{BlockIdFlag, CommitSig as RawCommitSig};

impl TryFrom<RawCommitSig> for CommitSig {
type Error = Error;

fn try_from(value: RawCommitSig) -> Result<Self, Self::Error> {
if value.block_id_flag == BlockIdFlag::Absent.to_i32().unwrap() {
if value.block_id_flag == BlockIdFlag::Absent as i32 {
if value.timestamp.is_some() {
let timestamp = value.timestamp.unwrap();
// 0001-01-01T00:00:00.000Z translates to EPOCH-62135596800 seconds
Expand All @@ -111,7 +110,7 @@ tendermint_pb_modules! {
return Ok(CommitSig::BlockIdFlagAbsent);
}

if value.block_id_flag == BlockIdFlag::Commit.to_i32().unwrap() {
if value.block_id_flag == BlockIdFlag::Commit as i32 {
if value.signature.is_empty() {
return Err(Error::invalid_signature(
"expected non-empty signature for regular commitsig".to_string(),
Expand All @@ -133,7 +132,7 @@ tendermint_pb_modules! {
signature: Signature::new(value.signature)?,
});
}
if value.block_id_flag == BlockIdFlag::Nil.to_i32().unwrap() {
if value.block_id_flag == BlockIdFlag::Nil as i32 {
if value.signature.is_empty() {
return Err(Error::invalid_signature(
"nil commitsig has no signature".to_string(),
Expand All @@ -159,7 +158,7 @@ tendermint_pb_modules! {
fn from(commit: CommitSig) -> RawCommitSig {
match commit {
CommitSig::BlockIdFlagAbsent => RawCommitSig {
block_id_flag: BlockIdFlag::Absent.to_i32().unwrap(),
block_id_flag: BlockIdFlag::Absent as i32,
validator_address: Vec::new(),
timestamp: Some(ZERO_TIMESTAMP),
signature: Vec::new(),
Expand All @@ -169,7 +168,7 @@ tendermint_pb_modules! {
timestamp,
signature,
} => RawCommitSig {
block_id_flag: BlockIdFlag::Nil.to_i32().unwrap(),
block_id_flag: BlockIdFlag::Nil as i32,
validator_address: validator_address.into(),
timestamp: Some(timestamp.into()),
signature: signature.map(|s| s.into_bytes()).unwrap_or_default(),
Expand All @@ -179,7 +178,7 @@ tendermint_pb_modules! {
timestamp,
signature,
} => RawCommitSig {
block_id_flag: BlockIdFlag::Commit.to_i32().unwrap(),
block_id_flag: BlockIdFlag::Commit as i32,
validator_address: validator_address.into(),
timestamp: Some(timestamp.into()),
signature: signature.map(|s| s.into_bytes()).unwrap_or_default(),
Expand Down
2 changes: 0 additions & 2 deletions tools/proto-compiler/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub const TENDERMINT_VERSIONS: &[TendermintVersion] = &[
];

/// Predefined custom attributes for message annotations
const PRIMITIVE_ENUM: &str = r#"#[derive(::num_derive::FromPrimitive, ::num_derive::ToPrimitive)]"#;
const SERIALIZED: &str = r#"#[derive(::serde::Deserialize, ::serde::Serialize)]"#;
const TYPE_TAG: &str = r#"#[serde(tag = "type", content = "value")]"#;

Expand Down Expand Up @@ -86,7 +85,6 @@ pub static CUSTOM_TYPE_ATTRIBUTES: &[(&str, &str)] = &[
(".tendermint.types.ABCIParams", SERIALIZED),
(".tendermint.types.Block", SERIALIZED),
(".tendermint.types.BlockID", SERIALIZED),
(".tendermint.types.BlockIDFlag", PRIMITIVE_ENUM),
(".tendermint.types.BlockMeta", SERIALIZED),
(".tendermint.types.CanonicalBlockID", SERIALIZED),
(".tendermint.types.CanonicalPartSetHeader", SERIALIZED),
Expand Down

0 comments on commit 5d23bea

Please sign in to comment.