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/abci/transaction/hash.rs b/tendermint/src/abci/transaction/hash.rs index 3d90c3740..328189751 100644 --- a/tendermint/src/abci/transaction/hash.rs +++ b/tendermint/src/abci/transaction/hash.rs @@ -2,6 +2,7 @@ use crate::error::{Error, ErrorKind}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +use sha2::{Digest, Sha256}; use std::{ fmt::{self, Debug, Display}, str::FromStr, @@ -28,6 +29,15 @@ impl Hash { } } +impl Default for Hash { + fn default() -> Hash { + // hash of empty data + let mut bytes = [0u8; LENGTH]; + bytes.copy_from_slice(&Sha256::digest(&[])[..LENGTH]); + Hash::new(bytes) + } +} + impl AsRef<[u8]> for Hash { fn as_ref(&self) -> &[u8] { self.as_bytes() diff --git a/tendermint/src/account.rs b/tendermint/src/account.rs index 59045c4e7..d3cc90f14 100644 --- a/tendermint/src/account.rs +++ b/tendermint/src/account.rs @@ -15,7 +15,7 @@ use subtle_encoding::hex; const LENGTH: usize = 20; /// Account IDs -#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)] +#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, Default)] pub struct Id([u8; LENGTH]); impl Id { 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/header.rs b/tendermint/src/block/header.rs index c609bd05a..e56453b5a 100644 --- a/tendermint/src/block/header.rs +++ b/tendermint/src/block/header.rs @@ -74,7 +74,7 @@ pub struct Header { /// application. /// /// -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, Default)] pub struct Version { /// Block version #[serde( 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/moniker.rs b/tendermint/src/moniker.rs index b5ca283da..e218b9099 100644 --- a/tendermint/src/moniker.rs +++ b/tendermint/src/moniker.rs @@ -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 { diff --git a/tendermint/src/net.rs b/tendermint/src/net.rs index fe8d419ad..d5d806efe 100644 --- a/tendermint/src/net.rs +++ b/tendermint/src/net.rs @@ -39,6 +39,16 @@ pub enum Address { }, } +impl Default for Address { + fn default() -> Address { + Address::Tcp { + peer_id: None, + host: "127.0.0.1".to_owned(), + port: 0, + } + } +} + impl<'de> Deserialize<'de> for Address { fn deserialize>(deserializer: D) -> Result { Self::from_str(&String::deserialize(deserializer)?) diff --git a/tendermint/src/node/info.rs b/tendermint/src/node/info.rs index 95db15fb7..be0a7f766 100644 --- a/tendermint/src/node/info.rs +++ b/tendermint/src/node/info.rs @@ -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( @@ -58,7 +58,7 @@ pub struct ProtocolVersionInfo { } /// Listen address information -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Default)] pub struct ListenAddress(String); impl ListenAddress { @@ -80,7 +80,7 @@ impl Display for ListenAddress { } /// Other information -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Default)] pub struct OtherInfo { /// TX index status pub tx_index: TxIndexStatus, @@ -101,6 +101,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/public_key.rs b/tendermint/src/public_key.rs index 52e7c0765..ddd2ff364 100644 --- a/tendermint/src/public_key.rs +++ b/tendermint/src/public_key.rs @@ -91,6 +91,13 @@ impl PublicKey { } } +impl Default for PublicKey { + fn default() -> PublicKey { + static DUMMY_BYTES: [u8; 32] = [0; 32]; + PublicKey::Ed25519(ed25519::PublicKey::new(DUMMY_BYTES)) + } +} + impl From for PublicKey { fn from(pk: ed25519::PublicKey) -> PublicKey { PublicKey::Ed25519(pk) 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_async.rs b/tendermint/src/rpc/endpoint/broadcast/tx_async.rs index 69fec9370..3320012d3 100644 --- a/tendermint/src/rpc/endpoint/broadcast/tx_async.rs +++ b/tendermint/src/rpc/endpoint/broadcast/tx_async.rs @@ -29,7 +29,7 @@ impl rpc::Request for Request { } /// Response from either an async or sync transaction broadcast request. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Default)] pub struct 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..ce2cc78fc 100644 --- a/tendermint/src/rpc/endpoint/broadcast/tx_commit.rs +++ b/tendermint/src/rpc/endpoint/broadcast/tx_commit.rs @@ -34,7 +34,7 @@ impl rpc::Request for Request { } /// Response from `/broadcast_tx_commit`. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Default)] pub struct Response { /// `CheckTx` result pub check_tx: TxResult, @@ -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/rpc/endpoint/broadcast/tx_sync.rs b/tendermint/src/rpc/endpoint/broadcast/tx_sync.rs index 8fc48ca4b..8eaef4227 100644 --- a/tendermint/src/rpc/endpoint/broadcast/tx_sync.rs +++ b/tendermint/src/rpc/endpoint/broadcast/tx_sync.rs @@ -29,7 +29,7 @@ impl rpc::Request for Request { } /// Response from either an async or sync transaction broadcast request. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Default)] pub struct Response { /// Code pub code: Code, diff --git a/tendermint/src/rpc/endpoint/status.rs b/tendermint/src/rpc/endpoint/status.rs index 336d34429..1508b8a47 100644 --- a/tendermint/src/rpc/endpoint/status.rs +++ b/tendermint/src/rpc/endpoint/status.rs @@ -31,7 +31,7 @@ pub struct Response { impl rpc::Response for Response {} /// Sync information -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Default)] pub struct SyncInfo { /// Latest block hash pub latest_block_hash: Hash, diff --git a/tendermint/src/time.rs b/tendermint/src/time.rs index 33b253f52..cc471ef9a 100644 --- a/tendermint/src/time.rs +++ b/tendermint/src/time.rs @@ -3,6 +3,8 @@ use crate::error::{Error, ErrorKind}; use chrono::{DateTime, 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))) } + /// Returns an RFC 3339 string, such as 1996-12-19T16:39:57-08:00. + pub fn to_rfc3339(&self) -> String { + self.0.to_rfc3339() + } + /// 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,26 @@ impl Time { } } +impl Default for Time { + fn default() -> Time { + Time::unix_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 { + Time::parse_from_rfc3339(s) + } +} + impl From> for Time { fn from(t: DateTime) -> Time { Time(t) diff --git a/tendermint/src/validator.rs b/tendermint/src/validator.rs index f7c2dc8e5..0bd19efce 100644 --- a/tendermint/src/validator.rs +++ b/tendermint/src/validator.rs @@ -37,7 +37,7 @@ impl Set { } /// Validator information -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Default)] pub struct Info { /// Validator account address pub address: account::Id, diff --git a/tendermint/src/version.rs b/tendermint/src/version.rs index 5142c72e6..ff76f3ee3 100644 --- a/tendermint/src/version.rs +++ b/tendermint/src/version.rs @@ -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 { 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 {