Skip to content

Commit

Permalink
feat(protocol): Add the underlying data structure. (#5)
Browse files Browse the repository at this point in the history
* feat(protocol): Add the underlying data structure.

* Update protocol/src/types/mod.rs

Co-Authored-By: Wenchao Hu <[email protected]>

* Update protocol/src/types/transaction.rs

Co-Authored-By: Wenchao Hu <[email protected]>

* cargo fmt.

* vec<u8> -> Bytes

* ensure len

* asset id.

* refactor: Remove the template code with derive_more.

* update proposal.

* add proof.

* add validator.

* typo

* Change the receip.spender type.
  • Loading branch information
yejiayu committed Oct 31, 2019
1 parent 75eb0b1 commit a382cf0
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 86 deletions.
3 changes: 3 additions & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_more = "0.15"
async-trait = "0.1"
ethbloom = "0.6"
bytes = "0.4"
sha3 = "0.8"
uint = "0.8"
hex = "0.3"
22 changes: 4 additions & 18 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#[macro_use]
extern crate uint;
#[macro_use]
extern crate derive_more;

pub mod codec;
pub mod traits;
pub mod types;

use std::error::Error;
use std::fmt;

#[derive(Debug, Clone)]
pub enum ProtocolErrorKind {
Expand All @@ -27,28 +28,13 @@ pub enum ProtocolErrorKind {
}

// refer to https://github.com/rust-lang/rust/blob/a17951c4f80eb5208030f91fdb4ae93919fa6b12/src/libstd/io/error.rs#L73
#[derive(Debug)]
#[derive(Debug, Constructor, Display)]
#[display(fmt = "[ProtocolError] Kind: {:?} Error: {:?}", kind, error)]
pub struct ProtocolError {
kind: ProtocolErrorKind,
error: Box<dyn Error + Send>,
}

impl ProtocolError {
pub fn new(kind: ProtocolErrorKind, error: Box<dyn Error + Send>) -> Self {
Self { kind, error }
}
}

impl Error for ProtocolError {}

impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[ProtocolError] Kind: {:?} Error: {:?}",
self.kind, self.error
)
}
}

pub type ProtocolResult<T> = Result<T, ProtocolError>;
47 changes: 47 additions & 0 deletions protocol/src/types/epoch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use bytes::Bytes;

use crate::types::{AccountAddress, Bloom, Hash, MerkleRoot};

#[derive(Clone, Debug)]
pub struct Epoch {
pub header: EpochHeader,
pub ordered_tx_hashes: Vec<Hash>,
}

#[derive(Clone, Debug)]
pub struct EpochHeader {
pub chain_id: Hash,
pub epoch_id: u64,
pub pre_hash: Hash,
pub timestamp: u64,
pub logs_bloom: Bloom,
pub order_root: MerkleRoot,
pub confirm_root: Vec<MerkleRoot>,
pub state_root: MerkleRoot,
pub receipt_root: Vec<MerkleRoot>,
pub cycles_used: u64,
pub proposer: AccountAddress,
pub proof: Proof,
pub validator_version: u64,
pub validators: Option<Vec<Validator>>,
}

#[derive(Clone, Debug)]
pub struct Proof {
pub epoch_id: u64,
pub round: u64,
pub epoch_hash: Hash,
pub signature: Bytes,
}

#[derive(Clone, Debug)]
pub struct Validator {
address: AccountAddress,
weight: u64,
}

#[derive(Clone, Debug)]
pub struct Pill {
pub epoch: Epoch,
pub propose_hashes: Vec<Hash>,
}
39 changes: 17 additions & 22 deletions protocol/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
mod epoch;
mod primitive;
mod receipt;
mod transaction;

use std::error::Error;
use std::fmt;

use crate::{ProtocolError, ProtocolErrorKind};

pub use primitive::{Address, Hash};
pub use transaction::{ContractType, Fee, RawTransaction, SignedTransaction, TransactionAction};
pub use epoch::{Epoch, EpochHeader, Pill, Proof, Validator};
pub use ethbloom::{Bloom, BloomRef, Input as BloomInput};
pub use primitive::{
AccountAddress, AssetID, Balance, ContractAddress, ContractType, Fee, Hash, MerkleRoot,
};
pub use receipt::{Receipt, ReceiptResult};
pub use transaction::{RawTransaction, SignedTransaction, TransactionAction};

#[derive(Debug)]
#[derive(Debug, Display, From)]
pub enum TypesError {
HashLengthMismatch { expect: usize, real: usize },
#[display(fmt = "Expect {:?}, get {:?}.", expect, real)]
LengthMismatch { expect: usize, real: usize },

#[display(fmt = "{:?}", error)]
FromHex { error: hex::FromHexError },

#[display(fmt = "{:?} is an invalid address", address)]
InvalidAddress { address: String },
}

impl Error for TypesError {}

impl fmt::Display for TypesError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let printable = match self {
TypesError::HashLengthMismatch { expect, real } => {
format!("Expect {:?} to get {:?}.", expect, real)
}
TypesError::FromHex { error } => format!("{:?}.", error),
};
write!(f, "{}", printable)
}
}
impl From<TypesError> for ProtocolError {
fn from(error: TypesError) -> ProtocolError {
ProtocolError::new(ProtocolErrorKind::Types, Box::new(error))
}
}

impl From<hex::FromHexError> for TypesError {
fn from(error: hex::FromHexError) -> Self {
TypesError::FromHex { error }
}
}
Loading

0 comments on commit a382cf0

Please sign in to comment.