Skip to content

Commit f5a9a0d

Browse files
feat: modify linea_getBlockTracesCountersV2 to return line counts for all modules (including ref tables and precompiles) (#814)
Resolves: #813 Signed-off-by: Tsvetan Dimitrov <[email protected]> Co-authored-by: Olivier Bégassat <[email protected]>
1 parent 5d4be7d commit f5a9a0d

File tree

14 files changed

+175
-72
lines changed

14 files changed

+175
-72
lines changed

tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ private void maybeThrowTracingExceptions() {
322322
public Map<String, Integer> getModulesLineCount() {
323323
maybeThrowTracingExceptions();
324324
final HashMap<String, Integer> modulesLineCount = new HashMap<>();
325+
325326
hub.getModulesToCount()
326327
.forEach(
327328
m ->
@@ -335,7 +336,7 @@ public Map<String, Integer> getModulesLineCount() {
335336
"Module "
336337
+ m.moduleKey()
337338
+ " not found in spillings.toml"))));
338-
modulesLineCount.put("BLOCK_TX", hub.cumulatedTxCount());
339+
modulesLineCount.put("BLOCK_TRANSACTIONS", hub.cumulatedTxCount());
339340
return modulesLineCount;
340341
}
341342

tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import net.consensys.linea.zktracer.module.euc.Euc;
4646
import net.consensys.linea.zktracer.module.exp.Exp;
4747
import net.consensys.linea.zktracer.module.ext.Ext;
48+
import net.consensys.linea.zktracer.module.gas.Gas;
4849
import net.consensys.linea.zktracer.module.hub.defer.*;
4950
import net.consensys.linea.zktracer.module.hub.fragment.*;
5051
import net.consensys.linea.zktracer.module.hub.fragment.imc.ImcFragment;
@@ -58,7 +59,8 @@
5859
import net.consensys.linea.zktracer.module.limits.Keccak;
5960
import net.consensys.linea.zktracer.module.limits.L2Block;
6061
import net.consensys.linea.zktracer.module.limits.L2L1Logs;
61-
import net.consensys.linea.zktracer.module.limits.precompiles.Blake2fRounds;
62+
import net.consensys.linea.zktracer.module.limits.precompiles.BlakeEffectiveCall;
63+
import net.consensys.linea.zktracer.module.limits.precompiles.BlakeRounds;
6264
import net.consensys.linea.zktracer.module.limits.precompiles.EcAddEffectiveCall;
6365
import net.consensys.linea.zktracer.module.limits.precompiles.EcMulEffectiveCall;
6466
import net.consensys.linea.zktracer.module.limits.precompiles.EcPairingEffectiveCall;
@@ -214,6 +216,7 @@ public void addTraceSection(TraceSection section) {
214216
private final Blockhash blockhash = new Blockhash(wcp);
215217
private final Euc euc;
216218
private final Ext ext = new Ext(this);
219+
private final Gas gas = new Gas();
217220
private final Module mul = new Mul(this);
218221
private final Mod mod = new Mod();
219222
private final Module shf = new Shf();
@@ -284,6 +287,7 @@ public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic) {
284287
this.modexpEffectiveCall = new ModexpEffectiveCall(this, this.blakeModexpData);
285288
final EcPairingEffectiveCall ecPairingCall = new EcPairingEffectiveCall(this);
286289
final L2Block l2Block = new L2Block(l2l1ContractAddress, LogTopic.of(l2l1Topic));
290+
final BlakeRounds blakeRounds = new BlakeRounds(this, this.blakeModexpData);
287291

288292
this.precompileLimitModules =
289293
List.of(
@@ -295,7 +299,8 @@ public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic) {
295299
new EcMulEffectiveCall(this),
296300
ecPairingCall,
297301
new EcPairingMillerLoop(ecPairingCall),
298-
new Blake2fRounds(this, this.blakeModexpData),
302+
blakeRounds,
303+
new BlakeEffectiveCall(blakeRounds),
299304
// Block level limits
300305
l2Block,
301306
new Keccak(this, ecRec, l2Block, shakiraData),
@@ -314,6 +319,7 @@ public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic) {
314319
this.ecData,
315320
this.euc,
316321
this.ext,
322+
this.gas,
317323
this.logData,
318324
this.logInfo,
319325
this.mmio,
@@ -356,6 +362,8 @@ public List<Module> getModulesToTrace() {
356362
this.ext,
357363
this.euc,
358364
this.exp,
365+
// TODO: GAS module has no columnHeaders and cannot be traced. Needs a fix!
366+
// this.gas,
359367
this.logData,
360368
this.logInfo,
361369
this.mmu, // WARN: must be called before the MMIO
@@ -385,36 +393,43 @@ public List<Module> getModulesToTrace() {
385393
* @return the modules to count
386394
*/
387395
public List<Module> getModulesToCount() {
396+
final Stream<Module> regularModulesStream =
397+
Stream.of(
398+
this,
399+
this.romLex,
400+
this.add,
401+
this.bin,
402+
this.blakeModexpData,
403+
this.blockdata,
404+
this.blockhash,
405+
this.ext,
406+
this.ecData,
407+
this.euc,
408+
this.gas,
409+
this.mmu,
410+
this.mmio,
411+
this.logData,
412+
this.logInfo,
413+
this.mod,
414+
this.mul,
415+
this.mxp,
416+
this.oob,
417+
this.exp,
418+
this.rlpAddr,
419+
this.rlpTxn,
420+
this.rlpTxnRcpt,
421+
this.rom,
422+
this.shakiraData,
423+
this.shf,
424+
this.stp,
425+
this.trm,
426+
this.txnData,
427+
this.wcp,
428+
this.l2Block);
429+
388430
return Stream.concat(
389-
Stream.of(
390-
this,
391-
this.romLex,
392-
this.add,
393-
this.bin,
394-
this.blockdata,
395-
this.blockhash,
396-
this.ext,
397-
this.ecData,
398-
this.euc,
399-
this.mmu,
400-
this.mmio,
401-
this.logData,
402-
this.logInfo,
403-
this.mod,
404-
this.mul,
405-
this.mxp,
406-
this.oob,
407-
this.exp,
408-
this.rlpAddr,
409-
this.rlpTxn,
410-
this.rlpTxnRcpt,
411-
this.rom,
412-
this.shf,
413-
this.trm,
414-
this.txnData,
415-
this.wcp,
416-
this.l2Block),
417-
this.precompileLimitModules.stream())
431+
this.refTableModules.stream(),
432+
Stream.concat(regularModulesStream, this.precompileLimitModules.stream()))
418433
.toList();
419434
}
420435

tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/precompiles/PrecompileInvocation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import lombok.Builder;
1919
import lombok.experimental.Accessors;
2020
import net.consensys.linea.zktracer.module.hub.Hub;
21-
import net.consensys.linea.zktracer.module.limits.precompiles.Blake2fRounds;
21+
import net.consensys.linea.zktracer.module.limits.precompiles.BlakeRounds;
2222
import net.consensys.linea.zktracer.module.limits.precompiles.EcAddEffectiveCall;
2323
import net.consensys.linea.zktracer.module.limits.precompiles.EcMulEffectiveCall;
2424
import net.consensys.linea.zktracer.module.limits.precompiles.EcPairingEffectiveCall;
@@ -97,7 +97,7 @@ public static PrecompileInvocation of(final Hub hub, Precompile p) {
9797
case EC_ADD -> hub.transients().op().gasAllowanceForCall() < 150;
9898
case EC_MUL -> hub.transients().op().gasAllowanceForCall() < 6000;
9999
case EC_PAIRING -> EcPairingEffectiveCall.isHubFailure(hub);
100-
case BLAKE2F -> Blake2fRounds.isHubFailure(hub);
100+
case BLAKE2F -> BlakeRounds.isHubFailure(hub);
101101
};
102102

103103
final boolean ramFailure =
@@ -109,7 +109,7 @@ && switch (p) {
109109
case EC_ADD -> EcAddEffectiveCall.isRamFailure(hub);
110110
case EC_MUL -> EcMulEffectiveCall.isRamFailure(hub);
111111
case EC_PAIRING -> EcPairingEffectiveCall.isRamFailure(hub);
112-
case BLAKE2F -> Blake2fRounds.isRamFailure(hub);
112+
case BLAKE2F -> BlakeRounds.isRamFailure(hub);
113113
};
114114

115115
final long opCodeGas = Hub.GAS_PROJECTOR.of(hub.messageFrame(), hub.opCode()).total();
@@ -138,7 +138,7 @@ && switch (p) {
138138
case EC_ADD -> EcAddEffectiveCall.gasCost();
139139
case EC_MUL -> EcMulEffectiveCall.gasCost();
140140
case EC_PAIRING -> EcPairingEffectiveCall.gasCost(hub);
141-
case BLAKE2F -> Blake2fRounds.gasCost(hub);
141+
case BLAKE2F -> BlakeRounds.gasCost(hub);
142142
};
143143

144144
final long returnGas =
@@ -156,7 +156,7 @@ && switch (p) {
156156
case EC_ADD -> null;
157157
case EC_MUL -> null;
158158
case EC_PAIRING -> null;
159-
case BLAKE2F -> Blake2fRounds.metadata(hub);
159+
case BLAKE2F -> BlakeRounds.metadata(hub);
160160
};
161161

162162
return PrecompileInvocation.builder()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright Consensys Software Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.limits.precompiles;
17+
18+
import java.util.List;
19+
20+
import lombok.Getter;
21+
import lombok.RequiredArgsConstructor;
22+
import net.consensys.linea.zktracer.ColumnHeader;
23+
import net.consensys.linea.zktracer.module.Module;
24+
25+
@RequiredArgsConstructor
26+
public final class BlakeEffectiveCall implements Module {
27+
@Getter private final BlakeRounds blakeRounds;
28+
29+
@Override
30+
public String moduleKey() {
31+
return "PRECOMPILE_BLAKE_EFFECTIVE_CALLS";
32+
}
33+
34+
@Override
35+
public void enterTransaction() {}
36+
37+
@Override
38+
public void popTransaction() {}
39+
40+
@Override
41+
public int lineCount() {
42+
int r = 0;
43+
for (BlakeLimit count : this.blakeRounds.counts()) {
44+
r += count.numberOfEffectiveCalls();
45+
}
46+
return r;
47+
}
48+
49+
@Override
50+
public List<ColumnHeader> columnsHeaders() {
51+
throw new UnsupportedOperationException("should never be called");
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Consensys Software Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.limits.precompiles;
17+
18+
record BlakeLimit(int numberOfRounds, int numberOfEffectiveCalls) {}

tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fRounds.java renamed to tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/BlakeRounds.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import lombok.Getter;
2424
import lombok.RequiredArgsConstructor;
25+
import lombok.experimental.Accessors;
2526
import net.consensys.linea.zktracer.ColumnHeader;
2627
import net.consensys.linea.zktracer.module.Module;
2728
import net.consensys.linea.zktracer.module.blake2fmodexpdata.BlakeComponents;
@@ -39,13 +40,14 @@
3940
import org.hyperledger.besu.evm.internal.Words;
4041

4142
@RequiredArgsConstructor
42-
public final class Blake2fRounds implements Module {
43+
@Accessors(fluent = true)
44+
public final class BlakeRounds implements Module {
4345
private static final int BLAKE2f_INPUT_SIZE = 213;
4446
private final Hub hub;
4547

4648
@Getter private final BlakeModexpData blakeModexpData;
4749

48-
private final Deque<Integer> counts = new ArrayDeque<>();
50+
@Getter private final Deque<BlakeLimit> counts = new ArrayDeque<>();
4951

5052
@Override
5153
public String moduleKey() {
@@ -54,7 +56,7 @@ public String moduleKey() {
5456

5557
@Override
5658
public void traceStartConflation(final long blockCount) {
57-
counts.push(0);
59+
counts.push(new BlakeLimit(0, 0));
5860
}
5961

6062
@Override
@@ -180,7 +182,12 @@ public void tracePreOpcode(MessageFrame frame) {
180182
this.blakeModexpData.call(
181183
new BlakeModexpDataOperation(
182184
hub.stamp(), null, new BlakeComponents(data, r, Bytes.of(f))));
183-
this.counts.push(this.counts.pop() + rInt);
185+
186+
final BlakeLimit currentLimit = this.counts.pop();
187+
this.counts.push(
188+
new BlakeLimit(
189+
currentLimit.numberOfRounds() + rInt,
190+
currentLimit.numberOfEffectiveCalls() + 1));
184191
}
185192
}
186193
}
@@ -190,7 +197,12 @@ public void tracePreOpcode(MessageFrame frame) {
190197

191198
@Override
192199
public int lineCount() {
193-
return counts.getFirst();
200+
final long totalR = counts.stream().mapToLong(BlakeLimit::numberOfRounds).sum();
201+
if (totalR > Integer.MAX_VALUE) {
202+
throw new RuntimeException("Ludicrous BlakeLimit calls");
203+
}
204+
205+
return (int) totalR;
194206
}
195207

196208
@Override

tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddEffectiveCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class EcAddEffectiveCall implements Module {
4040

4141
@Override
4242
public String moduleKey() {
43-
return "PRECOMPILE_ECADD_EFFECTIVE_CALL";
43+
return "PRECOMPILE_ECADD_EFFECTIVE_CALLS";
4444
}
4545

4646
private static final int PRECOMPILE_GAS_FEE = 150; // cf EIP-1108

tracer/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulEffectiveCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class EcMulEffectiveCall implements Module {
4040

4141
@Override
4242
public String moduleKey() {
43-
return "PRECOMPILE_ECMUL_EFFECTIVE_CALL";
43+
return "PRECOMPILE_ECMUL_EFFECTIVE_CALLS";
4444
}
4545

4646
private static final int PRECOMPILE_GAS_FEE = 6000; // cf EIP-1108

0 commit comments

Comments
 (0)