Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Sep 16, 2024
1 parent e96fd82 commit 26198f8
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package tech.pegasys.teku.validator.coordinator;

import static tech.pegasys.teku.infrastructure.logging.Converter.weiToGwei;

import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSPublicKey;
Expand Down Expand Up @@ -110,6 +112,7 @@ private ExecutionPayloadHeader createLocalBid(
schemaDefinitions
.getBlobKzgCommitmentsSchema()
.createFromBlobsBundle(getPayloadResponse.getBlobsBundle().orElseThrow());
// convert Wei to Gwei
return schemaDefinitions
.getExecutionPayloadHeaderSchema()
.createExecutionPayloadHeader(
Expand All @@ -121,9 +124,7 @@ private ExecutionPayloadHeader createLocalBid(
.gasLimit(getPayloadResponse.getExecutionPayload().getGasLimit())
.builderIndex(() -> builderIndex)
.slot(() -> slot)
.value(
() ->
UInt64.valueOf(getPayloadResponse.getExecutionPayloadValue().toLong()))
.value(() -> weiToGwei(getPayloadResponse.getExecutionPayloadValue()))
.blobKzgCommitmentsRoot(blobKzgCommitments::hashTreeRoot));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private List<PayloadAttesterDuty> createPayloadAttesterDuties(
final List<PayloadAttesterDuty> payloadAttesterDutyList = new ArrayList<>();

final Int2ObjectMap<UInt64> validatorIndexToCommitteeAssignmentMap =
spec.getValidatorIndexToPctAssignmentMap(state, epoch);
spec.getValidatorIndexToPtcAssignmentMap(state, epoch);
for (final int validatorIndex : validatorIndices) {
final UInt64 slot = validatorIndexToCommitteeAssignmentMap.get(validatorIndex);
if (slot != null) {
Expand Down
4 changes: 2 additions & 2 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,9 @@ public Int2ObjectMap<CommitteeAssignment> getValidatorIndexToCommitteeAssignment
.getValidatorIndexToCommitteeAssignmentMap(state, epoch);
}

public Int2ObjectMap<UInt64> getValidatorIndexToPctAssignmentMap(
public Int2ObjectMap<UInt64> getValidatorIndexToPtcAssignmentMap(
final BeaconState state, final UInt64 epoch) {
return atEpoch(epoch).getValidatorsUtil().getValidatorIndexToPctAssignmentMap(state, epoch);
return atEpoch(epoch).getValidatorsUtil().getValidatorIndexToPtcAssignmentMap(state, epoch);
}

// Attestation helpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Optional<UInt64> getPtcAssignment(
return Optional.empty();
}

public Int2ObjectMap<UInt64> getValidatorIndexToPctAssignmentMap(
public Int2ObjectMap<UInt64> getValidatorIndexToPtcAssignmentMap(
final BeaconState state, final UInt64 epoch) {
final Int2ObjectMap<UInt64> assignmentMap = new Int2ObjectArrayMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.ethereum.events.SlotEventsChannel;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand All @@ -25,12 +27,14 @@
import tech.pegasys.teku.spec.datastructures.execution.SignedExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.eip7732.ExecutionPayloadHeaderEip7732;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.logic.common.operations.validation.OperationInvalidReason;
import tech.pegasys.teku.statetransition.OperationAddedSubscriber;
import tech.pegasys.teku.statetransition.validation.InternalValidationResult;
import tech.pegasys.teku.statetransition.validation.OperationValidator;
import tech.pegasys.teku.statetransition.validation.ValidationResultCode;

public class ExecutionPayloadHeaderPool implements SlotEventsChannel {
private static final Logger LOG = LogManager.getLogger();

// builders can broadcast a bid for only the current or the next slot, so no need to keep bids for
// a long time in the pool
Expand Down Expand Up @@ -77,10 +81,14 @@ public Optional<SignedExecutionPayloadHeader> selectBidForBlock(
})
.toList();
for (SignedExecutionPayloadHeader bid : applicableBids) {
if (operationValidator.validateForBlockInclusion(stateAtBlockSlot, bid).isEmpty()) {
final Optional<OperationInvalidReason> blockInclusionValidation =
operationValidator.validateForBlockInclusion(stateAtBlockSlot, bid);
if (blockInclusionValidation.isEmpty()) {
return Optional.of(bid);
} else {
// The item is no longer valid to be included in a block so remove it from the pool.
LOG.warn(
"Bid is not valid to be included in a block: {}. Removing it from the pool.",
blockInclusionValidation.get().describe());
remove(bid);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public SafeFuture<InternalValidationResult> validateForGossip(
final Optional<Bytes32> parentBlockHash =
recentChainData.getExecutionBlockHashForBlockRoot(header.getParentBlockRoot());
if (parentBlockHash.isEmpty()
|| header.getParentBlockHash().equals(parentBlockHash.get())) {
|| !header.getParentBlockHash().equals(parentBlockHash.get())) {
return InternalValidationResult.IGNORE;
}
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
package tech.pegasys.teku.infrastructure.logging;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import org.apache.tuweni.units.bigints.UInt256;
import org.web3j.utils.Convert;
import org.web3j.utils.Convert.Unit;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

public class Converter {

Expand All @@ -28,8 +31,19 @@ public static String weiToEth(final UInt256 wei) {
}

public static String gweiToEth(final UInt256 gwei) {
return new BigDecimal(gwei.toBigInteger())
.divide(gweiToEthFactor, 6, RoundingMode.HALF_UP)
.toString();
return gweiToEth(new BigDecimal(gwei.toBigInteger()));
}

public static String gweiToEth(final UInt64 gwei) {
return gweiToEth(new BigDecimal(gwei.bigIntegerValue()));
}

public static UInt64 weiToGwei(final UInt256 wei) {
final BigInteger gwei = Convert.fromWei(wei.toDecimalString(), Unit.GWEI).toBigInteger();
return UInt64.valueOf(gwei);
}

private static String gweiToEth(final BigDecimal gwei) {
return gwei.divide(gweiToEthFactor, 6, RoundingMode.HALF_UP).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,16 @@ private String formatBlockRoots(final Set<Bytes32> blockRoots) {
return blockRoots.stream().map(LogFormatter::formatHashRoot).collect(Collectors.joining(", "));
}

public void logPublishedBid(final UInt64 slot, final UInt64 builderIndex, final String ethValue) {
public void logPublishedBid(
final UInt64 slot,
final UInt64 builderIndex,
final Bytes32 parentBlockRoot,
final String ethValue) {
log.info(
ColorConsolePrinter.print(
String.format(
"%sPublished bid Slot: %s, Builder: %s, Value: %s ETH",
PREFIX, slot, builderIndex, ethValue),
"%sPublished bid Slot: %s, Builder: %s, Parent Block Root: %s, Value: %s ETH",
PREFIX, slot, builderIndex, parentBlockRoot, ethValue),
Color.CYAN));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,37 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

class ConverterTest {

@ParameterizedTest
@MethodSource("getUInt256Values")
void test(final UInt256 wei, final String expected) {
@MethodSource("getWeiToEthArguments")
void testWeiToEth(final UInt256 wei, final String expected) {
String output = Converter.weiToEth(wei);
assertThat(output).isEqualTo(expected);
}

private static Stream<Arguments> getUInt256Values() {
@ParameterizedTest
@MethodSource("getWeiToGweiArguments")
void testWeiToGwei(final UInt256 wei, final UInt64 expected) {
UInt64 output = Converter.weiToGwei(wei);
assertThat(output).isEqualTo(expected);
}

private static Stream<Arguments> getWeiToEthArguments() {
return Stream.of(
Arguments.of(UInt256.valueOf(1), "0.000000"),
Arguments.of(UInt256.valueOf(1000), "0.000000"),
Arguments.of(UInt256.valueOf(3401220000000000L), "0.003401"),
Arguments.of(UInt256.valueOf(889999203452340000L), "0.889999"));
}

private static Stream<Arguments> getWeiToGweiArguments() {
return Stream.of(
Arguments.of(UInt256.valueOf(1), UInt64.valueOf(0)),
Arguments.of(UInt256.valueOf(1000), UInt64.valueOf(0)),
Arguments.of(UInt256.valueOf(3401220000000000L), UInt64.valueOf(3401220)),
Arguments.of(UInt256.valueOf(889999203452340000L), UInt64.valueOf(889999203)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.units.bigints.UInt256;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.metrics.Validator.DutyType;
Expand Down Expand Up @@ -161,7 +160,8 @@ private SafeFuture<Void> processExecutionPayloadHeader(
VALIDATOR_LOGGER.logPublishedBid(
bid.getSlot(),
bid.getBuilderIndex(),
gweiToEth(UInt256.valueOf(bid.getValue().longValue())));
bid.getParentBlockRoot(),
gweiToEth(bid.getValue()));
});
}

Expand Down

0 comments on commit 26198f8

Please sign in to comment.