Skip to content

Commit 2c87eac

Browse files
authored
Split EVM executor from EvmSpec (#8825)
This PR splits up the EVM executor used in the testing infrastructure into an EvmSpec part which is useful for looking up the precompile contracts belonging to a certain fork. It's also able to get the entire configuration for the EVM at a certain fork by just passing the fork name, which is useful in e.g. BenchmarkSubCommand. I looked around and couldn't find a way to do this elsewhere. In production we only seem to get the protocol schedule through timestamps or block numbers given a pre-populated set of milestones. Even in production, at some stage as the number of forks grow, this whole set of milestones might be wasteful at startup since the list of configured hardforks doesn't change after the initial setup and until process restart.
1 parent 27b84a3 commit 2c87eac

31 files changed

+1040
-967
lines changed

ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/benchmarks/AltBN128Benchmark.java

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
*/
1515
package org.hyperledger.besu.evmtool.benchmarks;
1616

17+
import org.hyperledger.besu.datatypes.Address;
1718
import org.hyperledger.besu.evm.EvmSpecVersion;
18-
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
19+
import org.hyperledger.besu.evm.fluent.EvmSpec;
1920
import org.hyperledger.besu.evm.precompile.AbstractAltBnPrecompiledContract;
20-
import org.hyperledger.besu.evm.precompile.AltBN128AddPrecompiledContract;
21-
import org.hyperledger.besu.evm.precompile.AltBN128MulPrecompiledContract;
22-
import org.hyperledger.besu.evm.precompile.AltBN128PairingPrecompiledContract;
21+
import org.hyperledger.besu.evm.precompile.PrecompiledContract;
2322

2423
import java.io.PrintStream;
2524
import java.util.LinkedHashMap;
@@ -51,17 +50,13 @@ public void runBenchmark(final Boolean attemptNative, final String fork) {
5150
}
5251
output.println(
5352
AbstractAltBnPrecompiledContract.isNative() ? "Native AltBN128" : "Java AltBN128");
54-
GasCalculator gasCalculator = gasCalculatorForFork(fork);
5553

56-
benchmarkAdd(output, gasCalculator, forkVersion);
57-
benchmarkMul(output, gasCalculator, forkVersion);
58-
benchmarkPairings(output, gasCalculator, forkVersion);
54+
benchmarkAdd(output, forkVersion);
55+
benchmarkMul(output, forkVersion);
56+
benchmarkPairings(output, forkVersion);
5957
}
6058

