Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
build.sh passes
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Jun 19, 2019
1 parent aedc271 commit 61a5661
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
8 changes: 5 additions & 3 deletions core/sr-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,17 +737,19 @@ pub trait Checkable<Context>: Sized {
pub trait BlindCheckable: Sized {
/// Returned if `check` succeeds.
type Checked;
/// Returned if `check` failed.
type Error;

/// Check self.
fn check(self) -> Result<Self::Checked, &'static str>;
fn check(self) -> Result<Self::Checked, Self::Error>;
}

// Every `BlindCheckable` is also a `StaticCheckable` for arbitrary `Context`.
impl<T: BlindCheckable, Context> Checkable<Context> for T {
type Checked = <Self as BlindCheckable>::Checked;
type Error = &'static str;
type Error = <Self as BlindCheckable>::Error;

fn check(self, _c: &Context) -> Result<Self::Checked, &'static str> {
fn check(self, _c: &Context) -> Result<Self::Checked, Self::Error> {
BlindCheckable::check(self)
}
}
Expand Down
8 changes: 5 additions & 3 deletions core/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use runtime_primitives::{
ApplyResult,
create_runtime_str,
transaction_validity::TransactionValidity,
Error,
traits::{
BlindCheckable, BlakeTwo256, Block as BlockT, Extrinsic as ExtrinsicT,
GetNodeBlockType, GetRuntimeBlockType, Verify
Expand Down Expand Up @@ -117,18 +118,19 @@ impl serde::Serialize for Extrinsic {

impl BlindCheckable for Extrinsic {
type Checked = Self;
type Error = Error;

fn check(self) -> Result<Self, &'static str> {
fn check(self) -> Result<Self, Error> {
match self {
Extrinsic::AuthoritiesChange(new_auth) => Ok(Extrinsic::AuthoritiesChange(new_auth)),
Extrinsic::Transfer(transfer, signature) => {
if runtime_primitives::verify_encoded_lazy(&signature, &transfer, &transfer.from) {
Ok(Extrinsic::Transfer(transfer, signature))
} else {
Err(runtime_primitives::BAD_SIGNATURE)
Err(Error::BadSignature)
}
},
Extrinsic::IncludeData(_) => Err(runtime_primitives::BAD_SIGNATURE),
Extrinsic::IncludeData(_) => Err(Error::BadSignature),
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions core/test-runtime/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ fn execute_block_with_state_root_handler(
// execute transactions
block.extrinsics.iter().enumerate().for_each(|(i, e)| {
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &(i as u32));
execute_transaction_backend(e).unwrap_or_else(|_| panic!("Invalid transaction"));
match execute_transaction_backend(e) {
ApplyResult::Success => (),
_ => panic!("Invalid transaction"),
};
storage::unhashed::kill(well_known_keys::EXTRINSIC_INDEX);
});

Expand Down Expand Up @@ -233,11 +236,13 @@ fn check_signature(utx: &Extrinsic) -> Result<(), ApplyError> {
}

fn execute_transaction_backend(utx: &Extrinsic) -> ApplyResult {
check_signature(utx)?;
match utx {
Extrinsic::Transfer(ref transfer, _) => execute_transfer_backend(transfer),
Extrinsic::AuthoritiesChange(ref new_auth) => execute_new_authorities_backend(new_auth),
Extrinsic::IncludeData(_) => ApplyResult::Success,
match check_signature(utx) {
Ok(_) => match utx {
Extrinsic::Transfer(ref transfer, _) => execute_transfer_backend(transfer),
Extrinsic::AuthoritiesChange(ref new_auth) => execute_new_authorities_backend(new_auth),
Extrinsic::IncludeData(_) => ApplyResult::Success,
},
Err(err) => ApplyResult::ApplyError(err)
}
}

Expand All @@ -246,7 +251,7 @@ fn execute_transfer_backend(tx: &Transfer) -> ApplyResult {
let nonce_key = tx.from.to_keyed_vec(NONCE_OF);
let expected_nonce: u64 = storage::hashed::get_or(&blake2_256, &nonce_key, 0);
if !(tx.nonce == expected_nonce) {
return Err(ApplyError::Stale)
return ApplyResult::ApplyError(ApplyError::Stale)
}

// increment nonce in storage
Expand All @@ -258,7 +263,7 @@ fn execute_transfer_backend(tx: &Transfer) -> ApplyResult {

// enact transfer
if !(tx.amount <= from_balance) {
return Err(ApplyError::CantPay)
return ApplyResult::ApplyError(ApplyError::CantPay)
}
let to_balance_key = tx.to.to_keyed_vec(BALANCE_OF);
let to_balance: u64 = storage::hashed::get_or(&blake2_256, &to_balance_key, 0);
Expand Down
2 changes: 2 additions & 0 deletions node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ impl system::Trait for Runtime {
type Event = Event;
/// The ubiquitous origin type.
type Origin = Origin;
/// The ubiquitous error type.
type Error = Error;
}

impl aura::Trait for Runtime {
Expand Down

0 comments on commit 61a5661

Please sign in to comment.