Skip to content

Commit

Permalink
Merge b7531cc into 25063ba
Browse files Browse the repository at this point in the history
  • Loading branch information
ar-conmit authored Jul 4, 2022
2 parents 25063ba + b7531cc commit 4fada65
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/relay/src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,10 @@ export const predefined = {
name: 'ChainId not supported',
code: -32000,
message: 'ChainId not supported'
})
}),
'GAS_PRICE_TOO_LOW': new JsonRpcError({
name: 'Gas price too low',
code: -32009,
message: 'Gas price below configured minimum gas price'
}),
};
6 changes: 6 additions & 0 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,12 @@ export class EthImpl implements Eth {
});
}

const gasPrice = await this.getFeeWeibars();
const gasPrecheck = this.precheck.gasPrice(transaction, gasPrice);
if (!gasPrecheck.passes) {
return gasPrecheck.error;
}

const transactionBuffer = Buffer.from(EthImpl.prune0x(transaction), 'hex');

try {
Expand Down
16 changes: 16 additions & 0 deletions packages/relay/src/lib/precheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,20 @@ export class Precheck {
chainId: txChainId
};
}

gasPrice(transaction: string, gasPrice: number) {
const tx = ethers.utils.parseTransaction(transaction);
const minGasPrice = ethers.ethers.BigNumber.from(gasPrice);
const txGasPrice = tx.gasPrice || tx.maxFeePerGas!.add(tx.maxPriorityFeePerGas!);
const passes = txGasPrice.gte(minGasPrice);

if (!passes) {
this.logger.trace('Failed gas price precheck for sendRawTransaction(transaction=%s, gasPrice=%s, requiredGasPrice=%s)', transaction, txGasPrice, minGasPrice);
}

return {
passes,
error: predefined.GAS_PRICE_TOO_LOW
}
}
}
26 changes: 26 additions & 0 deletions packages/relay/tests/lib/precheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import {Precheck} from "../../src/lib/precheck";
import {MirrorNodeClient} from "../../src/lib/clients";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import constants from '../../src/lib/constants';
const logger = pino();

describe('Precheck', async function() {

const txWithMatchingChainId = '0x02f87482012a0485a7a358200085a7a3582000832dc6c09400000000000000000000000000000000000003f78502540be40080c001a006f4cd8e6f84b76a05a5c1542a08682c928108ef7163d9c1bf1f3b636b1cd1fba032097cbf2dda17a2dcc40f62c97964d9d930cdce2e8a9df9a8ba023cda28e4ad';
const txWithNonMatchingChainId = '0xf86a0385a7a3582000832dc6c09400000000000000000000000000000000000003f78502540be400801ca06750e92db52fa708e27f94f27e0cfb7f5800f9b657180bb2e94c1520cfb1fb6da01bec6045068b6db38b55017bb8b50166699384bc1791fd8331febab0cf629a2a';
const defaultGasPrice = 720_000_000_000;

let precheck: Precheck;
let mock: MockAdapter;
Expand Down Expand Up @@ -71,4 +73,28 @@ describe('Precheck', async function() {
expect(result.chainId).to.eq('0x0');
});
});

