Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Sep 20, 2023
1 parent 89e544e commit 544701c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public AttesterSlashingGenerator(final Spec spec, final List<BLSKeyPair> 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public List<SignedBlockAndState> generateBlocksUpToSlot(final UInt64 slot) {
final List<SignedBlockAndState> generated = new ArrayList<>();

SignedBlockAndState latestBlock = getLatestBlockAndState();
while (latestBlock.getState().getSlot().compareTo(slot) < 0) {
while (latestBlock.getState().getSlot().isLessThan(slot)) {
latestBlock = generateNextBlock();
generated.add(latestBlock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand All @@ -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<Attestation> forkAttestations =
forkChain
.streamValidAttestationsWithTargetBlock(forkBlock1)
.limit(2)
.collect(Collectors.toList());
final List<Attestation> 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());
Expand Down

0 comments on commit 544701c

Please sign in to comment.