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 12, 2019
1 parent 10a3026 commit 17ce65f
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 22 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
10 changes: 10 additions & 0 deletions tendermint/src/abci/transaction/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
10 changes: 10 additions & 0 deletions tendermint/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_str(&String::deserialize(deserializer)?)
Expand Down
12 changes: 9 additions & 3 deletions 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 All @@ -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 {
Expand All @@ -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,
Expand All @@ -101,6 +101,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
7 changes: 7 additions & 0 deletions tendermint/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ed25519::PublicKey> for PublicKey {
fn from(pk: ed25519::PublicKey) -> PublicKey {
PublicKey::Ed25519(pk)
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_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/rpc/endpoint/broadcast/tx_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion tendermint/src/rpc/endpoint/broadcast/tx_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/rpc/endpoint/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions tendermint/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,13 +38,38 @@ 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<SystemTime, Error> {
let duration_since_epoch = self.duration_since(Self::unix_epoch())?;
Ok(UNIX_EPOCH + duration_since_epoch)
}
}

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<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/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 17ce65f

Please sign in to comment.