Skip to content

Commit

Permalink
Add transaction cost pre-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmichel committed Sep 3, 2021
1 parent 4b6c808 commit 8a2b890
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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,
});
});
});

0 comments on commit 8a2b890

Please sign in to comment.