Skip to content

Commit 3ef41ff

Browse files
committed
Improve block overrides E2E tests with custom contract code
1 parent 8296e0c commit 3ef41ff

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed

tests/fixtures/blockOverrides.byte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6080604052348015600e575f5ffd5b506101c58061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610034575f3560e01c80637b16441c14610038578063f8a8fd6d14610042575b5f5ffd5b61004061004c565b005b61004a61007e565b005b5f5f90505f5f90505b6103e881101561007a57808261006b919061015c565b91508080600101915050610055565b5050565b61238243036100905761008f61004c565b5b63674db1e142036100a4576100a361004c565b5b7f7914bb5b13bac6f621bc37bbf6e406fbf4472aaaaf17ec2f309a92aca4e27fc044036100d4576100d361004c565b5b73658bdf435d810c91414ec09147daa6db6240637973ffffffffffffffffffffffffffffffffffffffff164173ffffffffffffffffffffffffffffffffffffffff16036101245761012361004c565b5b565b5f819050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61016682610126565b915061017183610126565b92508282019050808211156101895761018861012f565b5b9291505056fea26469706673582212206f7542706dc282fc8d2da0c0cc27398671a845e2c6ae871e23591b016bf3969464736f6c634300081e0033

tests/fixtures/blockOverrides.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
3+
pragma solidity >=0.8.2 <0.9.0;
4+
5+
contract BlockOverrides {
6+
7+
function test() public view {
8+
// Add some hard-coded if conditions, to simulate overriding
9+
// certain block header fields. The condition body is a simple
10+
// way to check that the block header fields were indeed
11+
// overrided, resulting in higher gas usage.
12+
if (block.number == 9090) {
13+
sumValues();
14+
}
15+
16+
if (block.timestamp == 1733145057) {
17+
sumValues();
18+
}
19+
20+
if (block.prevrandao == 0x7914bb5b13bac6f621bc37bbf6e406fbf4472aaaaf17ec2f309a92aca4e27fc0) {
21+
sumValues();
22+
}
23+
24+
if (block.coinbase == 0x658Bdf435d810C91414eC09147DAA6DB62406379) {
25+
sumValues();
26+
}
27+
}
28+
29+
function sumValues() public pure {
30+
uint sum = 0;
31+
for (uint i = 0; i < 1000; i++) {
32+
sum += i;
33+
}
34+
}
35+
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"inputs": [],
4+
"name": "sumValues",
5+
"outputs": [],
6+
"stateMutability": "pure",
7+
"type": "function"
8+
},
9+
{
10+
"inputs": [],
11+
"name": "test",
12+
"outputs": [],
13+
"stateMutability": "view",
14+
"type": "function"
15+
}
16+
]

tests/web3js/estimate_gas_overrides_test.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let deployed = null
77
let contractAddress = null
88

