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

Validate transaction cost #465

Merged
merged 1 commit into from Sep 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Add transaction cost pre-validation
  • Loading branch information
tgmichel committed Sep 3, 2021
commit 8a2b890a2fb477d5fedd0e4335b00623832849ae
21 changes: 21 additions & 0 deletions frame/ethereum/src/lib.rs
Expand Up @@ -190,6 +190,27 @@ pub mod pallet {

fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
if let Call::transact(transaction) = call {
// We must ensure a transaction can pay the cost of its data bytes.
// If it can't it should not be included in a block.
let mut gasometer = evm::gasometer::Gasometer::new(
transaction.gas_limit.low_u64(),
<T as pallet_evm::Config>::config(),
);
let transaction_cost = match transaction.action {
TransactionAction::Call(_) => {
evm::gasometer::call_transaction_cost(&transaction.input)
}
TransactionAction::Create => {
evm::gasometer::create_transaction_cost(&transaction.input)
}
};
if gasometer.record_transaction(transaction_cost).is_err() {
return InvalidTransaction::Custom(
TransactionValidationError::InvalidGasLimit as u8,
)
.into();
}

if let Some(chain_id) = transaction.signature.chain_id() {
if chain_id != T::ChainId::get() {
return InvalidTransaction::Custom(
Expand Down
21 changes: 21 additions & 0 deletions ts-tests/tests/test-transaction-cost.ts
@@ -0,0 +1,21 @@
import { expect } from "chai";
import { step } from "mocha-steps";

import { describeWithFrontier, customRequest } from "./util";

describeWithFrontier("Frontier RPC (Transaction cost)", (context) => {

step("should take transaction cost into account and not submit it to the pool", async function () {
// Simple transfer with gas limit 0 manually signed to prevent web3 from rejecting client-side.
const tx = await customRequest(context.web3, "eth_sendRawTransaction", [
"0xf86180843b9aca00809412cb274aad8251c875c0bf6872b67d9983e53fdd01801ca00e28ba2dd3c5a3fd467\
d4afd7aefb4a34b373314fff470bb9db743a84d674a0aa06e5994f2d07eafe1c37b4ce5471caecec29011f6f5b\
f0b1a552c55ea348df35f",
]);
let msg =
"submit transaction to pool failed: Pool(InvalidTransaction(InvalidTransaction::Custom(3)))";
expect(tx.error).to.include({
message: msg,
});
});
});