Search in sources :

Example 1 with PreparedRoundArtifacts

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());
}
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 2 with PreparedRoundArtifacts

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

Example 3 with PreparedRoundArtifacts

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

Example 4 with PreparedRoundArtifacts

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

Example 5 with PreparedRoundArtifacts

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());
}
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) Test(org.junit.Test)

Aggregations

PreparedRoundArtifacts (org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts)16 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)11 Test (org.junit.Test)10 RoundChange (org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange)8 RoundChangeCertificate (org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate)7 Block (org.hyperledger.besu.ethereum.core.Block)7 IntegrationTestHelpers.createValidPreparedRoundArtifacts (org.hyperledger.besu.consensus.ibft.support.IntegrationTestHelpers.createValidPreparedRoundArtifacts)6 Test (org.junit.jupiter.api.Test)6 Prepare (org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare)5 Proposal (org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal)5 PreparedCertificate (org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate)4 SignedData (org.hyperledger.besu.consensus.common.bft.payload.SignedData)3 ValidatorPeer (org.hyperledger.besu.consensus.ibft.support.ValidatorPeer)2 RoundExpiry (org.hyperledger.besu.consensus.common.bft.events.RoundExpiry)1 Commit (org.hyperledger.besu.consensus.ibft.messagewrappers.Commit)1 MessageFactory (org.hyperledger.besu.consensus.ibft.payload.MessageFactory)1 RoundSpecificPeers (org.hyperledger.besu.consensus.ibft.support.RoundSpecificPeers)1 NodeKey (org.hyperledger.besu.crypto.NodeKey)1