Search in sources :

Example 1 with BFTValidator

use of com.radixdlt.consensus.bft.BFTValidator in project radixdlt by radixdlt.

the class WeightedRotatingLeaders method getProposer.

@Override
public BFTNode getProposer(View view) {
    nextLeaderComputer.computeToView(view);
    // validator will only be null if the view supplied is before the cache
    // window
    BFTValidator validator = nextLeaderComputer.checkCacheForProposer(view);
    if (validator != null) {
        // dynamic program cache successful
        return validator.getNode();
    } else {
        // cache doesn't have value, do the expensive operation
        CachingNextLeaderComputer computer = new CachingNextLeaderComputer(validatorSet, weightsComparator, 1);
        return computer.resetToView(view).getNode();
    }
}
Also used : BFTValidator(com.radixdlt.consensus.bft.BFTValidator)

Example 2 with BFTValidator

use of com.radixdlt.consensus.bft.BFTValidator in project radixdlt by radixdlt.

the class WeightedRotatingLeadersTest method when_validators_distributed_by_fibonacci__then_leaders_also_distributed_in_fibonacci.

@Test
public void when_validators_distributed_by_fibonacci__then_leaders_also_distributed_in_fibonacci() {
    // fibonacci sequence can quickly explode so keep sizes small
    final int validatorSetSize = 8;
    final int sizeOfCache = 4;
    final Supplier<IntStream> fibonacci = () -> Stream.iterate(new int[] { 1, 1 }, t -> new int[] { t[1], t[0] + t[1] }).mapToInt(t -> t[0]).limit(validatorSetSize);
    final int sumOfPower = fibonacci.get().sum();
    this.validatorsInOrder = fibonacci.get().mapToObj(p -> BFTValidator.from(BFTNode.random(), UInt256.from(p))).collect(ImmutableList.toImmutableList());
    BFTValidatorSet validatorSet = BFTValidatorSet.from(validatorsInOrder);
    this.weightedRotatingLeaders = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
    Map<BFTNode, UInt256> proposerCounts = Stream.iterate(View.of(0), View::next).limit(sumOfPower).map(this.weightedRotatingLeaders::getProposer).collect(groupingBy(p -> p, collectingAndThen(counting(), UInt256::from)));
    Map<BFTNode, UInt256> expected = validatorsInOrder.stream().collect(toMap(BFTValidator::getNode, BFTValidator::getPower));
    assertThat(proposerCounts).isEqualTo(expected);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Test(org.junit.Test) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) Supplier(java.util.function.Supplier) BFTValidator(com.radixdlt.consensus.bft.BFTValidator) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) BFTNode(com.radixdlt.consensus.bft.BFTNode) KeyComparator(com.radixdlt.utils.KeyComparator) Comparator(java.util.Comparator) View(com.radixdlt.consensus.bft.View) UInt256(com.radixdlt.utils.UInt256) BFTNode(com.radixdlt.consensus.bft.BFTNode) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) IntStream(java.util.stream.IntStream) UInt256(com.radixdlt.utils.UInt256) Test(org.junit.Test)

Aggregations

BFTValidator (com.radixdlt.consensus.bft.BFTValidator)2 ImmutableList (com.google.common.collect.ImmutableList)1 BFTNode (com.radixdlt.consensus.bft.BFTNode)1 BFTValidatorSet (com.radixdlt.consensus.bft.BFTValidatorSet)1 View (com.radixdlt.consensus.bft.View)1 ECKeyPair (com.radixdlt.crypto.ECKeyPair)1 KeyComparator (com.radixdlt.utils.KeyComparator)1 UInt256 (com.radixdlt.utils.UInt256)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 Supplier (java.util.function.Supplier)1 Collectors.collectingAndThen (java.util.stream.Collectors.collectingAndThen)1 Collectors.counting (java.util.stream.Collectors.counting)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.Test)1