Skip to content

Commit

Permalink
Merge branch 'arith-dev' into 1693-chore-update-besu-to-251-develop-1…
Browse files Browse the repository at this point in the history
…be83c5
  • Loading branch information
OlivierBBB authored Jan 14, 2025
2 parents a13de5d + bd8b4fd commit 65652e8
Show file tree
Hide file tree
Showing 18 changed files with 849 additions and 985 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.consensys.linea.zktracer.types.EWord;
import net.consensys.linea.zktracer.types.UnsignedByte;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.plugin.data.BlockHeader;

@Accessors(fluent = true)
Expand All @@ -51,6 +52,7 @@ public class BlockdataOperation extends ModuleOperation {
private final Bytes chainId;
private final BlockHeader blockHeader;
private final BlockHeader prevBlockHeader;
private final Address coinbaseAddress;
private final EWord POWER_256_20 = EWord.of(BigInteger.ONE.shiftLeft(20 * 8));
private final EWord POWER_256_6 = EWord.of(BigInteger.ONE.shiftLeft(6 * 8));

Expand Down Expand Up @@ -83,6 +85,7 @@ public BlockdataOperation(
this.hub = hub;
this.blockHeader = blockHeader;
this.prevBlockHeader = prevBlockHeader;
this.coinbaseAddress = hub.coinbaseAddress;

this.chainId = chainId;
this.ctMax = ctMax(opCode);
Expand Down Expand Up @@ -172,9 +175,11 @@ private void handleGasLimit() {
data = EWord.of(blockHeader.getGasLimit());

// row i
// comparison to minimum
wcpCallToGEQ(0, data, EWord.of(LINEA_GAS_LIMIT_MINIMUM));

// row i + 1
// comparison to maximum
wcpCallToLEQ(1, data, EWord.of(Bytes.ofUnsignedLong(LINEA_GAS_LIMIT_MAXIMUM)));

if (!firstBlockInConflation) {
Expand Down Expand Up @@ -227,8 +232,8 @@ public void trace(Trace trace) {
.isChainid(opCode == OpCode.CHAINID)
.isBasefee(opCode == OpCode.BASEFEE)
.inst(UnsignedByte.of(opCode.byteValue()))
.coinbaseHi(hub.coinbaseAddress.slice(0, 4).toLong())
.coinbaseLo(hub.coinbaseAddress.slice(4, LLARGE))
.coinbaseHi(coinbaseAddress.slice(0, 4).toLong())
.coinbaseLo(coinbaseAddress.slice(4, LLARGE))
.blockGasLimit(Bytes.ofUnsignedLong(blockHeader.getGasLimit()))
.basefee(
Bytes.ofUnsignedLong(blockHeader.getBaseFee().get().getAsBigInteger().longValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package net.consensys.linea.zktracer.module.blockhash;

import static com.google.common.base.Preconditions.checkArgument;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.BLOCKHASH_MAX_HISTORY;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.LLARGE;

import java.nio.MappedByteBuffer;
import java.util.HashMap;
Expand All @@ -33,7 +31,6 @@
import net.consensys.linea.zktracer.module.hub.defer.PostOpcodeDefer;
import net.consensys.linea.zktracer.module.wcp.Wcp;
import net.consensys.linea.zktracer.opcode.OpCode;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.operation.Operation;
Expand All @@ -52,20 +49,16 @@ public class Blockhash implements OperationSetModule<BlockhashOperation>, PostOp

/* Stores the result of BLOCKHASH if the result of the opcode is not 0 */
private final Map<Bytes32, Bytes32> blockHashMap = new HashMap<>();
/* Store the number of call (capped to 2) of BLOCKHASH of a BLOCK_NUMBER*/
private final Map<Bytes32, Integer> numberOfCall = new HashMap<>();

private long absoluteBlockNumber;
private short relativeBlock;
private short relBlock;
private long absBlock;

private Bytes32 opcodeArgument;
private boolean lowerBound;
private boolean upperBound;
private Bytes32 blockhashArg;

public Blockhash(Hub hub, Wcp wcp) {
this.hub = hub;
this.wcp = wcp;
this.relativeBlock = 0;
this.relBlock = 0;
}

@Override
Expand All @@ -75,31 +68,18 @@ public String moduleKey() {

@Override
public void traceStartBlock(final ProcessableBlockHeader processableBlockHeader) {
relativeBlock += 1;
absoluteBlockNumber = processableBlockHeader.getNumber();
relBlock += 1;
absBlock = processableBlockHeader.getNumber();
}

@Override
public void tracePreOpcode(MessageFrame frame) {
final OpCode opCode = OpCode.of(frame.getCurrentOperation().getOpcode());
checkArgument(opCode == OpCode.BLOCKHASH, "Expected BLOCKHASH opcode");

opcodeArgument = Bytes32.leftPad(frame.getStackItem(0));
lowerBound =
wcp.callGEQ(
opcodeArgument, Bytes.ofUnsignedLong(absoluteBlockNumber - BLOCKHASH_MAX_HISTORY));
upperBound = wcp.callLT(opcodeArgument, Bytes.ofUnsignedLong(absoluteBlockNumber));
blockhashArg = Bytes32.leftPad(frame.getStackItem(0));

hub.defers().scheduleForPostExecution(this);

/* To prove the lex order of BLOCK_NUMBER_HI/LO, we call WCP at endConflation, so we need to add rows in WCP now.
If a BLOCK_NUMBER is already called at least two times, no need for additional rows in WCP*/
final int numberOfCall = this.numberOfCall.getOrDefault(opcodeArgument, 0);
if (numberOfCall < 2) {
wcp.additionalRows.add(
Math.max(Math.min(LLARGE, opcodeArgument.trimLeadingZeros().size()), 1));
this.numberOfCall.replace(opcodeArgument, numberOfCall, numberOfCall + 1);
}
}

@Override
Expand All @@ -108,26 +88,27 @@ public void resolvePostExecution(

final OpCode opCode = OpCode.of(frame.getCurrentOperation().getOpcode());
if (opCode == OpCode.BLOCKHASH) {
final Bytes32 result = Bytes32.leftPad(frame.getStackItem(0));
operations.add(
new BlockhashOperation(
relativeBlock, opcodeArgument, absoluteBlockNumber, lowerBound, upperBound, result));
if (result != Bytes32.ZERO) {
blockHashMap.put(opcodeArgument, result);
final Bytes32 blockhashRes = Bytes32.leftPad(frame.getStackItem(0));
operations.add(new BlockhashOperation(relBlock, absBlock, blockhashArg, blockhashRes, wcp));
if (blockhashRes != Bytes32.ZERO) {
blockHashMap.put(blockhashArg, blockhashRes);
}
}
}

/**
* Operations are sorted wrt blockhashArg and the wcp module is called accordingly. We must call
* the WCP module before calling {@link #commit(List<MappedByteBuffer>)} as the headers sizes must
* be computed with the final list of operations ready.
*/
@Override
public void traceEndConflation(WorldView state) {
OperationSetModule.super.traceEndConflation(state);
sortedOperations = sortOperations(new BlockhashComparator());
if (!sortedOperations.isEmpty()) {
wcp.callGEQ(sortedOperations.getFirst().opcodeArgument(), Bytes32.ZERO);
for (int i = 1; i < sortedOperations.size(); i++) {
wcp.callGEQ(
sortedOperations.get(i).opcodeArgument(), sortedOperations.get(i - 1).opcodeArgument());
}
Bytes32 prevBlockhashArg = Bytes32.ZERO;
for (BlockhashOperation op : sortedOperations) {
op.handlePreprocessing(prevBlockhashArg);
prevBlockhashArg = op.blockhashArg();
}
}

Expand All @@ -139,13 +120,14 @@ public List<ColumnHeader> columnsHeaders() {
@Override
public void commit(List<MappedByteBuffer> buffers) {
final Trace trace = new Trace(buffers);
for (BlockhashOperation op : sortedOperations) {
final Bytes32 hash =
op.result() == Bytes32.ZERO
? this.blockHashMap.getOrDefault(op.opcodeArgument(), Bytes32.ZERO)
: op.result();

op.trace(trace, hash);
for (BlockhashOperation op : sortedOperations) {
final Bytes32 blockhashVal =
op.blockhashRes() == Bytes32.ZERO
? this.blockHashMap.getOrDefault(op.blockhashArg(), Bytes32.ZERO)
: op.blockhashRes();
op.traceMacro(trace, blockhashVal);
op.tracePreprocessing(trace);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public class BlockhashComparator implements Comparator<BlockhashOperation> {
@Override
public int compare(BlockhashOperation o1, BlockhashOperation o2) {
// First, sort by BLOCK_NUMBER
final int blockNumberComparison = o1.opcodeArgument().compareTo(o2.opcodeArgument());
final int blockNumberComparison = o1.blockhashArg().compareTo(o2.blockhashArg());
if (blockNumberComparison != 0) {
return blockNumberComparison;
} else {
// Second, sort by RELATIVE_BLOCK
return o1.relativeBlock() - o2.relativeBlock();
return o1.relBlock() - o2.relBlock();
}
}
}
Loading

0 comments on commit 65652e8

Please sign in to comment.