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

Commit

Permalink
Added tests for subsequent preprepare
Browse files Browse the repository at this point in the history
  • Loading branch information
tmohay committed Nov 26, 2018
1 parent 2c8c59f commit 722131e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import tech.pegasys.pantheon.ethereum.rlp.RLPInput;
import tech.pegasys.pantheon.ethereum.rlp.RLPOutput;

import java.util.Objects;

// NOTE: Implementation of all methods of this class is still pending. This class was added to show
// how a PreparedCertificate is encoded and decoded inside a RoundChange message
public class IbftUnsignedPrePrepareMessageData extends AbstractIbftUnsignedInRoundMessageData {
Expand Down Expand Up @@ -60,4 +62,22 @@ public Block getBlock() {
public int getMessageType() {
return TYPE;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IbftUnsignedPrePrepareMessageData that = (IbftUnsignedPrePrepareMessageData) o;
return Objects.equals(block, that.block)
&& Objects.equals(roundIdentifier, that.roundIdentifier);
}

@Override
public int hashCode() {
return Objects.hash(block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public MessageValidator(
public boolean addPreprepareMessage(
final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> msg) {

if (preprepareMessage.isPresent()) {
return handleSubsequentPreprepareMessage(msg);
}

if (!msg.getUnsignedMessageData().getRoundIdentifier().equals(roundIdentifier)) {
LOG.info("Invalid Preprepare message, does not match current round.");
return false;
Expand Down Expand Up @@ -95,6 +99,26 @@ public boolean addPreprepareMessage(
return true;
}

private boolean handleSubsequentPreprepareMessage(
final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> msg) {
IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> existingMsg = preprepareMessage.get();

if (!existingMsg.getSender().equals(msg.getSender())) {
LOG.debug("Received subsequent invalid Preprepare message; sender differs from original.");
return false;
}

final IbftUnsignedPrePrepareMessageData existingData = existingMsg.getUnsignedMessageData();
final IbftUnsignedPrePrepareMessageData newData = msg.getUnsignedMessageData();

if (!existingData.equals(newData)) {
LOG.debug("Received subsequent invalid Preprepare message; content differs from original.");
return false;
}

return true;
}

public boolean validatePrepareMessage(
final IbftSignedMessageData<IbftUnsignedPrepareMessageData> msg) {
final String msgType = "Prepare";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,44 @@ public void commitMessageContainingValidSealFromValidatorIsSuccessful() {
assertThat(validator.validateCommmitMessage(proposerCommitMsg)).isTrue();
assertThat(validator.validateCommmitMessage(validatorCommitMsg)).isTrue();
}

@Test
public void subsequentPreprepareHasDifferentSenderFails() {
when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true);

final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> preprepareMsg =
proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block);
assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue();

final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> secondPreprepareMsg =
validatorMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block);
assertThat(validator.addPreprepareMessage(secondPreprepareMsg)).isFalse();
}

@Test
public void subsequentPreprepareHasDifferentContentFails() {
when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true);

final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> preprepareMsg =
proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block);
assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue();

final ConsensusRoundIdentifier newRoundIdentifier = new ConsensusRoundIdentifier(3, 0);
final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> secondPreprepareMsg =
proposerMessageFactory.createIbftSignedPrePrepareMessageData(newRoundIdentifier, block);
assertThat(validator.addPreprepareMessage(secondPreprepareMsg)).isFalse();
}

@Test
public void subsequentPreprepareHasIdenticalSenderAndContentIsSuccessful() {
when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true);

final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> preprepareMsg =
proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block);
assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue();

final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> secondPreprepareMsg =
proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block);
assertThat(validator.addPreprepareMessage(secondPreprepareMsg)).isTrue();
}
}

0 comments on commit 722131e

Please sign in to comment.