Skip to content

Commit

Permalink
added GasCostSingleton
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzogentile404 committed Jan 17, 2025
1 parent 5fa5de6 commit c7fae6e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private static boolean isOutOfGas(MessageFrame frame, OpCode opCode, GasProjecto
+ (required > frame.getRemainingGas())
+ " ,remainingGas: "
+ frame.getRemainingGas());
GasCostSingleton gasCostSingleton = GasCostSingleton.getInstance();
gasCostSingleton.incrementGasCost(required);
return required > frame.getRemainingGas();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.consensys.linea.zktracer.module.hub.signals;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GasCostSingleton {
private static GasCostSingleton instance;
private long gasCost = 0;

private GasCostSingleton() {
// private constructor to prevent instantiation
}

public void incrementGasCost(long gasCost) {
this.gasCost += gasCost;
}

public static GasCostSingleton getInstance() {
if (instance == null) {
instance = new GasCostSingleton();
}
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import net.consensys.linea.testing.BytecodeCompiler;
import net.consensys.linea.testing.BytecodeRunner;
import net.consensys.linea.testing.ToyAccount;
import net.consensys.linea.zktracer.module.hub.signals.GasCostSingleton;
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.opcode.OpCodeData;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -54,7 +56,7 @@ public class OutOfGasExceptionTest {
@ParameterizedTest
@MethodSource("outOfGasExceptionWithEmptyAccountsAndNoMemoryExpansionCostTestSource")
void outOfGasExceptionWithEmptyAccountsAndNoMemoryExpansionCostTest(
OpCode opCode, int opCodeStaticCost, int nPushes, int corneCase) {
OpCode opCode, int opCodeStaticCost, int nPushes, int cornerCase) {
BytecodeCompiler program = BytecodeCompiler.newProgram();

for (int i = 0; i < nPushes; i++) {
Expand Down Expand Up @@ -85,13 +87,20 @@ void outOfGasExceptionWithEmptyAccountsAndNoMemoryExpansionCostTest(
default -> 0;
};

bytecodeRunner.run(
(long) GAS_CONST_G_TRANSACTION
final long gasCost =
GAS_CONST_G_TRANSACTION
+ (long) nPushes * GAS_CONST_G_VERY_LOW
+ opCodeStaticCost
+ opCodeDynamicCost
+ corneCase);
if (corneCase == -1) {
+ opCodeDynamicCost;
bytecodeRunner.run(gasCost + cornerCase);

// TODO: understand why the gas cost is not the same as the one calculated
// if the two programs start from the same state, then the gas cost should be the same
// and we can use getGastCost to obtain the gas cost without computing it explicitly
// we may create another instance of the tracer to run programs just to estimate the gas cost
// assertEquals(gasCost, getGasCost(program.compile()));

if (cornerCase == -1) {
assertEquals(
OUT_OF_GAS_EXCEPTION,
bytecodeRunner.getHub().previousTraceSection().commonValues.tracedException());
Expand Down Expand Up @@ -155,7 +164,7 @@ void outOfGasExceptionCallTest(

BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());

long gasLimit =
long gasCost =
GAS_CONST_G_TRANSACTION
+ // base gas cost
(isWarm ? GAS_CONST_G_VERY_LOW + GAS_CONST_G_COLD_ACCOUNT_ACCESS : 0) // PUSH + BALANCE
Expand All @@ -169,9 +178,9 @@ void outOfGasExceptionCallTest(
.nonce(10)
.address(Address.fromHexString("ca11ee"))
.build();
bytecodeRunner.run(gasLimit + cornerCase, List.of(calleeAccount));
bytecodeRunner.run(gasCost + cornerCase, List.of(calleeAccount));
} else {
bytecodeRunner.run(gasLimit + cornerCase);
bytecodeRunner.run(gasCost + cornerCase);
}

if (cornerCase == -1) {
Expand Down Expand Up @@ -247,4 +256,17 @@ void outOfGasExceptionSStore(int cornerCase) {
bytecodeRunner.getHub().previousTraceSection().commonValues.tracedException());
}
}

public static long getGasCost(Bytes program) {
// Retrieve singleton instance
GasCostSingleton gasCostSingleton = GasCostSingleton.getInstance();
// Set gas cost to 0
gasCostSingleton.setGasCost(0);

BytecodeRunner bytecodeRunner = BytecodeRunner.of(program);
bytecodeRunner.run();

// Return gas cost
return GAS_CONST_G_TRANSACTION + gasCostSingleton.getGasCost();
}
}

0 comments on commit c7fae6e

Please sign in to comment.