Skip to content

Commit

Permalink
Added queueless withdrawals for capella
Browse files Browse the repository at this point in the history
 - State was missing `next_withdrawal_index`
 - changed MaxWithdrawalsPerLayload to be int
 - had to breakout process_execution_payload logic so that I could add withdrawals into that, and the spec still does the TTD check in capella which is maybe to cover corner cases, not sure...

 fixes Consensys#6321

Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone committed Nov 10, 2022
1 parent 5d7a987 commit 1bcadd0
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public BeaconStateCapella(BeaconState beaconState) {
capella = beaconState.toVersionCapella().orElseThrow();
this.latestExecutionPayloadHeader =
new ExecutionPayloadHeaderCapella(capella.getLatestExecutionPayloadHeader());
this.latestWithdrawalValidatorIndex = capella.getLatestWithdrawalValidatorIndex();
this.latestWithdrawalValidatorIndex = capella.getNextWithdrawalValidatorIndex();
}

@Override
Expand Down Expand Up @@ -155,6 +155,6 @@ public static void applyCapellaFields(
instance.latestExecutionPayloadHeader.transactionsRoot,
instance.latestExecutionPayloadHeader.withdrawalsRoot));

state.setLatestWithdrawalValidatorIndex(instance.latestWithdrawalValidatorIndex);
state.setNextWithdrawalValidatorIndex(instance.latestWithdrawalValidatorIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ public class CapellaBuilder {
private UInt64 capellaForkEpoch;

private UInt64 maxBlsToExecutionChanges;
private UInt64 maxWithdrawalsPerPayload;
private int maxWithdrawalsPerPayload;

private CapellaBuilder() {}

Expand Down Expand Up @@ -911,7 +911,7 @@ public CapellaBuilder maxBlsToExecutionChanges(final UInt64 maxBlsToExecutionCha
return this;
}

public CapellaBuilder maxWithdrawalsPerPayload(final UInt64 maxWithdrawalsPerPayload) {
public CapellaBuilder maxWithdrawalsPerPayload(final int maxWithdrawalsPerPayload) {
this.maxWithdrawalsPerPayload = maxWithdrawalsPerPayload;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ static SpecConfigCapella required(SpecConfig specConfig) {

UInt64 getMaxBlsToExecutionChanges();

UInt64 getMaxWithdrawalsPerPayload();
int getMaxWithdrawalsPerPayload();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class SpecConfigCapellaImpl extends DelegatingSpecConfigBellatrix
private final UInt64 capellaForkEpoch;

private final UInt64 maxBlsToExecutionChanges;
private final UInt64 maxWithdrawalsPerPayload;
private final int maxWithdrawalsPerPayload;

public SpecConfigCapellaImpl(
final SpecConfigBellatrix specConfig,
final Bytes4 capellaForkVersion,
final UInt64 capellaForkEpoch,
final UInt64 maxBlsToExecutionChanges,
final UInt64 maxWithdrawalsPerPayload) {
final int maxWithdrawalsPerPayload) {
super(specConfig);
this.capellaForkVersion = capellaForkVersion;
this.capellaForkEpoch = capellaForkEpoch;
Expand Down Expand Up @@ -63,7 +63,7 @@ public boolean equals(final Object o) {
&& Objects.equals(capellaForkVersion, that.capellaForkVersion)
&& Objects.equals(capellaForkEpoch, that.capellaForkEpoch)
&& Objects.equals(maxBlsToExecutionChanges, that.maxBlsToExecutionChanges)
&& Objects.equals(maxWithdrawalsPerPayload, that.maxWithdrawalsPerPayload);
&& maxWithdrawalsPerPayload == that.maxWithdrawalsPerPayload;
}

@Override
Expand All @@ -82,7 +82,7 @@ public UInt64 getMaxBlsToExecutionChanges() {
}

@Override
public UInt64 getMaxWithdrawalsPerPayload() {
public int getMaxWithdrawalsPerPayload() {
return maxWithdrawalsPerPayload;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public ExecutionPayloadSchemaCapella(final SpecConfigCapella specConfig) {
namedSchema(
WITHDRAWALS,
SszListSchema.create(
new WithdrawalSchema(), specConfig.getMaxWithdrawalsPerPayload().longValue())));
new WithdrawalSchema(), specConfig.getMaxWithdrawalsPerPayload())));
this.defaultExecutionPayload = createFromBackingNode(getDefaultTree());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public enum BeaconStateFields implements SszFieldName {
NEXT_SYNC_COMMITTEE,
// Bellatrix fields
LATEST_EXECUTION_PAYLOAD_HEADER,
NEXT_WITHDRAWAL_INDEX,
// Capella fields
LATEST_WITHDRAWAL_VALIDATOR_INDEX;
NEXT_WITHDRAWAL_VALIDATOR_INDEX;

private final String sszFieldName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

package tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella;

import static tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields.LATEST_WITHDRAWAL_VALIDATOR_INDEX;
import static tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields.NEXT_WITHDRAWAL_INDEX;
import static tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX;

import com.google.common.base.MoreObjects;
import java.util.Optional;
Expand All @@ -35,8 +36,8 @@ static BeaconStateCapella required(final BeaconState state) {
static void describeCustomCapellaFields(
MoreObjects.ToStringHelper stringBuilder, BeaconStateCapella state) {
BeaconStateBellatrix.describeCustomBellatrixFields(stringBuilder, state);
stringBuilder.add(
"latest_withdrawal_validator_index", state.getLatestWithdrawalValidatorIndex());
stringBuilder.add("next_withdrawal_index", state.getNextWithdrawalIndex());
stringBuilder.add("next_withdrawal_validator_index", state.getNextWithdrawalValidatorIndex());
}

@Override
Expand All @@ -55,8 +56,13 @@ default Optional<BeaconStateCapella> toVersionCapella() {
return Optional.of(this);
}

default UInt64 getLatestWithdrawalValidatorIndex() {
final int index = getSchema().getFieldIndex(LATEST_WITHDRAWAL_VALIDATOR_INDEX);
default UInt64 getNextWithdrawalIndex() {
final int index = getSchema().getFieldIndex(NEXT_WITHDRAWAL_INDEX);
return ((SszUInt64) get(index)).get();
}

default UInt64 getNextWithdrawalValidatorIndex() {
final int index = getSchema().getFieldIndex(NEXT_WITHDRAWAL_VALIDATOR_INDEX);
return ((SszUInt64) get(index)).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

public class BeaconStateSchemaCapella
extends AbstractBeaconStateSchema<BeaconStateCapella, MutableBeaconStateCapella> {
private static final int LATEST_WITHDRAWAL_VALIDATOR_INDEX = 25;
private static final int NEXT_WITHDRAWAL_INDEX = 25;
private static final int NEXT_WITHDRAWAL_VALIDATOR_INDEX = 26;

@VisibleForTesting
BeaconStateSchemaCapella(final SpecConfig specConfig) {
Expand All @@ -50,14 +51,22 @@ private static List<SszField> getUniqueFields(final SpecConfig specConfig) {
LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX,
BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER,
() -> new ExecutionPayloadHeaderSchemaCapella(SpecConfigCapella.required(specConfig)));
final SszField latestWithdrawalValidatorIndexField =
final SszField nextWithdrawalIndexField =
new SszField(
LATEST_WITHDRAWAL_VALIDATOR_INDEX,
BeaconStateFields.LATEST_WITHDRAWAL_VALIDATOR_INDEX,
NEXT_WITHDRAWAL_INDEX,
BeaconStateFields.NEXT_WITHDRAWAL_INDEX,
() -> SszPrimitiveSchemas.UINT64_SCHEMA);
final SszField nextWithdrawalValidatorIndexField =
new SszField(
NEXT_WITHDRAWAL_VALIDATOR_INDEX,
BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX,
() -> SszPrimitiveSchemas.UINT64_SCHEMA);
return Stream.concat(
BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(),
Stream.of(latestExecutionPayloadHeaderField, latestWithdrawalValidatorIndexField))
Stream.of(
latestExecutionPayloadHeaderField,
nextWithdrawalIndexField,
nextWithdrawalValidatorIndexField))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ static MutableBeaconStateCapella required(final MutableBeaconState state) {
@Override
BeaconStateCapella commitChanges();

default void setLatestWithdrawalValidatorIndex(UInt64 latestWithdrawalValidatorIndex) {
default void setNextWithdrawalIndex(UInt64 nextWithdrawalIndex) {
final int fieldIndex = getSchema().getFieldIndex(BeaconStateFields.NEXT_WITHDRAWAL_INDEX);
set(fieldIndex, SszUInt64.of(nextWithdrawalIndex));
}

default void setNextWithdrawalValidatorIndex(UInt64 nextWithdrawalValidatorIndex) {
final int fieldIndex =
getSchema().getFieldIndex(BeaconStateFields.LATEST_WITHDRAWAL_VALIDATOR_INDEX);
set(fieldIndex, SszUInt64.of(latestWithdrawalValidatorIndex));
getSchema().getFieldIndex(BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX);
set(fieldIndex, SszUInt64.of(nextWithdrawalValidatorIndex));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,31 @@ public void processBlock(
}

processBlockHeader(state, block);
if (miscHelpersBellatrix.isExecutionEnabled(genericState, block)) {
processExecutionPayload(
state, executionPayloadHeader, blockBody.getOptionalExecutionPayload(), payloadExecutor);
}
maybeProcessExecutionPayload(genericState, block, executionPayloadHeader, payloadExecutor);
processRandaoNoValidation(state, block.getBody());
processEth1Data(state, block.getBody());
processOperationsNoValidation(state, block.getBody(), indexedAttestationCache);
processSyncAggregate(
state, blockBody.getOptionalSyncAggregate().orElseThrow(), signatureVerifier);
}

protected void maybeProcessExecutionPayload(
final MutableBeaconState state,
final BeaconBlock block,
final ExecutionPayloadHeader executionPayloadHeader,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor)
throws BlockProcessingException {
if (isExecutionEnabled(state, block)) {
final BeaconBlockBody blockBody = block.getBody();
processExecutionPayload(
state, executionPayloadHeader, blockBody.getOptionalExecutionPayload(), payloadExecutor);
}
}

public boolean isExecutionEnabled(final MutableBeaconState state, final BeaconBlock block) {
return miscHelpersBellatrix.isExecutionEnabled(state, block);
}

@Override
public void processExecutionPayload(
final MutableBeaconState genericState,
Expand Down
Loading

0 comments on commit 1bcadd0

Please sign in to comment.