Search in sources :

Example 1 with VoteTracker

use of tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker in project teku by ConsenSys.

the class ProtoArrayTest method shouldConsiderWeightFromVotesForOptimisticBlocks.

@Test
void shouldConsiderWeightFromVotesForOptimisticBlocks() {
    addValidBlock(1, block1a, GENESIS_CHECKPOINT.getRoot());
    addValidBlock(1, block1b, GENESIS_CHECKPOINT.getRoot());
    addOptimisticBlock(2, block2a, block1a);
    addOptimisticBlock(2, block2b, block1b);
    voteUpdater.putVote(UInt64.ZERO, new VoteTracker(Bytes32.ZERO, block1b, UInt64.ZERO));
    voteUpdater.putVote(UInt64.ONE, new VoteTracker(Bytes32.ZERO, block1b, UInt64.ZERO));
    voteUpdater.putVote(UInt64.valueOf(2), new VoteTracker(Bytes32.ZERO, block1b, UInt64.ZERO));
    protoArray.applyScoreChanges(computeDeltas(), UInt64.ZERO, UInt64.ZERO);
    assertHead(block2b);
    // Validators 0 and 1 switch forks to chain a
    voteUpdater.putVote(UInt64.ZERO, new VoteTracker(block1b, block2a, UInt64.ONE));
    voteUpdater.putVote(UInt64.ONE, new VoteTracker(block1b, block2a, UInt64.ONE));
    protoArray.applyScoreChanges(computeDeltas(), UInt64.ZERO, UInt64.ZERO);
    // And our head should switch
    assertHead(block2a);
}
Also used : VoteTracker(tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker) Test(org.junit.jupiter.api.Test)

Example 2 with VoteTracker

use of tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker in project teku by ConsenSys.

the class ProtoArrayScoreCalculator method computeDelta.

private static void computeDelta(final VoteUpdater store, final Function<Bytes32, Optional<Integer>> getIndexByRoot, final List<UInt64> oldBalances, final List<UInt64> newBalances, final List<Long> deltas, final UInt64 validatorIndex) {
    VoteTracker vote = store.getVote(validatorIndex);
    // or both their votes are for the zero hash (alias to the genesis block).
    if (vote.getCurrentRoot().equals(Bytes32.ZERO) && vote.getNextRoot().equals(Bytes32.ZERO)) {
        return;
    }
    int validatorIndexInt = toIntExact(validatorIndex.longValue());
    // If the validator was not included in the oldBalances (i.e. it did not exist yet)
    // then say its balance was zero.
    UInt64 oldBalance = oldBalances.size() > validatorIndexInt ? oldBalances.get(validatorIndexInt) : UInt64.ZERO;
    // If the validator vote is not known in the newBalances, then use a balance of zero.
    // It is possible that there is a vote for an unknown validator if we change our
    // justified state to a new state with a higher epoch that is on a different fork
    // because that may have on-boarded less validators than the prior fork.
    UInt64 newBalance = newBalances.size() > validatorIndexInt ? newBalances.get(validatorIndexInt) : UInt64.ZERO;
    if (!vote.getCurrentRoot().equals(vote.getNextRoot()) || !oldBalance.equals(newBalance)) {
        subtractBalance(getIndexByRoot, deltas, vote.getCurrentRoot(), oldBalance);
        addBalance(getIndexByRoot, deltas, vote.getNextRoot(), newBalance);
        VoteTracker newVote = new VoteTracker(vote.getNextRoot(), vote.getNextRoot(), vote.getNextEpoch());
        store.putVote(validatorIndex, newVote);
    }
}
Also used : VoteTracker(tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 3 with VoteTracker

use of tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker in project teku by ConsenSys.

the class ProtoArrayScoreCalculatorTest method computeDeltas_differentVotes.

@Test
void computeDeltas_differentVotes() {
    final UInt64 BALANCE = UInt64.valueOf(42);
    int validatorCount = 16;
    for (int i = 0; i < validatorCount; i++) {
        indices.put(getHash(i), i);
        VoteTracker vote = store.getVote(UInt64.valueOf(i));
        VoteTracker newVote = new VoteTracker(vote.getCurrentRoot(), getHash(i), vote.getNextEpoch());
        store.putVote(UInt64.valueOf(i), newVote);
        oldBalances.add(BALANCE);
        newBalances.add(BALANCE);
    }
    List<Long> deltas = computeDeltas(store, indices.size(), this::getIndex, oldBalances, newBalances, oldProposerBoostRoot, newProposerBoostRoot, oldProposerBoostAmount, newProposerBoostAmount);
    assertThat(deltas).hasSize(validatorCount);
    // Each root should have the same delta
    assertThat(deltas).containsOnly(BALANCE.longValue());
    votesShouldBeUpdated(store);
}
Also used : VoteTracker(tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Test(org.junit.jupiter.api.Test)

Example 4 with VoteTracker

use of tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker in project teku by ConsenSys.

the class ProtoArrayScoreCalculatorTest method votesShouldBeUpdated.

private void votesShouldBeUpdated(VoteUpdater store) {
    UInt64.rangeClosed(ZERO, store.getHighestVotedValidatorIndex()).forEach(i -> {
        VoteTracker vote = store.getVote(i);
        assertThat(vote.getCurrentRoot()).isEqualTo(vote.getNextRoot());
    });
}
Also used : VoteTracker(tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker)

Example 5 with VoteTracker

use of tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker in project teku by ConsenSys.

the class StoreVoteUpdaterTest method shouldSendUpdatesToStorageOnCommit.

@Test
void shouldSendUpdatesToStorageOnCommit() {
    final VoteUpdater voteUpdater = store.startVoteUpdate(voteUpdateChannel);
    final VoteTracker updatedVote = dataStructureUtil.randomVoteTracker();
    voteUpdater.putVote(UInt64.ZERO, updatedVote);
    verifyNoInteractions(voteUpdateChannel);
    voteUpdater.commit();
    verify(voteUpdateChannel).onVotesUpdated(Map.of(UInt64.ZERO, updatedVote));
}
Also used : VoteTracker(tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker) VoteUpdater(tech.pegasys.teku.spec.datastructures.forkchoice.VoteUpdater) Test(org.junit.jupiter.api.Test)

Aggregations

VoteTracker (tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker)20 Test (org.junit.jupiter.api.Test)14 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)11 HashMap (java.util.HashMap)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 VoteUpdater (tech.pegasys.teku.spec.datastructures.forkchoice.VoteUpdater)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 StoredBlockMetadata (tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata)1 BlockAndCheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs)1 CheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs)1 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)1 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)1 ExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload)1 SlotAndExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload)1 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)1 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1 DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)1