Search in sources :

Example 1 with PreparedCertificate

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());
}
Also used : RoundChangeMessageData(org.hyperledger.besu.consensus.ibft.messagedata.RoundChangeMessageData) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) Prepare(org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare) PreparedCertificate(org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate) RoundChangeMessageData(org.hyperledger.besu.consensus.ibft.messagedata.RoundChangeMessageData) RoundExpiry(org.hyperledger.besu.consensus.common.bft.events.RoundExpiry) Test(org.junit.Test)

Example 2 with PreparedCertificate

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());
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) Block(org.hyperledger.besu.ethereum.core.Block) PreparedRoundArtifacts(org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts) PreparedCertificate(org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate) NodeKey(org.hyperledger.besu.crypto.NodeKey) Proposal(org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal) Test(org.junit.Test)

Example 3 with PreparedCertificate

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();
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) PreparedCertificate(org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 4 with PreparedCertificate

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();
}
Also used : RoundChange(org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange) Prepare(org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare) PreparedRoundArtifacts(org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts) PreparedCertificate(org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate) Test(org.junit.Test)

Example 5 with PreparedCertificate

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());
}
Also used : RoundChange(org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange) PreparedRoundArtifacts(org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts) PreparedCertificate(org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate) Test(org.junit.Test)

Aggregations

PreparedCertificate (org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate)8 Test (org.junit.Test)6 PreparedRoundArtifacts (org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts)4 Prepare (org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare)3 RoundChange (org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange)3 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)2 MessageFactory (org.hyperledger.besu.consensus.ibft.payload.MessageFactory)2 NodeKey (org.hyperledger.besu.crypto.NodeKey)2 Block (org.hyperledger.besu.ethereum.core.Block)2 RoundExpiry (org.hyperledger.besu.consensus.common.bft.events.RoundExpiry)1 SignedData (org.hyperledger.besu.consensus.common.bft.payload.SignedData)1 RoundChangeMessageData (org.hyperledger.besu.consensus.ibft.messagedata.RoundChangeMessageData)1 Proposal (org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal)1 RoundChangePayload (org.hyperledger.besu.consensus.ibft.payload.RoundChangePayload)1 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)1