describe('gas price', async function() {
it('should return true for gas price gt to required gas price', async function() {
const result = precheck.gasPrice(txWithMatchingChainId, 10);
expect(result).to.exist;
expect(result.error).to.exist;
expect(result.passes).to.eq(true);
});

it('should return true for gas price equal to required gas price', async function() {
const result = precheck.gasPrice(txWithMatchingChainId, defaultGasPrice);
expect(result).to.exist;
expect(result.error).to.exist;
expect(result.passes).to.eq(true);
});

it('should return false for gas price not enough', async function() {
const minGasPrice = 1000 * constants.TINYBAR_TO_WEIBAR_COEF;
const result = precheck.gasPrice(txWithMatchingChainId, minGasPrice);
expect(result).to.exist;
expect(result.error).to.exist;
expect(result.passes).to.eq(false);
});
})
});
48 changes: 48 additions & 0 deletions packages/server/tests/acceptance/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('RPC Server Acceptance Tests', function () {

const CHAIN_ID = process.env.CHAIN_ID || 0;
const INCORRECT_CHAIN_ID = 999;
const GAS_PRICE_TOO_LOW = 1;
const ONE_TINYBAR = ethers.utils.parseUnits('1', 10);
const ONE_WEIBAR = ethers.utils.parseUnits('1', 18);

Expand Down Expand Up @@ -316,6 +317,17 @@ describe('RPC Server Acceptance Tests', function () {
}
});

it('should fail "eth_sendRawTransaction" for legacy EIP 155 transactions (with gas price too low)', async function () {
const transaction = {
...default155TransactionData,
gasPrice: GAS_PRICE_TOO_LOW,
to: mirrorContract.evm_address,
nonce: await relay.getAccountNonce(accounts[2].address)
};
const signedTx = await accounts[2].wallet.signTransaction(transaction);
await relay.callFailing('eth_sendRawTransaction', [signedTx], -32009, 'Gas price below configured minimum gas price');
});

it('should execute "eth_sendRawTransaction" for legacy EIP 155 transactions', async function () {
const receiverInitialBalance = await relay.getBalance(mirrorContract.evm_address);
const transaction = {
Expand Down Expand Up @@ -344,6 +356,18 @@ describe('RPC Server Acceptance Tests', function () {
await relay.callFailing('eth_sendRawTransaction', [signedTx], -32000, 'ChainId (0x0) not supported. The correct chainId is 0x12a.');
});

it('should fail "eth_sendRawTransaction" for Legacy transactions (with gas price too low)', async function () {
const transaction = {
...defaultLegacyTransactionData,
chainId: Number(CHAIN_ID),
gasPrice: GAS_PRICE_TOO_LOW,
to: mirrorContract.evm_address,
nonce: await relay.getAccountNonce(accounts[2].address)
};
const signedTx = await accounts[2].wallet.signTransaction(transaction);
await relay.callFailing('eth_sendRawTransaction', [signedTx], -32009, 'Gas price below configured minimum gas price');
});

it('should fail "eth_sendRawTransaction" for Legacy 2930 transactions', async function () {
const transaction = {
...defaultLegacy2930TransactionData,
Expand All @@ -354,6 +378,30 @@ describe('RPC Server Acceptance Tests', function () {
await relay.callFailing('eth_sendRawTransaction', [signedTx]);
});

it('should fail "eth_sendRawTransaction" for Legacy 2930 transactions (with gas price too low)', async function () {
const transaction = {
...defaultLegacy2930TransactionData,
gasPrice: GAS_PRICE_TOO_LOW,
to: mirrorContract.evm_address,
nonce: await relay.getAccountNonce(accounts[2].address)
};
console.log(transaction);
const signedTx = await accounts[2].wallet.signTransaction(transaction);
await relay.callFailing('eth_sendRawTransaction', [signedTx], -32009, 'Gas price below configured minimum gas price');
});

it('should execute "eth_sendRawTransaction" for London transactions (with gas price too low)', async function () {
const transaction = {
...defaultLondonTransactionData,
maxPriorityFeePerGas: GAS_PRICE_TOO_LOW,
maxFeePerGas: GAS_PRICE_TOO_LOW,
to: mirrorContract.evm_address,
nonce: await relay.getAccountNonce(accounts[2].address)
};
const signedTx = await accounts[2].wallet.signTransaction(transaction);
await relay.callFailing('eth_sendRawTransaction', [signedTx], -32009, 'Gas price below configured minimum gas price');
});

it('should execute "eth_sendRawTransaction" for London transactions', async function () {
const receiverInitialBalance = await relay.getBalance(mirrorContract.evm_address);

Expand Down

0 comments on commit 4fada65

Please sign in to comment.