Search in sources :

Example 1 with PreparePayload

use of org.hyperledger.besu.consensus.qbft.payload.PreparePayload in project besu by hyperledger.

the class ProposalTest method canRoundTripProposalMessage.

@Test
public void canRoundTripProposalMessage() {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final Address addr = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final ProposalPayload payload = new ProposalPayload(new ConsensusRoundIdentifier(1, 1), BLOCK);
    final SignedData<ProposalPayload> signedPayload = SignedData.create(payload, nodeKey.sign(payload.hashForSignature()));
    final PreparePayload preparePayload = new PreparePayload(new ConsensusRoundIdentifier(1, 0), BLOCK.getHash());
    final SignedData<PreparePayload> prepare = SignedData.create(preparePayload, nodeKey.sign(preparePayload.hashForSignature()));
    final RoundChangePayload roundChangePayload = new RoundChangePayload(new ConsensusRoundIdentifier(1, 0), Optional.of(new PreparedRoundMetadata(BLOCK.getHash(), 0)));
    final SignedData<RoundChangePayload> roundChange = SignedData.create(roundChangePayload, nodeKey.sign(roundChangePayload.hashForSignature()));
    final Proposal proposal = new Proposal(signedPayload, List.of(roundChange), List.of(prepare));
    final Proposal decodedProposal = Proposal.decode(proposal.encode(), bftExtraDataCodec);
    assertThat(decodedProposal.getAuthor()).isEqualTo(addr);
    assertThat(decodedProposal.getMessageType()).isEqualTo(QbftV1.PROPOSAL);
    assertThat(decodedProposal.getPrepares()).hasSize(1);
    assertThat(decodedProposal.getPrepares().get(0)).isEqualToComparingFieldByField(prepare);
    assertThat(decodedProposal.getRoundChanges()).hasSize(1);
    assertThat(decodedProposal.getRoundChanges().get(0)).isEqualToComparingFieldByField(roundChange);
    assertThat(decodedProposal.getSignedPayload().getPayload().getProposedBlock()).isEqualTo(BLOCK);
    assertThat(decodedProposal.getSignedPayload().getPayload().getRoundIdentifier()).isEqualTo(payload.getRoundIdentifier());
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) PreparePayload(org.hyperledger.besu.consensus.qbft.payload.PreparePayload) Address(org.hyperledger.besu.datatypes.Address) PreparedRoundMetadata(org.hyperledger.besu.consensus.qbft.payload.PreparedRoundMetadata) RoundChangePayload(org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload) ProposalPayload(org.hyperledger.besu.consensus.qbft.payload.ProposalPayload) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 2 with PreparePayload

use of org.hyperledger.besu.consensus.qbft.payload.PreparePayload in project besu by hyperledger.

the class ReceivedFutureProposalTest method futureProposalWithInvalidPrepareDoesNotTriggerNextRound.

@Test
public void futureProposalWithInvalidPrepareDoesNotTriggerNextRound() {
    final Block initialBlock = context.createBlockForProposalFromChainHead(15, peers.getProposer().getNodeAddress());
    final Block reproposedBlock = context.createBlockForProposalFromChainHead(15);
    final ConsensusRoundIdentifier nextRoundId = new ConsensusRoundIdentifier(1, 1);
    final PreparedCertificate preparedRoundArtifacts = createValidPreparedCertificate(context, roundId, initialBlock);
    final List<SignedData<RoundChangePayload>> roundChanges = peers.createSignedRoundChangePayload(nextRoundId, preparedRoundArtifacts);
    List<SignedData<PreparePayload>> prepares = peers.createSignedPreparePayloadOfAllPeers(roundId, initialBlock.getHash());
    prepares = prepares.stream().filter(p -> !p.getAuthor().equals(peers.getFirstNonProposer().getNodeAddress())).collect(Collectors.toList());
    final SignedData<PreparePayload> invalidPrepare = peers.getFirstNonProposer().getMessageFactory().createPrepare(nextRoundId, initialBlock.getHash()).getSignedPayload();
    prepares.add(invalidPrepare);
    final ValidatorPeer nextProposer = context.roundSpecificPeers(nextRoundId).getProposer();
    nextProposer.injectProposalForFutureRound(nextRoundId, roundChanges, prepares, reproposedBlock);
    peers.verifyNoMessagesReceived();
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) ValidatorPeer(org.hyperledger.besu.consensus.qbft.support.ValidatorPeer) PreparePayload(org.hyperledger.besu.consensus.qbft.payload.PreparePayload) SignedData(org.hyperledger.besu.consensus.common.bft.payload.SignedData) Block(org.hyperledger.besu.ethereum.core.Block) IntegrationTestHelpers.createValidPreparedCertificate(org.hyperledger.besu.consensus.qbft.support.IntegrationTestHelpers.createValidPreparedCertificate) PreparedCertificate(org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate) Test(org.junit.jupiter.api.Test)

