Skip to content

Commit

Permalink
fix wcp line count for operations coming from block data
Browse files Browse the repository at this point in the history
Signed-off-by: F Bojarski <[email protected]>
  • Loading branch information
letypequividelespoubelles committed Jan 22, 2025
1 parent 18aa409 commit 971c4f2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package net.consensys.linea.zktracer.module.blockdata;

import static net.consensys.linea.zktracer.module.blockdata.Trace.GAS_LIMIT_MAXIMUM;
import static net.consensys.linea.zktracer.module.blockdata.Trace.nROWS_DEPTH;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.LLARGE;

import java.math.BigInteger;
import java.nio.MappedByteBuffer;
Expand All @@ -30,7 +32,6 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.types.EWord;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.evm.worldstate.WorldView;
import org.hyperledger.besu.plugin.data.BlockBody;
import org.hyperledger.besu.plugin.data.BlockHeader;

Expand All @@ -41,12 +42,8 @@ public class Blockdata implements Module {
private final TxnData txnData;

private final Deque<BlockdataOperation> operations = new ArrayDeque<>();
private static final int TIMESTAMP_BYTESIZE = 4;
private BlockHeader prevBlockHeader;
private int traceCounter = 0;
private long firstBlockNumber;
private Bytes chainId;
private boolean shouldBeTraced = true;

final OpCode[] opCodes = {
OpCode.COINBASE,
Expand All @@ -69,42 +66,47 @@ public String moduleKey() {

@Override
public void traceStartConflation(final long blockCount) {
wcp.additionalRows.add(TIMESTAMP_BYTESIZE); // TODO: check
wcp.additionalRows.add(
LLARGE // for COINBASE
+ 6
+ 6 // for TIMESTAMP
+ 1
+ 6 // for NUMBER
+ 1 // for DIFFICULTY
+ Bytes.minimalBytes(GAS_LIMIT_MAXIMUM).size() * 4 // for GASLIMIT
+ LLARGE // for CHAINID
+ LLARGE // for BASEFEE
);

euc.additionalRows.add(8);
}

@Override
public void traceEndBlock(final BlockHeader blockHeader, final BlockBody blockBody) {
final long blockNumber = blockHeader.getNumber();
firstBlockNumber = (traceCounter < opCodes.length) ? blockNumber : firstBlockNumber;
if (shouldBeTraced) {
for (OpCode opCode : opCodes) {
BlockdataOperation operation =
new BlockdataOperation(
txnData.hub(),
blockHeader,
prevBlockHeader,
txnData.currentBlock().getNbOfTxsInBlock(),
wcp,
euc,
chainId,
opCode,
firstBlockNumber);
operations.addLast(operation);
// Increase counter to track where we are in the conflation
traceCounter++;
}
if (operations.isEmpty()) {
firstBlockNumber = blockNumber;
}
final BlockHeader previousBlockHeader =
operations.isEmpty() ? null : operations.getLast().blockHeader();
for (OpCode opCode : opCodes) {
final BlockdataOperation operation =
new BlockdataOperation(
txnData.hub(),
blockHeader,
previousBlockHeader,
txnData.currentBlock().getNbOfTxsInBlock(),
wcp,
euc,
chainId,
opCode,
firstBlockNumber);
operations.addLast(operation);
}
prevBlockHeader = blockHeader;
shouldBeTraced = false;
}

@Override
public void traceEndConflation(final WorldView state) {}

@Override
public void enterTransaction() {
shouldBeTraced = true;
}
public void enterTransaction() {}

@Override
public void popTransaction() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void handleCoinbase() {

private void handleTimestamp() {
data = EWord.of(blockHeader.getTimestamp());
EWord prevData =
final EWord prevData =
prevBlockHeader == null ? EWord.ZERO : EWord.of(prevBlockHeader.getTimestamp());

// row i
Expand Down Expand Up @@ -182,11 +182,11 @@ private void handleGasLimit() {
wcpCallToLEQ(1, data, EWord.of(Bytes.ofUnsignedLong(GAS_LIMIT_MAXIMUM)));

if (!firstBlockInConflation) {
EWord prevGasLimit = EWord.of(prevBlockHeader.getGasLimit());
final EWord prevGasLimit = EWord.of(prevBlockHeader.getGasLimit());
// row i + 2
Bytes maxDeviation = eucCall(2, prevGasLimit, EWord.of(GAS_LIMIT_ADJUSTMENT_FACTOR));
final Bytes maxDeviation = eucCall(2, prevGasLimit, EWord.of(GAS_LIMIT_ADJUSTMENT_FACTOR));
// row i + 3
BigInteger safeGasLimitUpperBound =
final BigInteger safeGasLimitUpperBound =
prevGasLimit.getAsBigInteger().add(maxDeviation.toUnsignedBigInteger());
wcpCallToLT(3, data, EWord.of(safeGasLimitUpperBound));

Expand Down Expand Up @@ -303,9 +303,6 @@ private boolean wcpCallToISZERO(int w, EWord arg1) {
}

private Bytes eucCall(int w, EWord arg1, EWord arg2) {
checkArgument(arg1.bitLength() / 8 <= 16);
checkArgument(arg2.bitLength() / 8 <= 16);

this.arg1[w] = arg1;
this.arg2[w] = arg2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.experimental.Accessors;
import net.consensys.linea.zktracer.ColumnHeader;
import net.consensys.linea.zktracer.container.module.OperationSetModule;
import net.consensys.linea.zktracer.container.stacked.CountOnlyOperation;
import net.consensys.linea.zktracer.container.stacked.ModuleOperationStackedSet;
import net.consensys.linea.zktracer.module.wcp.Wcp;
import org.apache.tuweni.bytes.Bytes;
Expand All @@ -39,6 +40,9 @@ public class Euc implements OperationSetModule<EucOperation> {
private final ModuleOperationStackedSet<EucOperation> operations =
new ModuleOperationStackedSet<>();

/** count the number of rows that could be added after the sequencer counts the number of line */
public final CountOnlyOperation additionalRows = new CountOnlyOperation();

@Override
public String moduleKey() {
return "EUC";
Expand All @@ -57,6 +61,13 @@ public void commit(List<MappedByteBuffer> buffers) {
}
}

@Override
public int lineCount() {
return operations.conflationFinished()
? operations.lineCount()
: operations().lineCount() + additionalRows.lineCount();
}

public EucOperation callEUC(final Bytes dividend, final Bytes divisor) {
final BigInteger dividendBI = dividend.toUnsignedBigInteger();
final BigInteger divisorBI = divisor.toUnsignedBigInteger();
Expand Down
2 changes: 1 addition & 1 deletion linea-constraints

0 comments on commit 971c4f2

Please sign in to comment.