From 6314a0e27f98a761d9d90383134fee3637b833b0 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Fri, 9 Sep 2022 15:29:47 -0600 Subject: [PATCH 1/3] Call post_dispatch for CheckedSignature::SelfContained --- .../self-contained/src/checked_extrinsic.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/primitives/self-contained/src/checked_extrinsic.rs b/primitives/self-contained/src/checked_extrinsic.rs index ed61c2844e..3a66a5d3a5 100644 --- a/primitives/self-contained/src/checked_extrinsic.rs +++ b/primitives/self-contained/src/checked_extrinsic.rs @@ -144,9 +144,23 @@ 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) } } } From 623fb0799762a2c8472ab1cb0be713a632e391ee Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Sat, 10 Sep 2022 12:11:24 +0200 Subject: [PATCH 2/3] add tests --- 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 | 2 - 4 files changed, 132 insertions(+), 2 deletions(-) diff --git a/frame/ethereum/src/tests/eip1559.rs b/frame/ethereum/src/tests/eip1559.rs index 7957de633d..cde38f9f55 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 transaction_with_extra_gas_should_adjust_weight() { + 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 e64fcd950f..0bc001d295 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 transaction_with_extra_gas_should_adjust_weight() { + 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 5af2e2fe02..fe13067dcf 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 transaction_with_extra_gas_should_adjust_weight() { + 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 3a66a5d3a5..ed1045189e 100644 --- a/primitives/self-contained/src/checked_extrinsic.rs +++ b/primitives/self-contained/src/checked_extrinsic.rs @@ -151,7 +151,6 @@ where Ok(info) => info, Err(err) => err.post_info, }; - Extra::post_dispatch( None, info, @@ -159,7 +158,6 @@ where len, &res.map(|_| ()).map_err(|e| e.error), )?; - Ok(res) } } From b75db324ef4b6adf22db8cd36de0e9f7535eaf7d Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Sat, 10 Sep 2022 12:12:37 +0200 Subject: [PATCH 3/3] rename tests --- frame/ethereum/src/tests/eip1559.rs | 2 +- frame/ethereum/src/tests/eip2930.rs | 2 +- frame/ethereum/src/tests/legacy.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/ethereum/src/tests/eip1559.rs b/frame/ethereum/src/tests/eip1559.rs index cde38f9f55..a059cf3672 100644 --- a/frame/ethereum/src/tests/eip1559.rs +++ b/frame/ethereum/src/tests/eip1559.rs @@ -409,7 +409,7 @@ fn call_should_handle_errors() { } #[test] -fn transaction_with_extra_gas_should_adjust_weight() { +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( diff --git a/frame/ethereum/src/tests/eip2930.rs b/frame/ethereum/src/tests/eip2930.rs index 0bc001d295..49b9c66c18 100644 --- a/frame/ethereum/src/tests/eip2930.rs +++ b/frame/ethereum/src/tests/eip2930.rs @@ -332,7 +332,7 @@ fn call_should_handle_errors() { } #[test] -fn transaction_with_extra_gas_should_adjust_weight() { +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( diff --git a/frame/ethereum/src/tests/legacy.rs b/frame/ethereum/src/tests/legacy.rs index fe13067dcf..0996b1d8da 100644 --- a/frame/ethereum/src/tests/legacy.rs +++ b/frame/ethereum/src/tests/legacy.rs @@ -332,7 +332,7 @@ fn call_should_handle_errors() { } #[test] -fn transaction_with_extra_gas_should_adjust_weight() { +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(