use of org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts in project besu by hyperledger.
the class RoundChangeCertificateValidatorTest method latestPreparedCertificateIsExtractedFromRoundChangeCertificate.
@Test
public void latestPreparedCertificateIsExtractedFromRoundChangeCertificate() {
// NOTE: This function does not validate that all RoundCHanges/Prepares etc. come from valid
// sources, it is only responsible for determine which of the list or RoundChange messages
// contains the newest
// NOTE: This capability is tested as part of the NewRoundMessageValidationTests.
final NodeKey proposerKey = NodeKeyUtils.generate();
final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
final Block proposedBlock = mock(Block.class);
when(proposedBlock.getHash()).thenReturn(Hash.fromHexStringLenient("1"));
final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 4);
final ConsensusRoundIdentifier preparedRound = ConsensusRoundHelpers.createFrom(roundIdentifier, 0, -1);
final Proposal differentProposal = proposerMessageFactory.createProposal(preparedRound, proposedBlock, Optional.empty());
final Optional<PreparedRoundArtifacts> latterPreparedRoundArtifacts = Optional.of(new PreparedRoundArtifacts(differentProposal, Lists.newArrayList(proposerMessageFactory.createPrepare(roundIdentifier, proposedBlock.getHash()), proposerMessageFactory.createPrepare(roundIdentifier, proposedBlock.getHash()))));
// An earlier PrepareCert is added to ensure the path to find the latest PrepareCert
// is correctly followed.
final ConsensusRoundIdentifier earlierPreparedRound = ConsensusRoundHelpers.createFrom(roundIdentifier, 0, -2);
final Proposal earlierProposal = proposerMessageFactory.createProposal(earlierPreparedRound, proposedBlock, Optional.empty());
final Optional<PreparedRoundArtifacts> earlierPreparedRoundArtifacts = Optional.of(new PreparedRoundArtifacts(earlierProposal, Lists.newArrayList(proposerMessageFactory.createPrepare(earlierPreparedRound, proposedBlock.getHash()), proposerMessageFactory.createPrepare(earlierPreparedRound, proposedBlock.getHash()))));
final Optional<PreparedCertificate> newestCert = RoundChangeCertificateValidator.findLatestPreparedCertificate(Lists.newArrayList(proposerMessageFactory.createRoundChange(roundIdentifier, earlierPreparedRoundArtifacts).getSignedPayload(), proposerMessageFactory.createRoundChange(roundIdentifier, latterPreparedRoundArtifacts).getSignedPayload()));
assertThat(newestCert.get()).isEqualTo(latterPreparedRoundArtifacts.get().getPreparedCertificate());
}
use of org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts in project besu by hyperledger.
the class RoundChangeCertificateValidatorTest method correctlyMatchesBlockAgainstLatestInRoundChangeCertificate.
@Test
public void correctlyMatchesBlockAgainstLatestInRoundChangeCertificate() {
final ConsensusRoundIdentifier latterPrepareRound = ConsensusRoundHelpers.createFrom(roundIdentifier, 0, -1);
final Block latterBlock = ProposedBlockHelpers.createProposalBlock(validators, latterPrepareRound, bftExtraDataEncoder);
final Proposal latterProposal = proposerMessageFactory.createProposal(latterPrepareRound, latterBlock, empty());
final Optional<PreparedRoundArtifacts> latterTerminatedRoundArtefacts = Optional.of(new PreparedRoundArtifacts(latterProposal, org.assertj.core.util.Lists.newArrayList(validatorMessageFactory.createPrepare(latterPrepareRound, proposedBlock.getHash()))));
// An earlier PrepareCert is added to ensure the path to find the latest PrepareCert
// is correctly followed.
final ConsensusRoundIdentifier earlierPreparedRound = new ConsensusRoundIdentifier(roundIdentifier.getSequenceNumber(), roundIdentifier.getRoundNumber() - 2);
final Block earlierBlock = ProposedBlockHelpers.createProposalBlock(validators.subList(0, 1), earlierPreparedRound, bftExtraDataEncoder);
final Proposal earlierProposal = proposerMessageFactory.createProposal(earlierPreparedRound, earlierBlock, empty());
final Optional<PreparedRoundArtifacts> earlierTerminatedRoundArtefacts = Optional.of(new PreparedRoundArtifacts(earlierProposal, org.assertj.core.util.Lists.newArrayList(validatorMessageFactory.createPrepare(earlierPreparedRound, earlierBlock.getHash()))));
final RoundChangeCertificate roundChangeCert = new RoundChangeCertificate(org.assertj.core.util.Lists.newArrayList(proposerMessageFactory.createRoundChange(roundIdentifier, earlierTerminatedRoundArtefacts).getSignedPayload(), validatorMessageFactory.createRoundChange(roundIdentifier, latterTerminatedRoundArtefacts).getSignedPayload()));
assertThat(validator.validateProposalMessageMatchesLatestPrepareCertificate(roundChangeCert, earlierBlock)).isFalse();
assertThat(validator.validateProposalMessageMatchesLatestPrepareCertificate(roundChangeCert, latterBlock)).isTrue();
}
use of org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts in project besu by hyperledger.
the class RoundChangeCertificateValidatorTest method detectsTheSuppliedBlockIsNotInLatestPrepareCertificate.
@Test
public void detectsTheSuppliedBlockIsNotInLatestPrepareCertificate() {
final ConsensusRoundIdentifier preparedRound = ConsensusRoundHelpers.createFrom(roundIdentifier, 0, -1);
// The previous proposedBlock has been constructed with less validators, so is thus not
// identical
// to the proposedBlock in the new proposal (so should fail).
final Block prevProposedBlock = ProposedBlockHelpers.createProposalBlock(validators.subList(0, 1), preparedRound, bftExtraDataEncoder);
final PreparedRoundArtifacts mismatchedRoundArtefacts = new PreparedRoundArtifacts(proposerMessageFactory.createProposal(preparedRound, prevProposedBlock, empty()), singletonList(validatorMessageFactory.createPrepare(preparedRound, prevProposedBlock.getHash())));
final RoundChangeCertificate roundChangeCert = new RoundChangeCertificate(singletonList(validatorMessageFactory.createRoundChange(roundIdentifier, Optional.of(mismatchedRoundArtefacts)).getSignedPayload()));
assertThat(validator.validateProposalMessageMatchesLatestPrepareCertificate(roundChangeCert, proposedBlock)).isFalse();
}
use of org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts in project besu by hyperledger.
the class RoundChangeSignedDataValidatorTest method roundChangeContainingValidProposalButNoPrepareMessagesFails.
@Test
public void roundChangeContainingValidProposalButNoPrepareMessagesFails() {
final PreparedRoundArtifacts preparedRoundArtifacts = new PreparedRoundArtifacts(proposerMessageFactory.createProposal(currentRound, block, Optional.empty()), Collections.emptyList());
final RoundChange msg = proposerMessageFactory.createRoundChange(targetRound, Optional.of(preparedRoundArtifacts));
when(basicValidator.validateProposal(any())).thenReturn(true);
assertThat(validator.validateRoundChange(msg.getSignedPayload())).isFalse();
}
use of org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts in project besu by hyperledger.
the class RoundChangeSignedDataValidatorTest method roundChangeInvalidPrepareMessageFromProposerFails.
@Test
public void roundChangeInvalidPrepareMessageFromProposerFails() {
final Prepare prepareMsg = validatorMessageFactory.createPrepare(currentRound, block.getHash());
final PreparedRoundArtifacts preparedRoundArtifacts = new PreparedRoundArtifacts(proposerMessageFactory.createProposal(currentRound, block, Optional.empty()), Lists.newArrayList(prepareMsg));
when(basicValidator.validateProposal(any())).thenReturn(true);
when(basicValidator.validatePrepare(any())).thenReturn(false);
final RoundChange msg = proposerMessageFactory.createRoundChange(targetRound, Optional.of(preparedRoundArtifacts));
assertThat(validator.validateRoundChange(msg.getSignedPayload())).isFalse();
verify(basicValidator, times(1)).validatePrepare(prepareMsg.getSignedPayload());
verify(basicValidator, never()).validateCommit(any());
}
Aggregations