Search in sources :

Example 1 with ValidationState

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

the class PendingVotesTest method when_voting_again__previous_timeoutvote_is_removed.

@Test
public void when_voting_again__previous_timeoutvote_is_removed() {
    BFTNode author = mock(BFTNode.class);
    Vote vote = makeSignedVoteFor(author, View.genesis(), HashUtils.random256());
    when(vote.getTimeoutSignature()).thenReturn(Optional.of(mock(ECDSASignature.class)));
    when(vote.isTimeout()).thenReturn(true);
    BFTValidatorSet validatorSet = mock(BFTValidatorSet.class);
    ValidationState validationState = mock(ValidationState.class);
    TimestampedECDSASignatures signatures = mock(TimestampedECDSASignatures.class);
    when(validationState.signatures()).thenReturn(signatures);
    when(validationState.isEmpty()).thenReturn(true);
    when(validatorSet.newValidationState()).thenReturn(validationState);
    when(validatorSet.containsNode(any())).thenReturn(true);
    VoteData voteData = mock(VoteData.class);
    BFTHeader proposed = vote.getVoteData().getProposed();
    when(voteData.getProposed()).thenReturn(proposed);
    // Preconditions
    assertEquals(VoteProcessingResult.accepted(), this.pendingVotes.insertVote(vote, validatorSet));
    assertEquals(1, this.pendingVotes.voteStateSize());
    assertEquals(1, this.pendingVotes.timeoutVoteStateSize());
    assertEquals(1, this.pendingVotes.previousVotesSize());
    Vote vote2 = makeSignedVoteFor(author, View.of(1), HashUtils.random256());
    // Need a different hash for this (different) vote
    assertEquals(VoteProcessingResult.accepted(), this.pendingVotes.insertVote(vote2, validatorSet));
    assertEquals(1, this.pendingVotes.voteStateSize());
    assertEquals(0, this.pendingVotes.timeoutVoteStateSize());
    assertEquals(1, this.pendingVotes.previousVotesSize());
}
Also used : BFTNode(com.radixdlt.consensus.bft.BFTNode) ValidationState(com.radixdlt.consensus.bft.ValidationState) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) Test(org.junit.Test)

Example 2 with ValidationState

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

the class PendingVotes method processVoteForQC.

private Optional<QuorumCertificate> processVoteForQC(Vote vote, BFTValidatorSet validatorSet) {
    final VoteData voteData = vote.getVoteData();
    final HashCode voteDataHash = this.hasher.hash(voteData);
    final BFTNode node = vote.getAuthor();
    final ValidationState validationState = this.voteState.computeIfAbsent(voteDataHash, k -> validatorSet.newValidationState());
    final boolean signatureAdded = validationState.addSignature(node, vote.getTimestamp(), vote.getSignature());
    if (signatureAdded && validationState.complete()) {
        return Optional.of(new QuorumCertificate(voteData, validationState.signatures()));
    } else {
        return Optional.empty();
    }
}
Also used : BFTNode(com.radixdlt.consensus.bft.BFTNode) ValidationState(com.radixdlt.consensus.bft.ValidationState) HashCode(com.google.common.hash.HashCode)

Example 3 with ValidationState

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

the class PendingVotes method processVoteForTC.

private Optional<TimeoutCertificate> processVoteForTC(Vote vote, BFTValidatorSet validatorSet) {
    if (!vote.isTimeout()) {
        // TC can't be formed if vote is not timed out
        return Optional.empty();
    }
    final ECDSASignature timeoutSignature = vote.getTimeoutSignature().orElseThrow();
    final VoteTimeout voteTimeout = VoteTimeout.of(vote);
    final HashCode voteTimeoutHash = this.hasher.hash(voteTimeout);
    final BFTNode node = vote.getAuthor();
    final ValidationState validationState = this.timeoutVoteState.computeIfAbsent(voteTimeoutHash, k -> validatorSet.newValidationState());
    final boolean signatureAdded = validationState.addSignature(node, vote.getTimestamp(), timeoutSignature);
    if (signatureAdded && validationState.complete()) {
        return Optional.of(new TimeoutCertificate(voteTimeout.getEpoch(), voteTimeout.getView(), validationState.signatures()));
    } else {
        return Optional.empty();
    }
}
Also used : BFTNode(com.radixdlt.consensus.bft.BFTNode) ValidationState(com.radixdlt.consensus.bft.ValidationState) HashCode(com.google.common.hash.HashCode) ECDSASignature(com.radixdlt.crypto.ECDSASignature) VoteTimeout(com.radixdlt.consensus.liveness.VoteTimeout)

