Skip to content

Commit

Permalink
switch to hex integers for eth_feeHistory (hyperledger#2517)
Browse files Browse the repository at this point in the history

Signed-off-by: Ratan Rai Sur <[email protected]>
  • Loading branch information
RatanRSur committed Jul 8, 2021
1 parent be45409 commit 3d909e9
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> computeRewards(final List<Double> rewardPercentiles, final Block block) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long> getBaseFeePerGas();

List<Double> getGasUsedRatio();

Optional<List<List<Long>>> getReward();

@Value.Immutable
@Value.Style(allParameters = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
interface FeeHistoryResult {
@JsonProperty("oldestBlock")
String getOldestBlock();

@JsonProperty("baseFeePerGas")
List<String> getBaseFeePerGas();

@JsonProperty("gasUsedRatio")
List<Double> getGasUsedRatio();

@Nullable
@JsonProperty("reward")
List<List<String>> getReward();

static FeeHistoryResult from(final FeeHistory feeHistory) {
return ImmutableFeeHistoryResult.of(
Quantity.create(feeHistory.getOldestBlock()),
feeHistory.getBaseFeePerGas().stream()
.map(Quantity::create)
.collect(toUnmodifiableList()),
feeHistory.getGasUsedRatio(),
feeHistory
.getReward()
.map(
outerList ->
outerList.stream()
.map(
innerList ->
innerList.stream()
.map(Quantity::create)
.collect(toUnmodifiableList()))
.collect(toUnmodifiableList()))
.orElse(null));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
"jsonrpc": "2.0",
"id": 28,
"result": {
"oldestBlock": 31,
"oldestBlock": "0x1f",
"baseFeePerGas": [
0,
0,
0
"0x0",
"0x0",
"0x0"
],
"gasUsedRatio": [
0.00773588677333021,
0.007545537421791245
],
"reward": [
[
1,
1,
1
"0x1",
"0x1",
"0x1"
],
[
1,
1,
1
"0x1",
"0x1",
"0x1"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"jsonrpc": "2.0",
"id": 28,
"result": {
"oldestBlock": 31,
"oldestBlock": "0x1f",
"baseFeePerGas": [
0,
0,
0
"0x0",
"0x0",
"0x0"
],
"gasUsedRatio": [
0.00773588677333021,
Expand Down

0 comments on commit 3d909e9

Please sign in to comment.