diff --git a/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/TestDefinition.java b/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/TestDefinition.java index 31748bf7288..72a8166d77e 100644 --- a/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/TestDefinition.java +++ b/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/TestDefinition.java @@ -60,37 +60,21 @@ public Spec getSpec() { } private void createSpec() { - final Eth2Network network; - final SpecMilestone highestSupportedMilestone; - switch (configName) { - case TestSpecConfig.MAINNET: - network = Eth2Network.MAINNET; - break; - case TestSpecConfig.MINIMAL: - network = Eth2Network.MINIMAL; - break; - default: - throw new IllegalArgumentException("Unknown configName: " + configName); - } - switch (fork) { - case TestFork.PHASE0: - highestSupportedMilestone = SpecMilestone.PHASE0; - break; - case TestFork.ALTAIR: - highestSupportedMilestone = SpecMilestone.ALTAIR; - break; - case TestFork.BELLATRIX: - highestSupportedMilestone = SpecMilestone.BELLATRIX; - break; - case TestFork.CAPELLA: - highestSupportedMilestone = SpecMilestone.CAPELLA; - break; - case TestFork.DENEB: - highestSupportedMilestone = SpecMilestone.DENEB; - break; - default: - throw new IllegalArgumentException("Unknown fork: " + fork); - } + final Eth2Network network = + switch (configName) { + case TestSpecConfig.MAINNET -> Eth2Network.MAINNET; + case TestSpecConfig.MINIMAL -> Eth2Network.MINIMAL; + default -> throw new IllegalArgumentException("Unknown configName: " + configName); + }; + final SpecMilestone highestSupportedMilestone = + switch (fork) { + case TestFork.PHASE0 -> SpecMilestone.PHASE0; + case TestFork.ALTAIR -> SpecMilestone.ALTAIR; + case TestFork.BELLATRIX -> SpecMilestone.BELLATRIX; + case TestFork.CAPELLA -> SpecMilestone.CAPELLA; + case TestFork.DENEB -> SpecMilestone.DENEB; + default -> throw new IllegalArgumentException("Unknown fork: " + fork); + }; spec = TestSpecFactory.create(highestSupportedMilestone, network); } diff --git a/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/AttesterSlashingGenerator.java b/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/AttesterSlashingGenerator.java index cf5fbf2af32..cd00de4af58 100644 --- a/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/AttesterSlashingGenerator.java +++ b/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/AttesterSlashingGenerator.java @@ -50,7 +50,10 @@ public AttesterSlashingGenerator(final Spec spec, final List validat public AttesterSlashing createAttesterSlashingForAttestation( final Attestation goodAttestation, final SignedBlockAndState blockAndState) { if (!goodAttestation.getData().getSlot().equals(blockAndState.getSlot())) { - throw new RuntimeException("Good attestation slot and input block slot should match"); + throw new RuntimeException( + String.format( + "Good attestation slot %s and input block slot %s should match", + goodAttestation.getData().getSlot(), blockAndState.getSlot())); } AttestationUtil attestationUtil = spec.atSlot(blockAndState.getSlot()).getAttestationUtil(); IndexedAttestation indexedGoodAttestation = diff --git a/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/ChainBuilder.java b/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/ChainBuilder.java index 8587c17e7f7..bea535f99e7 100644 --- a/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/ChainBuilder.java +++ b/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/generator/ChainBuilder.java @@ -353,7 +353,7 @@ public List generateBlocksUpToSlot(final UInt64 slot) { final List generated = new ArrayList<>(); SignedBlockAndState latestBlock = getLatestBlockAndState(); - while (latestBlock.getState().getSlot().compareTo(slot) < 0) { + while (latestBlock.getState().getSlot().isLessThan(slot)) { latestBlock = generateNextBlock(); generated.add(latestBlock); } diff --git a/ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/forkchoice/ForkChoiceTest.java b/ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/forkchoice/ForkChoiceTest.java index 2a659d77d45..f7657bb5814 100644 --- a/ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/forkchoice/ForkChoiceTest.java +++ b/ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/forkchoice/ForkChoiceTest.java @@ -155,8 +155,8 @@ private void setupWithSpec(final Spec spec) { new TickProcessor(spec, recentChainData), transitionBlockValidator, DEFAULT_FORK_CHOICE_UPDATE_HEAD_ON_BLOCK_IMPORT_ENABLED, - // will use the static const parameter in an upcoming PR which will update the ref tests - // and set the const to true + // will use DEFAULT_FORK_CHOICE_PROPOSER_BOOST_UNIQUENESS_ENABLED in an upcoming PR + // which will update to the new ref tests and set the const to true true, metricsSystem); @@ -366,50 +366,49 @@ void onBlock_shouldReorgWhenProposerWeightingMakesForkBestChain( @Test void onBlock_shouldUpdateVotesBasedOnAttestationsInBlocks() { final ChainBuilder forkChain = chainBuilder.fork(); - final SignedBlockAndState forkBlock1 = + final SignedBlockAndState forkBlock = forkChain.generateBlockAtSlot( ONE, BlockOptions.create() .setEth1Data(new Eth1Data(Bytes32.ZERO, UInt64.valueOf(6), Bytes32.ZERO))); - final SignedBlockAndState betterBlock1 = chainBuilder.generateBlockAtSlot(1); + // eventually better chain with an empty block + final SignedBlockAndState betterBlock = chainBuilder.generateNextBlock(1); - importBlock(forkBlock1); - // Should automatically follow the fork as its the first child block - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); + importBlock(forkBlock); + // Should automatically follow the fork as it is the only one imported + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock.getRoot()); // Add an attestation for the fork so that it initially has higher weight // Otherwise ties are split based on the hash which is too hard to control in the test final BlockOptions forkBlockOptions = BlockOptions.create(); forkChain - .streamValidAttestationsWithTargetBlock(forkBlock1) + .streamValidAttestationsWithTargetBlock(forkBlock) .limit(1) .forEach(forkBlockOptions::addAttestation); - final SignedBlockAndState forkBlock2 = - forkChain.generateBlockAtSlot(forkBlock1.getSlot().plus(1), forkBlockOptions); - importBlock(forkBlock2); + final SignedBlockAndState forkBlock1 = forkChain.generateNextBlock(forkBlockOptions); + importBlock(forkBlock1); // The fork is still the only option so gets selected - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // Now import what will become the canonical chain - importBlock(betterBlock1); + importBlock(betterBlock); // Process head to ensure we clear any additional proposer weighting for this first block. // Should still pick forkBlock as it's the best option even though we have a competing chain processHead(ONE); - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // Import a block with two attestations which makes this chain better than the fork final BlockOptions options = BlockOptions.create(); chainBuilder - .streamValidAttestationsWithTargetBlock(betterBlock1) + .streamValidAttestationsWithTargetBlock(betterBlock) .limit(2) .forEach(options::addAttestation); - final SignedBlockAndState blockWithAttestations = - chainBuilder.generateBlockAtSlot(UInt64.valueOf(2), options); + final SignedBlockAndState blockWithAttestations = chainBuilder.generateNextBlock(options); importBlock(blockWithAttestations); // Haven't run fork choice so won't have re-orged yet - fork still has more applied votes - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // When attestations are applied we should switch away from the fork to our better chain processHead(blockWithAttestations.getSlot()); @@ -418,70 +417,67 @@ void onBlock_shouldUpdateVotesBasedOnAttestationsInBlocks() { @Test void onBlock_shouldUpdateVotesBasedOnAttesterSlashingEquivocationsInBlocks() { + final SignedBlockAndState commonBlock = chainBuilder.generateNextBlock(); + importBlock(commonBlock); final ChainBuilder forkChain = chainBuilder.fork(); - final SignedBlockAndState forkBlock1 = - forkChain.generateBlockAtSlot( - ONE, + final SignedBlockAndState forkBlock = + forkChain.generateNextBlock( BlockOptions.create() .setEth1Data(new Eth1Data(Bytes32.ZERO, UInt64.valueOf(6), Bytes32.ZERO))); - final SignedBlockAndState betterBlock1 = chainBuilder.generateBlockAtSlot(1); + // eventually better chain with an empty block + final SignedBlockAndState betterBlock = chainBuilder.generateNextBlock(1); - importBlock(forkBlock1); - // Should automatically follow the fork as its the first child block - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); + importBlock(forkBlock); + + // Should automatically follow the fork as it is the only one imported + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock.getRoot()); // Add an attestation for the fork so that it initially has higher weight // Otherwise ties are split based on the hash which is too hard to control in the test final BlockOptions forkBlockOptions = BlockOptions.create(); - List forkAttestations = - forkChain - .streamValidAttestationsWithTargetBlock(forkBlock1) - .limit(2) - .collect(Collectors.toList()); + final List forkAttestations = + forkChain.streamValidAttestationsWithTargetBlock(forkBlock).limit(2).toList(); forkAttestations.forEach(forkBlockOptions::addAttestation); - final SignedBlockAndState forkBlock2 = - forkChain.generateBlockAtSlot(forkBlock1.getSlot().plus(1), forkBlockOptions); - importBlock(forkBlock2); + final SignedBlockAndState forkBlock1 = forkChain.generateNextBlock(forkBlockOptions); + importBlock(forkBlock1); // The fork is still the only option so gets selected - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // Now import what will become the canonical chain - importBlock(betterBlock1); + importBlock(betterBlock); // Process head to ensure we clear any additional proposer weighting for this first block. // Should still pick forkBlock as it's the best option even though we have a competing chain processHead(ONE); - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // Import a block with one attestation on what will be better chain final BlockOptions options = BlockOptions.create(); chainBuilder - .streamValidAttestationsWithTargetBlock(betterBlock1) + .streamValidAttestationsWithTargetBlock(betterBlock) .limit(1) .forEach(options::addAttestation); - final SignedBlockAndState blockWithAttestations = - chainBuilder.generateBlockAtSlot(UInt64.valueOf(2), options); + final SignedBlockAndState blockWithAttestations = chainBuilder.generateNextBlock(options); importBlock(blockWithAttestations); // Haven't run fork choice so won't have re-orged yet - fork still has more applied votes - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // Verify that fork is still better processHead(blockWithAttestations.getSlot()); - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // Add 2 AttesterSlashing on betterBlock chain, so it will become finally better final BlockOptions options2 = BlockOptions.create(); forkAttestations.forEach( attestation -> options2.addAttesterSlashing( - chainBuilder.createAttesterSlashingForAttestation(attestation, forkBlock1))); - final SignedBlockAndState blockWithAttesterSlashings = - chainBuilder.generateBlockAtSlot(UInt64.valueOf(3), options2); + chainBuilder.createAttesterSlashingForAttestation(attestation, forkBlock))); + final SignedBlockAndState blockWithAttesterSlashings = chainBuilder.generateNextBlock(options2); importBlock(blockWithAttesterSlashings); // Haven't run fork choice so won't have re-orged yet - fork still has more applied votes - assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock2.getRoot()); + assertThat(recentChainData.getBestBlockRoot()).contains(forkBlock1.getRoot()); // When attester slashings are applied we should switch away from the fork to our better chain processHead(blockWithAttesterSlashings.getSlot());