99
before(async () => {
10-
deployed = await helpers.deployContract('storage')
10+
deployed = await helpers.deployContract('blockOverrides')
1111
contractAddress = deployed.receipt.contractAddress
1212

1313
assert.equal(deployed.receipt.status, conf.successStatus)
@@ -20,14 +20,14 @@ it('should apply block overrides on eth_estimateGas', async () => {
2020
assert.equal(receipt.contractAddress, contractAddress)
2121

2222
// Check the `block.number` value, without overrides
23-
let blockNumberSelector = deployed.contract.methods.blockNumber().encodeABI()
23+
let testFuncSelector = deployed.contract.methods.test().encodeABI()
2424
let txArgs = {
2525
from: conf.eoa.address,
2626
to: contractAddress,
27-
gas: '0x75ab',
27+
gas: '0x493E0',
2828
gasPrice: web3.utils.toHex(conf.minGasPrice),
2929
value: '0x0',
30-
data: blockNumberSelector,
30+
data: testFuncSelector,
3131
}
3232

3333
let response = await helpers.callRPCMethod(
@@ -36,28 +36,25 @@ it('should apply block overrides on eth_estimateGas', async () => {
3636
)
3737
assert.equal(response.status, 200)
3838
assert.isDefined(response.body)
39-
assert.equal(web3.utils.hexToNumber(response.body.result), 21651n)
39+
assert.equal(web3.utils.hexToNumber(response.body.result), 21473n)
4040

41-
// Override the `block.number` value to `2`.
41+
// Override the `block.number` value to `9090`.
4242
response = await helpers.callRPCMethod(
4343
'eth_estimateGas',
44-
[txArgs, 'latest', null, { number: '0x2' }]
44+
[txArgs, 'latest', null, { number: '0x2382' }]
4545
)
4646
assert.equal(response.status, 200)
4747
assert.isDefined(response.body)
48-
assert.equal(web3.utils.hexToNumber(response.body.result), 21651n)
48+
assert.equal(web3.utils.hexToNumber(response.body.result), 273693n)
4949

5050
// Check the `block.timestamp` value, without overrides
51-
let blockTimeSelector = deployed.contract.methods.blockTime().encodeABI()
52-
txArgs.data = blockTimeSelector
53-
5451
response = await helpers.callRPCMethod(
5552
'eth_estimateGas',
5653
[txArgs, 'latest', null, null]
5754
)
5855
assert.equal(response.status, 200)
5956
assert.isDefined(response.body)
60-
assert.equal(web3.utils.hexToNumber(response.body.result), 21607n)
57+
assert.equal(web3.utils.hexToNumber(response.body.result), 21473n)
6158

6259
// Override the `block.timestamp` value to `0x674DB1E1`.
6360
response = await helpers.callRPCMethod(
@@ -66,18 +63,16 @@ it('should apply block overrides on eth_estimateGas', async () => {
6663
)
6764
assert.equal(response.status, 200)
6865
assert.isDefined(response.body)
69-
assert.equal(web3.utils.hexToNumber(response.body.result), 21607n)
66+
assert.equal(web3.utils.hexToNumber(response.body.result), 273693n)
7067

7168
// Check the `block.prevrandao` value, without overrides
72-
let randomSelector = deployed.contract.methods.random().encodeABI()
73-
txArgs.data = randomSelector
74-
7569
response = await helpers.callRPCMethod(
7670
'eth_estimateGas',
7771
[txArgs, 'latest', null, null]
7872
)
7973
assert.equal(response.status, 200)
8074
assert.isDefined(response.body)
75+
assert.equal(web3.utils.hexToNumber(response.body.result), 21473n)
8176

8277
// Override the `block.prevrandao` value to `0x7914bb5b13bac6f621bc37bbf6e406fbf4472aaaaf17ec2f309a92aca4e27fc0`.
8378
let random = '0x7914bb5b13bac6f621bc37bbf6e406fbf4472aaaaf17ec2f309a92aca4e27fc0'
@@ -87,5 +82,23 @@ it('should apply block overrides on eth_estimateGas', async () => {
8782
)
8883
assert.equal(response.status, 200)
8984
assert.isDefined(response.body)
90-
assert.equal(web3.utils.hexToNumber(response.body.result), 21584n)
85+
assert.equal(web3.utils.hexToNumber(response.body.result), 273693n)
86+
87+
// Check the `block.coinbase` value, without overrides
88+
response = await helpers.callRPCMethod(
89+
'eth_estimateGas',
90+
[txArgs, 'latest', null, null]
91+
)
92+
assert.equal(response.status, 200)
93+
assert.isDefined(response.body)
94+
assert.equal(web3.utils.hexToNumber(response.body.result), 21473n)
95+
96+
// Override the `block.coinbase` value to `0x658Bdf435d810C91414eC09147DAA6DB62406379`.
97+
response = await helpers.callRPCMethod(
98+
'eth_estimateGas',
99+
[txArgs, 'latest', null, { coinbase: '0x658Bdf435d810C91414eC09147DAA6DB62406379' }]
100+
)
101+
assert.equal(response.status, 200)
102+
assert.isDefined(response.body)
103+
assert.equal(web3.utils.hexToNumber(response.body.result), 273693n)
91104
})

0 commit comments

Comments
 (0)