Skip to content

Commit

Permalink
Log imported block info post merge (hyperledger#4310)
Browse files Browse the repository at this point in the history
Removed ommers and added base fee.

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 authored and eum602 committed Nov 3, 2023
1 parent 7a0a64e commit 95525f4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Better management of jemalloc presence/absence in startup script [#4237](https://github.com/hyperledger/besu/pull/4237)
- Filter out disconnected peers when fetching available peers [#4269](https://github.com/hyperledger/besu/pull/4269)
- Updated the default value of fast-sync-min-peers post merge [#4298](https://github.com/hyperledger/besu/pull/4298)
- Log imported block info post merge [#4310](https://github.com/hyperledger/besu/pull/4310)

### Bug Fixes
- Accept wit/80 from Nethermind [#4279](https://github.com/hyperledger/besu/pull/4279)
Expand Down
49 changes: 49 additions & 0 deletions datatypes/src/main/java/org/hyperledger/besu/datatypes/Wei.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.plugin.data.Quantity;

import java.math.BigInteger;
import java.util.Arrays;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.BaseUInt256Value;
Expand Down Expand Up @@ -98,4 +99,52 @@ public String toShortHexString() {
public static Wei fromQuantity(final Quantity quantity) {
return Wei.wrap((Bytes) quantity);
}

public String toHumanReadableString() {
final BigInteger amount = toBigInteger();
final int numOfDigits = amount.toString().length();
final Unit preferredUnit = Unit.getPreferred(numOfDigits);
final double res = amount.doubleValue() / preferredUnit.divisor;
return String.format("%1." + preferredUnit.decimals + "f %s", res, preferredUnit);
}

enum Unit {
Wei(0, 0),
KWei(3),
MWei(6),
GWei(9),
Szabo(12),
Finney(15),
Ether(18),
KEther(21),
MEther(24),
GEther(27),
TEther(30);

final int pow;
final double divisor;
final int decimals;

Unit(final int pow) {
this(pow, 2);
}

Unit(final int pow, final int decimals) {
this.pow = pow;
this.decimals = decimals;
this.divisor = Math.pow(10, pow);
}

static Unit getPreferred(final int numOfDigits) {
return Arrays.stream(values())
.filter(u -> numOfDigits <= u.pow + 3)
.findFirst()
.orElse(TEther);
}

@Override
public String toString() {
return name().toLowerCase();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.datatypes;

import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigInteger;
import java.util.Arrays;

import org.junit.Test;

public class WeiTest {

@Test
public void toHumanReadableString() {
assertThat(Wei.ZERO.toHumanReadableString()).isEqualTo("0 wei");
assertThat(Wei.ONE.toHumanReadableString()).isEqualTo("1 wei");

assertThat(Wei.of(999).toHumanReadableString()).isEqualTo("999 wei");
assertThat(Wei.of(1000).toHumanReadableString()).isEqualTo("1.00 kwei");

assertThat(Wei.of(1009).toHumanReadableString()).isEqualTo("1.01 kwei");
assertThat(Wei.of(1011).toHumanReadableString()).isEqualTo("1.01 kwei");

assertThat(Wei.of(new BigInteger("1000000000")).toHumanReadableString()).isEqualTo("1.00 gwei");

assertThat(Wei.of(new BigInteger("1000000000000000000")).toHumanReadableString())
.isEqualTo("1.00 ether");

final char[] manyZeros = new char[32];
Arrays.fill(manyZeros, '0');
assertThat(Wei.of(new BigInteger("1" + String.valueOf(manyZeros))).toHumanReadableString())
.isEqualTo("100.00 tether");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.BlockValidator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
Expand All @@ -41,6 +42,7 @@
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.mainnet.BodyValidation;
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.rlp.RLPException;
Expand All @@ -62,13 +64,16 @@ public class EngineNewPayload extends ExecutionEngineJsonRpcMethod {
private static final Logger LOG = LoggerFactory.getLogger(EngineNewPayload.class);
private static final BlockHeaderFunctions headerFunctions = new MainnetBlockHeaderFunctions();
private final MergeMiningCoordinator mergeCoordinator;
private final EthPeers ethPeers;

public EngineNewPayload(
final Vertx vertx,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeCoordinator) {
final MergeMiningCoordinator mergeCoordinator,
final EthPeers ethPeers) {
super(vertx, protocolContext);
this.mergeCoordinator = mergeCoordinator;
this.ethPeers = ethPeers;
}

@Override
Expand Down Expand Up @@ -200,9 +205,11 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
}

// execute block and return result response
final long startTimeMs = System.currentTimeMillis();
final BlockValidator.Result executionResult = mergeCoordinator.rememberBlock(block);

if (executionResult.errorMessage.isEmpty()) {
logImportedBlockInfo(block, (System.currentTimeMillis() - startTimeMs) / 1000.0);
return respondWith(reqId, blockParam, newBlockHeader.getHash(), VALID);
} else {
LOG.debug("New payload is invalid: {}", executionResult.errorMessage.get());
Expand Down Expand Up @@ -252,4 +259,18 @@ JsonRpcResponse respondWithInvalid(
requestId,
new EnginePayloadStatusResult(INVALID, latestValidHash, Optional.of(validationError)));
}

private void logImportedBlockInfo(final Block block, final double timeInS) {
LOG.info(
String.format(
"Imported #%,d / %d tx / base fee %s / %,d (%01.1f%%) gas / (%s) in %01.3fs. Peers: %d",
block.getHeader().getNumber(),
block.getBody().getTransactions().size(),
block.getHeader().getBaseFee().map(Wei::toHumanReadableString).orElse("N/A"),
block.getHeader().getGasUsed(),
(block.getHeader().getGasUsed() * 100.0) / block.getHeader().getGasLimit(),
block.getHash().toHexString(),
timeInS,
ethPeers.peerCount()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayload;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;

import java.util.Map;
import java.util.Optional;
Expand All @@ -38,14 +39,19 @@ public class ExecutionEngineJsonRpcMethods extends ApiGroupJsonRpcMethods {
private final Optional<MergeMiningCoordinator> mergeCoordinator;
private final ProtocolContext protocolContext;

private final EthPeers ethPeers;

ExecutionEngineJsonRpcMethods(
final MiningCoordinator miningCoordinator, final ProtocolContext protocolContext) {
final MiningCoordinator miningCoordinator,
final ProtocolContext protocolContext,
final EthPeers ethPeers) {
this.mergeCoordinator =
Optional.ofNullable(miningCoordinator)
.filter(mc -> mc.isCompatibleWithEngineApi())
.map(MergeMiningCoordinator.class::cast);

this.protocolContext = protocolContext;
this.ethPeers = ethPeers;
}

@Override
Expand All @@ -59,7 +65,7 @@ protected Map<String, JsonRpcMethod> create() {
if (mergeCoordinator.isPresent()) {
return mapOf(
new EngineGetPayload(syncVertx, protocolContext, blockResultFactory),
new EngineNewPayload(syncVertx, protocolContext, mergeCoordinator.get()),
new EngineNewPayload(syncVertx, protocolContext, mergeCoordinator.get(), ethPeers),
new EngineForkchoiceUpdated(syncVertx, protocolContext, mergeCoordinator.get()),
new EngineExchangeTransitionConfiguration(syncVertx, protocolContext));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Map<String, JsonRpcMethod> methods(
blockchainQueries, protocolSchedule, metricsSystem, transactionPool, dataDir),
new EeaJsonRpcMethods(
blockchainQueries, protocolSchedule, transactionPool, privacyParameters),
new ExecutionEngineJsonRpcMethods(miningCoordinator, protocolContext),
new ExecutionEngineJsonRpcMethods(miningCoordinator, protocolContext, ethPeers),
new GoQuorumJsonRpcPrivacyMethods(
blockchainQueries, protocolSchedule, transactionPool, privacyParameters),
new EthJsonRpcMethods(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -77,11 +78,14 @@ public class EngineNewPayloadTest {

@Mock private MutableBlockchain blockchain;

@Mock private EthPeers ethPeers;

@Before
public void before() {
when(protocolContext.safeConsensusContext(Mockito.any())).thenReturn(Optional.of(mergeContext));
when(protocolContext.getBlockchain()).thenReturn(blockchain);
this.method = new EngineNewPayload(vertx, protocolContext, mergeCoordinator);
when(ethPeers.peerCount()).thenReturn(1);
this.method = new EngineNewPayload(vertx, protocolContext, mergeCoordinator, ethPeers);
}

@Test
Expand Down

0 comments on commit 95525f4

Please sign in to comment.