Per-transaction weight for srml (#2799)

* debug checkpoint.

* new

* Worked.

* Worked and weight propagated to executive.

* Works with some tests.

* Cleanup debug prints.

* More cleanup.

* Undo more logs.

* Undo a few more.

* Fix build.

* Allow len to be used in weight calculation.

* Remove noop function from dispath.

* Cleanup.

* Unify traits.

* Update docs and nits.

* line width

* Update core/sr-primitives/src/weights.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update core/sr-primitives/src/weights.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update core/sr-primitives/src/weights.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update core/sr-primitives/src/weights.rs

Co-Authored-By: Amar Singh <asinghchrony@protonmail.com>

* Update srml/example/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Final cleanup.

* Fix build.
This commit is contained in:
Kian Peymani
2019-06-12 14:38:30 +02:00
committed by GitHub
parent e53e9e9bda
commit 3496f11404
15 changed files with 288 additions and 54 deletions
+49 -18
View File
@@ -82,14 +82,15 @@ use primitives::traits::{
OnInitialize, Digest, NumberFor, Block as BlockT, OffchainWorker,
ValidateUnsigned, DigestItem,
};
use primitives::weights::Weighable;
use primitives::{ApplyOutcome, ApplyError};
use primitives::transaction_validity::{TransactionValidity, TransactionPriority, TransactionLongevity};
use srml_support::{Dispatchable, traits::MakePayment};
use parity_codec::{Codec, Encode};
use system::extrinsics_root;
use primitives::{ApplyOutcome, ApplyError};
use primitives::transaction_validity::{TransactionValidity, TransactionPriority, TransactionLongevity};
mod internal {
pub const MAX_TRANSACTIONS_SIZE: u32 = 4 * 1024 * 1024;
pub const MAX_TRANSACTIONS_WEIGHT: u32 = 4 * 1024 * 1024;
pub enum ApplyError {
BadSignature(&'static str),
@@ -125,9 +126,11 @@ impl<
> ExecuteBlock<Block> for Executive<System, Block, Context, Payment, UnsignedValidator, AllModules>
where
Block::Extrinsic: Checkable<Context> + Codec,
<Block::Extrinsic as Checkable<Context>>::Checked: Applyable<Index=System::Index, AccountId=System::AccountId>,
<Block::Extrinsic as Checkable<Context>>::Checked:
Applyable<Index=System::Index, AccountId=System::AccountId> + Weighable,
<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call: Dispatchable,
<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call as Dispatchable>::Origin: From<Option<System::AccountId>>,
<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call as Dispatchable>::Origin:
From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call=<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call>
{
fn execute_block(block: Block) {
@@ -145,9 +148,11 @@ impl<
> Executive<System, Block, Context, Payment, UnsignedValidator, AllModules>
where
Block::Extrinsic: Checkable<Context> + Codec,
<Block::Extrinsic as Checkable<Context>>::Checked: Applyable<Index=System::Index, AccountId=System::AccountId>,
<Block::Extrinsic as Checkable<Context>>::Checked:
Applyable<Index=System::Index, AccountId=System::AccountId> + Weighable,
<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call: Dispatchable,
<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call as Dispatchable>::Origin: From<Option<System::AccountId>>,
<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call as Dispatchable>::Origin:
From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call=<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call>
{
/// Start the execution of a particular block.
@@ -157,7 +162,12 @@ where
Self::initialize_block_impl(header.number(), header.parent_hash(), header.extrinsics_root(), &digests);
}
fn initialize_block_impl(block_number: &System::BlockNumber, parent_hash: &System::Hash, extrinsics_root: &System::Hash, digest: &System::Digest) {
fn initialize_block_impl(
block_number: &System::BlockNumber,
parent_hash: &System::Hash,
extrinsics_root: &System::Hash,
digest: &System::Digest
) {
<system::Module<System>>::initialize(block_number, parent_hash, extrinsics_root, digest);
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
}
@@ -168,7 +178,8 @@ where
// Check that `parent_hash` is correct.
let n = header.number().clone();
assert!(
n > System::BlockNumber::zero() && <system::Module<System>>::block_hash(n - System::BlockNumber::one()) == *header.parent_hash(),
n > System::BlockNumber::zero()
&& <system::Module<System>>::block_hash(n - System::BlockNumber::one()) == *header.parent_hash(),
"Parent hash should be valid."
);
@@ -195,6 +206,7 @@ where
/// Execute given extrinsics and take care of post-extrinsics book-keeping.
fn execute_extrinsics_with_book_keeping(extrinsics: Vec<Block::Extrinsic>, block_number: NumberFor<Block>) {
extrinsics.into_iter().for_each(Self::apply_extrinsic_no_note);
// post-extrinsics book-keeping
@@ -244,12 +256,17 @@ where
}
/// Actually apply an extrinsic given its `encoded_len`; this doesn't note its hash.
fn apply_extrinsic_with_len(uxt: Block::Extrinsic, encoded_len: usize, to_note: Option<Vec<u8>>) -> result::Result<internal::ApplyOutcome, internal::ApplyError> {
fn apply_extrinsic_with_len(
uxt: Block::Extrinsic,
encoded_len: usize,
to_note: Option<Vec<u8>>
) -> result::Result<internal::ApplyOutcome, internal::ApplyError> {
// Verify that the signature is good.
let xt = uxt.check(&Default::default()).map_err(internal::ApplyError::BadSignature)?;
// Check the size of the block if that extrinsic is applied.
if <system::Module<System>>::all_extrinsics_len() + encoded_len as u32 > internal::MAX_TRANSACTIONS_SIZE {
// Check the weight of the block if that extrinsic is applied.
let weight = xt.weight(encoded_len);
if <system::Module<System>>::all_extrinsics_weight() + weight > internal::MAX_TRANSACTIONS_WEIGHT {
return Err(internal::ApplyError::FullBlock);
}
@@ -259,7 +276,6 @@ where
if index != &expected_index { return Err(
if index < &expected_index { internal::ApplyError::Stale } else { internal::ApplyError::Future }
) }
// pay any fees
Payment::make_payment(sender, encoded_len).map_err(|_| internal::ApplyError::CantPay)?;
@@ -534,27 +550,27 @@ mod tests {
}
#[test]
fn block_size_limit_enforced() {
fn block_weight_limit_enforced() {
let run_test = |should_fail: bool| {
let mut t = new_test_ext();
let xt = primitives::testing::TestXt(Some(1), 0, Call::transfer(33, 69));
let xt2 = primitives::testing::TestXt(Some(1), 1, Call::transfer(33, 69));
let encoded = xt2.encode();
let len = if should_fail { (internal::MAX_TRANSACTIONS_SIZE - 1) as usize } else { encoded.len() };
let len = if should_fail { (internal::MAX_TRANSACTIONS_WEIGHT - 1) as usize } else { encoded.len() };
with_externalities(&mut t, || {
Executive::initialize_block(&Header::new(1, H256::default(), H256::default(), [69u8; 32].into(), Digest::default()));
assert_eq!(<system::Module<Runtime>>::all_extrinsics_len(), 0);
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), 0);
Executive::apply_extrinsic(xt).unwrap();
let res = Executive::apply_extrinsic_with_len(xt2, len, Some(encoded));
if should_fail {
assert!(res.is_err());
assert_eq!(<system::Module<Runtime>>::all_extrinsics_len(), 28);
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), 28);
assert_eq!(<system::Module<Runtime>>::extrinsic_index(), Some(1));
} else {
assert!(res.is_ok());
assert_eq!(<system::Module<Runtime>>::all_extrinsics_len(), 56);
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), 56);
assert_eq!(<system::Module<Runtime>>::extrinsic_index(), Some(2));
}
});
@@ -564,6 +580,21 @@ mod tests {
run_test(true);
}
#[test]
fn default_block_weight() {
let xt = primitives::testing::TestXt(None, 0, Call::set_balance(33, 69, 69));
let mut t = new_test_ext();
with_externalities(&mut t, || {
Executive::apply_extrinsic(xt.clone()).unwrap();
Executive::apply_extrinsic(xt.clone()).unwrap();
Executive::apply_extrinsic(xt.clone()).unwrap();
assert_eq!(
<system::Module<Runtime>>::all_extrinsics_weight(),
3 * (0 /*base*/ + 22 /*len*/ * 1 /*byte*/)
);
});
}
#[test]
fn validate_unsigned() {
let xt = primitives::testing::TestXt(None, 0, Call::set_balance(33, 69, 69));