Skip to content

Commit

Permalink
Log wei amount in human readable form
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Aug 25, 2022
1 parent dc7895e commit 907a39d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
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 Down Expand Up @@ -59,7 +60,6 @@
public class EngineNewPayload extends ExecutionEngineJsonRpcMethod {

private static final Hash OMMERS_HASH_CONSTANT = Hash.EMPTY_LIST_HASH;
private static final double TO_GWEI_DIVISOR = Math.pow(10, 9);
private static final Logger LOG = LoggerFactory.getLogger(EngineNewPayload.class);
private static final BlockHeaderFunctions headerFunctions = new MainnetBlockHeaderFunctions();
private final MergeMiningCoordinator mergeCoordinator;
Expand Down Expand Up @@ -259,16 +259,12 @@ JsonRpcResponse respondWithInvalid(
private void logImportedBlockInfo(final Block block, final double timeInS) {
LOG.info(
String.format(
"Imported #%,d / %d tx / %,d (%01.1f%%) gas / base fee %01.2f%% gwei / (%s) in %01.3fs.",
"Imported #%,d / %d tx / %,d (%01.1f%%) gas / base fee %s / (%s) in %01.3fs.",
block.getHeader().getNumber(),
block.getBody().getTransactions().size(),
block.getHeader().getGasUsed(),
(block.getHeader().getGasUsed() * 100.0) / block.getHeader().getGasLimit(),
block
.getHeader()
.getBaseFee()
.map(wei -> wei.getAsBigInteger().doubleValue() / TO_GWEI_DIVISOR)
.orElse(Double.NaN),
block.getHeader().getBaseFee().map(Wei::toHumanReadableString).orElse("N/A"),
block.getHash().toHexString(),
timeInS));
}
Expand Down

0 comments on commit 907a39d

Please sign in to comment.