Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post dispatch weight #851

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implies that we are adding Substrate`s notion of base weight and Ethereum's. I don't believe these should be added in this way, as they both account for the same things (signature verification and basic accounting of fees).

It would be great if we could convince substrate not to include the extrinsic_base_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