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);
}
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);
}
}
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);
}
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());
});
}
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));
}
Aggregations