Example 4 with ValidationState

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

the class PendingVotesTest method when_inserting_valid_and_accepted_votes__then_qc_is_formed.

@Test
public void when_inserting_valid_and_accepted_votes__then_qc_is_formed() {
    BFTNode author = mock(BFTNode.class);
    Vote vote = makeSignedVoteFor(author, View.genesis(), HashUtils.random256());
    BFTValidatorSet validatorSet = mock(BFTValidatorSet.class);
    ValidationState validationState = mock(ValidationState.class);
    TimestampedECDSASignatures signatures = mock(TimestampedECDSASignatures.class);
    when(validationState.addSignature(any(), anyLong(), any())).thenReturn(true);
    when(validationState.complete()).thenReturn(true);
    when(validationState.signatures()).thenReturn(signatures);
    when(validatorSet.newValidationState()).thenReturn(validationState);
    when(validatorSet.containsNode(any())).thenReturn(true);
    VoteData voteData = mock(VoteData.class);
    BFTHeader proposed = vote.getVoteData().getProposed();
    when(voteData.getProposed()).thenReturn(proposed);
    assertTrue(this.pendingVotes.insertVote(vote, validatorSet) instanceof VoteProcessingResult.QuorumReached);
}
Also used : BFTNode(com.radixdlt.consensus.bft.BFTNode) ValidationState(com.radixdlt.consensus.bft.ValidationState) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) VoteProcessingResult(com.radixdlt.consensus.bft.VoteProcessingResult) Test(org.junit.Test)

Example 5 with ValidationState

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

the class PendingVotesTest method when_voting_again__previous_vote_is_removed.

@Test
public void when_voting_again__previous_vote_is_removed() {
    BFTNode author = mock(BFTNode.class);
    Vote vote = makeSignedVoteFor(author, View.genesis(), HashUtils.random256());
    BFTValidatorSet validatorSet = mock(BFTValidatorSet.class);
    ValidationState validationState = mock(ValidationState.class);
    TimestampedECDSASignatures signatures = mock(TimestampedECDSASignatures.class);
    when(validationState.signatures()).thenReturn(signatures);
    when(validationState.isEmpty()).thenReturn(true);
    when(validatorSet.newValidationState()).thenReturn(validationState);
    when(validatorSet.containsNode(any())).thenReturn(true);
    VoteData voteData = mock(VoteData.class);
    BFTHeader proposed = vote.getVoteData().getProposed();
    when(voteData.getProposed()).thenReturn(proposed);
    // Preconditions
    assertEquals(VoteProcessingResult.accepted(), this.pendingVotes.insertVote(vote, validatorSet));
    assertEquals(1, this.pendingVotes.voteStateSize());
    assertEquals(1, this.pendingVotes.previousVotesSize());
    Vote vote2 = makeSignedVoteFor(author, View.of(1), HashUtils.random256());
    // Need a different hash for this (different) vote
    assertEquals(VoteProcessingResult.accepted(), this.pendingVotes.insertVote(vote2, validatorSet));
    assertEquals(1, this.pendingVotes.voteStateSize());
    assertEquals(1, this.pendingVotes.previousVotesSize());
}
Also used : BFTNode(com.radixdlt.consensus.bft.BFTNode) ValidationState(com.radixdlt.consensus.bft.ValidationState) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) Test(org.junit.Test)

Aggregations

BFTNode (com.radixdlt.consensus.bft.BFTNode)5 ValidationState (com.radixdlt.consensus.bft.ValidationState)5 BFTValidatorSet (com.radixdlt.consensus.bft.BFTValidatorSet)3 Test (org.junit.Test)3 HashCode (com.google.common.hash.HashCode)2 VoteProcessingResult (com.radixdlt.consensus.bft.VoteProcessingResult)1 VoteTimeout (com.radixdlt.consensus.liveness.VoteTimeout)1 ECDSASignature (com.radixdlt.crypto.ECDSASignature)1