Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merge] Move execution payload validation out of state transition #4648

Merged
merged 19 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a0b7f58
Support optimistic sync in ProtoArray.
ajsutton Nov 15, 2021
1b8c46e
Merge branch 'master' of github.com:ConsenSys/teku into optimistic-sync
ajsutton Nov 15, 2021
f8c4ade
Fix error
ajsutton Nov 15, 2021
6ce0ade
Merge branch 'master' of github.com:ConsenSys/teku into optimistic-sync
ajsutton Nov 15, 2021
66b29ff
Support returning Optional from findHead
ajsutton Nov 15, 2021
32866b5
Merge branch 'master' of github.com:ConsenSys/teku into validate-payload
ajsutton Nov 16, 2021
a94cb3e
Move execution payload validation from state transition to fork choice
ajsutton Nov 16, 2021
ff3cf56
Merge branch 'master' of github.com:ConsenSys/teku into validate-payload
ajsutton Nov 16, 2021
0c85f9d
Slightly messy but I think this actually handles execution payloads c…
ajsutton Nov 16, 2021
1bb0006
Ensure fork choice update is done on fork choice thread.
ajsutton Nov 16, 2021
6c8bc90
Still messy but gets reference tests passing and is starting to move …
ajsutton Nov 16, 2021
b0bed22
Merge branch 'master' of github.com:ConsenSys/teku into validate-payload
ajsutton Nov 16, 2021
2040f45
Tidy up a bit
ajsutton Nov 16, 2021
30b9bf3
Split out a separate class.
ajsutton Nov 16, 2021
8d472b8
Add tests
ajsutton Nov 17, 2021
9c4751b
Merge branch 'master' of github.com:ConsenSys/teku into validate-payload
ajsutton Nov 17, 2021
eb08e72
Hide ugliness a bit and make errorprone happy.
ajsutton Nov 17, 2021
9aeb5d6
Use Optional.
ajsutton Nov 17, 2021
ba651d1
Merge branch 'master' of github.com:ConsenSys/teku into validate-payload
ajsutton Nov 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import tech.pegasys.teku.spec.config.SpecConfigLoader;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionengine.StubExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;

