Skip to content

Commit

Permalink
Merge pull request #2174 from rsksmart/implement-basefee
Browse files Browse the repository at this point in the history
Adding BASEFEE Method
  • Loading branch information
Vovchyk authored Dec 11, 2023
2 parents a8006f4 + eed7d47 commit df71611
Show file tree
Hide file tree
Showing 24 changed files with 642 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public enum ConsensusRule {
RSKIP385("rskip385"),
RSKIP398("rskip398"),
RSKIP400("rskip400"), // From EIP-2028 calldata gas cost reduction
RSKIP412("rskip412"), // From EIP-3198 BASEFEE opcode
;

private String configKey;
Expand Down
5 changes: 5 additions & 0 deletions rskj-core/src/main/java/org/ethereum/vm/OpCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ public enum OpCode {
*/
CHAINID(0x46, 0, 1, BASE_TIER),

/**
* (0x48) Get the block base fee
*/
BASEFEE(0x48, 0, 1, BASE_TIER),

/* Memory, Storage and Flow Operations */

/**
Expand Down
5 changes: 5 additions & 0 deletions rskj-core/src/main/java/org/ethereum/vm/OpCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ private OpCodes() {
*/
public static final byte OP_SELFBALANCE = 0x47 ;

/**
* (0x48) Get the block base fee
*/
public static final byte OP_BASEFEE = 0x48 ;

/* Memory Storage and F Operations */

/**
Expand Down
21 changes: 20 additions & 1 deletion rskj-core/src/main/java/org/ethereum/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ protected void doRETURNDATACOPY() {
protected void doGASPRICE(){
spendOpCodeGas();
// EXECUTION PHASE
DataWord gasPrice = program.getGasPrice();
DataWord gasPrice = program.getTxGasPrice();

if (isLogEnabled) {
hint = "price: " + gasPrice.toString();
Expand Down Expand Up @@ -1058,6 +1058,19 @@ protected void doGASLIMIT() {
program.step();
}

protected void doBASEFEE() {
spendOpCodeGas();
// EXECUTION PHASE
DataWord minimumGasPrice = program.getMinimumGasPrice();

if (isLogEnabled) {
hint = "baseFee: " + minimumGasPrice;
}

program.stackPush(minimumGasPrice);
program.step();
}

protected void doCHAINID() {
spendOpCodeGas();
// EXECUTION PHASE
Expand Down Expand Up @@ -1834,6 +1847,12 @@ protected void executeOpcode() {
break;
case OpCodes.OP_GASLIMIT: doGASLIMIT();
break;
case OpCodes.OP_BASEFEE:
if (!activations.isActive(RSKIP412)) {
throw Program.ExceptionHelper.invalidOpCode(program);
}
doBASEFEE();
break;
case OpCodes.OP_CHAINID:
if (!activations.isActive(RSKIP152)) {
throw Program.ExceptionHelper.invalidOpCode(program);
Expand Down
10 changes: 7 additions & 3 deletions rskj-core/src/main/java/org/ethereum/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private InternalTransaction addInternalTx(byte[] nonce, DataWord gasLimit, RskAd
transaction,
getCallDeep(),
senderNonce,
getGasPrice(),
getTxGasPrice(),
gasLimit,
senderAddress.getBytes(),
receiveAddress.getBytes(),
Expand Down Expand Up @@ -1022,8 +1022,12 @@ public DataWord getCallerAddress() {
return invoke.getCallerAddress();
}

public DataWord getGasPrice() {
return invoke.getMinGasPrice();
public DataWord getTxGasPrice() {
return invoke.getTxGasPrice();
}

public DataWord getMinimumGasPrice() {
return invoke.getMinimumGasPrice();
}

public long getRemainingGas() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface ProgramInvoke extends InvokeData {

DataWord getOriginAddress();

DataWord getMinGasPrice();
DataWord getTxGasPrice();

DataWord getPrevHash();

Expand All @@ -49,6 +49,8 @@ public interface ProgramInvoke extends InvokeData {

DataWord getGaslimit();

DataWord getMinimumGasPrice();

boolean byTransaction();

boolean byTestingSuite();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ProgramInvoke createProgramInvoke(Transaction tx, int txindex, Block bloc
Coin balance = repository.getBalance(addr);

/*** GASPRICE op ***/
Coin gasPrice = tx.getGasPrice();
Coin txGasPrice = tx.getGasPrice();

/*** GAS op ***/
byte[] gas = tx.getGasLimit();
Expand Down Expand Up @@ -95,13 +95,16 @@ public ProgramInvoke createProgramInvoke(Transaction tx, int txindex, Block bloc
/*** GASLIMIT op ***/
byte[] gaslimit = block.getGasLimit();

/*** BASEFEE op ***/
Coin minimumGasPrice = block.getMinimumGasPrice();

if (logger.isInfoEnabled()) {
logger.info("Top level call: \n" +
"address={}\n" +
"origin={}\n" +
"caller={}\n" +
"balance={}\n" +
"gasPrice={}\n" +
"txGasPrice={}\n" +
"gas={}\n" +
"callValue={}\n" +
"data={}\n" +
Expand All @@ -111,13 +114,14 @@ public ProgramInvoke createProgramInvoke(Transaction tx, int txindex, Block bloc
"blockNumber={}\n" +
"transactionIndex={}\n" +
"difficulty={}\n" +
"gaslimit={}\n",
"gaslimit={}\n" +
"minimumGasPrice={}\n",

addr,
ByteUtil.toHexString(origin),
ByteUtil.toHexString(caller),
balance,
gasPrice,
txGasPrice,
new BigInteger(1, gas).longValue(),
callValue,
ByteUtil.toHexString(data),
Expand All @@ -127,11 +131,14 @@ public ProgramInvoke createProgramInvoke(Transaction tx, int txindex, Block bloc
number,
txindex,
ByteUtil.toHexString(difficulty),
gaslimit);
gaslimit,
minimumGasPrice);
}

return new ProgramInvokeImpl(addr.getBytes(), origin, caller, balance.getBytes(), gasPrice.getBytes(), gas, callValue.getBytes(), data,
lastHash, coinbase, timestamp, number, txindex,difficulty, gaslimit,
byte[] minGasPrice = minimumGasPrice != null ? minimumGasPrice.getBytes() : ByteUtil.EMPTY_BYTE_ARRAY;

return new ProgramInvokeImpl(addr.getBytes(), origin, caller, balance.getBytes(), txGasPrice.getBytes(), gas, callValue.getBytes(), data,
lastHash, coinbase, timestamp, number, txindex,difficulty, gaslimit, minGasPrice,
repository, blockStore);
}

Expand All @@ -151,7 +158,7 @@ public ProgramInvoke createProgramInvoke(Program program, DataWord toAddress, Da
DataWord caller = callerAddress;

DataWord balance = DataWord.valueOf(balanceInt.getBytes());
DataWord gasPrice = program.getGasPrice();
DataWord txGasPrice = program.getTxGasPrice();
long agas = inGas;
DataWord callValue = inValue;

Expand All @@ -163,14 +170,15 @@ public ProgramInvoke createProgramInvoke(Program program, DataWord toAddress, Da
DataWord transactionIndex = program.getTransactionIndex();
DataWord difficulty = program.getDifficulty();
DataWord gasLimit = program.getGasLimit();
DataWord minimumGasPrice = program.getMinimumGasPrice();

if (logger.isInfoEnabled()) {
logger.info("Internal call: \n" +
"address={}\n" +
"origin={}\n" +
"caller={}\n" +
"balance={}\n" +
"gasPrice={}\n" +
"txGasPrice={}\n" +
"gas={}\n" +
"callValue={}\n" +
"data={}\n" +
Expand All @@ -180,12 +188,13 @@ public ProgramInvoke createProgramInvoke(Program program, DataWord toAddress, Da
"blockNumber={}\n" +
"transactionIndex={}\n" +
"difficulty={}\n" +
"gaslimit={}\n",
"gaslimit={}\n" +
"minimumGasPrice={}\n",
ByteUtil.toHexString(address.getLast20Bytes()),
ByteUtil.toHexString(origin.getLast20Bytes()),
ByteUtil.toHexString(caller.getLast20Bytes()),
balance.toString(),
gasPrice.longValue(),
txGasPrice.longValue(),
agas,
ByteUtil.toHexString(callValue.getNoLeadZeroesData()),
data == null ? "" : ByteUtil.toHexString(data),
Expand All @@ -195,12 +204,13 @@ public ProgramInvoke createProgramInvoke(Program program, DataWord toAddress, Da
number.longValue(),
transactionIndex.intValue(),
ByteUtil.toHexString(difficulty.getNoLeadZeroesData()),
gasLimit.bigIntValue());
gasLimit.bigIntValue(),
minimumGasPrice.longValue());
}

return new ProgramInvokeImpl(address, origin, caller, balance, gasPrice, agas, callValue,
return new ProgramInvokeImpl(address, origin, caller, balance, txGasPrice, agas, callValue,
data, lastHash, coinbase, timestamp, number, transactionIndex, difficulty, gasLimit,
repository, program.getCallDeep() + 1, blockStore,
minimumGasPrice, repository, program.getCallDeep() + 1, blockStore,
isStaticCall, byTestingSuite);
}
}
Loading

0 comments on commit df71611

Please sign in to comment.