Example 3 with PreparePayload

use of org.hyperledger.besu.consensus.qbft.payload.PreparePayload in project besu by hyperledger.

the class QbftRoundTest method aProposalMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWithCertificate.

@Test
public void aProposalMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWithCertificate() {
    final ConsensusRoundIdentifier priorRoundChange = new ConsensusRoundIdentifier(1, 0);
    final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
    final QbftRound round = new QbftRound(roundState, blockCreator, protocolContext, blockImporter, subscribers, nodeKey, messageFactory, transmitter, roundTimer, bftExtraDataCodec);
    final SignedData<PreparePayload> preparedPayload = messageFactory.createPrepare(priorRoundChange, proposedBlock.getHash()).getSignedPayload();
    final RoundChange roundChange = messageFactory.createRoundChange(roundIdentifier, Optional.of(new PreparedCertificate(proposedBlock, singletonList(preparedPayload), 2)));
    final RoundChangeArtifacts roundChangeArtifacts = RoundChangeArtifacts.create(singletonList(roundChange));
    round.startRoundWith(roundChangeArtifacts, 15);
    verify(transmitter, times(1)).multicastProposal(eq(roundIdentifier), blockCaptor.capture(), eq(singletonList(roundChange.getSignedPayload())), eq(singletonList(preparedPayload)));
    verify(transmitter, times(1)).multicastPrepare(eq(roundIdentifier), eq(blockCaptor.getValue().getHash()));
    final BftExtraData proposedExtraData = new QbftExtraDataCodec().decode(blockCaptor.getValue().getHeader());
    assertThat(proposedExtraData.getRound()).isEqualTo(roundIdentifier.getRoundNumber());
    // Inject a single Prepare message, and confirm the roundState has gone to Prepared (which
    // indicates the block has entered the roundState (note: all msgs are deemed valid due to mocks)
    round.handlePrepareMessage(messageFactory2.createPrepare(roundIdentifier, proposedBlock.getHash()));
    assertThat(roundState.isPrepared()).isTrue();
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) RoundChange(org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange) PreparePayload(org.hyperledger.besu.consensus.qbft.payload.PreparePayload) QbftExtraDataCodec(org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Example 4 with PreparePayload

use of org.hyperledger.besu.consensus.qbft.payload.PreparePayload in project besu by hyperledger.

the class PrepareTest method canRoundTripAPrepareMessage.

@Test
public void canRoundTripAPrepareMessage() {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final Address addr = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final PreparePayload preparePayload = new PreparePayload(new ConsensusRoundIdentifier(1, 1), Hash.ZERO);
    final SignedData<PreparePayload> signedPreparePayload = SignedData.create(preparePayload, nodeKey.sign(preparePayload.hashForSignature()));
    final Prepare prepareMsg = new Prepare(signedPreparePayload);
    final Prepare decodedPrepare = Prepare.decode(prepareMsg.encode());
    assertThat(decodedPrepare.getMessageType()).isEqualTo(QbftV1.PREPARE);
    assertThat(decodedPrepare.getAuthor()).isEqualTo(addr);
    assertThat(decodedPrepare.getSignedPayload()).isEqualToComparingFieldByField(signedPreparePayload);
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) PreparePayload(org.hyperledger.besu.consensus.qbft.payload.PreparePayload) Address(org.hyperledger.besu.datatypes.Address) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 5 with PreparePayload

use of org.hyperledger.besu.consensus.qbft.payload.PreparePayload in project besu by hyperledger.

the class RoundChangeTest method canRoundTripARoundChangeMessage.

@Test
public void canRoundTripARoundChangeMessage() {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final Address addr = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final RoundChangePayload payload = new RoundChangePayload(new ConsensusRoundIdentifier(1, 1), Optional.of(new PreparedRoundMetadata(BLOCK.getHash(), 0)));
    final SignedData<RoundChangePayload> signedRoundChangePayload = SignedData.create(payload, nodeKey.sign(payload.hashForSignature()));
    final PreparePayload preparePayload = new PreparePayload(new ConsensusRoundIdentifier(1, 0), BLOCK.getHash());
    final SignedData<PreparePayload> signedPreparePayload = SignedData.create(preparePayload, nodeKey.sign(preparePayload.hashForSignature()));
    final RoundChange roundChange = new RoundChange(signedRoundChangePayload, Optional.of(BLOCK), List.of(signedPreparePayload));
    final RoundChange decodedRoundChange = RoundChange.decode(roundChange.encode(), bftExtraDataCodec);
    assertThat(decodedRoundChange.getMessageType()).isEqualTo(QbftV1.ROUND_CHANGE);
    assertThat(decodedRoundChange.getAuthor()).isEqualTo(addr);
    assertThat(decodedRoundChange.getSignedPayload()).isEqualToComparingFieldByField(signedRoundChangePayload);
    assertThat(decodedRoundChange.getProposedBlock()).isNotEmpty();
    assertThat(decodedRoundChange.getProposedBlock().get()).isEqualToComparingFieldByField(BLOCK);
    assertThat(decodedRoundChange.getPrepares()).hasSize(1);
    assertThat(decodedRoundChange.getPrepares().get(0)).isEqualToComparingFieldByField(signedPreparePayload);
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) PreparePayload(org.hyperledger.besu.consensus.qbft.payload.PreparePayload) Address(org.hyperledger.besu.datatypes.Address) PreparedRoundMetadata(org.hyperledger.besu.consensus.qbft.payload.PreparedRoundMetadata) RoundChangePayload(org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Aggregations

ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)5 PreparePayload (org.hyperledger.besu.consensus.qbft.payload.PreparePayload)5 Test (org.junit.Test)4 NodeKey (org.hyperledger.besu.crypto.NodeKey)3 Address (org.hyperledger.besu.datatypes.Address)3 PreparedRoundMetadata (org.hyperledger.besu.consensus.qbft.payload.PreparedRoundMetadata)2 RoundChangePayload (org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload)2 BftExtraData (org.hyperledger.besu.consensus.common.bft.BftExtraData)1 SignedData (org.hyperledger.besu.consensus.common.bft.payload.SignedData)1 QbftExtraDataCodec (org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec)1 RoundChange (org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange)1 ProposalPayload (org.hyperledger.besu.consensus.qbft.payload.ProposalPayload)1 PreparedCertificate (org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate)1 IntegrationTestHelpers.createValidPreparedCertificate (org.hyperledger.besu.consensus.qbft.support.IntegrationTestHelpers.createValidPreparedCertificate)1 ValidatorPeer (org.hyperledger.besu.consensus.qbft.support.ValidatorPeer)1 Block (org.hyperledger.besu.ethereum.core.Block)1 Test (org.junit.jupiter.api.Test)1