Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Moved IBFT Message factory to use wrapped message types (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain-on authored Feb 1, 2019
1 parent 1a823ae commit 6f04f02
Show file tree
Hide file tree
Showing 27 changed files with 597 additions and 546 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import tech.pegasys.pantheon.consensus.ibft.messagedata.PrepareMessageData;
import tech.pegasys.pantheon.consensus.ibft.messagedata.ProposalMessageData;
import tech.pegasys.pantheon.consensus.ibft.messagedata.RoundChangeMessageData;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.IbftMessage;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.RoundChange;
import tech.pegasys.pantheon.consensus.ibft.payload.Payload;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparePayload;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate;
Expand Down Expand Up @@ -80,7 +82,7 @@ public List<SignedData<RoundChangePayload>> roundChangeForNonProposing(
final ConsensusRoundIdentifier targetRound) {
return nonProposingPeers
.stream()
.map(peer -> peer.injectRoundChange(targetRound, empty()))
.map(peer -> peer.injectRoundChange(targetRound, empty()).getSignedPayload())
.collect(Collectors.toList());
}

Expand All @@ -89,20 +91,24 @@ public void commit(final ConsensusRoundIdentifier roundId, final Hash hash) {
}

public List<SignedData<RoundChangePayload>> roundChange(final ConsensusRoundIdentifier roundId) {
final List<SignedData<RoundChangePayload>> changes = Lists.newArrayList();
final List<RoundChange> changes = Lists.newArrayList();

for (final ValidatorPeer peer : peers) {
changes.add(peer.injectRoundChange(roundId, empty()));
}

return changes;
return changes.stream().map(RoundChange::getSignedPayload).collect(Collectors.toList());
}

public List<SignedData<RoundChangePayload>> createSignedRoundChangePayload(
final ConsensusRoundIdentifier roundId) {
return peers
.stream()
.map(p -> p.getMessageFactory().createSignedRoundChangePayload(roundId, empty()))
.map(
p ->
p.getMessageFactory()
.createSignedRoundChangePayload(roundId, empty())
.getSignedPayload())
.collect(Collectors.toList());
}

Expand All @@ -113,7 +119,8 @@ public List<SignedData<RoundChangePayload>> createSignedRoundChangePayload(
.map(
p ->
p.getMessageFactory()
.createSignedRoundChangePayload(roundId, Optional.of(preparedCertificate)))
.createSignedRoundChangePayload(roundId, Optional.of(preparedCertificate))
.getSignedPayload())
.collect(Collectors.toList());
}

Expand All @@ -125,11 +132,15 @@ public void commitForNonProposing(final ConsensusRoundIdentifier roundId, final
nonProposingPeers.forEach(peer -> peer.injectCommit(roundId, hash));
}

