From d3beddc6911a559a3ecc9b3f08e153dbe37a8658 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 19 Sep 2022 21:39:37 -0600 Subject: [PATCH] Fix post dispatch weight (#851) * Call post_dispatch for CheckedSignature::SelfContained * add tests * rename tests Co-authored-by: Nisheeth Barthwal --- frame/ethereum/src/tests/eip1559.rs | 44 +++++++++++++++++++ frame/ethereum/src/tests/eip2930.rs | 44 +++++++++++++++++++ frame/ethereum/src/tests/legacy.rs | 44 +++++++++++++++++++ .../self-contained/src/checked_extrinsic.rs | 16 ++++++- 4 files changed, 146 insertions(+), 2 deletions(-) diff --git a/frame/ethereum/src/tests/eip1559.rs b/frame/ethereum/src/tests/eip1559.rs index 7957de633d2a5..a059cf3672039 100644 --- a/frame/ethereum/src/tests/eip1559.rs +++ b/frame/ethereum/src/tests/eip1559.rs @@ -407,3 +407,47 @@ fn call_should_handle_errors() { Ethereum::execute(alice.address, &t3, None).ok().unwrap(); }); } + +#[test] +fn self_contained_transaction_with_extra_gas_should_adjust_weight_with_post_dispatch() { + let (pairs, mut ext) = new_test_ext(1); + let alice = &pairs[0]; + let base_extrinsic_weight = frame_system::limits::BlockWeights::with_sensible_defaults( + 2000000000000, + sp_runtime::Perbill::from_percent(75), + ) + .per_class + .get(frame_support::weights::DispatchClass::Normal) + .base_extrinsic; + + ext.execute_with(|| { + let mut transaction = eip1559_erc20_creation_unsigned_transaction(); + transaction.gas_limit = 9_000_000.into(); + let signed = transaction.sign(&alice.private_key, None); + let call = crate::Call::::transact { + transaction: signed, + }; + let source = call.check_self_contained().unwrap().unwrap(); + let extrinsic = CheckedExtrinsic::<_, _, frame_system::CheckWeight, _> { + signed: fp_self_contained::CheckedSignature::SelfContained(source), + function: Call::Ethereum(call), + }; + let dispatch_info = extrinsic.get_dispatch_info(); + let post_dispatch_weight = extrinsic + .apply::(&dispatch_info, 0) + .unwrap() + .unwrap() + .actual_weight + .unwrap(); + + let expected_weight = base_extrinsic_weight.saturating_add(post_dispatch_weight); + let actual_weight = *frame_system::Pallet::::block_weight() + .get(frame_support::weights::DispatchClass::Normal); + assert_eq!( + expected_weight, + actual_weight, + "the block weight was unexpected, excess '{}'", + actual_weight as i128 - expected_weight as i128 + ); + }); +} diff --git a/frame/ethereum/src/tests/eip2930.rs b/frame/ethereum/src/tests/eip2930.rs index e64fcd950f3f2..49b9c66c18560 100644 --- a/frame/ethereum/src/tests/eip2930.rs +++ b/frame/ethereum/src/tests/eip2930.rs @@ -330,3 +330,47 @@ fn call_should_handle_errors() { Ethereum::execute(alice.address, &t3, None).ok().unwrap(); }); } + +#[test] +fn self_contained_transaction_with_extra_gas_should_adjust_weight_with_post_dispatch() { + let (pairs, mut ext) = new_test_ext(1); + let alice = &pairs[0]; + let base_extrinsic_weight = frame_system::limits::BlockWeights::with_sensible_defaults( + 2000000000000, + sp_runtime::Perbill::from_percent(75), + ) + .per_class + .get(frame_support::weights::DispatchClass::Normal) + .base_extrinsic; + + ext.execute_with(|| { + let mut transaction = eip2930_erc20_creation_unsigned_transaction(); + transaction.gas_limit = 9_000_000.into(); + let signed = transaction.sign(&alice.private_key, None); + let call = crate::Call::::transact { + transaction: signed, + }; + let source = call.check_self_contained().unwrap().unwrap(); + let extrinsic = CheckedExtrinsic::<_, _, frame_system::CheckWeight, _> { + signed: fp_self_contained::CheckedSignature::SelfContained(source), + function: Call::Ethereum(call), + }; + let dispatch_info = extrinsic.get_dispatch_info(); + let post_dispatch_weight = extrinsic + .apply::(&dispatch_info, 0) + .unwrap() + .unwrap() + .actual_weight + .unwrap(); + + let expected_weight = base_extrinsic_weight.saturating_add(post_dispatch_weight); + let actual_weight = *frame_system::Pallet::::block_weight() + .get(frame_support::weights::DispatchClass::Normal); + assert_eq!( + expected_weight, + actual_weight, + "the block weight was unexpected, excess '{}'", + actual_weight as i128 - expected_weight as i128 + ); + }); +} diff --git a/frame/ethereum/src/tests/legacy.rs b/frame/ethereum/src/tests/legacy.rs index 5af2e2fe02520..0996b1d8da5a0 100644 --- a/frame/ethereum/src/tests/legacy.rs +++ b/frame/ethereum/src/tests/legacy.rs @@ -330,3 +330,47 @@ fn call_should_handle_errors() { Ethereum::execute(alice.address, &t3, None).ok().unwrap(); }); } + +#[test] +fn self_contained_transaction_with_extra_gas_should_adjust_weight_with_post_dispatch() { + let (pairs, mut ext) = new_test_ext(1); + let alice = &pairs[0]; + let base_extrinsic_weight = frame_system::limits::BlockWeights::with_sensible_defaults( + 2000000000000, + sp_runtime::Perbill::from_percent(75), + ) + .per_class + .get(frame_support::weights::DispatchClass::Normal) + .base_extrinsic; + + ext.execute_with(|| { + let mut transaction = legacy_erc20_creation_unsigned_transaction(); + transaction.gas_limit = 9_000_000.into(); + let signed = transaction.sign(&alice.private_key); + let call = crate::Call::::transact { + transaction: signed, + }; + let source = call.check_self_contained().unwrap().unwrap(); + let extrinsic = CheckedExtrinsic::<_, _, frame_system::CheckWeight, _> { + signed: fp_self_contained::CheckedSignature::SelfContained(source), + function: Call::Ethereum(call), + }; + let dispatch_info = extrinsic.get_dispatch_info(); + let post_dispatch_weight = extrinsic + .apply::(&dispatch_info, 0) + .unwrap() + .unwrap() + .actual_weight + .unwrap(); + + let expected_weight = base_extrinsic_weight.saturating_add(post_dispatch_weight); + let actual_weight = *frame_system::Pallet::::block_weight() + .get(frame_support::weights::DispatchClass::Normal); + assert_eq!( + expected_weight, + actual_weight, + "the block weight was unexpected, excess '{}'", + actual_weight as i128 - expected_weight as i128 + ); + }); +} diff --git a/primitives/self-contained/src/checked_extrinsic.rs b/primitives/self-contained/src/checked_extrinsic.rs index ed61c2844e47c..ed1045189e240 100644 --- a/primitives/self-contained/src/checked_extrinsic.rs +++ b/primitives/self-contained/src/checked_extrinsic.rs @@ -144,9 +144,21 @@ where .ok_or(TransactionValidityError::Invalid( InvalidTransaction::BadProof, ))??; - Ok(self.function.apply_self_contained(signed_info).ok_or( + let res = self.function.apply_self_contained(signed_info).ok_or( TransactionValidityError::Invalid(InvalidTransaction::BadProof), - )?) + )?; + let post_info = match res { + Ok(info) => info, + Err(err) => err.post_info, + }; + Extra::post_dispatch( + None, + info, + &post_info, + len, + &res.map(|_| ()).map_err(|e| e.error), + )?; + Ok(res) } } }