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

Implement utility traits for tendermint data types #64

Merged
merged 1 commit into from
Nov 22, 2019
Merged
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
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/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)]
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
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
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Hash {
#[allow(clippy::new_ret_no_self)]
/// Create a new `Hash` with the given algorithm type
Expand Down
11 changes: 11 additions & 0 deletions tendermint/src/node/info.rs
Original file line number Diff line number Diff line change
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/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