Skip to content

Commit

Permalink
feat: add arbitrary to some types
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo authored and KaiserKarel committed May 2, 2024
1 parent 3b4027f commit 0bbfe8a
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 31 deletions.
2 changes: 2 additions & 0 deletions tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ ed25519-consensus = { version = "2", optional = true, default-features = false }
sha2 = { version = "0.10", optional = true, default-features = false }
k256 = { version = "0.13", optional = true, default-features = false, features = ["alloc", "ecdsa"] }
ripemd = { version = "0.1.3", optional = true, default-features = false }
arbitrary = { version = "1.3.2", features = ["derive"], optional = true }

[features]
default = ["std", "rust-crypto"]
std = ["flex-error/std", "clock"]
clock = ["time/std"]
secp256k1 = ["k256", "ripemd"]
rust-crypto = ["sha2", "ed25519-consensus"]
arbitrary = ["dep:arbitrary"]

[dev-dependencies]
k256 = { version = "0.13", default-features = false, features = ["ecdsa"] }
Expand Down
32 changes: 1 addition & 31 deletions tendermint/src/abci/event.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
use serde::{Deserialize, Serialize, Deserializer};
use serde::{Deserialize, Serialize};

use crate::prelude::*;

fn deserialize_null_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}


/// An event that occurred while processing a request.
///
/// Application developers can attach additional information to
Expand Down Expand Up @@ -200,26 +190,6 @@ where
}
}

/// A key-value pair describing an [`Event`].
///
/// Generic methods are provided for more ergonomic attribute construction, see
/// [`Event::new`] for details.
///
/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#events)
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Hash)]
pub struct EventAttribute {
/// The event key.
pub key: String,

/// The event value.
#[serde(deserialize_with = "deserialize_null_default")]
pub value: String,
/// Whether Tendermint's indexer should index this event.
///
/// **This field is nondeterministic**.
pub index: bool,
}

impl EventAttribute {
/// Checks whether `&self` is equal to `other`, ignoring the `index` field.
pub fn eq_ignoring_index(&self, other: &Self) -> bool {
Expand Down
1 change: 1 addition & 0 deletions tendermint/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub const LENGTH: usize = 20;

/// Account IDs
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Id([u8; LENGTH]); // JSON custom serialization for priv_validator_key.json

impl Protobuf<Vec<u8>> for Id {}
Expand Down
1 change: 1 addition & 0 deletions tendermint/src/crypto/ed25519/verification_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Error;

#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct VerificationKey([u8; 32]);

impl core::fmt::Display for VerificationKey {
Expand Down
1 change: 1 addition & 0 deletions tendermint/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{error::Error, prelude::*};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(tag = "type", content = "value")] // JSON custom serialization for priv_validator_key.json
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum PublicKey {
/// Ed25519 keys
#[serde(
Expand Down
2 changes: 2 additions & 0 deletions tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Info {
/// Validator account address
pub address: account::Id,
Expand Down Expand Up @@ -228,6 +229,7 @@ impl Info {
// Todo: Is there more knowledge/restrictions about proposerPriority?
/// Proposer priority
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ProposerPriority(i64);

impl From<i64> for ProposerPriority {
Expand Down
1 change: 1 addition & 0 deletions tendermint/src/vote/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{error::Error, prelude::*};

/// Voting power
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Power(u64);

impl fmt::Display for Power {
Expand Down

0 comments on commit 0bbfe8a

Please sign in to comment.