Skip to content

Commit

Permalink
Merge branch 'main' into zkbesu
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarla committed Dec 9, 2024
2 parents cf50479 + 4033d3b commit 8e3d51f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 34 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- Proper support for `pending` block tag when calling `eth_estimateGas` and `eth_createAccessList` [#7951](https://github.com/hyperledger/besu/pull/7951)

### Bug fixes
- Correct default parameters for frontier transactions in `eth_call` and `eth_estimateGas` [#7965](https://github.com/hyperledger/besu/pull/7965)
- Correct default parameters for frontier transactions in `eth_call` and `eth_estimateGas` [#7965](https://github.com/hyperledger/besu/pull/7965)

## 24.12.0

Expand Down Expand Up @@ -73,6 +73,7 @@
- Fix registering new metric categories from plugins [#7825](https://github.com/hyperledger/besu/pull/7825)
- Fix CVE-2024-47535 [7878](https://github.com/hyperledger/besu/pull/7878)
- Fix QBFT prepared block based proposal validation [#7875](https://github.com/hyperledger/besu/pull/7875)
- Correctly parse nonce as hex in `eth_call` account overrides [#7999](https://github.com/hyperledger/besu/pull/7999)

## 24.10.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.apache.tuweni.bytes.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -106,11 +107,11 @@ public Builder withBalance(final Wei balance) {
/**
* Sets the nonce override
*
* @param nonce the nonce override
* @param nonce the nonce override in hex
* @return the builder
*/
public Builder withNonce(final Long nonce) {
this.nonce = Optional.ofNullable(nonce);
public Builder withNonce(final String nonce) {
this.nonce = Optional.of(Bytes.fromHexStringLenient(nonce).toLong());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void noAccountOverrides() {
@Test
public void someAccountOverrides() {
AccountOverrideMap expectedOverrides = new AccountOverrideMap();
AccountOverride override = new AccountOverride.Builder().withNonce(88L).build();
AccountOverride override = new AccountOverride.Builder().withNonce("0x9e").build();
final Address address = Address.fromHexString("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3");
expectedOverrides.put(address, override);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void noAccountOverrides() {
@Test
public void someAccountOverrides() {
AccountOverrideMap expectedOverrides = new AccountOverrideMap();
AccountOverride override = new AccountOverride.Builder().withNonce(88L).build();
AccountOverride override = new AccountOverride.Builder().withNonce("0x9e").build();
final Address address = Address.fromHexString("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3");
expectedOverrides.put(address, override);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void jsonDeserializesCorrectly() throws Exception {
+ "\":"
+ "{"
+ "\"balance\": \"0x01\","
+ "\"nonce\": 88"
+ "\"nonce\": \"0x9e\""
+ "}}],\"id\":1}";

final JsonRpcRequestContext request = new JsonRpcRequestContext(readJsonAsJsonRpcRequest(json));
Expand All @@ -62,7 +62,7 @@ public void jsonDeserializesCorrectly() throws Exception {
final AccountOverride accountOverride =
accountOverrideParam.get(Address.fromHexString(ADDRESS_HEX1));

assertThat(accountOverride.getNonce()).isEqualTo(Optional.of(88L));
assertThat(accountOverride.getNonce().get()).isEqualTo(158);
assertThat(accountOverride.getBalance()).isEqualTo(Optional.of(Wei.of(1)));
assertFalse(accountOverride.getStateDiff().isPresent());
}
Expand Down Expand Up @@ -96,6 +96,34 @@ public void jsonWithCodeDeserializesCorrectly() throws Exception {
assertFalse(accountOverride.getStateDiff().isPresent());
}

@Test
public void jsonWithHexNonceDeserializesCorrectly() throws Exception {
final String json =
"{\"jsonrpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{"
+ "\"from\":\"0x0\", \"to\": \"0x0\"}, "
+ "\"latest\","
+ "{\""
+ ADDRESS_HEX1
+ "\":"
+ "{"
+ "\"balance\": \"0x01\","
+ "\"nonce\": \""
+ "0x9e"
+ "\""
+ "}}],\"id\":1}";

final JsonRpcRequestContext request = new JsonRpcRequestContext(readJsonAsJsonRpcRequest(json));
final AccountOverrideMap accountOverrideParam =
request.getRequiredParameter(2, AccountOverrideMap.class);

final AccountOverride accountOverride =
accountOverrideParam.get(Address.fromHexString(ADDRESS_HEX1));

assertThat(accountOverride.getBalance()).isEqualTo(Optional.of(Wei.of(1)));
assertThat(accountOverride.getNonce().get()).isEqualTo(158); // 0x9e
assertFalse(accountOverride.getStateDiff().isPresent());
}

@Test
public void jsonWithStorageOverridesDeserializesCorrectly() throws Exception {
final String json =
Expand All @@ -107,7 +135,7 @@ public void jsonWithStorageOverridesDeserializesCorrectly() throws Exception {
+ "\":"
+ "{"
+ "\"balance\": \"0x01\","
+ "\"nonce\": 88,"
+ "\"nonce\": \"0x9E\","
+ "\"stateDiff\": {"
+ "\""
+ STORAGE_KEY
Expand All @@ -124,7 +152,7 @@ public void jsonWithStorageOverridesDeserializesCorrectly() throws Exception {

final AccountOverride accountOverride =
accountOverrideParam.get(Address.fromHexString(ADDRESS_HEX1));
assertThat(accountOverride.getNonce()).isEqualTo(Optional.of(88L));
assertThat(accountOverride.getNonce().get()).isEqualTo(158);

assertTrue(accountOverride.getStateDiff().isPresent());
assertThat(accountOverride.getStateDiff().get().get(STORAGE_KEY)).isEqualTo(STORAGE_VALUE);
Expand All @@ -141,7 +169,7 @@ public void jsonWithMultipleAccountOverridesDeserializesCorrectly() throws Excep
+ "\":"
+ "{"
+ "\"balance\": \"0x01\","
+ "\"nonce\": 88,"
+ "\"nonce\": \"0x9E\","
+ "\"stateDiff\": {"
+ "\""
+ STORAGE_KEY
Expand All @@ -154,7 +182,7 @@ public void jsonWithMultipleAccountOverridesDeserializesCorrectly() throws Excep
+ "\":"
+ "{"
+ "\"balance\": \"0xFF\","
+ "\"nonce\": 99,"
+ "\"nonce\": \"0x9D\","
+ "\"stateDiff\": {"
+ "\""
+ STORAGE_KEY
Expand All @@ -171,14 +199,14 @@ public void jsonWithMultipleAccountOverridesDeserializesCorrectly() throws Excep

final AccountOverride accountOverride1 =
accountOverrideParam.get(Address.fromHexString(ADDRESS_HEX1));
assertThat(accountOverride1.getNonce()).isEqualTo(Optional.of(88L));
assertThat(accountOverride1.getNonce().get()).isEqualTo(158);
assertThat(accountOverride1.getBalance()).isEqualTo(Optional.of(Wei.fromHexString("0x01")));
assertTrue(accountOverride1.getStateDiff().isPresent());
assertThat(accountOverride1.getStateDiff().get().get(STORAGE_KEY)).isEqualTo(STORAGE_VALUE);

final AccountOverride accountOverride2 =
accountOverrideParam.get(Address.fromHexString(ADDRESS_HEX2));
assertThat(accountOverride2.getNonce()).isEqualTo(Optional.of(99L));
assertThat(accountOverride2.getNonce().get()).isEqualTo(157);
assertThat(accountOverride2.getBalance()).isEqualTo(Optional.of(Wei.fromHexString("0xFF")));
assertTrue(accountOverride2.getStateDiff().isPresent());
assertThat(accountOverride2.getStateDiff().get().get(STORAGE_KEY)).isEqualTo(STORAGE_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.hyperledger.besu.evm.operation;

import static org.hyperledger.besu.evm.internal.Words.clampedToLong;
import static org.hyperledger.besu.evm.operation.AbstractCallOperation.LEGACY_FAILURE_STACK_ITEM;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -158,12 +157,18 @@ protected int getPcIncrement() {
*/
protected abstract Code getInitCode(MessageFrame frame, EVM evm);

private void fail(final MessageFrame frame) {
/**
* Handles stack items when operation fails for validation reasons (noe enough ether, bad eof
* code)
*
* @param frame the current execution frame
*/
protected void fail(final MessageFrame frame) {
final long inputOffset = clampedToLong(frame.getStackItem(1));
final long inputSize = clampedToLong(frame.getStackItem(2));
frame.readMutableMemory(inputOffset, inputSize);
frame.popStackItems(getStackItemsConsumed());
frame.pushStackItem(LEGACY_FAILURE_STACK_ITEM);
frame.pushStackItem(Bytes.EMPTY);
}

private void spawnChildMessage(final MessageFrame parent, final Code code, final EVM evm) {
Expand Down Expand Up @@ -227,12 +232,12 @@ private void complete(final MessageFrame frame, final MessageFrame childFrame, f
} else {
frame.getWorldUpdater().deleteAccount(childFrame.getRecipientAddress());
frame.setReturnData(childFrame.getOutputData());
frame.pushStackItem(LEGACY_FAILURE_STACK_ITEM);
frame.pushStackItem(Bytes.EMPTY);
onInvalid(frame, (CodeInvalid) outputCode);
}
} else {
frame.setReturnData(childFrame.getOutputData());
frame.pushStackItem(LEGACY_FAILURE_STACK_ITEM);
frame.pushStackItem(Bytes.EMPTY);
onFailure(frame, childFrame.getExceptionalHaltReason());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.evm.operation;

import static org.hyperledger.besu.evm.internal.Words.clampedAdd;
import static org.hyperledger.besu.evm.worldstate.DelegatedCodeGasCostHelper.deductDelegatedCodeGasCost;

import org.hyperledger.besu.datatypes.Address;
Expand Down Expand Up @@ -116,9 +117,11 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
}
if (toBytes.size() > Address.SIZE) {
return new OperationResult(
gasCalculator.memoryExpansionGasCost(frame, inputOffset, inputLength)
+ (zeroValue ? 0 : gasCalculator.callValueTransferGasCost())
+ gasCalculator.getColdAccountAccessCost(),
clampedAdd(
clampedAdd(
gasCalculator.memoryExpansionGasCost(frame, inputOffset, inputLength),
(zeroValue ? 0 : gasCalculator.callValueTransferGasCost())),
gasCalculator.getColdAccountAccessCost()),
ExceptionalHaltReason.ADDRESS_OUT_OF_RANGE);
}
Address to = Words.toAddress(toBytes);
Expand All @@ -135,16 +138,21 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

boolean accountCreation = contract == null && !zeroValue;
long cost =
gasCalculator.memoryExpansionGasCost(frame, inputOffset, inputLength)
+ (zeroValue ? 0 : gasCalculator.callValueTransferGasCost())
+ (frame.warmUpAddress(to) || gasCalculator.isPrecompile(to)
? gasCalculator.getWarmStorageReadCost()
: gasCalculator.getColdAccountAccessCost())
+ (accountCreation ? gasCalculator.newAccountGasCost() : 0);
long currentGas = frame.getRemainingGas() - cost;
if (currentGas < 0) {
clampedAdd(
clampedAdd(
clampedAdd(
gasCalculator.memoryExpansionGasCost(frame, inputOffset, inputLength),
(zeroValue ? 0 : gasCalculator.callValueTransferGasCost())),
(frame.warmUpAddress(to) || gasCalculator.isPrecompile(to)
? gasCalculator.getWarmStorageReadCost()
: gasCalculator.getColdAccountAccessCost())),
(accountCreation ? gasCalculator.newAccountGasCost() : 0));
long currentGas = frame.getRemainingGas();
if (currentGas < cost) {
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}
currentGas -= cost;
frame.expandMemory(inputOffset, inputLength);

final Code code =
contract == null
Expand Down Expand Up @@ -202,7 +210,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
.build();

frame.setState(MessageFrame.State.CODE_SUSPENDED);
return new OperationResult(cost + childGas, null, 0);
return new OperationResult(clampedAdd(cost, childGas), null, 0);
}

private @Nonnull OperationResult softFailure(final MessageFrame frame, final long cost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static org.hyperledger.besu.crypto.Hash.keccak256;
import static org.hyperledger.besu.evm.internal.Words.clampedAdd;
import static org.hyperledger.besu.evm.internal.Words.clampedToInt;
import static org.hyperledger.besu.evm.internal.Words.clampedToLong;

import org.hyperledger.besu.datatypes.Address;
Expand Down Expand Up @@ -49,8 +48,8 @@ public EOFCreateOperation(final GasCalculator gasCalculator) {

@Override
public long cost(final MessageFrame frame, final Supplier<Code> codeSupplier) {
final int inputOffset = clampedToInt(frame.getStackItem(2));
final int inputSize = clampedToInt(frame.getStackItem(3));
final long inputOffset = clampedToLong(frame.getStackItem(2));
final long inputSize = clampedToLong(frame.getStackItem(3));
return clampedAdd(
gasCalculator().memoryExpansionGasCost(frame, inputOffset, inputSize),
clampedAdd(
Expand Down Expand Up @@ -86,4 +85,13 @@ protected Bytes getInputData(final MessageFrame frame) {
protected int getPcIncrement() {
return 2;
}

@Override
protected void fail(final MessageFrame frame) {
final long inputOffset = clampedToLong(frame.getStackItem(2));
final long inputSize = clampedToLong(frame.getStackItem(3));
frame.readMutableMemory(inputOffset, inputSize);
frame.popStackItems(getStackItemsConsumed());
frame.pushStackItem(Bytes.EMPTY);
}
}

0 comments on commit 8e3d51f

Please sign in to comment.