Skip to content

Commit

Permalink
[ePBS] Fix block/payload production/processing
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Sep 16, 2024
1 parent 4368139 commit fb36439
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodyElectra;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSummary;
import tech.pegasys.teku.spec.datastructures.execution.SignedExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionPayloadElectra;
import tech.pegasys.teku.spec.datastructures.execution.versions.eip7732.ExecutionPayloadEip7732;
import tech.pegasys.teku.spec.datastructures.operations.PayloadAttestation;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;

Expand All @@ -44,6 +45,11 @@ default Optional<ExecutionPayload> getOptionalExecutionPayload() {
return Optional.empty();
}

@Override
default Optional<ExecutionPayloadSummary> getOptionalExecutionPayloadSummary() {
return Optional.empty();
}

@Override
default Optional<SszList<SszKZGCommitment>> getOptionalBlobKzgCommitments() {
return Optional.empty();
Expand All @@ -60,7 +66,7 @@ default Optional<SszList<PayloadAttestation>> getOptionalPayloadAttestations() {
}

@Override
default ExecutionPayloadElectra getExecutionPayload() {
default ExecutionPayloadEip7732 getExecutionPayload() {
throw new UnsupportedOperationException("ExecutionPayload removed in Eip7732");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ void processWithdrawalsUnchecked(
final SszList<Withdrawal> expectedWithdrawals =
getExpectedWithdrawalsSszList(schemaDefinitionsCapella);

// new in ePBS
genericState
.toMutableVersionEip7732()
.ifPresent(
genericStateEip7732 ->
genericStateEip7732.setLatestWithdrawalsRoot(expectedWithdrawals.hashTreeRoot()));

for (int i = 0; i < expectedWithdrawals.size(); i++) {
final Withdrawal withdrawal = expectedWithdrawals.get(i);
beaconStateMutators.decreaseBalance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public BeaconStateEip7732 upgrade(final BeaconState preState) {

state.setLatestBlockHash(
preStateElectra.getLatestExecutionPayloadHeader().getBlockHash());
state.setLatestFullSlot(UInt64.ZERO);
state.setLatestFullSlot(preState.getSlot());
state.setLatestWithdrawalsRoot(Bytes32.ZERO);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
Expand Down Expand Up @@ -109,7 +110,7 @@ public Optional<BlobSidecar> getBlobSidecar(final UInt64 index) {
}

public Stream<BlobIdentifier> getMissingBlobSidecars() {
final Optional<Integer> blockCommitmentsCount = getBlockKzgCommitmentsCount();
final Optional<Integer> blockCommitmentsCount = getBlobKzgCommitmentsCount();
if (blockCommitmentsCount.isPresent()) {
return UInt64.range(UInt64.ZERO, UInt64.valueOf(blockCommitmentsCount.get()))
.filter(blobIndex -> !blobSidecars.containsKey(blobIndex))
Expand All @@ -127,7 +128,7 @@ public Stream<BlobIdentifier> getMissingBlobSidecars() {
}

public Stream<BlobIdentifier> getUnusedBlobSidecarsForBlock() {
final Optional<Integer> blockCommitmentsCount = getBlockKzgCommitmentsCount();
final Optional<Integer> blockCommitmentsCount = getBlobKzgCommitmentsCount();
checkState(blockCommitmentsCount.isPresent(), "Block must me known to call this method");

final UInt64 firstUnusedIndex = UInt64.valueOf(blockCommitmentsCount.get());
Expand Down Expand Up @@ -231,12 +232,12 @@ private void checkCompletion() {
}

private void pruneExcessiveBlobSidecars() {
getBlockKzgCommitmentsCount()
getBlobKzgCommitmentsCount()
.ifPresent(count -> blobSidecars.tailMap(UInt64.valueOf(count), true).clear());
}

private boolean isExcessiveBlobSidecar(final BlobSidecar blobSidecar) {
return getBlockKzgCommitmentsCount()
return getBlobKzgCommitmentsCount()
.map(count -> blobSidecar.getIndex().isGreaterThanOrEqualTo(count))
.orElse(false);
}
Expand All @@ -256,17 +257,17 @@ public void setFetchTriggered() {
}

private boolean areBlobsComplete() {
return getBlockKzgCommitmentsCount().map(count -> blobSidecars.size() >= count).orElse(false);
return getBlobKzgCommitmentsCount().map(count -> blobSidecars.size() >= count).orElse(false);
}

private Optional<Integer> getBlockKzgCommitmentsCount() {
private Optional<Integer> getBlobKzgCommitmentsCount() {
return block
.get()
.map(
.flatMap(
b ->
BeaconBlockBodyDeneb.required(b.getMessage().getBody())
.getBlobKzgCommitments()
.size());
.getOptionalBlobKzgCommitments()
.map(SszList::size));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,20 @@
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.constants.Domain;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.execution.SignedExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.execution.versions.eip7732.ExecutionPayloadEip7732;
import tech.pegasys.teku.spec.datastructures.execution.versions.eip7732.ExecutionPayloadHeaderEip7732;
import tech.pegasys.teku.spec.datastructures.state.Validator;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip7732.BeaconStateEip7732;
import tech.pegasys.teku.statetransition.block.ReceivedBlockEventsChannel;
import tech.pegasys.teku.storage.client.RecentChainData;

public class ExecutionPayloadValidator implements ReceivedBlockEventsChannel {
public class ExecutionPayloadValidator {

private final Spec spec;
private final RecentChainData recentChainData;

private final Set<Bytes32> seenBlockRootInfoSet =
LimitedSet.createSynchronized(VALID_BLOCK_SET_SIZE);
private final Set<BlockRootAndBuilderIndex> receivedValidEnvelopeInfoSet =
LimitedSet.createSynchronized(VALID_BLOCK_SET_SIZE);

Expand All @@ -58,7 +54,7 @@ public SafeFuture<InternalValidationResult> validate(
/*
* [IGNORE] The envelope's block root envelope.block_root has been seen (via both gossip and non-gossip sources) (a client MAY queue payload for processing once the block is retrieved).
*/
if (seenBlockRootInfoSet.add(envelope.getBeaconBlockRoot())) {
if (!recentChainData.containsBlock(envelope.getBeaconBlockRoot())) {
return SafeFuture.completedFuture(InternalValidationResult.IGNORE);
}
/*
Expand Down Expand Up @@ -138,15 +134,5 @@ private boolean verifyBuilderSignature(
return BLS.verify(publicKey, signingRoot, signedExecutionPayloadEnvelope.getSignature());
}

@Override
public void onBlockValidated(final SignedBeaconBlock block) {
seenBlockRootInfoSet.add(block.getRoot());
}

@Override
public void onBlockImported(final SignedBeaconBlock block, final boolean executionOptimistic) {
// NO-OP
}

record BlockRootAndBuilderIndex(Bytes32 blockRoot, UInt64 builderIndex) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@

public class Converter {

static BigDecimal gweiToEthFactor = BigDecimal.TEN.pow(9);

public static String weiToEth(final UInt256 wei) {
final BigDecimal result = Convert.fromWei(wei.toDecimalString(), Convert.Unit.ETHER);
return result.setScale(6, RoundingMode.HALF_UP).toString();
final BigDecimal eth = Convert.fromWei(wei.toDecimalString(), Convert.Unit.ETHER);
return eth.setScale(6, RoundingMode.HALF_UP).toString();
}

public static String gweiToEth(final UInt64 gwei) {
return new BigDecimal(gwei.bigIntegerValue())
.divide(gweiToEthFactor, 6, RoundingMode.HALF_UP)
.toString();
final BigDecimal wei = Convert.toWei(gwei.toString(), Unit.GWEI);
return weiToEth(UInt256.valueOf(wei.toBigInteger()));
}

public static UInt64 weiToGwei(final UInt256 wei) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,6 @@ public void initExecutionPayloadManager() {
executionPayloadManager =
new ExecutionPayloadManager(
executionPayloadValidator, forkChoice, recentChainData, executionLayer);
eventChannels.subscribe(ReceivedBlockEventsChannel.class, executionPayloadValidator);
}

public void initPayloadAttestationManager() {
Expand Down

0 comments on commit fb36439

Please sign in to comment.