Skip to content

Commit

Permalink
Format code with cargo fmt (paritytech#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
koushiro authored and gguoss committed Nov 16, 2018
1 parent a41f262 commit 8da5194
Show file tree
Hide file tree
Showing 64 changed files with 3,561 additions and 2,156 deletions.
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
SHELL := /bin/bash
#ENABLE_FEATURES ?= default

default: release

.PHONY: all

all: format build test

pre-clippy: unset-override
@rustup component add clippy-preview

clippy: pre-clippy
@cargo clippy --all --all-targets -- \
-A clippy::module_inception -A clippy::needless_pass_by_value \
-A clippy::cyclomatic_complexity -A clippy::unreadable_literal \
-A clippy::should_implement_trait -A clippy::verbose_bit_mask \
-A clippy::implicit_hasher -A clippy::large_enum_variant \
-A clippy::new_without_default -A clippy::new_without_default_derive \
-A clippy::neg_cmp_op_on_partial_ord -A clippy::too_many_arguments \
-A clippy::excessive_precision -A clippy::collapsible_if \
-A clippy::blacklisted_name

build:
cargo build #--features "${ENABLE_FEATURES}"

release:
@cargo build --release #--features "${ENABLE_FEATURES}"

test:
export LOG_LEVEL=DEBUG && \
export RUST_BACKTRACE=1 && \
cargo test #--features "${ENABLE_FEATURES}" --all -- --nocapture

bench:
LOG_LEVEL=ERROR RUST_BACKTRACE=1 cargo bench #--features "${ENABLE_FEATURES}" --all -- --nocapture

unset-override:
@# unset first in case of any previous overrides
@if rustup override list | grep `pwd` > /dev/null; then rustup override unset; fi

pre-format: unset-override
@rustup component add rustfmt-preview

format: pre-format
@cargo fmt --all -- --check >/dev/null || \
cargo fmt --all

clean:
@cargo clean
32 changes: 17 additions & 15 deletions api/src/implement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,33 @@ impl ChainXApi for TClient {
self.call_api_at(at, "timestamp", &())
}

fn index(&self, at: &BlockId, account: AccountId) -> Result<Index> {
self.call_api_at(at, "account_nonce", &account)
}

fn lookup(&self, at: &BlockId, address: Address) -> Result<Option<AccountId>> {
self.call_api_at(at, "lookup_address", &address)
}

fn evaluate_block(&self, at: &BlockId, block: Block) -> Result<bool> {
let res: Result<()> = self.call_api_at(at, "execute_block", &block);
match res {
Ok(_) => Ok(true),
Err(err) => {
match err.kind() {
&ErrorKind::Execution(_) => Ok(false),
_ => Err(err),
}
}
Err(err) => match err.kind() {
&ErrorKind::Execution(_) => Ok(false),
_ => Err(err),
},
}
}

fn validate_transaction(&self, at: &BlockId, tx: UncheckedExtrinsic) -> Result<TransactionValidity> {
fn validate_transaction(
&self,
at: &BlockId,
tx: UncheckedExtrinsic,
) -> Result<TransactionValidity> {
self.call_api_at(at, "validate_transaction", &tx)
}

fn index(&self, at: &BlockId, account: AccountId) -> Result<Index> {
self.call_api_at(at, "account_nonce", &account)
}

fn lookup(&self, at: &BlockId, address: Address) -> Result<Option<AccountId>> {
self.call_api_at(at, "lookup_address", &address)
}

fn build_block(&self, at: &BlockId, inherent_data: InherentData) -> Result<Self::BlockBuilder> {
let runtime_version = self.runtime_version_at(at)?;

Expand Down
46 changes: 29 additions & 17 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
// Copyright 2018 chainpool.

extern crate sr_primitives as runtime_primitives;
extern crate substrate_executor as substrate_executor;
extern crate substrate_client_db as client_db;
extern crate substrate_client as client;
extern crate parity_codec as codec;
extern crate substrate_primitives;
extern crate sr_io as runtime_io;
extern crate sr_primitives as runtime_primitives;
extern crate srml_executive;
extern crate substrate_client as client;
extern crate substrate_client_db as client_db;
extern crate substrate_executor as substrate_executor;
extern crate substrate_primitives;

extern crate chainx_executor;
extern crate chainx_primitives as primitives;
extern crate chainx_runtime as runtime;
extern crate chainx_executor;

use primitives::{
AccountId, Block, BlockId, Hash, Index, SessionKey, Timestamp, BlockNumber,
UncheckedExtrinsic, InherentData,
};
use runtime_primitives::{transaction_validity::TransactionValidity, traits::{CurrentHeight, BlockNumberToHash}};
use chainx_executor::NativeExecutor;
use client::block_builder::BlockBuilder as ClientBlockBuilder;
pub use client::error::{Error, ErrorKind, Result};
use substrate_primitives::Blake2Hasher;
use chainx_executor::NativeExecutor;
use primitives::{
AccountId, Block, BlockId, BlockNumber, Hash, Index, InherentData, SessionKey, Timestamp,
UncheckedExtrinsic,
};
use runtime::Address;
use runtime_primitives::{
traits::{BlockNumberToHash, CurrentHeight},
transaction_validity::TransactionValidity,
};
use substrate_primitives::Blake2Hasher;

mod implement;

Expand All @@ -40,11 +43,12 @@ pub type TExecutor = client::LocalCallExecutor<TBackend, NativeExecutor<chainx_e
pub type TClient = client::Client<TBackend, TExecutor, Block>;
pub type TClientBlockBuilder = ClientBlockBuilder<TBackend, TExecutor, Block, Blake2Hasher>;


/// Trait encapsulating the ChainX API.
///
/// All calls should fail when the exact runtime is unknown.
pub trait ChainXApi: CurrentHeight<BlockNumber=BlockNumber> + BlockNumberToHash<BlockNumber=BlockNumber, Hash=Hash> {
pub trait ChainXApi:
CurrentHeight<BlockNumber = BlockNumber> + BlockNumberToHash<BlockNumber = BlockNumber, Hash = Hash>
{
/// The block builder for this API type.
type BlockBuilder: BlockBuilder;

Expand Down Expand Up @@ -73,14 +77,22 @@ pub trait ChainXApi: CurrentHeight<BlockNumber=BlockNumber> + BlockNumberToHash<
/// and an error if we can't evaluate for some reason.
fn evaluate_block(&self, at: &BlockId, block: Block) -> Result<bool>;

fn validate_transaction(&self, at: &BlockId, transaction: UncheckedExtrinsic) -> Result<TransactionValidity>;
fn validate_transaction(
&self,
at: &BlockId,
transaction: UncheckedExtrinsic,
) -> Result<TransactionValidity>;

/// Build a block on top of the given, with inherent extrinsics pre-pushed.
fn build_block(&self, at: &BlockId, inherent_data: InherentData) -> Result<Self::BlockBuilder>;

/// Attempt to produce the (encoded) inherent extrinsics for a block being built upon the given.
/// This may vary by runtime and will fail if a runtime doesn't follow the same API.
fn inherent_extrinsics(&self, at: &BlockId, inherent_data: InherentData) -> Result<Vec<UncheckedExtrinsic>>;
fn inherent_extrinsics(
&self,
at: &BlockId,
inherent_data: InherentData,
) -> Result<Vec<UncheckedExtrinsic>>;
}

/// Mark for all ChainX API implementations, that are making use of state data, stored locally.
Expand Down
52 changes: 26 additions & 26 deletions consensus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
use primitives::AuthorityId;

error_chain! {
links {
ChainXApi(::chainx_api::Error, ::chainx_api::ErrorKind);
Bft(::bft::Error, ::bft::ErrorKind);
}
links {
ChainXApi(::chainx_api::Error, ::chainx_api::ErrorKind);
Bft(::bft::Error, ::bft::ErrorKind);
}

errors {
InvalidDutyRosterLength(expected: usize, got: usize) {
description("Duty Roster had invalid length"),
display("Invalid duty roster length: expected {}, got {}", expected, got),
}
NotValidator(id: AuthorityId) {
description("Local account ID not a validator at this block."),
display("Local account ID ({:?}) not a validator at this block.", id),
}
PrematureDestruction {
description("Proposer destroyed before finishing proposing or evaluating"),
display("Proposer destroyed before finishing proposing or evaluating"),
}
Timer(e: ::tokio::timer::Error) {
description("Failed to register or resolve async timer."),
display("Timer failed: {}", e),
}
Executor(e: ::futures::future::ExecuteErrorKind) {
description("Unable to dispatch agreement future"),
display("Unable to dispatch agreement future: {:?}", e),
}
}
errors {
InvalidDutyRosterLength(expected: usize, got: usize) {
description("Duty Roster had invalid length"),
display("Invalid duty roster length: expected {}, got {}", expected, got),
}
NotValidator(id: AuthorityId) {
description("Local account ID not a validator at this block."),
display("Local account ID ({:?}) not a validator at this block.", id),
}
PrematureDestruction {
description("Proposer destroyed before finishing proposing or evaluating"),
display("Proposer destroyed before finishing proposing or evaluating"),
}
Timer(e: ::tokio::timer::Error) {
description("Failed to register or resolve async timer."),
display("Timer failed: {}", e),
}
Executor(e: ::futures::future::ExecuteErrorKind) {
description("Unable to dispatch agreement future"),
display("Unable to dispatch agreement future: {:?}", e),
}
}
}

impl From<::bft::InputStreamConcluded> for Error {
Expand Down
73 changes: 37 additions & 36 deletions consensus/src/evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
// Copyright 2018 Chainpool.

//! ChainX block evaluation and evaluation errors.
use chainx_primitives::{Block, BlockNumber, Hash, Timestamp};
use chainx_runtime::{Block as ChainXGenericBlock, CheckedBlock};
use chainx_primitives::{Block, Hash, BlockNumber, Timestamp};

use super::MAX_TRANSACTIONS_SIZE;

use codec::{Decode, Encode};

error_chain! {
links {
ChainXApi(::chainx_api::Error, ::chainx_api::ErrorKind);
}
links {
ChainXApi(::chainx_api::Error, ::chainx_api::ErrorKind);
}

errors {
ProposalNotForChainX {
description("Proposal provided not a ChainX block."),
display("Proposal provided not a ChainX block."),
}
TimestampInFuture {
description("Proposal had timestamp too far in the future."),
display("Proposal had timestamp too far in the future."),
}
TooManyCandidates(expected: usize, got: usize) {
description("Proposal included more candidates than is possible."),
display("Proposal included {} candidates for {} parachains", got, expected),
}
WrongParentHash(expected: Hash, got: Hash) {
description("Proposal had wrong parent hash."),
display("Proposal had wrong parent hash. Expected {:?}, got {:?}", expected, got),
}
WrongNumber(expected: BlockNumber, got: BlockNumber) {
description("Proposal had wrong number."),
display("Proposal had wrong number. Expected {:?}, got {:?}", expected, got),
}
ProposalTooLarge(size: usize) {
description("Proposal exceeded the maximum size."),
display(
"Proposal exceeded the maximum size of {} by {} bytes.",
MAX_TRANSACTIONS_SIZE, MAX_TRANSACTIONS_SIZE.saturating_sub(*size)
),
}
}
errors {
ProposalNotForChainX {
description("Proposal provided not a ChainX block."),
display("Proposal provided not a ChainX block."),
}
TimestampInFuture {
description("Proposal had timestamp too far in the future."),
display("Proposal had timestamp too far in the future."),
}
TooManyCandidates(expected: usize, got: usize) {
description("Proposal included more candidates than is possible."),
display("Proposal included {} candidates for {} parachains", got, expected),
}
WrongParentHash(expected: Hash, got: Hash) {
description("Proposal had wrong parent hash."),
display("Proposal had wrong parent hash. Expected {:?}, got {:?}", expected, got),
}
WrongNumber(expected: BlockNumber, got: BlockNumber) {
description("Proposal had wrong number."),
display("Proposal had wrong number. Expected {:?}, got {:?}", expected, got),
}
ProposalTooLarge(size: usize) {
description("Proposal exceeded the maximum size."),
display(
"Proposal exceeded the maximum size of {} by {} bytes.",
MAX_TRANSACTIONS_SIZE, MAX_TRANSACTIONS_SIZE.saturating_sub(*size)
),
}
}
}

/// Attempt to evaluate a substrate block as a chainx block, returning error
Expand All @@ -59,9 +59,10 @@ pub fn evaluate_initial(
.and_then(|b| CheckedBlock::new(b).ok())
.ok_or_else(|| ErrorKind::ProposalNotForChainX)?;

let transactions_size = proposal.extrinsics.iter().fold(0, |a, tx| {
a + Encode::encode(tx).len()
});
let transactions_size = proposal
.extrinsics
.iter()
.fold(0, |a, tx| a + Encode::encode(tx).len());

if transactions_size > MAX_TRANSACTIONS_SIZE {
bail!(ErrorKind::ProposalTooLarge(transactions_size))
Expand Down
Loading

0 comments on commit 8da5194

Please sign in to comment.