use of org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate in project besu by hyperledger.
the class IbftBlockHeightManagerTest method preparedCertificateIncludedInRoundChangeMessageOnRoundTimeoutExpired.
@Test
public void preparedCertificateIncludedInRoundChangeMessageOnRoundTimeoutExpired() {
final IbftBlockHeightManager manager = new IbftBlockHeightManager(headerTestFixture.buildHeader(), finalState, roundChangeManager, roundFactory, clock, messageValidatorFactory, messageFactory);
// Trigger a Proposal creation.
manager.handleBlockTimerExpiry(roundIdentifier);
final Prepare firstPrepare = validatorMessageFactory.get(0).createPrepare(roundIdentifier, Hash.fromHexStringLenient("0"));
final Prepare secondPrepare = validatorMessageFactory.get(1).createPrepare(roundIdentifier, Hash.fromHexStringLenient("0"));
manager.handlePreparePayload(firstPrepare);
manager.handlePreparePayload(secondPrepare);
manager.roundExpired(new RoundExpiry(roundIdentifier));
verify(validatorMulticaster, times(1)).send(sentMessageArgCaptor.capture());
final MessageData capturedMessageData = sentMessageArgCaptor.getValue();
assertThat(capturedMessageData).isInstanceOf(RoundChangeMessageData.class);
final RoundChangeMessageData roundChange = (RoundChangeMessageData) capturedMessageData;
Optional<PreparedCertificate> preparedCert = roundChange.decode().getPreparedCertificate();
Assertions.assertThat(preparedCert).isNotEmpty();
assertThat(preparedCert.get().getPreparePayloads()).containsOnly(firstPrepare.getSignedPayload(), secondPrepare.getSignedPayload());
}
use of org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate 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.payload.PreparedCertificate in project besu by hyperledger.
the class RoundChangeCertificateValidatorTest method allRoundChangeHaveNoPreparedReturnsEmptyOptional.
@Test
public void allRoundChangeHaveNoPreparedReturnsEmptyOptional() {
final NodeKey proposerKey = NodeKeyUtils.generate();
final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 4);
final Optional<PreparedCertificate> newestCert = RoundChangeCertificateValidator.findLatestPreparedCertificate(Lists.newArrayList(proposerMessageFactory.createRoundChange(roundIdentifier, Optional.empty()).getSignedPayload(), proposerMessageFactory.createRoundChange(roundIdentifier, Optional.empty()).getSignedPayload()));
assertThat(newestCert).isEmpty();
}
use of org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate in project besu by hyperledger.
the class RoundChangeSignedDataValidatorTest method roundChangeWithDuplicatedPreparesFails.
@Test
public void roundChangeWithDuplicatedPreparesFails() {
final RoundChangePayloadValidator validatorRequiringTwoPrepares = new RoundChangePayloadValidator(validatorFactory, validators, 2, chainHeight);
final Prepare prepareMsg = validatorMessageFactory.createPrepare(currentRound, block.getHash());
final PreparedRoundArtifacts preparedRoundArtifacts = new PreparedRoundArtifacts(proposerMessageFactory.createProposal(currentRound, block, Optional.empty()), Lists.newArrayList(prepareMsg, prepareMsg));
final PreparedCertificate prepareCertificate = preparedRoundArtifacts.getPreparedCertificate();
final RoundChange msg = proposerMessageFactory.createRoundChange(targetRound, Optional.of(preparedRoundArtifacts));
when(basicValidator.validateProposal(prepareCertificate.getProposalPayload())).thenReturn(true);
when(basicValidator.validatePrepare(prepareMsg.getSignedPayload())).thenReturn(true);
assertThat(validatorRequiringTwoPrepares.validateRoundChange(msg.getSignedPayload())).isFalse();
}
use of org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate in project besu by hyperledger.
the class RoundChangeSignedDataValidatorTest method roundChangeContainingInvalidProposalFails.
@Test
public void roundChangeContainingInvalidProposalFails() {
final PreparedRoundArtifacts preparedRoundArtifacts = new PreparedRoundArtifacts(proposerMessageFactory.createProposal(currentRound, block, Optional.empty()), Collections.emptyList());
final PreparedCertificate prepareCertificate = preparedRoundArtifacts.getPreparedCertificate();
final RoundChange msg = proposerMessageFactory.createRoundChange(targetRound, Optional.of(preparedRoundArtifacts));
when(basicValidator.validateProposal(any())).thenReturn(false);
assertThat(validator.validateRoundChange(msg.getSignedPayload())).isFalse();
verify(validatorFactory, times(1)).createAt(prepareCertificate.getProposalPayload().getPayload().getRoundIdentifier());
verify(basicValidator, times(1)).validateProposal(prepareCertificate.getProposalPayload());
verify(basicValidator, never()).validatePrepare(any());
verify(basicValidator, never()).validateCommit(any());
}
Aggregations