From 6b740f49c468f88d40358b3de70e2e6ca727c927 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Wed, 7 Jul 2021 19:30:41 -0500 Subject: [PATCH 1/3] switch to hex Signed-off-by: Ratan Rai Sur --- .../internal/methods/EthFeeHistory.java | 23 +++--- .../priv/PrivGetTransactionReceipt.java | 2 +- .../jsonrpc/internal/results/FeeHistory.java | 76 +++++++++++++++++++ .../internal/results/FeeHistoryResult.java | 40 ---------- .../internal/methods/EthFeeHistoryTest.java | 33 ++++---- .../eth_feeHistory_fieldNamesWithReward.json | 20 ++--- ...th_feeHistory_fieldNamesWithoutReward.json | 8 +- 7 files changed, 122 insertions(+), 80 deletions(-) create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java delete mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistoryResult.java diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java index 2d1ad9614f7..2ae8b78d75c 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java @@ -23,7 +23,8 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableFeeHistoryResult; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.FeeHistory; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableFeeHistory; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockHeader; @@ -134,15 +135,17 @@ public JsonRpcResponse response(final JsonRpcRequestContext request) { block)) .collect(toUnmodifiableList())); - final ImmutableFeeHistoryResult.Builder feeHistoryResultBuilder = - ImmutableFeeHistoryResult.builder() - .oldestBlock(oldestBlock) - .baseFeePerGas( - Stream.concat(explicitlyRequestedBaseFees.stream(), Stream.of(nextBaseFee)) - .collect(toUnmodifiableList())) - .gasUsedRatio(gasUsedRatios); - maybeRewards.ifPresent(feeHistoryResultBuilder::reward); - return new JsonRpcSuccessResponse(requestId, feeHistoryResultBuilder.build()); + return new JsonRpcSuccessResponse( + requestId, + FeeHistory.FeeHistoryResult.from( + ImmutableFeeHistory.builder() + .oldestBlock(oldestBlock) + .baseFeePerGas( + Stream.concat(explicitlyRequestedBaseFees.stream(), Stream.of(nextBaseFee)) + .collect(toUnmodifiableList())) + .gasUsedRatio(gasUsedRatios) + .reward(maybeRewards) + .build())); } private List computeRewards(final List rewardPercentiles, final Block block) { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java index f38062ed514..c93e1527a3d 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java @@ -106,7 +106,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { buildPrivateTransactionReceiptResult(privateTransaction, privateTransactionReceipt); LOG.trace( - "Created Private Transaction Receipt Result from given Transaction Hash {}", + "Created Private Transaction Receipt FeeHistoryResult from given Transaction Hash {}", privateTransaction.getPmtHash()); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), result); diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java new file mode 100644 index 00000000000..5a1c229eccd --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java @@ -0,0 +1,76 @@ +/* + * Copyright ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; + +import static java.util.stream.Collectors.toUnmodifiableList; + +import java.util.List; +import java.util.Optional; +import javax.annotation.Nullable; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.immutables.value.Value; + +@Value.Immutable +public interface FeeHistory { + + long getOldestBlock(); + + List getBaseFeePerGas(); + + List getGasUsedRatio(); + + Optional>> getReward(); + + @Value.Immutable + @Value.Style(allParameters = true) + @JsonInclude(JsonInclude.Include.NON_NULL) + interface FeeHistoryResult { + @JsonProperty("oldestBlock") + String getOldestBlock(); + + @JsonProperty("baseFeePerGas") + List getBaseFeePerGas(); + + @JsonProperty("gasUsedRatio") + List getGasUsedRatio(); + + @Nullable + @JsonProperty("reward") + List> getReward(); + + static FeeHistoryResult from(final FeeHistory feeHistory) { + return ImmutableFeeHistoryResult.of( + "0x" + Long.toHexString(feeHistory.getOldestBlock()), + feeHistory.getBaseFeePerGas().stream() + .map(baseFeePerGas -> "0x" + Long.toHexString(baseFeePerGas)) + .collect(toUnmodifiableList()), + feeHistory.getGasUsedRatio(), + feeHistory + .getReward() + .map( + outerList -> + outerList.stream() + .map( + innerList -> + innerList.stream() + .map(gasUsedRatio -> "0x" + Long.toHexString(gasUsedRatio)) + .collect(toUnmodifiableList())) + .collect(toUnmodifiableList())) + .orElse(null)); + } + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistoryResult.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistoryResult.java deleted file mode 100644 index 7311c6e6c3a..00000000000 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistoryResult.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; - -import java.util.List; -import javax.annotation.Nullable; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.immutables.value.Value; - -@Value.Immutable -@JsonInclude(JsonInclude.Include.NON_NULL) -public interface FeeHistoryResult { - - @JsonProperty("oldestBlock") - long getOldestBlock(); - - @JsonProperty("baseFeePerGas") - List getBaseFeePerGas(); - - @JsonProperty("gasUsedRatio") - List getGasUsedRatio(); - - @Nullable - @JsonProperty("reward") - List> getReward(); -} diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java index acd88e17983..6ca74ee874c 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java @@ -30,7 +30,8 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.FeeHistoryResult; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.FeeHistory; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableFeeHistory; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableFeeHistoryResult; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.core.Block; @@ -88,12 +89,13 @@ public void allFieldsPresentForLatestBlock() { ((JsonRpcSuccessResponse) feeHistoryRequest(1, "latest", new double[] {100.0})) .getResult()) .isEqualTo( - ImmutableFeeHistoryResult.builder() - .oldestBlock(10) - .baseFeePerGas(List.of(25496L, 28683L)) - .gasUsedRatio(List.of(0.9999999992132459)) - .reward(List.of(List.of(1524763764L))) - .build()); + FeeHistory.FeeHistoryResult.from( + ImmutableFeeHistory.builder() + .oldestBlock(10) + .baseFeePerGas(List.of(25496L, 28683L)) + .gasUsedRatio(List.of(0.9999999992132459)) + .reward(List.of(List.of(1524763764L))) + .build())); } @Test @@ -125,10 +127,10 @@ public void doesntGoPastChainHeadWithHighBlockCount() { final ProtocolSpec londonSpec = mock(ProtocolSpec.class); when(londonSpec.getEip1559()).thenReturn(Optional.of(new EIP1559(5))); when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(londonSpec); - final FeeHistoryResult result = + final FeeHistory.FeeHistoryResult result = (ImmutableFeeHistoryResult) ((JsonRpcSuccessResponse) feeHistoryRequest(20, "latest")).getResult(); - assertThat(result.getOldestBlock()).isEqualTo(0); + assertThat(Long.decode(result.getOldestBlock())).isEqualTo(0); assertThat(result.getBaseFeePerGas()).hasSize(12); assertThat(result.getGasUsedRatio()).hasSize(11); assertThat(result.getReward()).isNull(); @@ -139,9 +141,10 @@ public void correctlyHandlesForkBlock() { final ProtocolSpec londonSpec = mock(ProtocolSpec.class); when(londonSpec.getEip1559()).thenReturn(Optional.of(new EIP1559(11))); when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(londonSpec); - final FeeHistoryResult result = - (FeeHistoryResult) ((JsonRpcSuccessResponse) feeHistoryRequest(1, "latest")).getResult(); - assertThat(result.getBaseFeePerGas().get(1)) + final FeeHistory.FeeHistoryResult result = + (FeeHistory.FeeHistoryResult) + ((JsonRpcSuccessResponse) feeHistoryRequest(1, "latest")).getResult(); + assertThat(Long.decode(result.getBaseFeePerGas().get(1))) .isEqualTo(ExperimentalEIPs.EIP1559_BASEFEE_DEFAULT_VALUE); } @@ -156,11 +159,11 @@ public void allZeroPercentilesForZeroBlock() { blockOptions.setBlockNumber(11); final Block emptyBlock = gen.block(blockOptions); blockchain.appendBlock(emptyBlock, gen.receipts(emptyBlock)); - final FeeHistoryResult result = - (FeeHistoryResult) + final FeeHistory.FeeHistoryResult result = + (FeeHistory.FeeHistoryResult) ((JsonRpcSuccessResponse) feeHistoryRequest(1, "latest", new double[] {100.0})) .getResult(); - assertThat(result.getReward()).isEqualTo(List.of(List.of(0L))); + assertThat(result.getReward()).isEqualTo(List.of(List.of("0x0"))); } private JsonRpcResponse feeHistoryRequest(final Object... params) { diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithReward.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithReward.json index 5fb9038d758..259371bb3fe 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithReward.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithReward.json @@ -17,11 +17,11 @@ "jsonrpc": "2.0", "id": 28, "result": { - "oldestBlock": 31, + "oldestBlock": "0x1f", "baseFeePerGas": [ - 0, - 0, - 0 + "0x0", + "0x0", + "0x0" ], "gasUsedRatio": [ 0.00773588677333021, @@ -29,14 +29,14 @@ ], "reward": [ [ - 1, - 1, - 1 + "0x1", + "0x1", + "0x1" ], [ - 1, - 1, - 1 + "0x1", + "0x1", + "0x1" ] ] } diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithoutReward.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithoutReward.json index 04c9e1dd19e..a8cad630bcd 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithoutReward.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_feeHistory_fieldNamesWithoutReward.json @@ -12,11 +12,11 @@ "jsonrpc": "2.0", "id": 28, "result": { - "oldestBlock": 31, + "oldestBlock": "0x1f", "baseFeePerGas": [ - 0, - 0, - 0 + "0x0", + "0x0", + "0x0" ], "gasUsedRatio": [ 0.00773588677333021, From 65fb8338f826ac711808a24dd88ace43371405e0 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Thu, 8 Jul 2021 09:01:22 -0500 Subject: [PATCH 2/3] oops from refactor Signed-off-by: Ratan Rai Sur --- .../privacy/methods/priv/PrivGetTransactionReceipt.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java index c93e1527a3d..f38062ed514 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java @@ -106,7 +106,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { buildPrivateTransactionReceiptResult(privateTransaction, privateTransactionReceipt); LOG.trace( - "Created Private Transaction Receipt FeeHistoryResult from given Transaction Hash {}", + "Created Private Transaction Receipt Result from given Transaction Hash {}", privateTransaction.getPmtHash()); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), result); From 1c3296c42c6780db3ca71bed3d4a7ccf8d995791 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Thu, 8 Jul 2021 10:02:33 -0500 Subject: [PATCH 3/3] Quantity.create Signed-off-by: Ratan Rai Sur --- .../ethereum/api/jsonrpc/internal/results/FeeHistory.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java index 5a1c229eccd..b44be3fb8a0 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FeeHistory.java @@ -54,9 +54,9 @@ interface FeeHistoryResult { static FeeHistoryResult from(final FeeHistory feeHistory) { return ImmutableFeeHistoryResult.of( - "0x" + Long.toHexString(feeHistory.getOldestBlock()), + Quantity.create(feeHistory.getOldestBlock()), feeHistory.getBaseFeePerGas().stream() - .map(baseFeePerGas -> "0x" + Long.toHexString(baseFeePerGas)) + .map(Quantity::create) .collect(toUnmodifiableList()), feeHistory.getGasUsedRatio(), feeHistory @@ -67,7 +67,7 @@ static FeeHistoryResult from(final FeeHistory feeHistory) { .map( innerList -> innerList.stream() - .map(gasUsedRatio -> "0x" + Long.toHexString(gasUsedRatio)) + .map(Quantity::create) .collect(toUnmodifiableList())) .collect(toUnmodifiableList())) .orElse(null));