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

Commit c5c2e5a

Browse files
committed
revert some unnecessary changes
1 parent 3039e7c commit c5c2e5a

File tree

5 files changed

+70
-83
lines changed

5 files changed

+70
-83
lines changed

core/client/src/block_builder/block_builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use super::api::BlockBuilder as BlockBuilderApi;
1818
use std::vec::Vec;
1919
use parity_codec::Encode;
20-
use runtime_primitives::ApplyResult;
2120
use runtime_primitives::generic::BlockId;
2221
use runtime_primitives::traits::{
2322
Header as HeaderT, Hash, Block as BlockT, One, HashFor, ProvideRuntimeApi, ApiRef, DigestFor,
@@ -104,11 +103,11 @@ where
104103
ExecutionContext::BlockConstruction,
105104
xt.clone()
106105
)? {
107-
ApplyResult::Success | ApplyResult::DispatchError(_) => {
106+
Ok(_) => {
108107
extrinsics.push(xt);
109108
Ok(())
110109
}
111-
ApplyResult::ApplyError(e) => {
110+
Err(e) => {
112111
Err(error::Error::ApplyExtrinsicFailed(e))
113112
}
114113
}

core/sr-primitives/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -544,18 +544,18 @@ pub struct DispatchError {
544544
pub message: Option<&'static str>,
545545
}
546546

547-
// TODO: custom implement Encode & Decode to make it a two byte value
548547
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode)]
549-
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
548+
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
549+
/// Outcome of a valid extrinsic application. Capable of being sliced.
550+
pub enum ApplyOutcome {
551+
/// Successful application (extrinsic reported no issue).
552+
Success,
553+
/// Failed application (extrinsic was probably a no-op other than fees).
554+
Fail(DispatchError),
555+
}
556+
550557
/// Result from attempt to apply an extrinsic.
551-
pub enum ApplyResult {
552-
/// Successful application (extrinsic reported no issue).
553-
Success,
554-
/// Failed application (extrinsic was probably a no-op other than fees).
555-
DispatchError(DispatchError),
556-
/// Invalid extrinsic application.
557-
ApplyError(ApplyError),
558-
}
558+
pub type ApplyResult = Result<ApplyOutcome, ApplyError>;
559559

560560
/// Verify a signature on an encoded value in a lazy manner. This can be
561561
/// an optimization if the signature scheme has an "unsigned" escape hash.

