Skip to content

Commit

Permalink
implement utility traits for tendermint data types
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Nov 20, 2019
1 parent 10a3026 commit b231168
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 15 deletions.
6 changes: 6 additions & 0 deletions tendermint/src/abci/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub enum Code {
Err(u32),
}

impl Default for Code {
fn default() -> Code {
Code::Ok
}
}

impl Code {
/// Was the response OK?
pub fn is_ok(self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/abci/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use subtle_encoding::hex;
///
/// Transactions are opaque binary blobs which are validated according to
/// application-specific rules.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Data(Vec<u8>);

impl Data {
Expand Down
8 changes: 7 additions & 1 deletion tendermint/src/abci/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

/// ABCI log data
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Default)]
pub struct Log(String);

impl Log {
Expand All @@ -13,6 +13,12 @@ impl Log {
}
}

impl From<&str> for Log {
fn from(s: &str) -> Self {
Log(s.to_owned())
}
}

impl AsRef<str> for Log {
fn as_ref(&self) -> &str {
self.0.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/abci/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Serialize for Transaction {
/// transactions are arbitrary byte arrays.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#data>
#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct Data {
txs: Option<Vec<Transaction>>,
}
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/block/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Commit {
}

/// Precommits which certify that a block is valid
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Precommits(Option<Vec<Option<Vote>>>);

impl Precommits {
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct Header {
/// application.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#version>
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Version {
/// Block version
#[serde(
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/block/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub const PREFIX_LENGTH: usize = 10;
/// as well as the number of parts in the block.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#blockid>
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Default)]
pub struct Id {
/// The block's main hash is the Merkle root of all the fields in the
/// block header.
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Channel {
}

/// Channel collections
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Default)]
pub struct Channels(String);

impl Display for Channels {
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Serialize for Evidence {
/// Evidence data is a wrapper for a list of `Evidence`.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#evidencedata>
#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct Data {
evidence: Option<Vec<Evidence>>,
}
Expand Down
6 changes: 6 additions & 0 deletions tendermint/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub enum Hash {
Null,
}

impl Default for Hash {
fn default() -> Hash {
Hash::Null
}
}

impl Hash {
#[allow(clippy::new_ret_no_self)]
/// Create a new `Hash` with the given algorithm type
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

/// Validator display names
#[derive(Serialize, Deserialize, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Default)]
pub struct Moniker(String);

impl FromStr for Moniker {
Expand Down
13 changes: 12 additions & 1 deletion tendermint/src/node/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Info {
}

/// Protocol version information
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Default)]
pub struct ProtocolVersionInfo {
/// P2P protocol version
#[serde(
Expand Down Expand Up @@ -62,6 +62,11 @@ pub struct ProtocolVersionInfo {
pub struct ListenAddress(String);

impl ListenAddress {
/// Construct `ListenAddress`
pub fn new(s: String) -> ListenAddress {
ListenAddress(s)
}

/// Convert `ListenAddress` to a `net::Address`
pub fn to_net_address(&self) -> Option<net::Address> {
// TODO(tarcieri): validate these and handle them better at parse time
Expand Down Expand Up @@ -101,6 +106,12 @@ pub enum TxIndexStatus {
Off,
}

impl Default for TxIndexStatus {
fn default() -> TxIndexStatus {
TxIndexStatus::On
}
}

impl From<TxIndexStatus> for bool {
fn from(status: TxIndexStatus) -> bool {
match status {
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/rpc/endpoint/abci_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Response {
impl rpc::Response for Response {}

/// ABCI query results
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct AbciQuery {
/// Response code
pub code: Code,
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/rpc/endpoint/broadcast/tx_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct Response {
impl rpc::Response for Response {}

/// Results from either `CheckTx` or `DeliverTx`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct TxResult {
/// Code
pub code: Code,
Expand Down
23 changes: 22 additions & 1 deletion tendermint/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Timestamps used by Tendermint blockchains

use crate::error::{Error, ErrorKind};
use chrono::{DateTime, Utc};
use chrono::{DateTime, SecondsFormat, Utc};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tai64::TAI64N;

Expand Down Expand Up @@ -36,13 +38,32 @@ impl Time {
Ok(Time(DateTime::parse_from_rfc3339(s)?.with_timezone(&Utc)))
}

/// Return an RFC 3339 and ISO 8601 date and time string with 6 subseconds digits and Z.
pub fn to_rfc3339(&self) -> String {
self.0.to_rfc3339_opts(SecondsFormat::Nanos, true)
}

/// Convert this timestamp to a `SystemTime`
pub fn to_system_time(&self) -> Result<SystemTime, Error> {
let duration_since_epoch = self.duration_since(Self::unix_epoch())?;
Ok(UNIX_EPOCH + duration_since_epoch)
}
}

impl fmt::Display for Time {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.to_rfc3339())
}
}

impl FromStr for Time {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Time::parse_from_rfc3339(s)
}
}

impl From<DateTime<Utc>> for Time {
fn from(t: DateTime<Utc>) -> Time {
Time(t)
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

/// Tendermint version
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Default)]
pub struct Version(String);

impl Display for Version {
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/vote/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

/// Voting power
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Default)]
pub struct Power(u64);

impl Power {
Expand Down

0 comments on commit b231168

Please sign in to comment.