@Test void computeDeltas_changingBalances() { final UInt64 OLD_BALANCE = UInt64.valueOf(42); final UInt64 NEW_BALANCE = OLD_BALANCE.times(2); int validatorCount = 16; for (int i = 0; i < validatorCount; i++) { indices.put(getHash(i), i); VoteTracker vote = store.getVote(UInt64.valueOf(i)); vote.setCurrentRoot(getHash(0)); vote.setNextRoot(getHash(1)); oldBalances.add(OLD_BALANCE); newBalances.add(NEW_BALANCE); } List<Long> deltas = computeDeltas(store, indices, oldBalances, newBalances); assertThat(deltas).hasSize(validatorCount); for (int i = 0; i < deltas.size(); i++) { long delta = deltas.get(i); if (i == 0) { assertThat(delta).isEqualTo(-OLD_BALANCE.longValue() * validatorCount); } else if (i == 1) { assertThat(delta).isEqualTo(NEW_BALANCE.longValue() * validatorCount); } else { assertThat(delta).isEqualTo(0L); } } votesShouldBeUpdated(store); }
static List<Long> computeDeltas( MutableStore store, Map<Bytes32, Integer> indices, List<UInt64> oldBalances, List<UInt64> newBalances) { List<Long> deltas = new ArrayList<>(Collections.nCopies(indices.size(), 0L)); for (UInt64 validatorIndex : store.getVotedValidatorIndices()) { VoteTracker vote = store.getVote(validatorIndex); if (vote.getCurrentRoot().equals(Bytes32.ZERO) && vote.getNextRoot().equals(Bytes32.ZERO)) { LOG.warn("ProtoArrayForkChoiceStrategy: Unexpected zero hashes in voted validator votes"); continue; } int validatorIndexInt = toIntExact(validatorIndex.longValue()); UInt64 oldBalance = oldBalances.size() > validatorIndexInt ? oldBalances.get(validatorIndexInt) : UInt64.ZERO; UInt64 newBalance = newBalances.size() > validatorIndexInt ? newBalances.get(validatorIndexInt) : UInt64.ZERO; if (!vote.getCurrentRoot().equals(vote.getNextRoot()) || !oldBalance.equals(newBalance)) { Integer currentDeltaIndex = indices.get(vote.getCurrentRoot()); if (currentDeltaIndex != null) { checkState( currentDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = subtractExact(deltas.get(currentDeltaIndex), oldBalance.longValue()); deltas.set(currentDeltaIndex, delta); } Integer nextDeltaIndex = indices.get(vote.getNextRoot()); if (nextDeltaIndex != null) { checkState( nextDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = addExact(deltas.get(nextDeltaIndex), newBalance.longValue()); deltas.set(nextDeltaIndex, delta); } vote.setCurrentRoot(vote.getNextRoot()); } } return deltas; }
ProtoArrayScoreCalculator { static List<Long> computeDeltas( MutableStore store, Map<Bytes32, Integer> indices, List<UInt64> oldBalances, List<UInt64> newBalances) { List<Long> deltas = new ArrayList<>(Collections.nCopies(indices.size(), 0L)); for (UInt64 validatorIndex : store.getVotedValidatorIndices()) { VoteTracker vote = store.getVote(validatorIndex); if (vote.getCurrentRoot().equals(Bytes32.ZERO) && vote.getNextRoot().equals(Bytes32.ZERO)) { LOG.warn("ProtoArrayForkChoiceStrategy: Unexpected zero hashes in voted validator votes"); continue; } int validatorIndexInt = toIntExact(validatorIndex.longValue()); UInt64 oldBalance = oldBalances.size() > validatorIndexInt ? oldBalances.get(validatorIndexInt) : UInt64.ZERO; UInt64 newBalance = newBalances.size() > validatorIndexInt ? newBalances.get(validatorIndexInt) : UInt64.ZERO; if (!vote.getCurrentRoot().equals(vote.getNextRoot()) || !oldBalance.equals(newBalance)) { Integer currentDeltaIndex = indices.get(vote.getCurrentRoot()); if (currentDeltaIndex != null) { checkState( currentDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = subtractExact(deltas.get(currentDeltaIndex), oldBalance.longValue()); deltas.set(currentDeltaIndex, delta); } Integer nextDeltaIndex = indices.get(vote.getNextRoot()); if (nextDeltaIndex != null) { checkState( nextDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = addExact(deltas.get(nextDeltaIndex), newBalance.longValue()); deltas.set(nextDeltaIndex, delta); } vote.setCurrentRoot(vote.getNextRoot()); } } return deltas; } }
ProtoArrayScoreCalculator { static List<Long> computeDeltas( MutableStore store, Map<Bytes32, Integer> indices, List<UInt64> oldBalances, List<UInt64> newBalances) { List<Long> deltas = new ArrayList<>(Collections.nCopies(indices.size(), 0L)); for (UInt64 validatorIndex : store.getVotedValidatorIndices()) { VoteTracker vote = store.getVote(validatorIndex); if (vote.getCurrentRoot().equals(Bytes32.ZERO) && vote.getNextRoot().equals(Bytes32.ZERO)) { LOG.warn("ProtoArrayForkChoiceStrategy: Unexpected zero hashes in voted validator votes"); continue; } int validatorIndexInt = toIntExact(validatorIndex.longValue()); UInt64 oldBalance = oldBalances.size() > validatorIndexInt ? oldBalances.get(validatorIndexInt) : UInt64.ZERO; UInt64 newBalance = newBalances.size() > validatorIndexInt ? newBalances.get(validatorIndexInt) : UInt64.ZERO; if (!vote.getCurrentRoot().equals(vote.getNextRoot()) || !oldBalance.equals(newBalance)) { Integer currentDeltaIndex = indices.get(vote.getCurrentRoot()); if (currentDeltaIndex != null) { checkState( currentDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = subtractExact(deltas.get(currentDeltaIndex), oldBalance.longValue()); deltas.set(currentDeltaIndex, delta); } Integer nextDeltaIndex = indices.get(vote.getNextRoot()); if (nextDeltaIndex != null) { checkState( nextDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = addExact(deltas.get(nextDeltaIndex), newBalance.longValue()); deltas.set(nextDeltaIndex, delta); } vote.setCurrentRoot(vote.getNextRoot()); } } return deltas; }  }
ProtoArrayScoreCalculator { static List<Long> computeDeltas( MutableStore store, Map<Bytes32, Integer> indices, List<UInt64> oldBalances, List<UInt64> newBalances) { List<Long> deltas = new ArrayList<>(Collections.nCopies(indices.size(), 0L)); for (UInt64 validatorIndex : store.getVotedValidatorIndices()) { VoteTracker vote = store.getVote(validatorIndex); if (vote.getCurrentRoot().equals(Bytes32.ZERO) && vote.getNextRoot().equals(Bytes32.ZERO)) { LOG.warn("ProtoArrayForkChoiceStrategy: Unexpected zero hashes in voted validator votes"); continue; } int validatorIndexInt = toIntExact(validatorIndex.longValue()); UInt64 oldBalance = oldBalances.size() > validatorIndexInt ? oldBalances.get(validatorIndexInt) : UInt64.ZERO; UInt64 newBalance = newBalances.size() > validatorIndexInt ? newBalances.get(validatorIndexInt) : UInt64.ZERO; if (!vote.getCurrentRoot().equals(vote.getNextRoot()) || !oldBalance.equals(newBalance)) { Integer currentDeltaIndex = indices.get(vote.getCurrentRoot()); if (currentDeltaIndex != null) { checkState( currentDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = subtractExact(deltas.get(currentDeltaIndex), oldBalance.longValue()); deltas.set(currentDeltaIndex, delta); } Integer nextDeltaIndex = indices.get(vote.getNextRoot()); if (nextDeltaIndex != null) { checkState( nextDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = addExact(deltas.get(nextDeltaIndex), newBalance.longValue()); deltas.set(nextDeltaIndex, delta); } vote.setCurrentRoot(vote.getNextRoot()); } } return deltas; }   }
ProtoArrayScoreCalculator { static List<Long> computeDeltas( MutableStore store, Map<Bytes32, Integer> indices, List<UInt64> oldBalances, List<UInt64> newBalances) { List<Long> deltas = new ArrayList<>(Collections.nCopies(indices.size(), 0L)); for (UInt64 validatorIndex : store.getVotedValidatorIndices()) { VoteTracker vote = store.getVote(validatorIndex); if (vote.getCurrentRoot().equals(Bytes32.ZERO) && vote.getNextRoot().equals(Bytes32.ZERO)) { LOG.warn("ProtoArrayForkChoiceStrategy: Unexpected zero hashes in voted validator votes"); continue; } int validatorIndexInt = toIntExact(validatorIndex.longValue()); UInt64 oldBalance = oldBalances.size() > validatorIndexInt ? oldBalances.get(validatorIndexInt) : UInt64.ZERO; UInt64 newBalance = newBalances.size() > validatorIndexInt ? newBalances.get(validatorIndexInt) : UInt64.ZERO; if (!vote.getCurrentRoot().equals(vote.getNextRoot()) || !oldBalance.equals(newBalance)) { Integer currentDeltaIndex = indices.get(vote.getCurrentRoot()); if (currentDeltaIndex != null) { checkState( currentDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = subtractExact(deltas.get(currentDeltaIndex), oldBalance.longValue()); deltas.set(currentDeltaIndex, delta); } Integer nextDeltaIndex = indices.get(vote.getNextRoot()); if (nextDeltaIndex != null) { checkState( nextDeltaIndex < deltas.size(), "ProtoArrayForkChoice: Invalid node delta index"); long delta = addExact(deltas.get(nextDeltaIndex), newBalance.longValue()); deltas.set(nextDeltaIndex, delta); } vote.setCurrentRoot(vote.getNextRoot()); } } return deltas; }    }