Skip to content

Commit

Permalink
Fix post dispatch weight (paritytech#851)
Browse files Browse the repository at this point in the history
* Call post_dispatch for CheckedSignature::SelfContained

* add tests

* rename tests

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
  • Loading branch information
notlesh and nbaztec committed Sep 20, 2022
1 parent 96ba32a commit d3beddc
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
44 changes: 44 additions & 0 deletions frame/ethereum/src/tests/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::transact {
transaction: signed,
};
let source = call.check_self_contained().unwrap().unwrap();
let extrinsic = CheckedExtrinsic::<_, _, frame_system::CheckWeight<Test>, _> {
signed: fp_self_contained::CheckedSignature::SelfContained(source),
function: Call::Ethereum(call),
};
let dispatch_info = extrinsic.get_dispatch_info();
let post_dispatch_weight = extrinsic
.apply::<Test>(&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::<Test>::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
);
});
}
44 changes: 44 additions & 0 deletions frame/ethereum/src/tests/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::transact {
transaction: signed,
};
let source = call.check_self_contained().unwrap().unwrap();
let extrinsic = CheckedExtrinsic::<_, _, frame_system::CheckWeight<Test>, _> {
signed: fp_self_contained::CheckedSignature::SelfContained(source),
function: Call::Ethereum(call),
};
let dispatch_info = extrinsic.get_dispatch_info();
let post_dispatch_weight = extrinsic
.apply::<Test>(&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::<Test>::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
);
});
}
44 changes: 44 additions & 0 deletions frame/ethereum/src/tests/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::transact {
transaction: signed,
};
let source = call.check_self_contained().unwrap().unwrap();
let extrinsic = CheckedExtrinsic::<_, _, frame_system::CheckWeight<Test>, _> {
signed: fp_self_contained::CheckedSignature::SelfContained(source),
function: Call::Ethereum(call),
};
let dispatch_info = extrinsic.get_dispatch_info();
let post_dispatch_weight = extrinsic
.apply::<Test>(&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::<Test>::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
);
});
}
16 changes: 14 additions & 2 deletions primitives/self-contained/src/checked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit d3beddc

Please sign in to comment.