diff --git a/tendermint/src/abci/code.rs b/tendermint/src/abci/code.rs index 81ca1c523..6b0809447 100644 --- a/tendermint/src/abci/code.rs +++ b/tendermint/src/abci/code.rs @@ -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 { diff --git a/tendermint/src/abci/data.rs b/tendermint/src/abci/data.rs index 68da7abda..1a60d8ed6 100644 --- a/tendermint/src/abci/data.rs +++ b/tendermint/src/abci/data.rs @@ -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); impl Data { diff --git a/tendermint/src/abci/log.rs b/tendermint/src/abci/log.rs index 3c9f9be8f..668acf006 100644 --- a/tendermint/src/abci/log.rs +++ b/tendermint/src/abci/log.rs @@ -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 { @@ -13,6 +13,12 @@ impl Log { } } +impl From<&str> for Log { + fn from(s: &str) -> Self { + Log(s.to_owned()) + } +} + impl AsRef for Log { fn as_ref(&self) -> &str { self.0.as_ref() diff --git a/tendermint/src/abci/transaction.rs b/tendermint/src/abci/transaction.rs index a9c61cb45..9c9438637 100644 --- a/tendermint/src/abci/transaction.rs +++ b/tendermint/src/abci/transaction.rs @@ -61,7 +61,7 @@ impl Serialize for Transaction { /// transactions are arbitrary byte arrays. /// /// -#[derive(Deserialize, Serialize, Clone, Debug)] +#[derive(Deserialize, Serialize, Clone, Debug, Default)] pub struct Data { txs: Option>, } diff --git a/tendermint/src/block/commit.rs b/tendermint/src/block/commit.rs index 7b313eee1..269a0717f 100644 --- a/tendermint/src/block/commit.rs +++ b/tendermint/src/block/commit.rs @@ -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>>); impl Precommits { diff --git a/tendermint/src/block/id.rs b/tendermint/src/block/id.rs index 7bc37c74d..b0108ecbe 100644 --- a/tendermint/src/block/id.rs +++ b/tendermint/src/block/id.rs @@ -16,7 +16,7 @@ pub const PREFIX_LENGTH: usize = 10; /// as well as the number of parts in the block. /// /// -#[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. diff --git a/tendermint/src/channel.rs b/tendermint/src/channel.rs index 331dfc8d1..880b92c15 100644 --- a/tendermint/src/channel.rs +++ b/tendermint/src/channel.rs @@ -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 { diff --git a/tendermint/src/evidence.rs b/tendermint/src/evidence.rs index 981acca2b..ff0d81d73 100644 --- a/tendermint/src/evidence.rs +++ b/tendermint/src/evidence.rs @@ -51,7 +51,7 @@ impl Serialize for Evidence { /// Evidence data is a wrapper for a list of `Evidence`. /// /// -#[derive(Deserialize, Serialize, Clone, Debug)] +#[derive(Deserialize, Serialize, Clone, Debug, Default)] pub struct Data { evidence: Option>, } diff --git a/tendermint/src/hash.rs b/tendermint/src/hash.rs index 349fb9f95..df37aa3e4 100644 --- a/tendermint/src/hash.rs +++ b/tendermint/src/hash.rs @@ -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 diff --git a/tendermint/src/node/info.rs b/tendermint/src/node/info.rs index 95db15fb7..11cc44b62 100644 --- a/tendermint/src/node/info.rs +++ b/tendermint/src/node/info.rs @@ -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 { // TODO(tarcieri): validate these and handle them better at parse time @@ -101,6 +106,12 @@ pub enum TxIndexStatus { Off, } +impl Default for TxIndexStatus { + fn default() -> TxIndexStatus { + TxIndexStatus::On + } +} + impl From for bool { fn from(status: TxIndexStatus) -> bool { match status { diff --git a/tendermint/src/rpc/endpoint/abci_query.rs b/tendermint/src/rpc/endpoint/abci_query.rs index d7010ad18..9f41ca814 100644 --- a/tendermint/src/rpc/endpoint/abci_query.rs +++ b/tendermint/src/rpc/endpoint/abci_query.rs @@ -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, diff --git a/tendermint/src/rpc/endpoint/broadcast/tx_commit.rs b/tendermint/src/rpc/endpoint/broadcast/tx_commit.rs index 2561889b7..c1b4fa44b 100644 --- a/tendermint/src/rpc/endpoint/broadcast/tx_commit.rs +++ b/tendermint/src/rpc/endpoint/broadcast/tx_commit.rs @@ -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, diff --git a/tendermint/src/time.rs b/tendermint/src/time.rs index 33b253f52..407347e68 100644 --- a/tendermint/src/time.rs +++ b/tendermint/src/time.rs @@ -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; @@ -36,6 +38,11 @@ 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 { let duration_since_epoch = self.duration_since(Self::unix_epoch())?; @@ -43,6 +50,20 @@ impl Time { } } +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 { + Time::parse_from_rfc3339(s) + } +} + impl From> for Time { fn from(t: DateTime) -> Time { Time(t) diff --git a/tendermint/src/vote/power.rs b/tendermint/src/vote/power.rs index 8534a9b32..7ac8901fe 100644 --- a/tendermint/src/vote/power.rs +++ b/tendermint/src/vote/power.rs @@ -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 {