Skip to content

Commit 0b962f2

Browse files
authored
Add transaction cost pre-validation (polkadot-evm#465)
1 parent 4b6c808 commit 0b962f2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

frame/ethereum/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ pub mod pallet {
190190

191191
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
192192
if let Call::transact(transaction) = call {
193+
// We must ensure a transaction can pay the cost of its data bytes.
194+
// If it can't it should not be included in a block.
195+
let mut gasometer = evm::gasometer::Gasometer::new(
196+
transaction.gas_limit.low_u64(),
197+
<T as pallet_evm::Config>::config(),
198+
);
199+
let transaction_cost = match transaction.action {
200+
TransactionAction::Call(_) => {
201+
evm::gasometer::call_transaction_cost(&transaction.input)
202+
}
203+
TransactionAction::Create => {
204+
evm::gasometer::create_transaction_cost(&transaction.input)
205+
}
206+
};
207+
if gasometer.record_transaction(transaction_cost).is_err() {
208+
return InvalidTransaction::Custom(
209+
TransactionValidationError::InvalidGasLimit as u8,
210+
)
211+
.into();
212+
}
213+
193214
if let Some(chain_id) = transaction.signature.chain_id() {
194215
if chain_id != T::ChainId::get() {
195216
return InvalidTransaction::Custom(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect } from "chai";
2+
import { step } from "mocha-steps";
3+
4+
import { describeWithFrontier, customRequest } from "./util";
5+
6+
describeWithFrontier("Frontier RPC (Transaction cost)", (context) => {
7+
8+
step("should take transaction cost into account and not submit it to the pool", async function () {
9+
// Simple transfer with gas limit 0 manually signed to prevent web3 from rejecting client-side.
10+
const tx = await customRequest(context.web3, "eth_sendRawTransaction", [
11+
"0xf86180843b9aca00809412cb274aad8251c875c0bf6872b67d9983e53fdd01801ca00e28ba2dd3c5a3fd467\
12+
d4afd7aefb4a34b373314fff470bb9db743a84d674a0aa06e5994f2d07eafe1c37b4ce5471caecec29011f6f5b\
13+
f0b1a552c55ea348df35f",
14+
]);
15+
let msg =
16+
"submit transaction to pool failed: Pool(InvalidTransaction(InvalidTransaction::Custom(3)))";
17+
expect(tx.error).to.include({
18+
message: msg,
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)