Skip to content

Commit

Permalink
account overrides: parse hex string into nonce long (hyperledger#7999)
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarla authored Dec 9, 2024
1 parent 1855836 commit 4033d3b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 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

0 comments on commit 4033d3b

Please sign in to comment.