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());
}
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();
}
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();
}
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);
}
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);
}
Aggregations