core/test-runtime/src/system.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use runtime_support::storage::{self, StorageValue, StorageMap};
2323
use runtime_support::storage_items;
2424
use runtime_primitives::traits::{Hash as HashT, BlakeTwo256, Header as _};
2525
use runtime_primitives::generic;
26-
use runtime_primitives::{ApplyError, ApplyResult, transaction_validity::TransactionValidity};
26+
use runtime_primitives::{ApplyError, ApplyOutcome, ApplyResult, transaction_validity::TransactionValidity};
2727
use parity_codec::{KeyedVec, Encode};
2828
use super::{
2929
AccountId, BlockNumber, Extrinsic, Transfer, H256 as Hash, Block, Header, Digest, AuthorityId
@@ -106,10 +106,7 @@ fn execute_block_with_state_root_handler(
106106
// execute transactions
107107
block.extrinsics.iter().enumerate().for_each(|(i, e)| {
108108
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &(i as u32));
109-
match execute_transaction_backend(e) {
110-
ApplyResult::Success => (),
111-
_ => panic!("Invalid transaction"),
112-
};
109+
execute_transaction_backend(e).unwrap_or_else(|_| panic!("Invalid transaction"));
113110
storage::unhashed::kill(well_known_keys::EXTRINSIC_INDEX);
114111
});
115112

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

238235
fn execute_transaction_backend(utx: &Extrinsic) -> ApplyResult {
239-
match check_signature(utx) {
240-
Ok(_) => match utx {
241-
Extrinsic::Transfer(ref transfer, _) => execute_transfer_backend(transfer),
242-
Extrinsic::AuthoritiesChange(ref new_auth) => execute_new_authorities_backend(new_auth),
243-
Extrinsic::IncludeData(_) => ApplyResult::Success,
244-
},
245-
Err(err) => ApplyResult::ApplyError(err)
236+
check_signature(utx)?;
237+
match utx {
238+
Extrinsic::Transfer(ref transfer, _) => execute_transfer_backend(transfer),
239+
Extrinsic::AuthoritiesChange(ref new_auth) => execute_new_authorities_backend(new_auth),
240+
Extrinsic::IncludeData(_) => Ok(ApplyOutcome::Success),
246241
}
247242
}
248243

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

257252
// increment nonce in storage
@@ -263,19 +258,19 @@ fn execute_transfer_backend(tx: &Transfer) -> ApplyResult {
263258

264259
// enact transfer
265260
if !(tx.amount <= from_balance) {
266-
return ApplyResult::ApplyError(ApplyError::CantPay)
261+
return Err(ApplyError::CantPay)
267262
}
268263
let to_balance_key = tx.to.to_keyed_vec(BALANCE_OF);
269264
let to_balance: u64 = storage::hashed::get_or(&blake2_256, &to_balance_key, 0);
270265
storage::hashed::put(&blake2_256, &from_balance_key, &(from_balance - tx.amount));
271266
storage::hashed::put(&blake2_256, &to_balance_key, &(to_balance + tx.amount));
272-
ApplyResult::Success
267+
Ok(ApplyOutcome::Success)
273268
}
274269

275270
fn execute_new_authorities_backend(new_authorities: &[AuthorityId]) -> ApplyResult {
276271
let new_authorities: Vec<AuthorityId> = new_authorities.iter().cloned().collect();
277272
<NewAuthorities>::put(new_authorities);
278-
ApplyResult::Success
273+
Ok(ApplyOutcome::Success)
279274
}
280275

281276
#[cfg(feature = "std")]

node/executor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod tests {
3838
NativeOrEncoded};
3939
use node_primitives::{Hash, BlockNumber, AccountId};
4040
use runtime_primitives::traits::{Header as HeaderT, Hash as HashT};
41-
use runtime_primitives::{generic::Era, ApplyError, ApplyResult, Perbill};
41+
use runtime_primitives::{generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill};
4242
use {balances, indices, system, staking, timestamp, treasury, contract};
4343
use contract::ContractAddressFor;
4444
use system::{EventRecord, Phase};
@@ -912,7 +912,7 @@ mod tests {
912912
let r = WasmExecutor::new()
913913
.call(&mut t, 8, COMPACT_CODE, "BlockBuilder_apply_extrinsic", &vec![].and(&xt())).unwrap();
914914
let r = ApplyResult::decode(&mut &r[..]).unwrap();
915-
assert_eq!(r, ApplyResult::Success);
915+
assert_eq!(r, Ok(ApplyOutcome::Success));
916916

917917
runtime_io::with_externalities(&mut t, || {
918918
assert_eq!(Balances::total_balance(&alice()), 42);

srml/executive/src/lib.rs

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use rstd::prelude::*;
7878
use rstd::marker::PhantomData;
7979
use rstd::convert::TryInto;
8080
use primitives::{
81-
generic::Digest, ApplyResult, ApplyError, DispatchError, Error as PrimitiveError,
81+
generic::Digest, ApplyResult, ApplyOutcome, ApplyError, DispatchError, Error as PrimitiveError,
8282
traits::{
8383
self, Header, Zero, One, Checkable, Applyable, CheckEqual, OnFinalize,
8484
OnInitialize, NumberFor, Block as BlockT, OffchainWorker,
@@ -231,8 +231,8 @@ where
231231
fn apply_extrinsic_no_note(uxt: Block::Extrinsic) {
232232
let l = uxt.encode().len();
233233
match Self::apply_extrinsic_with_len(uxt, l, None) {
234-
ApplyResult::Success => (),
235-
ApplyResult::DispatchError(e) => {
234+
Ok(ApplyOutcome::Success) => (),
235+
Ok(ApplyOutcome::Fail(e)) => {
236236
runtime_io::print("Error:");
237237
// as u8 first to ensure not using sign-extend
238238
runtime_io::print(e.module as u8 as u64);
@@ -241,10 +241,10 @@ where
241241
runtime_io::print(msg);
242242
}
243243
},
244-
ApplyResult::ApplyError(ApplyError::CantPay) => panic!("All extrinsics should have sender able to pay their fees"),
245-
ApplyResult::ApplyError(ApplyError::BadSignature) => panic!("All extrinsics should be properly signed"),
246-
ApplyResult::ApplyError(ApplyError::Stale) | ApplyResult::ApplyError(ApplyError::Future) => panic!("All extrinsics should have the correct nonce"),
247-
ApplyResult::ApplyError(ApplyError::FullBlock) => panic!("Extrinsics should not exceed block limit"),
244+
Err(ApplyError::CantPay) => panic!("All extrinsics should have sender able to pay their fees"),
245+
Err(ApplyError::BadSignature) => panic!("All extrinsics should be properly signed"),
246+
Err(ApplyError::Stale) | Err(ApplyError::Future) => panic!("All extrinsics should have the correct nonce"),
247+
Err(ApplyError::FullBlock) => panic!("Extrinsics should not exceed block limit"),
248248
}
249249
}
250250

@@ -255,56 +255,49 @@ where
255255
to_note: Option<Vec<u8>>,
256256
) -> ApplyResult {
257257
// Verify that the signature is good.
258-
match uxt.check(&Default::default()) {
259-
Err(_) => ApplyResult::ApplyError(ApplyError::BadSignature),
260-
Ok(xt) => {
261-
// Check the weight of the block if that extrinsic is applied.
262-
let weight = xt.weight(encoded_len);
263-
if <system::Module<System>>::all_extrinsics_weight() + weight > internal::MAX_TRANSACTIONS_WEIGHT {
264-
return ApplyResult::ApplyError(ApplyError::FullBlock);
265-
}
258+
let xt = uxt.check(&Default::default()).map_err(|_| ApplyError::BadSignature)?;
259+
// Check the weight of the block if that extrinsic is applied.
260+
let weight = xt.weight(encoded_len);
261+
if <system::Module<System>>::all_extrinsics_weight() + weight > internal::MAX_TRANSACTIONS_WEIGHT {
262+
return Err(ApplyError::FullBlock);
263+
}
266264

267-
if let (Some(sender), Some(index)) = (xt.sender(), xt.index()) {
268-
// check index
269-
let expected_index = <system::Module<System>>::account_nonce(sender);
270-
if index != &expected_index {
271-
return if index < &expected_index {
272-
ApplyResult::ApplyError(ApplyError::Stale)
273-
} else {
274-
ApplyResult::ApplyError(ApplyError::Future)
275-
}
276-
}
277-
// pay any fees
278-
// TODO: propagate why can't pay
279-
match Payment::make_payment(sender, encoded_len) {
280-
Err(_) => return ApplyResult::ApplyError(ApplyError::CantPay),
281-
Ok(_) => ()
282-
};
283-
284-
// AUDIT: Under no circumstances may this function panic from here onwards.
285-
// FIXME: ensure this at compile-time (such as by not defining a panic function, forcing
286-
// a linker error unless the compiler can prove it cannot be called).
287-
// increment nonce in storage
288-
<system::Module<System>>::inc_account_nonce(sender);
265+
if let (Some(sender), Some(index)) = (xt.sender(), xt.index()) {
266+
// check index
267+
let expected_index = <system::Module<System>>::account_nonce(sender);
268+
if index != &expected_index {
269+
return if index < &expected_index {
270+
Err(ApplyError::Stale)
271+
} else {
272+
Err(ApplyError::Future)
289273
}
274+
}
275+
// pay any fees
276+
// TODO: propagate why can't pay
277+
Payment::make_payment(sender, encoded_len).map_err(|_| ApplyError::CantPay)?;
278+
279+
// AUDIT: Under no circumstances may this function panic from here onwards.
280+
// FIXME: ensure this at compile-time (such as by not defining a panic function, forcing
281+
// a linker error unless the compiler can prove it cannot be called).
282+
// increment nonce in storage
283+
<system::Module<System>>::inc_account_nonce(sender);
284+
}
290285

291-
// Make sure to `note_extrinsic` only after we know it's going to be executed
292-
// to prevent it from leaking in storage.
293-
if let Some(encoded) = to_note {
294-
<system::Module<System>>::note_extrinsic(encoded);
295-
}
286+
// Make sure to `note_extrinsic` only after we know it's going to be executed
287+
// to prevent it from leaking in storage.
288+
if let Some(encoded) = to_note {
289+
<system::Module<System>>::note_extrinsic(encoded);
290+
}
296291

297-
// Decode parameters and dispatch
298-
let (f, s) = xt.deconstruct();
299-
let r = f.dispatch(s.into()).map_err(Into::<DispatchError>::into);
300-
<system::Module<System>>::note_applied_extrinsic(&r, encoded_len as u32);
292+
// Decode parameters and dispatch
293+
let (f, s) = xt.deconstruct();
294+
let r = f.dispatch(s.into()).map_err(Into::<DispatchError>::into);
295+
<system::Module<System>>::note_applied_extrinsic(&r, encoded_len as u32);
301296

302-
match r {
303-
Ok(_) => ApplyResult::Success,
304-
Err(e) => ApplyResult::DispatchError(e),
305-
}
306-
}
307-
}
297+
Ok(match r {
298+
Ok(_) => ApplyOutcome::Success,
299+
Err(e) => ApplyOutcome::Fail(e),
300+
})
308301
}
309302

310303
fn final_checks(header: &System::Header) {

0 commit comments

Comments
 (0)