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 1 commit
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 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::<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 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::<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 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::<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
);
});
}
2 changes: 0 additions & 2 deletions primitives/self-contained/src/checked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,13 @@ where
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