public class TransitionTestExecutor implements TestExecutor {

Expand Down Expand Up @@ -79,7 +79,7 @@ private void processAltairUpgrade(final TestDefinition testDefinition, final Met
metadata.blsSetting == 2 ? BLSSignatureVerifier.NO_OP : BLSSignatureVerifier.SIMPLE;
result =
spec.processBlock(
result, block, signatureVerifier, new StubExecutionEngineChannel(spec));
result, block, signatureVerifier, OptimisticExecutionPayloadExecutor.NOOP);
} catch (final StateTransitionException e) {
Assertions.fail(
"Failed to process block " + i + " at slot " + block.getSlot() + ": " + e.getMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing;
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;

public class DefaultOperationProcessor implements OperationProcessor {
private final Spec spec;
Expand Down Expand Up @@ -104,9 +104,9 @@ public void processSyncCommittee(final MutableBeaconState state, final SyncAggre
public void processExecutionPayload(
final MutableBeaconState state,
final ExecutionPayload executionPayload,
final ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws BlockProcessingException {
spec.getBlockProcessor(state.getSlot())
.processExecutionPayload(state, executionPayload, executionEngine);
.processExecutionPayload(state, executionPayload, payloadExecutor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing;
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;

public interface OperationProcessor {

Expand All @@ -50,6 +50,6 @@ void processSyncCommittee(MutableBeaconState state, SyncAggregate aggregate)
void processExecutionPayload(
MutableBeaconState state,
ExecutionPayload executionPayload,
ExecutionEngineChannel executionEngine)
OptimisticExecutionPayloadExecutor payloadExecutor)
throws BlockProcessingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.reference.TestDataUtils.loadSsz;
import static tech.pegasys.teku.reference.TestDataUtils.loadStateFromSsz;
import static tech.pegasys.teku.reference.TestDataUtils.loadYaml;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import java.util.Optional;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.reference.TestExecutor;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodySchemaAltair;
Expand All @@ -38,9 +34,6 @@
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutePayloadResult;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.executionengine.ExecutionPayloadStatus;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.ssz.SszData;

Expand Down Expand Up @@ -200,7 +193,6 @@ private void processOperation(
processor.processSyncCommittee(state, syncAggregate);
break;
case EXECUTION_PAYLOAD:
final ExecutionEngineChannel executionEngine = mock(ExecutionEngineChannel.class);
final ExecutionMeta executionMeta =
loadYaml(testDefinition, "execution.yaml", ExecutionMeta.class);
final ExecutionPayload payload =
Expand All @@ -213,16 +205,8 @@ private void processOperation(
.toVersionMerge()
.orElseThrow()
.getExecutionPayloadSchema());
when(executionEngine.executePayload(payload))
.thenReturn(
SafeFuture.completedFuture(
new ExecutePayloadResult(
executionMeta.executionValid
? ExecutionPayloadStatus.VALID
: ExecutionPayloadStatus.INVALID,
Optional.empty(),
Optional.empty())));
processor.processExecutionPayload(state, payload, executionEngine);
processor.processExecutionPayload(
state, payload, payloadToExecute -> executionMeta.executionValid);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionengine.StubExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;

public class SanityBlocksTestExecutor implements TestExecutor {

Expand Down Expand Up @@ -95,7 +95,7 @@ private BeaconState applyBlocks(
metaData.getBlsSetting() == IGNORED
? BLSSignatureVerifier.NO_OP
: BLSSignatureVerifier.SIMPLE,
new StubExecutionEngineChannel(spec));
OptimisticExecutionPayloadExecutor.NOOP);
}
return result;
} catch (StateTransitionException e) {
Expand Down
12 changes: 6 additions & 6 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateInvariants;
import tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult;
import tech.pegasys.teku.spec.datastructures.util.ForkAndSpecMilestone;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.genesis.GenesisGenerator;
import tech.pegasys.teku.spec.logic.StateTransition;
import tech.pegasys.teku.spec.logic.common.block.BlockProcessor;
Expand All @@ -70,6 +69,7 @@
import tech.pegasys.teku.spec.logic.common.util.AsyncBLSSignatureVerifier;
import tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil;
import tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;
import tech.pegasys.teku.ssz.collections.SszBitlist;
import tech.pegasys.teku.ssz.type.Bytes4;
Expand Down Expand Up @@ -463,10 +463,10 @@ public BlockImportResult onBlock(
final SignedBeaconBlock signedBlock,
final BeaconState blockSlotState,
final IndexedAttestationCache indexedAttestationCache,
final ExecutionEngineChannel executionEngine) {
final OptimisticExecutionPayloadExecutor payloadExecutor) {
return atBlock(signedBlock)
.getForkChoiceUtil()
.onBlock(store, signedBlock, blockSlotState, indexedAttestationCache, executionEngine);
.onBlock(store, signedBlock, blockSlotState, indexedAttestationCache, payloadExecutor);
}

public boolean blockDescendsFromLatestFinalizedBlock(
Expand Down Expand Up @@ -507,7 +507,7 @@ public BeaconState processBlock(
final BeaconState preState,
final SignedBeaconBlock block,
final BLSSignatureVerifier signatureVerifier,
final ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws StateTransitionException {
try {
final BeaconState blockSlotState = stateTransition.processSlots(preState, block.getSlot());
Expand All @@ -517,7 +517,7 @@ public BeaconState processBlock(
blockSlotState,
IndexedAttestationCache.NOOP,
signatureVerifier,
executionEngine);
payloadExecutor);
} catch (SlotProcessingException | EpochProcessingException e) {
throw new StateTransitionException(e);
}
Expand All @@ -533,7 +533,7 @@ public BeaconState replayValidatedBlock(final BeaconState preState, final Signed
block.getMessage(),
IndexedAttestationCache.NOOP,
BLSSignatureVerifier.NO_OP,
ExecutionEngineChannel.NOOP);
OptimisticExecutionPayloadExecutor.NOOP);
} catch (SlotProcessingException | EpochProcessingException | BlockProcessingException e) {
throw new StateTransitionException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.teku.spec.datastructures.blocks.Eth1Data;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.merge.BeaconBlockBodyMerge;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
import tech.pegasys.teku.spec.datastructures.operations.Deposit;
Expand All @@ -44,6 +45,10 @@ public interface BeaconBlockBody extends SszContainer {

SszList<SignedVoluntaryExit> getVoluntaryExits();

default Optional<ExecutionPayload> getOptionalExecutionPayload() {
return Optional.empty();
}

@Override
BeaconBlockBodySchema<? extends BeaconBlockBody> getSchema();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ static BeaconBlockBodyMerge required(final BeaconBlockBody body) {

ExecutionPayload getExecutionPayload();

@Override
default Optional<ExecutionPayload> getOptionalExecutionPayload() {
return Optional.of(getExecutionPayload());
}

@Override
BeaconBlockBodySchemaMerge<?> getSchema();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
import tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators;
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers;
Expand All @@ -75,6 +74,7 @@
import tech.pegasys.teku.spec.logic.common.util.AttestationUtil;
import tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil;
import tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.ssz.SszList;
import tech.pegasys.teku.ssz.type.Bytes4;

Expand Down Expand Up @@ -127,7 +127,7 @@ public BeaconState processAndValidateBlock(
final SignedBeaconBlock signedBlock,
final BeaconState blockSlotState,
final IndexedAttestationCache indexedAttestationCache,
final ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws StateTransitionException {
final BatchSignatureVerifier signatureVerifier = new BatchSignatureVerifier();
final BeaconState result =
Expand All @@ -136,7 +136,7 @@ public BeaconState processAndValidateBlock(
blockSlotState,
indexedAttestationCache,
signatureVerifier,
executionEngine);
payloadExecutor);
if (!signatureVerifier.batchVerify()) {
throw new StateTransitionException(
"Batch signature verification failed for block "
Expand All @@ -151,7 +151,7 @@ public BeaconState processAndValidateBlock(
final BeaconState blockSlotState,
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws StateTransitionException {
try {
// Process_block
Expand All @@ -161,7 +161,7 @@ public BeaconState processAndValidateBlock(
signedBlock.getMessage(),
indexedAttestationCache,
signatureVerifier,
executionEngine);
payloadExecutor);

BlockValidationResult blockValidationResult =
validateBlock(
Expand Down Expand Up @@ -294,20 +294,20 @@ public BeaconState processUnsignedBlock(
final BeaconBlock block,
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws BlockProcessingException {
return preState.updated(
state ->
processBlock(
state, block, indexedAttestationCache, signatureVerifier, executionEngine));
state, block, indexedAttestationCache, signatureVerifier, payloadExecutor));
}

protected void processBlock(
final MutableBeaconState state,
final BeaconBlock block,
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws BlockProcessingException {
processBlockHeader(state, block);
processRandaoNoValidation(state, block.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.operations.validation.OperationInvalidReason;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.ssz.SszList;

public interface BlockProcessor {
Expand All @@ -46,7 +46,7 @@ BeaconState processAndValidateBlock(
SignedBeaconBlock signedBlock,
BeaconState blockSlotState,
IndexedAttestationCache indexedAttestationCache,
ExecutionEngineChannel executionEngine)
final OptimisticExecutionPayloadExecutor payloadExecutor)
throws StateTransitionException;

/**
Expand All @@ -57,7 +57,7 @@ BeaconState processAndValidateBlock(
* already be advanced to the block's slot
* @param indexedAttestationCache A cache of indexed attestations
* @param signatureVerifier The signature verifier to use
* @param executionEngine The execution engine to verify payloads via
* @param payloadExecutor the optimistic payload executor to begin execution with
* @return The post state after processing the block on top of {@code blockSlotState}
* @throws StateTransitionException If the block is invalid or cannot be processed
*/
Expand All @@ -66,15 +66,15 @@ BeaconState processAndValidateBlock(
BeaconState blockSlotState,
IndexedAttestationCache indexedAttestationCache,
BLSSignatureVerifier signatureVerifier,
ExecutionEngineChannel executionEngine)
OptimisticExecutionPayloadExecutor payloadExecutor)
throws StateTransitionException;

BeaconState processUnsignedBlock(
BeaconState preState,
BeaconBlock block,
IndexedAttestationCache indexedAttestationCache,
BLSSignatureVerifier signatureVerifier,
ExecutionEngineChannel executionEngine)
OptimisticExecutionPayloadExecutor payloadExecutor)
throws BlockProcessingException;

void processBlockHeader(MutableBeaconState state, BeaconBlockSummary blockHeader)
Expand Down Expand Up @@ -121,6 +121,6 @@ void processSyncAggregate(
void processExecutionPayload(
MutableBeaconState state,
ExecutionPayload executionPayload,
ExecutionEngineChannel executionEngine)
OptimisticExecutionPayloadExecutor payloadExecutor)
throws BlockProcessingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.block.BlockProcessor;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.merge.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;

public class BlockProposalUtil {
Expand Down Expand Up @@ -80,7 +80,7 @@ public BeaconBlockAndState createNewUnsignedBlock(
newBlock,
IndexedAttestationCache.NOOP,
BLSSignatureVerifier.NO_OP,
ExecutionEngineChannel.NOOP);
OptimisticExecutionPayloadExecutor.NOOP);

Bytes32 stateRoot = newState.hashTreeRoot();
BeaconBlock newCompleteBlock = newBlock.withStateRoot(stateRoot);
Expand Down
Loading