forked from nervosnetwork/muta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protocol): Add the underlying data structure. (#5)
* 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
Showing
7 changed files
with
373 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
Oops, something went wrong.