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

proto: Add missing serde derives on Protobuf definitions #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[cometbft-proto]` Remove redundant impl of `num_traits::ToPrimitive` for `BlockIDFlag`
([\#]())
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1389-missing-serde-derives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[cometbft-proto]` Add missing `serde` derives on Protobuf definitions
([\#]())
14 changes: 6 additions & 8 deletions cometbft/src/block/commit_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ mod v1 {
use crate::{error::Error, prelude::*, Signature};
use cometbft_proto::types::v1::{self as pb, BlockIdFlag};

use num_traits::ToPrimitive;

impl TryFrom<pb::CommitSig> for CommitSig {
type Error = Error;

fn try_from(value: pb::CommitSig) -> 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 +109,7 @@ mod v1 {
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 +131,7 @@ mod v1 {
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 +157,7 @@ mod v1 {
fn from(commit: CommitSig) -> pb::CommitSig {
match commit {
CommitSig::BlockIdFlagAbsent => pb::CommitSig {
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 +167,7 @@ mod v1 {
timestamp,
signature,
} => pb::CommitSig {
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 +177,7 @@ mod v1 {
timestamp,
signature,
} => pb::CommitSig {
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 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
24 changes: 11 additions & 13 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,30 @@

extern crate alloc;

mod prelude;

/// Built-in prost_types with slight customization to enable JSON-encoding
#[allow(warnings)]
pub mod google {
pub mod protobuf {
// custom Timeout and Duration types that have valid doctest documentation texts
include!("protobuf.rs");
}
}

#[allow(warnings)]
mod cometbft;
mod error;
mod prelude;

use bytes::{Buf, BufMut};
use core::{convert::TryFrom, fmt::Display};
use prost::Message;

use bytes::{Buf, BufMut};
pub use cometbft::*;
pub use error::Error;
use prost::Message;

pub mod serializers;

use prelude::*;

/// Built-in prost_types with slight customization to enable JSON-encoding
pub mod google {
pub mod protobuf {
// custom Timeout and Duration types that have valid doctest documentation texts
include!("protobuf.rs");
}
}

/// Allows for easy Google Protocol Buffers encoding and decoding of domain
/// types with validation.
///
Expand Down
4 changes: 2 additions & 2 deletions proto/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Re-export according to alloc::prelude::v1 because it is not yet stabilized
// https://doc.rust-lang.org/src/alloc/prelude/v1.rs.html

#[allow(unused_imports)]
pub use alloc::vec;
#![allow(unused_imports)]
pub use alloc::{
borrow::ToOwned,
format,
string::{String, ToString},
vec,
vec::Vec,
};
pub use core::prelude::v1::*;
Loading
Loading