@ -20,11 +20,11 @@ import static org.mockito.Mockito.when;
import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier ;
import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier ;
import tech.pegasys.pantheon.consensus.ibft.IbftContext ;
import tech.pegasys.pantheon.consensus.ibft.IbftContext ;
import tech.pegasys.pantheon.consensus.ibft.IbftExtraData ;
import tech.pegasys.pantheon.consensus.ibft.IbftExtraData ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftMessageFactory ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.CommitPayload ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.MessageFactory ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedCommitMessageData ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrePrepareMessageData ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrepareMessage Data ;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.Signed Data ;
import tech.pegasys.pantheon.crypto.SECP256K1 ;
import tech.pegasys.pantheon.crypto.SECP256K1 ;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair ;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair ;
import tech.pegasys.pantheon.ethereum.ProtocolContext ;
import tech.pegasys.pantheon.ethereum.ProtocolContext ;
@ -55,10 +55,9 @@ public class MessageValidatorTest {
private final KeyPair proposerKey = KeyPair . generate ( ) ;
private final KeyPair proposerKey = KeyPair . generate ( ) ;
private final KeyPair validatorKey = KeyPair . generate ( ) ;
private final KeyPair validatorKey = KeyPair . generate ( ) ;
private final KeyPair nonValidatorKey = KeyPair . generate ( ) ;
private final KeyPair nonValidatorKey = KeyPair . generate ( ) ;
private final IbftMessageFactory proposerMessageFactory = new IbftMessageFactory ( proposerKey ) ;
private final MessageFactory proposerMessageFactory = new MessageFactory ( proposerKey ) ;
private final IbftMessageFactory validatorMessageFactory = new IbftMessageFactory ( validatorKey ) ;
private final MessageFactory validatorMessageFactory = new MessageFactory ( validatorKey ) ;
private final IbftMessageFactory nonValidatorMessageFactory =
private final MessageFactory nonValidatorMessageFactory = new MessageFactory ( nonValidatorKey ) ;
new IbftMessageFactory ( nonValidatorKey ) ;
private final List < Address > validators = Lists . newArrayList ( ) ;
private final List < Address > validators = Lists . newArrayList ( ) ;
@ -107,16 +106,16 @@ public class MessageValidatorTest {
@Test
@Test
public void receivingAPrepareMessageBeforePrePrepareFails ( ) {
public void receivingAPrepareMessageBeforePrePrepareFails ( ) {
final Ibft SignedMessage Data< IbftUnsignedPrepareMessageData > prepareMsg =
final SignedData < PreparePayload > prepareMsg =
proposerMessageFactory . createIbftSignedPrepareMessageData ( roundIdentifier , Hash . ZERO ) ;
proposerMessageFactory . createSignedPreparePayload ( roundIdentifier , Hash . ZERO ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void receivingACommitMessageBeforePreprepareFails ( ) {
public void receivingACommitMessageBeforePreprepareFails ( ) {
final Ibft SignedMessage Data< IbftUnsignedCommitMessageData > commitMsg =
final SignedData < CommitPayload > commitMsg =
proposerMessageFactory . createIbftSignedCommitMessageData (
proposerMessageFactory . createSignedCommitPayload (
roundIdentifier , Hash . ZERO , SECP256K1 . sign ( Hash . ZERO , proposerKey ) ) ;
roundIdentifier , Hash . ZERO , SECP256K1 . sign ( Hash . ZERO , proposerKey ) ) ;
assertThat ( validator . validateCommmitMessage ( commitMsg ) ) . isFalse ( ) ;
assertThat ( validator . validateCommmitMessage ( commitMsg ) ) . isFalse ( ) ;
@ -124,48 +123,46 @@ public class MessageValidatorTest {
@Test
@Test
public void receivingPreprepareMessageFromNonProposerFails ( ) {
public void receivingPreprepareMessageFromNonProposerFails ( ) {
final IbftSignedMessageData < IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
validatorMessageFactory . createIbftSignedPrePrepareMessageData (
validatorMessageFactory . createSignedProposalPayload ( roundIdentifier , mock ( Block . class ) ) ;
roundIdentifier , mock ( Block . class ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void receivingPreprepareMessageWithIllegalBlockFails ( ) {
public void receivingPreprepareMessageWithIllegalBlockFails ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( false ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( false ) ;
final IbftSignedMessageData < IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData (
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , mock ( Block . class ) ) ;
roundIdentifier , mock ( Block . class ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void receivingPrepareFromProposerFails ( ) {
public void receivingPrepareFromProposerFails ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
final Ibft SignedMessage Data< IbftUnsignedPrepareMessageData > prepareMsg =
final SignedData < PreparePayload > prepareMsg =
proposerMessageFactory . createIbftSignedPrepareMessageData ( roundIdentifier , block . getHash ( ) ) ;
proposerMessageFactory . createSignedPreparePayload ( roundIdentifier , block . getHash ( ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void receivingPrepareFromNonValidatorFails ( ) {
public void receivingPrepareFromNonValidatorFails ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final IbftSignedMessageData < IbftUnsignedPrePrepareMessageData > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
final IbftSignedMessageData < IbftUnsignedPrepareMessageData > prepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
nonValidatorMessageFactory . createIbftSignedPrepareMessageData (
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
roundIdentifier , block . getHash ( ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
final SignedData < PreparePayload > prepareMsg =
nonValidatorMessageFactory . createSignedPreparePayload ( roundIdentifier , block . getHash ( ) ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
}
}
@ -173,20 +170,19 @@ public class MessageValidatorTest {
public void receivingMessagesWithDifferentRoundIdFromPreprepareFails ( ) {
public void receivingMessagesWithDifferentRoundIdFromPreprepareFails ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
final ConsensusRoundIdentifier invalidRoundIdentifier =
final ConsensusRoundIdentifier invalidRoundIdentifier =
new ConsensusRoundIdentifier (
new ConsensusRoundIdentifier (
roundIdentifier . getSequenceNumber ( ) , roundIdentifier . getRoundNumber ( ) + 1 ) ;
roundIdentifier . getSequenceNumber ( ) , roundIdentifier . getRoundNumber ( ) + 1 ) ;
final IbftSignedMessageData < IbftUnsignedPrepareMessageData > prepareMsg =
final SignedData < PreparePayload > prepareMsg =
validatorMessageFactory . createIbftSignedPrepareMessageData (
validatorMessageFactory . createSignedPreparePayload ( invalidRoundIdentifier , block . getHash ( ) ) ;
invalidRoundIdentifier , block . getHash ( ) ) ;
final SignedData < CommitPayload > commitMsg =
final IbftSignedMessageData < IbftUnsignedCommitMessageData > commitMsg =
validatorMessageFactory . createSignedCommitPayload (
validatorMessageFactory . createIbftSignedCommitMessageData (
invalidRoundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , proposerKey ) ) ;
invalidRoundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , proposerKey ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . validateCommmitMessage ( commitMsg ) ) . isFalse ( ) ;
assertThat ( validator . validateCommmitMessage ( commitMsg ) ) . isFalse ( ) ;
}
}
@ -195,13 +191,12 @@ public class MessageValidatorTest {
public void receivingPrepareNonProposerValidatorWithCorrectRoundIsSuccessful ( ) {
public void receivingPrepareNonProposerValidatorWithCorrectRoundIsSuccessful ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final IbftSignedMessageData < IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
final IbftSignedMessageData < IbftUnsignedPrepareMessageData > prepareMsg =
final SignedData < PreparePayload > prepareMsg =
validatorMessageFactory . createIbftSignedPrepareMessageData (
validatorMessageFactory . createSignedPreparePayload ( roundIdentifier , block . getHash ( ) ) ;
roundIdentifier , block . getHash ( ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validatePrepareMessage ( prepareMsg ) ) . isTrue ( ) ;
}
}
@ -209,14 +204,14 @@ public class MessageValidatorTest {
public void receivingACommitMessageWithAnInvalidCommitSealFails ( ) {
public void receivingACommitMessageWithAnInvalidCommitSealFails ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
final Ibft SignedMessage Data< IbftUnsignedCommitMessageData > commitMsg =
final SignedData < CommitPayload > commitMsg =
proposerMessageFactory . createIbftSignedCommitMessageData (
proposerMessageFactory . createSignedCommitPayload (
roundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , nonValidatorKey ) ) ;
roundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , nonValidatorKey ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validateCommmitMessage ( commitMsg ) ) . isFalse ( ) ;
assertThat ( validator . validateCommmitMessage ( commitMsg ) ) . isFalse ( ) ;
}
}
@ -224,18 +219,18 @@ public class MessageValidatorTest {
public void commitMessageContainingValidSealFromValidatorIsSuccessful ( ) {
public void commitMessageContainingValidSealFromValidatorIsSuccessful ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
final Ibft SignedMessage Data< IbftUnsignedCommitMessageData > proposerCommitMsg =
final SignedData < CommitPayload > proposerCommitMsg =
proposerMessageFactory . createIbftSignedCommitMessageData (
proposerMessageFactory . createSignedCommitPayload (
roundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , proposerKey ) ) ;
roundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , proposerKey ) ) ;
final Ibft SignedMessage Data< IbftUnsignedCommitMessageData > validatorCommitMsg =
final SignedData < CommitPayload > validatorCommitMsg =
validatorMessageFactory . createIbftSignedCommitMessageData (
validatorMessageFactory . createSignedCommitPayload (
roundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , validatorKey ) ) ;
roundIdentifier , block . getHash ( ) , SECP256K1 . sign ( block . getHash ( ) , validatorKey ) ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . validateCommmitMessage ( proposerCommitMsg ) ) . isTrue ( ) ;
assertThat ( validator . validateCommmitMessage ( proposerCommitMsg ) ) . isTrue ( ) ;
assertThat ( validator . validateCommmitMessage ( validatorCommitMsg ) ) . isTrue ( ) ;
assertThat ( validator . validateCommmitMessage ( validatorCommitMsg ) ) . isTrue ( ) ;
}
}
@ -244,39 +239,39 @@ public class MessageValidatorTest {
public void subsequentPreprepareHasDifferentSenderFails ( ) {
public void subsequentPreprepareHasDifferentSenderFails ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > secondPreprepareMsg =
final SignedData < ProposalPayload > secondPreprepareMsg =
validatorMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
validatorMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( secondPreprepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . addSignedProposalPayload ( secondPreprepareMsg ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void subsequentPreprepareHasDifferentContentFails ( ) {
public void subsequentPreprepareHasDifferentContentFails ( ) {
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
final ConsensusRoundIdentifier newRoundIdentifier = new ConsensusRoundIdentifier ( 3 , 0 ) ;
final ConsensusRoundIdentifier newRoundIdentifier = new ConsensusRoundIdentifier ( 3 , 0 ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > secondPreprepareMsg =
final SignedData < ProposalPayload > secondPreprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( newRoundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( newRoundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( secondPreprepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . addSignedProposalPayload ( secondPreprepareMsg ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void subsequentPreprepareHasIdenticalSenderAndContentIsSuccessful ( ) {
public void subsequentPreprepareHasIdenticalSenderAndContentIsSuccessful ( ) {
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isTrue ( ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > secondPreprepareMsg =
final SignedData < ProposalPayload > secondPreprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( secondPreprepareMsg ) ) . isTrue ( ) ;
assertThat ( validator . addSignedProposalPayload ( secondPreprepareMsg ) ) . isTrue ( ) ;
}
}
@Test
@Test
@ -284,9 +279,9 @@ public class MessageValidatorTest {
insertRoundToBlockHeader ( roundIdentifier . getRoundNumber ( ) + 1 ) ;
insertRoundToBlockHeader ( roundIdentifier . getRoundNumber ( ) + 1 ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
when ( headerValidator . validateHeader ( any ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( true ) ;
final Ibft SignedMessage Data< IbftUnsignedPrePrepareMessageData > preprepareMsg =
final SignedData < ProposalPayload > preprepareMsg =
proposerMessageFactory . createIbftSignedPrePrepareMessageData ( roundIdentifier , block ) ;
proposerMessageFactory . createSignedProposalPayload ( roundIdentifier , block ) ;
assertThat ( validator . addPreprepareMessage ( preprepareMsg ) ) . isFalse ( ) ;
assertThat ( validator . addSignedProposalPayload ( preprepareMsg ) ) . isFalse ( ) ;
}
}
}
}