Unsigned transactions should also note weight (#4998)

* Make unsigned also note weight

* Bump.

* Fix tests

* Better fee test

* Fix another test

* Update lock file
This commit is contained in:
Kian Paimani
2020-02-20 16:46:11 +01:00
committed by GitHub
parent 9a0b8b5be5
commit b368736be4
6 changed files with 4545 additions and 4497 deletions
+4441 -4448
View File
File diff suppressed because it is too large Load Diff
+6 -5
View File
@@ -347,7 +347,7 @@ fn full_native_block_import_works() {
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::pallet_treasury(pallet_treasury::RawEvent::Deposit(1984800000000)),
event: Event::pallet_treasury(pallet_treasury::RawEvent::Deposit(fees * 8 / 10)),
topics: vec![],
},
EventRecord {
@@ -381,13 +381,14 @@ fn full_native_block_import_works() {
).0.unwrap();
t.execute_with(|| {
let fees = transfer_fee(&xt(), fm);
assert_eq!(
Balances::total_balance(&alice()),
alice_last_known_balance - 10 * DOLLARS - transfer_fee(&xt(), fm),
alice_last_known_balance - 10 * DOLLARS - fees,
);
assert_eq!(
Balances::total_balance(&bob()),
179 * DOLLARS - transfer_fee(&xt(), fm),
179 * DOLLARS - fees,
);
let events = vec![
EventRecord {
@@ -399,7 +400,7 @@ fn full_native_block_import_works() {
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::pallet_treasury(pallet_treasury::RawEvent::Deposit(1984788199392)),
event: Event::pallet_treasury(pallet_treasury::RawEvent::Deposit(fees * 8 / 10)),
topics: vec![],
},
EventRecord {
@@ -422,7 +423,7 @@ fn full_native_block_import_works() {
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::pallet_treasury(pallet_treasury::RawEvent::Deposit(1984788199392)),
event: Event::pallet_treasury(pallet_treasury::RawEvent::Deposit(fees * 8 / 10)),
topics: vec![],
},
EventRecord {
+14 -10
View File
@@ -25,13 +25,13 @@ use sp_core::{
storage::Storage,
};
use sp_runtime::{
Fixed64,
Fixed64, Perbill,
traits::Convert,
};
use node_runtime::{
CheckedExtrinsic, Call, Runtime, Balances,
TransactionPayment, TransactionBaseFee, TransactionByteFee,
WeightFeeCoefficient, constants::currency::*,
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, TransactionBaseFee,
TransactionByteFee, WeightFeeCoefficient,
constants::currency::*,
};
use node_runtime::impls::LinearWeightToFee;
use node_primitives::Balance;
@@ -60,12 +60,12 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
GENESIS_HASH.into(),
vec![
CheckedExtrinsic {
signed: None,
function: Call::Timestamp(pallet_timestamp::Call::set(42 * 1000)),
signed: None,
function: Call::Timestamp(pallet_timestamp::Call::set(42 * 1000)),
},
CheckedExtrinsic {
signed: Some((charlie(), signed_extra(0, 0))),
function: Call::System(frame_system::Call::fill_block()),
function: Call::System(frame_system::Call::fill_block(Perbill::from_percent(90))),
}
]
);
@@ -77,8 +77,8 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
block1.1.clone(),
vec![
CheckedExtrinsic {
signed: None,
function: Call::Timestamp(pallet_timestamp::Call::set(52 * 1000)),
signed: None,
function: Call::Timestamp(pallet_timestamp::Call::set(52 * 1000)),
},
CheckedExtrinsic {
signed: Some((charlie(), signed_extra(1, 0))),
@@ -87,7 +87,11 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
]
);
println!("++ Block 1 size: {} / Block 2 size {}", block1.0.encode().len(), block2.0.encode().len());
println!(
"++ Block 1 size: {} / Block 2 size {}",
block1.0.encode().len(),
block2.0.encode().len(),
);
// execute a big block.
executor_call::<NeverNativeValue, fn() -> _>(
+2 -2
View File
@@ -81,8 +81,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 223,
impl_version: 1,
spec_version: 224,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};
+30 -13
View File
@@ -546,7 +546,7 @@ mod tests {
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(1, 211)],
}.assimilate_storage(&mut t).unwrap();
let xt = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(2, 69)));
let xt = TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(2, 69)));
let weight = xt.get_dispatch_info().weight as u64;
let mut t = sp_io::TestExternalities::new(t);
t.execute_with(|| {
@@ -626,7 +626,7 @@ mod tests {
fn bad_extrinsic_not_inserted() {
let mut t = new_test_ext(1);
// bad nonce check!
let xt = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 30, 0), Call::Balances(BalancesCall::transfer(33, 69)));
let xt = TestXt::new_signed(sign_extra(1, 30, 0), Call::Balances(BalancesCall::transfer(33, 69)));
t.execute_with(|| {
Executive::initialize_block(&Header::new(
1,
@@ -644,7 +644,7 @@ mod tests {
fn block_weight_limit_enforced() {
let mut t = new_test_ext(10000);
// given: TestXt uses the encoded len as fixed Len:
let xt = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let xt = TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let encoded = xt.encode();
let encoded_len = encoded.len() as Weight;
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get() - 175;
@@ -661,7 +661,7 @@ mod tests {
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 175);
for nonce in 0..=num_to_exhaust_block {
let xt = sp_runtime::testing::TestXt::new_signed(
let xt = TestXt::new_signed(
sign_extra(1, nonce.into(), 0), Call::Balances(BalancesCall::transfer(33, 0)),
);
let res = Executive::apply_extrinsic(xt);
@@ -681,14 +681,14 @@ mod tests {
#[test]
fn block_weight_and_size_is_stored_per_tx() {
let xt = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let x1 = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 1, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let x2 = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 2, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let xt = TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let x1 = TestXt::new_signed(sign_extra(1, 1, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let x2 = TestXt::new_signed(sign_extra(1, 2, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let len = xt.clone().encode().len() as u32;
let mut t = new_test_ext(1);
t.execute_with(|| {
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 0);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 0);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0);
assert!(Executive::apply_extrinsic(xt.clone()).unwrap().is_ok());
assert!(Executive::apply_extrinsic(x1.clone()).unwrap().is_ok());
@@ -701,13 +701,13 @@ mod tests {
let _ = <frame_system::Module<Runtime>>::finalize();
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 0);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 0);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0);
});
}
#[test]
fn validate_unsigned() {
let xt = sp_runtime::testing::TestXt::new_unsigned(Call::Balances(BalancesCall::set_balance(33, 69, 69)));
let xt = TestXt::new_unsigned(Call::Balances(BalancesCall::set_balance(33, 69, 69)));
let mut t = new_test_ext(1);
t.execute_with(|| {
@@ -716,9 +716,26 @@ mod tests {
});
}
#[test]
fn unsigned_weight_is_noted_when_applied() {
let xt = TestXt::new_unsigned(Call::Balances(BalancesCall::set_balance(33, 69, 69)));
let len = xt.clone().encode().len() as u32;
let mut t = new_test_ext(1);
t.execute_with(|| {
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 0);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0);
// This is okay -- balances transfer will panic since it requires ensure_signed.
assert_eq!(Executive::apply_extrinsic(xt), Ok(Err(DispatchError::BadOrigin)));
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), len);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), len);
});
}
#[test]
fn apply_trusted_skips_signature_check_but_not_others() {
let xt1 = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)))
let xt1 = TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)))
.badly_signed();
let mut t = new_test_ext(1);
@@ -727,7 +744,7 @@ mod tests {
assert_eq!(Executive::apply_trusted_extrinsic(xt1), Ok(Ok(())));
});
let xt2 = sp_runtime::testing::TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)))
let xt2 = TestXt::new_signed(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)))
.invalid(TransactionValidityError::Invalid(InvalidTransaction::Call));
t.execute_with(|| {
@@ -750,7 +767,7 @@ mod tests {
110,
lock,
);
let xt = sp_runtime::testing::TestXt::new_signed(
let xt = TestXt::new_signed(
sign_extra(1, 0, 0),
Call::System(SystemCall::remark(vec![1u8])),
);
+52 -19
View File
@@ -117,7 +117,7 @@ use frame_support::{
Contains, Get, ModuleToIndex, OnNewAccount, OnReapAccount, IsDeadAccount, Happened,
StoredMap
},
weights::{Weight, DispatchInfo, DispatchClass, SimpleDispatchInfo},
weights::{Weight, DispatchInfo, DispatchClass, SimpleDispatchInfo, FunctionOf},
};
use codec::{Encode, Decode, FullCodec, EncodeLike};
@@ -414,11 +414,15 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
/// A big dispatch that will disallow any other transaction to be included.
/// A dispatch that will fill the block weight up to the given ratio.
// TODO: This should only be available for testing, rather than in general usage, but
// that's not possible at present (since it's within the decl_module macro).
#[weight = SimpleDispatchInfo::MaxOperational]
fn fill_block(origin) {
#[weight = FunctionOf(
|(ratio,): (&Perbill,)| *ratio * T::MaximumBlockWeight::get(),
DispatchClass::Operational,
true,
)]
fn fill_block(origin, _ratio: Perbill) {
ensure_root(origin)?;
}
@@ -1080,6 +1084,34 @@ impl<T: Trait + Send + Sync> CheckWeight<T> {
pub fn new() -> Self {
Self(PhantomData)
}
/// Do the pre-dispatch checks. This can be applied to both signed and unsigned.
///
/// It checks and notes the new weight and length.
fn do_pre_dispatch(
info: <Self as SignedExtension>::DispatchInfo,
len: usize,
) -> Result<(), TransactionValidityError> {
let next_len = Self::check_block_length(info, len)?;
let next_weight = Self::check_weight(info)?;
AllExtrinsicsLen::put(next_len);
AllExtrinsicsWeight::put(next_weight);
Ok(())
}
/// Do the validate checks. This can be applied to both signed and unsigned.
///
/// It only checks that the block weight and length limit will not exceed.
fn do_validate(
info: <Self as SignedExtension>::DispatchInfo,
len: usize,
) -> TransactionValidity {
// ignore the next weight and length. If they return `Ok`, then it is below the limit.
let _ = Self::check_block_length(info, len)?;
let _ = Self::check_weight(info)?;
Ok(ValidTransaction { priority: Self::get_priority(info), ..Default::default() })
}
}
impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> {
@@ -1099,11 +1131,7 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> {
info: Self::DispatchInfo,
len: usize,
) -> Result<(), TransactionValidityError> {
let next_len = Self::check_block_length(info, len)?;
AllExtrinsicsLen::put(next_len);
let next_weight = Self::check_weight(info)?;
AllExtrinsicsWeight::put(next_weight);
Ok(())
Self::do_pre_dispatch(info, len)
}
fn validate(
@@ -1113,18 +1141,23 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> {
info: Self::DispatchInfo,
len: usize,
) -> TransactionValidity {
// There is no point in writing to storage here since changes are discarded. This basically
// discards any transaction which is bigger than the length or weight limit **alone**, which
// is a guarantee that it will fail in the pre-dispatch phase.
if let Err(e) = Self::check_block_length(info, len) {
return Err(e);
}
Self::do_validate(info, len)
}
if let Err(e) = Self::check_weight(info) {
return Err(e);
}
fn pre_dispatch_unsigned(
_call: &Self::Call,
info: Self::DispatchInfo,
len: usize,
) -> Result<(), TransactionValidityError> {
Self::do_pre_dispatch(info, len)
}
Ok(ValidTransaction { priority: Self::get_priority(info), ..Default::default() })
fn validate_unsigned(
_call: &Self::Call,
info: Self::DispatchInfo,
len: usize,
) -> TransactionValidity {
Self::do_validate(info, len)
}
}