Skip to content

Commit

Permalink
refactor + fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Ameziane H. <[email protected]>
  • Loading branch information
ahamlat committed Nov 29, 2024
1 parent af708b6 commit 09cb8da
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
22 changes: 21 additions & 1 deletion datatypes/src/main/java/org/hyperledger/besu/datatypes/Wei.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,31 @@ public static Wei fromQuantity(final Quantity quantity) {
* @return the string
*/
public String toHumanReadableString() {
return toHumanReadableStringWithPadding(1);
}

/**
* Wei to human-readable string, with padding
*
* @return the string
*/
public String toHumanReadablePaddedString() {
return toHumanReadableStringWithPadding(6);
}

/**
* Returns a human-readable String, the number of returned characters depends on the width
* parameter
*
* @param width the number of digits to use
* @return a human-readable String
*/
private String toHumanReadableStringWithPadding(final int width) {
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("%6." + preferredUnit.decimals + "f %s", res, preferredUnit);
return String.format("%" + width + "." + preferredUnit.decimals + "f %s", res, preferredUnit);
}

/** The enum Unit. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,28 @@ public void toHumanReadableString() {
assertThat(Wei.of(new BigInteger("1" + String.valueOf(manyZeros))).toHumanReadableString())
.isEqualTo("100.00 tether");
}

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

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

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

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

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

final char[] manyZeros = new char[32];
Arrays.fill(manyZeros, '0');
assertThat(
Wei.of(new BigInteger("1" + String.valueOf(manyZeros))).toHumanReadablePaddedString())
.isEqualTo("100.00 tether");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ private void logImportedBlockInfo(
messageArgs.addAll(
List.of(
blobCount,
block.getHeader().getBaseFee().map(Wei::toHumanReadableString).orElse("N/A"),
block.getHeader().getBaseFee().map(Wei::toHumanReadablePaddedString).orElse("N/A"),
block.getHeader().getGasUsed(),
(block.getHeader().getGasUsed() * 100.0) / block.getHeader().getGasLimit(),
timeInS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public class BlockProcessingResult extends BlockValidationResult {
/** A result indicating that processing failed. */
public static final BlockProcessingResult FAILED = new BlockProcessingResult("processing failed");

/**
* A result indicating that processing was successful but incomplete.
*
* @param yield the outputs of processing a block
*/
public BlockProcessingResult(final Optional<BlockProcessingOutputs> yield) {
this.yield = yield;
this.isPartial = false;
}

/**
* A result indicating that processing was successful but incomplete.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public BlockProcessingResult processBlock(

metadataUpdater.commit();
BlockProcessingOutputs blockProcessingOutput = new BlockProcessingOutputs(worldState, receipts);
return new BlockProcessingResult(Optional.of(blockProcessingOutput), Optional.empty());
return new BlockProcessingResult(Optional.of(blockProcessingOutput));
}

void storePrivateMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public BlockProcessingResult processBlock(
return BlockProcessingResult.FAILED;
}
BlockProcessingOutputs blockProcessingOutput = new BlockProcessingOutputs(worldState, receipts);
return new BlockProcessingResult(Optional.of(blockProcessingOutput), Optional.empty());
return new BlockProcessingResult(Optional.of(blockProcessingOutput));
}

private boolean rewardCoinbase(
Expand Down

0 comments on commit 09cb8da

Please sign in to comment.