gas cost modification

Signed-off-by: Karim Taam <karim.t2am@gmail.com>
pull/7004/head
Karim Taam 7 months ago committed by garyschulte
parent 80b8cd7669
commit 229b867c95
  1. 19
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/AccessWitness.java
  2. 8
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java
  3. 7
      evm/src/main/java/org/hyperledger/besu/evm/EVM.java
  4. 2
      evm/src/main/java/org/hyperledger/besu/evm/gascalculator/PragueGasCalculator.java
  5. 43
      evm/src/main/java/org/hyperledger/besu/evm/gascalculator/ShanghaiGasCalculator.java

@ -9,6 +9,7 @@ import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.MAIN_STO
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.NONCE_LEAF_KEY; import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.NONCE_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERKLE_NODE_WIDTH; import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERKLE_NODE_WIDTH;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERSION_LEAF_KEY; import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERSION_LEAF_KEY;
import static org.hyperledger.besu.evm.internal.Words.clampedAdd;
import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Address;
@ -114,7 +115,6 @@ public class AccessWitness implements org.hyperledger.besu.datatypes.AccessWitne
if (createSendsValue) { if (createSendsValue) {
gas += touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY); gas += touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY);
} }
return gas; return gas;
} }
@ -185,17 +185,24 @@ public class AccessWitness implements org.hyperledger.besu.datatypes.AccessWitne
@Override @Override
public long touchCodeChunks( public long touchCodeChunks(
final Address address, final long offset, final long readSize, final long codeLength) { final Address address, final long startPc, final long readSize, final long codeLength) {
long gas = 0; long gas = 0;
if (readSize == 0 || codeLength == 0 || offset > codeLength) { if ((readSize == 0 && codeLength == 0) || startPc > codeLength) {
return 0; return 0;
} }
for (long i = offset / 31; i <= (Math.min(offset + readSize, codeLength) - 1) / 31; i++) { long endPc = startPc+readSize;
gas += if(endPc>codeLength){
endPc = codeLength;
}
if(endPc>0){
endPc-=1;
}
for (long i = startPc / 31; i <= endPc/31; i++) {
gas = clampedAdd(gas,
touchAddressOnReadAndComputeGas( touchAddressOnReadAndComputeGas(
address, address,
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH), CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH),
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH)); CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH)));
} }
return gas; return gas;
} }

@ -462,11 +462,11 @@ public class MainnetTransactionProcessor {
final Wei balancePriorToRefund = sender.getBalance(); final Wei balancePriorToRefund = sender.getBalance();
sender.incrementBalance(refundedWei); sender.incrementBalance(refundedWei);
LOG.atTrace() LOG.atTrace()
.setMessage("refunded sender {} {} wei ({} -> {})") .setMessage("refunded sender {} {} wei (balance before:{} -> after:{})")
.addArgument(senderAddress) .addArgument(senderAddress)
.addArgument(refundedWei) .addArgument(refundedWei.toShortHexString())
.addArgument(balancePriorToRefund) .addArgument(balancePriorToRefund.toShortHexString())
.addArgument(sender.getBalance()) .addArgument(sender.getBalance().toShortHexString())
.log(); .log();
final long gasUsedByTransaction = transaction.getGasLimit() - initialFrame.getRemainingGas(); final long gasUsedByTransaction = transaction.getGasLimit() - initialFrame.getRemainingGas();
LOG.info( LOG.info(

@ -183,11 +183,17 @@ public class EVM {
var operationTracer = tracing == OperationTracer.NO_TRACING ? null : tracing; var operationTracer = tracing == OperationTracer.NO_TRACING ? null : tracing;
byte[] code = frame.getCode().getBytes().toArrayUnsafe(); byte[] code = frame.getCode().getBytes().toArrayUnsafe();
Operation[] operationArray = operations.getOperations(); Operation[] operationArray = operations.getOperations();
while (frame.getState() == MessageFrame.State.CODE_EXECUTING) { while (frame.getState() == MessageFrame.State.CODE_EXECUTING) {
Operation currentOperation; Operation currentOperation;
int opcode; int opcode;
int pc = frame.getPC(); int pc = frame.getPC();
if (!frame.wasCreatedInTransaction(frame.getContractAddress())) {
frame.getAccessWitness().touchCodeChunks(frame.getContractAddress(), pc, 1, code.length);
}
try { try {
opcode = code[pc] & 0xff; opcode = code[pc] & 0xff;
currentOperation = operationArray[opcode]; currentOperation = operationArray[opcode];
@ -278,7 +284,6 @@ public class EVM {
} catch (final UnderflowException ue) { } catch (final UnderflowException ue) {
result = UNDERFLOW_RESPONSE; result = UNDERFLOW_RESPONSE;
} }
System.out.println("operation "+currentOperation.getName()+" "+result.getGasCost()+" "+frame.getRemainingGas());
final ExceptionalHaltReason haltReason = result.getHaltReason(); final ExceptionalHaltReason haltReason = result.getHaltReason();
if (haltReason != null) { if (haltReason != null) {
LOG.trace("MessageFrame evaluation halted because of {}", haltReason); LOG.trace("MessageFrame evaluation halted because of {}", haltReason);

@ -26,7 +26,7 @@ import static org.hyperledger.besu.datatypes.Address.KZG_POINT_EVAL;
* <LI>TBD * <LI>TBD
* </UL> * </UL>
*/ */
public class PragueGasCalculator extends CancunGasCalculator { public class PragueGasCalculator extends ShanghaiGasCalculator {
/** Instantiates a new Prague Gas Calculator. */ /** Instantiates a new Prague Gas Calculator. */
public PragueGasCalculator() { public PragueGasCalculator() {

@ -295,32 +295,27 @@ public class ShanghaiGasCalculator extends LondonGasCalculator {
final MessageFrame frame, final long codeOffset, final long readSize, final long codeSize) { final MessageFrame frame, final long codeOffset, final long readSize, final long codeSize) {
long gasCost = super.pushOperationGasCost(frame, codeOffset, readSize, codeSize); long gasCost = super.pushOperationGasCost(frame, codeOffset, readSize, codeSize);
if (!frame.wasCreatedInTransaction(frame.getContractAddress())) { if (!frame.wasCreatedInTransaction(frame.getContractAddress())) {
gasCost = if(readSize==1){
clampedAdd( if( (codeOffset%31==0)){
gasCost, gasCost =
frame clampedAdd(
.getAccessWitness() gasCost,
.touchCodeChunks(frame.getContractAddress(), codeOffset, readSize, codeSize)); frame
System.out.println("push "+isPrecompile(frame.getContractAddress())+" "+frame.getContractAddress()+" "+codeOffset+" "+readSize+" "+codeSize+" "+gasCost); .getAccessWitness()
} .touchCodeChunks(frame.getContractAddress(), codeOffset+1, readSize, codeSize));
return gasCost;
} }
} else {
gasCost =
clampedAdd(
gasCost,
frame
.getAccessWitness()
.touchCodeChunks(frame.getContractAddress(), codeOffset, readSize, codeSize));
@Override }
public long extCodeSizeOperationGasCost(final MessageFrame frame) {
if(!isPrecompile(frame.getContractAddress())) {
long gasCost =
frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(frame.getContractAddress(), UInt256.ZERO, VERSION_LEAF_KEY);
return
clampedAdd(
gasCost,
frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(frame.getContractAddress(), UInt256.ZERO, CODE_SIZE_LEAF_KEY));
} }
return 0; return gasCost;
} }
@Override @Override

Loading…
Cancel
Save