Collection<SignedData<PreparePayload>> createSignedPreparePayloadOfNonProposing(
public Collection<SignedData<PreparePayload>> createSignedPreparePayloadOfNonProposing(
final ConsensusRoundIdentifier preparedRound, final Hash hash) {
return nonProposingPeers
.stream()
.map(role -> role.getMessageFactory().createSignedPreparePayload(preparedRound, hash))
.map(
role ->
role.getMessageFactory()
.createSignedPreparePayload(preparedRound, hash)
.getSignedPayload())
.collect(Collectors.toList());
}

Expand All @@ -146,38 +157,35 @@ public void verifyNoMessagesReceivedProposer() {
}

@SafeVarargs
public final void verifyMessagesReceivedPropser(final SignedData<? extends Payload>... msgs) {
public final void verifyMessagesReceivedProposer(final IbftMessage<? extends Payload>... msgs) {
verifyMessagesReceived(ImmutableList.of(proposer), msgs);
}

@SafeVarargs
public final void verifyMessagesReceivedNonPropsingExcluding(
final ValidatorPeer exclude, final SignedData<? extends Payload>... msgs) {
final ValidatorPeer exclude, final IbftMessage<? extends Payload>... msgs) {
final Collection<ValidatorPeer> candidates = Lists.newArrayList(nonProposingPeers);
candidates.remove(exclude);
verifyMessagesReceived(candidates, msgs);
}

@SafeVarargs
public final void verifyMessagesReceivedNonPropsing(final SignedData<? extends Payload>... msgs) {
public final void verifyMessagesReceivedNonPropsing(final IbftMessage<?>... msgs) {
verifyMessagesReceived(nonProposingPeers, msgs);
}

@SafeVarargs
public final void verifyMessagesReceived(final SignedData<? extends Payload>... msgs) {
public final void verifyMessagesReceived(final IbftMessage<?>... msgs) {
verifyMessagesReceived(peers, msgs);
}

@SafeVarargs
private final void verifyMessagesReceived(
final Collection<ValidatorPeer> candidates, final SignedData<? extends Payload>... msgs) {
private void verifyMessagesReceived(
final Collection<ValidatorPeer> candidates, final IbftMessage<?>... msgs) {
candidates.forEach(n -> assertThat(n.getReceivedMessages().size()).isEqualTo(msgs.length));

List<SignedData<? extends Payload>> msgList = Arrays.asList(msgs);
List<IbftMessage<? extends Payload>> msgList = Arrays.asList(msgs);

for (int i = 0; i < msgList.size(); i++) {
final int index = i;
final SignedData<? extends Payload> msg = msgList.get(index);
final IbftMessage<? extends Payload> msg = msgList.get(index);
candidates.forEach(
n -> {
final List<MessageData> rxMsgs = n.getReceivedMessages();
Expand All @@ -188,36 +196,29 @@ private final void verifyMessagesReceived(
candidates.forEach(ValidatorPeer::clearReceivedMessages);
}

private void verifyMessage(
final MessageData actual, final SignedData<? extends Payload> signedExpectedPayload) {
final Payload expectedPayload = signedExpectedPayload.getPayload();
SignedData<?> actualSignedPayload = null;
private void verifyMessage(final MessageData actual, final IbftMessage<?> expectedMessage) {
IbftMessage<?> actualSignedPayload = null;

switch (expectedPayload.getMessageType()) {
switch (expectedMessage.getMessageType()) {
case IbftV2.PROPOSAL:
actualSignedPayload =
ProposalMessageData.fromMessageData(actual).decode().getSignedPayload();
actualSignedPayload = ProposalMessageData.fromMessageData(actual).decode();
break;
case IbftV2.PREPARE:
actualSignedPayload =
PrepareMessageData.fromMessageData(actual).decode().getSignedPayload();
actualSignedPayload = PrepareMessageData.fromMessageData(actual).decode();
break;
case IbftV2.COMMIT:
actualSignedPayload = CommitMessageData.fromMessageData(actual).decode().getSignedPayload();
actualSignedPayload = CommitMessageData.fromMessageData(actual).decode();
break;
case IbftV2.NEW_ROUND:
actualSignedPayload =
NewRoundMessageData.fromMessageData(actual).decode().getSignedPayload();
actualSignedPayload = NewRoundMessageData.fromMessageData(actual).decode();
break;
case IbftV2.ROUND_CHANGE:
actualSignedPayload =
RoundChangeMessageData.fromMessageData(actual).decode().getSignedPayload();
actualSignedPayload = RoundChangeMessageData.fromMessageData(actual).decode();
break;
default:
fail("Illegal IBFTV2 message type.");
break;
}
assertThat(signedExpectedPayload)
.isEqualToComparingFieldByFieldRecursively(actualSignedPayload);
assertThat(expectedMessage).isEqualToComparingFieldByFieldRecursively(actualSignedPayload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier;
import tech.pegasys.pantheon.consensus.ibft.IbftBlockHashing;
import tech.pegasys.pantheon.consensus.ibft.IbftExtraData;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.NewRound;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Proposal;
import tech.pegasys.pantheon.consensus.ibft.payload.CommitPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory;
import tech.pegasys.pantheon.consensus.ibft.payload.NewRoundPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate;
import tech.pegasys.pantheon.consensus.ibft.payload.ProposalPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangeCertificate;
import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangePayload;
import tech.pegasys.pantheon.consensus.ibft.payload.SignedData;
Expand All @@ -44,28 +44,36 @@ public static SignedData<CommitPayload> createSignedCommitPayload(

final MessageFactory messageFactory = new MessageFactory(signingKeyPair);

return messageFactory.createSignedCommitPayload(roundId, block.getHash(), commitSeal);
return messageFactory
.createSignedCommitPayload(roundId, block.getHash(), commitSeal)
.getSignedPayload();
}

public static PreparedCertificate createValidPreparedCertificate(
final TestContext context, final ConsensusRoundIdentifier preparedRound, final Block block) {
final RoundSpecificPeers peers = context.roundSpecificPeers(preparedRound);

return new PreparedCertificate(
peers.getProposer().getMessageFactory().createSignedProposalPayload(preparedRound, block),
peers
.getProposer()
.getMessageFactory()
.createSignedProposalPayload(preparedRound, block)
.getSignedPayload(),
peers.createSignedPreparePayloadOfNonProposing(preparedRound, block.getHash()));
}

public static SignedData<NewRoundPayload> injectEmptyNewRound(
public static NewRound injectEmptyNewRound(
final ConsensusRoundIdentifier targetRoundId,
final ValidatorPeer proposer,
final List<SignedData<RoundChangePayload>> roundChangePayloads,
final Block blockToPropose) {

final SignedData<ProposalPayload> proposal =
final Proposal proposal =
proposer.getMessageFactory().createSignedProposalPayload(targetRoundId, blockToPropose);

return proposer.injectNewRound(
targetRoundId, new RoundChangeCertificate(roundChangePayloads), proposal);
targetRoundId,
new RoundChangeCertificate(roundChangePayloads),
proposal.getSignedPayload());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Prepare;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Proposal;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.RoundChange;
import tech.pegasys.pantheon.consensus.ibft.payload.CommitPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory;
import tech.pegasys.pantheon.consensus.ibft.payload.NewRoundPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparePayload;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate;
import tech.pegasys.pantheon.consensus.ibft.payload.ProposalPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangeCertificate;
import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangePayload;
import tech.pegasys.pantheon.consensus.ibft.payload.SignedData;
import tech.pegasys.pantheon.crypto.SECP256K1;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
Expand Down Expand Up @@ -82,58 +78,52 @@ public KeyPair getNodeKeys() {
return nodeKeys;
}

public SignedData<ProposalPayload> injectProposal(
final ConsensusRoundIdentifier rId, final Block block) {
final SignedData<ProposalPayload> payload =
messageFactory.createSignedProposalPayload(rId, block);
public Proposal injectProposal(final ConsensusRoundIdentifier rId, final Block block) {
final Proposal payload = messageFactory.createSignedProposalPayload(rId, block);

injectMessage(ProposalMessageData.create(new Proposal(payload)));
injectMessage(ProposalMessageData.create(payload));
return payload;
}

public SignedData<PreparePayload> injectPrepare(
final ConsensusRoundIdentifier rId, final Hash digest) {
final SignedData<PreparePayload> payload =
messageFactory.createSignedPreparePayload(rId, digest);
injectMessage(PrepareMessageData.create(new Prepare(payload)));
public Prepare injectPrepare(final ConsensusRoundIdentifier rId, final Hash digest) {
final Prepare payload = messageFactory.createSignedPreparePayload(rId, digest);
injectMessage(PrepareMessageData.create(payload));
return payload;
}

public Signature getBlockSignature(final Hash digest) {
return SECP256K1.sign(digest, nodeKeys);
}

public SignedData<CommitPayload> injectCommit(
final ConsensusRoundIdentifier rId, final Hash digest) {
public Commit injectCommit(final ConsensusRoundIdentifier rId, final Hash digest) {
final Signature commitSeal = SECP256K1.sign(digest, nodeKeys);

return injectCommit(rId, digest, commitSeal);
}

public SignedData<CommitPayload> injectCommit(
public Commit injectCommit(
final ConsensusRoundIdentifier rId, final Hash digest, final Signature commitSeal) {
final SignedData<CommitPayload> payload =
messageFactory.createSignedCommitPayload(rId, digest, commitSeal);
injectMessage(CommitMessageData.create(new Commit(payload)));
final Commit payload = messageFactory.createSignedCommitPayload(rId, digest, commitSeal);
injectMessage(CommitMessageData.create(payload));
return payload;
}

public SignedData<NewRoundPayload> injectNewRound(
public NewRound injectNewRound(
final ConsensusRoundIdentifier rId,
final RoundChangeCertificate roundChangeCertificate,
final SignedData<ProposalPayload> proposalPayload) {

final SignedData<NewRoundPayload> payload =
final NewRound payload =
messageFactory.createSignedNewRoundPayload(rId, roundChangeCertificate, proposalPayload);
injectMessage(NewRoundMessageData.create(new NewRound(payload)));
injectMessage(NewRoundMessageData.create(payload));
return payload;
}

public SignedData<RoundChangePayload> injectRoundChange(
public RoundChange injectRoundChange(
final ConsensusRoundIdentifier rId, final Optional<PreparedCertificate> preparedCertificate) {
final SignedData<RoundChangePayload> payload =
final RoundChange payload =
messageFactory.createSignedRoundChangePayload(rId, preparedCertificate);
injectMessage(RoundChangeMessageData.create(new RoundChange(payload)));
injectMessage(RoundChangeMessageData.create(payload));
return payload;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier;
import tech.pegasys.pantheon.consensus.ibft.IbftHelpers;
import tech.pegasys.pantheon.consensus.ibft.ibftevent.NewChainHead;
import tech.pegasys.pantheon.consensus.ibft.payload.CommitPayload;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Commit;
import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Prepare;
import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparePayload;
import tech.pegasys.pantheon.consensus.ibft.payload.SignedData;
import tech.pegasys.pantheon.consensus.ibft.support.RoundSpecificPeers;
import tech.pegasys.pantheon.consensus.ibft.support.TestContext;
import tech.pegasys.pantheon.consensus.ibft.support.TestContextBuilder;
Expand Down Expand Up @@ -89,13 +88,16 @@ public void messagesForFutureHeightAreBufferedUntilChainHeightCatchesUp() {
.getController()
.handleNewBlockEvent(new NewChainHead(signedCurrentHeightBlock.getHeader()));

final SignedData<PreparePayload> expectedPrepareMessage =
final Prepare expectedPrepareMessage =
localNodeMessageFactory.createSignedPreparePayload(
futureHeightRoundId, futureHeightBlock.getHash());

final SignedData<CommitPayload> expectedCommitMessage =
createSignedCommitPayload(
futureHeightRoundId, futureHeightBlock, context.getLocalNodeParams().getNodeKeyPair());
final Commit expectedCommitMessage =
new Commit(
createSignedCommitPayload(
futureHeightRoundId,
futureHeightBlock,
context.getLocalNodeParams().getNodeKeyPair()));

peers.verifyMessagesReceived(expectedPrepareMessage, expectedCommitMessage);
assertThat(context.getCurrentChainHeight()).isEqualTo(2);
Expand All @@ -110,7 +112,7 @@ public void messagesFromPreviousHeightAreDiscarded() {
peers.getProposer().injectProposal(roundId, currentHeightBlock);
peers.getNonProposing(0).injectPrepare(roundId, currentHeightBlock.getHash());

final SignedData<PreparePayload> expectedPrepareMessage =
final Prepare expectedPrepareMessage =
localNodeMessageFactory.createSignedPreparePayload(roundId, currentHeightBlock.getHash());

peers.verifyMessagesReceived(expectedPrepareMessage);
Expand Down Expand Up @@ -148,9 +150,10 @@ public void multipleNewChainHeadEventsDoesNotRestartCurrentHeightManager() {
// Should only require 1 more prepare to close it out
peers.getNonProposing(1).injectPrepare(roundId, currentHeightBlock.getHash());

final SignedData<CommitPayload> expectedCommitMessage =
createSignedCommitPayload(
roundId, currentHeightBlock, context.getLocalNodeParams().getNodeKeyPair());
final Commit expectedCommitMessage =
new Commit(
createSignedCommitPayload(
roundId, currentHeightBlock, context.getLocalNodeParams().getNodeKeyPair()));
peers.verifyMessagesReceived(expectedCommitMessage);
}

Expand Down Expand Up @@ -185,7 +188,7 @@ public void correctMessagesAreExtractedFromFutureHeightBuffer() {
peers.verifyNoMessagesReceived();
peers.getProposer().injectProposal(nextHeightRoundId, nextHeightBlock);

final SignedData<PreparePayload> expectedPrepareMessage =
final Prepare expectedPrepareMessage =
localNodeMessageFactory.createSignedPreparePayload(
nextHeightRoundId, nextHeightBlock.getHash());

Expand All @@ -202,13 +205,16 @@ public void correctMessagesAreExtractedFromFutureHeightBuffer() {
.getController()
.handleNewBlockEvent(new NewChainHead(signedNextHeightBlock.getHeader()));

final SignedData<PreparePayload> expectedFuturePrepareMessage =
final Prepare expectedFuturePrepareMessage =
localNodeMessageFactory.createSignedPreparePayload(
futureHeightRoundId, futureHeightBlock.getHash());

final SignedData<CommitPayload> expectedCommitMessage =
createSignedCommitPayload(
futureHeightRoundId, futureHeightBlock, context.getLocalNodeParams().getNodeKeyPair());
final Commit expectedCommitMessage =
new Commit(
createSignedCommitPayload(
futureHeightRoundId,
futureHeightBlock,
context.getLocalNodeParams().getNodeKeyPair()));

// Assert ONLY a prepare message was received, not any commits (i.e. futureHeightRoundId
// messages have not been used.
Expand Down
Loading

0 comments on commit 6f04f02

Please sign in to comment.