61-
private void benchmarkAdd(
62-
final PrintStream output,
63-
final GasCalculator gasCalculator,
64-
final EvmSpecVersion forkVersion) {
59+
private void benchmarkAdd(final PrintStream output, final EvmSpecVersion forkVersion) {
6560
final Map<String, Bytes> addTestCases = new LinkedHashMap<>();
6661
addTestCases.put(
6762
"Add",
@@ -71,10 +66,8 @@ private void benchmarkAdd(
7166
+ "17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa9"
7267
+ "2e83f8d734803fc370eba25ed1f6b8768bd6d83887b87165fc2434fe11a830cb"));
7368

74-
AltBN128AddPrecompiledContract addContract =
75-
EvmSpecVersion.ISTANBUL.compareTo(forkVersion) < 0
76-
? AltBN128AddPrecompiledContract.byzantium(gasCalculator)
77-
: AltBN128AddPrecompiledContract.istanbul(gasCalculator);
69+
PrecompiledContract addContract =
70+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.ALTBN128_ADD);
7871
warmIterations = MATH_WARMUP / addTestCases.size();
7972
execIterations = MATH_ITERATIONS / addTestCases.size();
8073
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -90,10 +83,7 @@ private void benchmarkAdd(
9083
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
9184
}
9285

93-
private void benchmarkMul(
94-
final PrintStream output,
95-
final GasCalculator gasCalculator,
96-
final EvmSpecVersion forkVersion) {
86+
private void benchmarkMul(final PrintStream output, final EvmSpecVersion forkVersion) {
9787
final Map<String, Bytes> mulTestCases = new LinkedHashMap<>();
9888
mulTestCases.put(
9989
"mul",
@@ -103,10 +93,8 @@ private void benchmarkMul(
10393
+ "17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa9"
10494
+ "2e83f8d734803fc370eba25ed1f6b8768bd6d83887b87165fc2434fe11a830cb"));
10595

106-
AltBN128MulPrecompiledContract mulContract =
107-
EvmSpecVersion.ISTANBUL.compareTo(forkVersion) < 0
108-
? AltBN128MulPrecompiledContract.byzantium(gasCalculator)
109-
: AltBN128MulPrecompiledContract.istanbul(gasCalculator);
96+
PrecompiledContract mulContract =
97+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.ALTBN128_MUL);
11098
warmIterations = MATH_WARMUP / mulTestCases.size();
11199
execIterations = MATH_ITERATIONS / mulTestCases.size();
112100
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -122,10 +110,7 @@ private void benchmarkMul(
122110
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
123111
}
124112

125-
private void benchmarkPairings(
126-
final PrintStream output,
127-
final GasCalculator gasCalculator,
128-
final EvmSpecVersion forkVersion) {
113+
private void benchmarkPairings(final PrintStream output, final EvmSpecVersion forkVersion) {
129114
final Bytes[] pairings = {
130115
Bytes.fromHexString(
131116
"0x0fc6ebd1758207e311a99674dc77d28128643c057fb9ca2c92b4205b6bf57ed2"
@@ -167,10 +152,8 @@ private void benchmarkPairings(
167152
+ "1cacce8776f5ada6b35036f9343faab26c91b9aea83d3cb59cf5628ffe18ab1b"
168153
+ "03b48ca7e6d84fca619aaf81745fbf9c30e5a78ed4766cc62b0f12aea5044f56")
169154
};
170-
final AltBN128PairingPrecompiledContract contract =
171-
EvmSpecVersion.ISTANBUL.compareTo(forkVersion) < 0
172-
? AltBN128PairingPrecompiledContract.byzantium(gasCalculator)
173-
: AltBN128PairingPrecompiledContract.istanbul(gasCalculator);
155+
final PrecompiledContract contract =
156+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.ALTBN128_PAIRING);
174157

175158
warmIterations = MATH_WARMUP / 20;
176159
execIterations = MATH_ITERATIONS / 20;

ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/benchmarks/BLS12Benchmark.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
*/
1515
package org.hyperledger.besu.evmtool.benchmarks;
1616

17+
import org.hyperledger.besu.datatypes.Address;
1718
import org.hyperledger.besu.evm.EvmSpecVersion;
19+
import org.hyperledger.besu.evm.fluent.EvmSpec;
1820
import org.hyperledger.besu.evm.precompile.AbstractBLS12PrecompiledContract;
19-
import org.hyperledger.besu.evm.precompile.BLS12G1AddPrecompiledContract;
20-
import org.hyperledger.besu.evm.precompile.BLS12G1MultiExpPrecompiledContract;
21-
import org.hyperledger.besu.evm.precompile.BLS12G2AddPrecompiledContract;
22-
import org.hyperledger.besu.evm.precompile.BLS12G2MultiExpPrecompiledContract;
23-
import org.hyperledger.besu.evm.precompile.BLS12MapFp2ToG2PrecompiledContract;
24-
import org.hyperledger.besu.evm.precompile.BLS12MapFpToG1PrecompiledContract;
25-
import org.hyperledger.besu.evm.precompile.BLS12PairingPrecompiledContract;
21+
import org.hyperledger.besu.evm.precompile.PrecompiledContract;
2622

2723
import java.io.PrintStream;
2824
import java.util.LinkedHashMap;
@@ -167,22 +163,23 @@ public void runBenchmark(final Boolean attemptNative, final String fork) {
167163
return;
168164
}
169165

170-
benchmarkG1Add(output);
171-
benchmarkG1MultiExp32Pairs(output);
172-
benchmarkMapFpToG1(output);
173-
benchmarkG2Add(output);
174-
benchmarkG2MultiExp32Pairs(output);
175-
benchmarkMapFp2ToG2(output);
176-
benchmarkBlsPairing(output);
166+
benchmarkG1Add(output, forkVersion);
167+
benchmarkG1MultiExp32Pairs(output, forkVersion);
168+
benchmarkMapFpToG1(output, forkVersion);
169+
benchmarkG2Add(output, forkVersion);
170+
benchmarkG2MultiExp32Pairs(output, forkVersion);
171+
benchmarkMapFp2ToG2(output, forkVersion);
172+
benchmarkBlsPairing(output, forkVersion);
177173
}
178174

179-
private void benchmarkG1Add(final PrintStream output) {
175+
private void benchmarkG1Add(final PrintStream output, final EvmSpecVersion forkVersion) {
180176
final Map<String, Bytes> testCases = new LinkedHashMap<>();
181177
for (int i = 0; i < g1PointPairs.length - 1; i++) {
182178
testCases.put("G1 Add " + i, Bytes.fromHexString(g1PointPairs[i] + g1PointPairs[i + 1]));
183179
}
184180

185-
BLS12G1AddPrecompiledContract g1addContract = new BLS12G1AddPrecompiledContract();
181+
PrecompiledContract g1addContract =
182+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.BLS12_G1ADD);
186183
warmIterations = MATH_WARMUP / testCases.size();
187184
execIterations = MATH_ITERATIONS / testCases.size();
188185
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -198,7 +195,8 @@ private void benchmarkG1Add(final PrintStream output) {
198195
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
199196
}
200197

201-
private void benchmarkG1MultiExp32Pairs(final PrintStream output) {
198+
private void benchmarkG1MultiExp32Pairs(
199+
final PrintStream output, final EvmSpecVersion forkVersion) {
202200
final Map<String, Bytes> testCases = new LinkedHashMap<>();
203201

204202
// add test cases for 2, 4, 8, 16, and 32 point/scalar pairs
@@ -210,7 +208,8 @@ private void benchmarkG1MultiExp32Pairs(final PrintStream output) {
210208
testCases.put("G1 MSM " + (1 << i) + " pairs", Bytes.fromHexString(g1msmPairs.toString()));
211209
}
212210

213-
BLS12G1MultiExpPrecompiledContract g1msmContract = new BLS12G1MultiExpPrecompiledContract();
211+
PrecompiledContract g1msmContract =
212+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.BLS12_G1MULTIEXP);
214213
warmIterations = MATH_WARMUP / testCases.size();
215214
execIterations = MATH_ITERATIONS / testCases.size();
216215
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -224,13 +223,16 @@ private void benchmarkG1MultiExp32Pairs(final PrintStream output) {
224223
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
225224
}
226225

227-
private void benchmarkMapFpToG1(final PrintStream output) {
226+
private void benchmarkMapFpToG1(final PrintStream output, final EvmSpecVersion forkVersion) {
228227
final Map<String, Bytes> testCases = new LinkedHashMap<>();
229228
for (int i = 0; i < g1PointPairs.length; i++) {
230229
testCases.put("Map Fp to G1 " + i, Bytes.fromHexString(g1PointPairs[i].substring(0, 128)));
231230
}
232231

233-
BLS12MapFpToG1PrecompiledContract g1MapFpToG1Contract = new BLS12MapFpToG1PrecompiledContract();
232+
PrecompiledContract g1MapFpToG1Contract =
233+
EvmSpec.evmSpec(forkVersion)
234+
.getPrecompileContractRegistry()
235+
.get(Address.BLS12_MAP_FP_TO_G1);
234236
warmIterations = MATH_WARMUP / testCases.size();
235237
execIterations = MATH_ITERATIONS / testCases.size();
236238
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -247,13 +249,14 @@ private void benchmarkMapFpToG1(final PrintStream output) {
247249
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
248250
}
249251

250-
private void benchmarkG2Add(final PrintStream output) {
252+
private void benchmarkG2Add(final PrintStream output, final EvmSpecVersion forkVersion) {
251253
final Map<String, Bytes> testCases = new LinkedHashMap<>();
252254
for (int i = 0; i < g2PointPairs.length - 1; i++) {
253255
testCases.put("G2 Add " + i, Bytes.fromHexString(g2PointPairs[i] + g2PointPairs[i + 1]));
254256
}
255257

256-
BLS12G2AddPrecompiledContract g1addContract = new BLS12G2AddPrecompiledContract();
258+
PrecompiledContract g1addContract =
259+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.BLS12_G2ADD);
257260
warmIterations = MATH_WARMUP / testCases.size();
258261
execIterations = MATH_ITERATIONS / testCases.size();
259262
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -269,7 +272,8 @@ private void benchmarkG2Add(final PrintStream output) {
269272
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
270273
}
271274

272-
private void benchmarkG2MultiExp32Pairs(final PrintStream output) {
275+
private void benchmarkG2MultiExp32Pairs(
276+
final PrintStream output, final EvmSpecVersion forkVersion) {
273277
final Map<String, Bytes> testCases = new LinkedHashMap<>();
274278

275279
// add test cases for 2, 4, 8, 16, and 32 point/scalar pairs
@@ -281,7 +285,8 @@ private void benchmarkG2MultiExp32Pairs(final PrintStream output) {
281285
testCases.put("G2 MSM " + (1 << i) + " pairs", Bytes.fromHexString(g2msmPairs.toString()));
282286
}
283287

284-
BLS12G2MultiExpPrecompiledContract g2msmContract = new BLS12G2MultiExpPrecompiledContract();
288+
PrecompiledContract g2msmContract =
289+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.BLS12_G2MULTIEXP);
285290
warmIterations = MATH_WARMUP / testCases.size();
286291
execIterations = MATH_ITERATIONS / testCases.size();
287292
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -295,14 +300,16 @@ private void benchmarkG2MultiExp32Pairs(final PrintStream output) {
295300
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
296301
}
297302

298-
private void benchmarkMapFp2ToG2(final PrintStream output) {
303+
private void benchmarkMapFp2ToG2(final PrintStream output, final EvmSpecVersion forkVersion) {
299304
final Map<String, Bytes> testCases = new LinkedHashMap<>();
300305
for (int i = 0; i < g2PointPairs.length; i++) {
301306
testCases.put("Map Fp2 to G2 " + i, Bytes.fromHexString(g2PointPairs[i].substring(0, 256)));
302307
}
303308

304-
BLS12MapFp2ToG2PrecompiledContract g1MapFp2ToG2Contract =
305-
new BLS12MapFp2ToG2PrecompiledContract();
309+
PrecompiledContract g1MapFp2ToG2Contract =
310+
EvmSpec.evmSpec(forkVersion)
311+
.getPrecompileContractRegistry()
312+
.get(Address.BLS12_MAP_FP2_TO_G2);
306313
warmIterations = MATH_WARMUP / testCases.size();
307314
execIterations = MATH_ITERATIONS / testCases.size();
308315
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero
@@ -319,7 +326,7 @@ private void benchmarkMapFp2ToG2(final PrintStream output) {
319326
gasCost, execTime * 1_000_000, gasCost / execTime / 1_000_000);
320327
}
321328

322-
private void benchmarkBlsPairing(final PrintStream output) {
329+
private void benchmarkBlsPairing(final PrintStream output, final EvmSpecVersion forkVersion) {
323330
final Map<String, Bytes> testCases = new LinkedHashMap<>();
324331

325332
// add test cases for 2, 4, 8, 16, and 32 point/scalar pairs
@@ -331,7 +338,8 @@ private void benchmarkBlsPairing(final PrintStream output) {
331338
testCases.put("BLS Pairing " + (1 << i) + " pairs", Bytes.fromHexString(pairs.toString()));
332339
}
333340

334-
BLS12PairingPrecompiledContract blsPairingContract = new BLS12PairingPrecompiledContract();
341+
PrecompiledContract blsPairingContract =
342+
EvmSpec.evmSpec(forkVersion).getPrecompileContractRegistry().get(Address.BLS12_PAIRING);
335343
warmIterations = MATH_WARMUP / testCases.size();
336344
execIterations = MATH_ITERATIONS / testCases.size();
337345
double execTime = Double.MIN_VALUE; // a way to dodge divide by zero

ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/benchmarks/ModExpBenchmark.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
*/
1515
package org.hyperledger.besu.evmtool.benchmarks;
1616

17+
import org.hyperledger.besu.datatypes.Address;
18+
import org.hyperledger.besu.evm.EvmSpecVersion;
19+
import org.hyperledger.besu.evm.fluent.EvmSpec;
1720
import org.hyperledger.besu.evm.precompile.BigIntegerModularExponentiationPrecompiledContract;
21+
import org.hyperledger.besu.evm.precompile.PrecompiledContract;
1822

1923
import java.io.PrintStream;
2024
import java.util.LinkedHashMap;
@@ -399,8 +403,10 @@ public void runBenchmark(final Boolean attemptNative, final String fork) {
399403
+ "ffffffffffffffffffffffff"
400404
+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
401405

402-
final BigIntegerModularExponentiationPrecompiledContract contract =
403-
BigIntegerModularExponentiationPrecompiledContract.osaka(gasCalculatorForFork(fork));
406+
final PrecompiledContract contract =
407+
EvmSpec.evmSpec(EvmSpecVersion.fromName(fork))
408+
.getPrecompileContractRegistry()
409+
.get(Address.MODEXP);
404410

405411
if (attemptNative != null
406412
&& (!attemptNative

0 commit comments

Comments
 (0)