From e388de7b4edd4bac78450bd06a35f552d08bb039 Mon Sep 17 00:00:00 2001 From: tmohay <37158202+rain-on@users.noreply.github.com> Date: Fri, 1 Feb 2019 16:48:35 +1100 Subject: [PATCH] Updated IbftRound and RoudnState APIs to use wrapped messages (#740) Signed-off-by: Adrian Sutton --- .../statemachine/IbftBlockHeightManager.java | 11 +- .../ibft/statemachine/IbftRound.java | 62 ++++----- .../ibft/statemachine/RoundState.java | 41 +++--- .../ibft/statemachine/IbftRoundTest.java | 40 +++--- .../ibft/statemachine/RoundStateTest.java | 120 ++++++++++-------- 5 files changed, 153 insertions(+), 121 deletions(-) diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftBlockHeightManager.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftBlockHeightManager.java index 2586df2b31..247d273516 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftBlockHeightManager.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftBlockHeightManager.java @@ -31,7 +31,6 @@ import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory; import tech.pegasys.pantheon.consensus.ibft.payload.Payload; import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate; import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangeCertificate; -import tech.pegasys.pantheon.consensus.ibft.payload.SignedData; import tech.pegasys.pantheon.consensus.ibft.validation.MessageValidatorFactory; import tech.pegasys.pantheon.consensus.ibft.validation.NewRoundMessageValidator; import tech.pegasys.pantheon.ethereum.core.BlockHeader; @@ -177,18 +176,18 @@ public class IbftBlockHeightManager implements BlockHeightManager { private

> void actionOrBufferMessage( final M ibftMessage, - final Consumer> inRoundHandler, - final BiConsumer> buffer) { + final Consumer inRoundHandler, + final BiConsumer buffer) { final MessageAge messageAge = determineAgeOfPayload(ibftMessage.getRoundIdentifier().getRoundNumber()); if (messageAge == CURRENT_ROUND) { - inRoundHandler.accept(ibftMessage.getSignedPayload()); + inRoundHandler.accept(ibftMessage); } else if (messageAge == FUTURE_ROUND) { final ConsensusRoundIdentifier msgRoundId = ibftMessage.getRoundIdentifier(); final RoundState roundstate = futureRoundStateBuffer.computeIfAbsent( msgRoundId.getRoundNumber(), k -> roundStateCreator.apply(msgRoundId)); - buffer.accept(roundstate, ibftMessage.getSignedPayload()); + buffer.accept(roundstate, ibftMessage); } } @@ -248,7 +247,7 @@ public class IbftBlockHeightManager implements BlockHeightManager { if (messageAge == FUTURE_ROUND) { startNewRound(newRound.getRoundIdentifier().getRoundNumber()); } - currentRound.handleProposalFromNewRound(newRound.getSignedPayload()); + currentRound.handleProposalFromNewRound(newRound); } } diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRound.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRound.java index e8da16c704..2dc31bda06 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRound.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRound.java @@ -20,15 +20,14 @@ import tech.pegasys.pantheon.consensus.ibft.IbftContext; import tech.pegasys.pantheon.consensus.ibft.IbftExtraData; import tech.pegasys.pantheon.consensus.ibft.IbftHelpers; import tech.pegasys.pantheon.consensus.ibft.blockcreation.IbftBlockCreator; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Commit; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.NewRound; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Prepare; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Proposal; import tech.pegasys.pantheon.consensus.ibft.network.IbftMessageTransmitter; -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.SignedData; import tech.pegasys.pantheon.crypto.SECP256K1; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.crypto.SECP256K1.Signature; @@ -94,7 +93,8 @@ public class IbftRound { transmitter.multicastProposal(roundState.getRoundIdentifier(), block); updateStateWithProposedBlock( - messageFactory.createSignedProposalPayload(roundState.getRoundIdentifier(), block)); + new Proposal( + messageFactory.createSignedProposalPayload(roundState.getRoundIdentifier(), block))); } public void startRoundWith( @@ -102,22 +102,24 @@ public class IbftRound { final Optional latestCertificate = findLatestPreparedCertificate(roundChangeCertificate.getRoundChangePayloads()); - SignedData proposal; + Proposal proposal; if (!latestCertificate.isPresent()) { LOG.trace("Multicasting NewRound with new block. round={}", roundState.getRoundIdentifier()); final Block block = blockCreator.createBlock(headerTimestamp); - proposal = messageFactory.createSignedProposalPayload(getRoundIdentifier(), block); + proposal = + new Proposal(messageFactory.createSignedProposalPayload(getRoundIdentifier(), block)); } else { LOG.trace( "Multicasting NewRound from PreparedCertificate. round={}", roundState.getRoundIdentifier()); proposal = createProposalFromPreparedCertificate(latestCertificate.get()); } - transmitter.multicastNewRound(getRoundIdentifier(), roundChangeCertificate, proposal); + transmitter.multicastNewRound( + getRoundIdentifier(), roundChangeCertificate, proposal.getSignedPayload()); updateStateWithProposedBlock(proposal); } - private SignedData createProposalFromPreparedCertificate( + private Proposal createProposalFromPreparedCertificate( final PreparedCertificate preparedCertificate) { final Block block = preparedCertificate.getProposalPayload().getPayload().getBlock(); @@ -143,10 +145,10 @@ public class IbftRound { "Created proposal from prepared certificate blockHeader={} extraData={}", block.getHeader(), extraDataToPublish); - return messageFactory.createSignedProposalPayload(getRoundIdentifier(), newBlock); + return new Proposal(messageFactory.createSignedProposalPayload(getRoundIdentifier(), newBlock)); } - public void handleProposalMessage(final SignedData msg) { + public void handleProposalMessage(final Proposal msg) { LOG.info("Handling a Proposal message."); if (getRoundIdentifier().getRoundNumber() != 0) { @@ -156,35 +158,36 @@ public class IbftRound { actionReceivedProposal(msg); } - public void handleProposalFromNewRound(final SignedData msg) { + public void handleProposalFromNewRound(final NewRound msg) { LOG.info("Handling a New Round Proposal."); if (getRoundIdentifier().getRoundNumber() == 0) { LOG.error("Illegally received a NewRound message when in Round 0."); return; } - actionReceivedProposal(msg.getPayload().getProposalPayload()); + actionReceivedProposal(new Proposal(msg.getSignedPayload().getPayload().getProposalPayload())); } - private void actionReceivedProposal(final SignedData msg) { - final Block block = msg.getPayload().getBlock(); + private void actionReceivedProposal(final Proposal msg) { + final Block block = msg.getSignedPayload().getPayload().getBlock(); if (updateStateWithProposedBlock(msg)) { LOG.info("Sending prepare message."); transmitter.multicastPrepare(getRoundIdentifier(), block.getHash()); - final SignedData localPrepareMessage = - messageFactory.createSignedPreparePayload( - roundState.getRoundIdentifier(), block.getHash()); + final Prepare localPrepareMessage = + new Prepare( + messageFactory.createSignedPreparePayload( + roundState.getRoundIdentifier(), block.getHash())); peerIsPrepared(localPrepareMessage); } } - public void handlePrepareMessage(final SignedData msg) { + public void handlePrepareMessage(final Prepare msg) { LOG.debug("Received a prepare message. round={}", roundState.getRoundIdentifier()); peerIsPrepared(msg); } - public void handleCommitMessage(final SignedData msg) { + public void handleCommitMessage(final Commit msg) { LOG.debug("Received a commit message. round={}", roundState.getRoundIdentifier()); peerIsCommitted(msg); } @@ -193,7 +196,7 @@ public class IbftRound { return roundState.constructPreparedCertificate(); } - private boolean updateStateWithProposedBlock(final SignedData msg) { + private boolean updateStateWithProposedBlock(final Proposal msg) { final boolean wasPrepared = roundState.isPrepared(); final boolean wasCommitted = roundState.isCommitted(); final boolean blockAccepted = roundState.setProposedBlock(msg); @@ -208,18 +211,19 @@ public class IbftRound { importBlockToChain(); } - final SignedData localCommitMessage = - messageFactory.createSignedCommitPayload( - roundState.getRoundIdentifier(), - msg.getPayload().getBlock().getHash(), - createCommitSeal(roundState.getProposedBlock().get())); + final Commit localCommitMessage = + new Commit( + messageFactory.createSignedCommitPayload( + roundState.getRoundIdentifier(), + msg.getSignedPayload().getPayload().getBlock().getHash(), + createCommitSeal(roundState.getProposedBlock().get()))); peerIsCommitted(localCommitMessage); } return blockAccepted; } - private void peerIsPrepared(final SignedData msg) { + private void peerIsPrepared(final Prepare msg) { final boolean wasPrepared = roundState.isPrepared(); roundState.addPrepareMessage(msg); if (wasPrepared != roundState.isPrepared()) { @@ -229,7 +233,7 @@ public class IbftRound { } } - private void peerIsCommitted(final SignedData msg) { + private void peerIsCommitted(final Commit msg) { final boolean wasCommitted = roundState.isCommitted(); roundState.addCommitMessage(msg); if (wasCommitted != roundState.isCommitted()) { diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundState.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundState.java index e63a62a01e..655182a917 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundState.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundState.java @@ -15,11 +15,10 @@ package tech.pegasys.pantheon.consensus.ibft.statemachine; import static tech.pegasys.pantheon.consensus.ibft.IbftHelpers.prepareMessageCountForQuorum; import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; -import tech.pegasys.pantheon.consensus.ibft.payload.CommitPayload; -import tech.pegasys.pantheon.consensus.ibft.payload.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Commit; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Prepare; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Proposal; import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate; -import tech.pegasys.pantheon.consensus.ibft.payload.ProposalPayload; -import tech.pegasys.pantheon.consensus.ibft.payload.SignedData; import tech.pegasys.pantheon.consensus.ibft.validation.MessageValidator; import tech.pegasys.pantheon.crypto.SECP256K1.Signature; import tech.pegasys.pantheon.ethereum.core.Block; @@ -41,12 +40,12 @@ public class RoundState { private final MessageValidator validator; private final long quorum; - private Optional> proposalMessage = Optional.empty(); + private Optional proposalMessage = Optional.empty(); // Must track the actual Prepare message, not just the sender, as these may need to be reused // to send out in a PrepareCertificate. - private final Set> preparePayloads = Sets.newLinkedHashSet(); - private final Set> commitPayloads = Sets.newLinkedHashSet(); + private final Set preparePayloads = Sets.newLinkedHashSet(); + private final Set commitPayloads = Sets.newLinkedHashSet(); private boolean prepared = false; private boolean committed = false; @@ -64,13 +63,13 @@ public class RoundState { return roundIdentifier; } - public boolean setProposedBlock(final SignedData msg) { + public boolean setProposedBlock(final Proposal msg) { if (!proposalMessage.isPresent()) { - if (validator.addSignedProposalPayload(msg)) { + if (validator.addSignedProposalPayload(msg.getSignedPayload())) { proposalMessage = Optional.of(msg); - preparePayloads.removeIf(p -> !validator.validatePrepareMessage(p)); - commitPayloads.removeIf(p -> !validator.validateCommmitMessage(p)); + preparePayloads.removeIf(p -> !validator.validatePrepareMessage(p.getSignedPayload())); + commitPayloads.removeIf(p -> !validator.validateCommmitMessage(p.getSignedPayload())); updateState(); return true; } @@ -79,16 +78,16 @@ public class RoundState { return false; } - public void addPrepareMessage(final SignedData msg) { - if (!proposalMessage.isPresent() || validator.validatePrepareMessage(msg)) { + public void addPrepareMessage(final Prepare msg) { + if (!proposalMessage.isPresent() || validator.validatePrepareMessage(msg.getSignedPayload())) { preparePayloads.add(msg); LOG.debug("Round state added prepare message prepare={}", msg); } updateState(); } - public void addCommitMessage(final SignedData msg) { - if (!proposalMessage.isPresent() || validator.validateCommmitMessage(msg)) { + public void addCommitMessage(final Commit msg) { + if (!proposalMessage.isPresent() || validator.validateCommmitMessage(msg.getSignedPayload())) { commitPayloads.add(msg); LOG.debug("Round state added commit message commit={}", msg); } @@ -111,7 +110,7 @@ public class RoundState { } public Optional getProposedBlock() { - return proposalMessage.map(p -> p.getPayload().getBlock()); + return proposalMessage.map(p -> p.getSignedPayload().getPayload().getBlock()); } public boolean isPrepared() { @@ -125,13 +124,19 @@ public class RoundState { public Collection getCommitSeals() { return commitPayloads .stream() - .map(cp -> cp.getPayload().getCommitSeal()) + .map(cp -> cp.getSignedPayload().getPayload().getCommitSeal()) .collect(Collectors.toList()); } public Optional constructPreparedCertificate() { if (isPrepared()) { - return Optional.of(new PreparedCertificate(proposalMessage.get(), preparePayloads)); + return Optional.of( + new PreparedCertificate( + proposalMessage.get().getSignedPayload(), + preparePayloads + .stream() + .map(Prepare::getSignedPayload) + .collect(Collectors.toList()))); } return Optional.empty(); } diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRoundTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRoundTest.java index 26431c1ba3..216079327a 100644 --- a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRoundTest.java +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRoundTest.java @@ -28,6 +28,9 @@ import tech.pegasys.pantheon.consensus.ibft.IbftBlockHashing; import tech.pegasys.pantheon.consensus.ibft.IbftContext; import tech.pegasys.pantheon.consensus.ibft.IbftExtraData; import tech.pegasys.pantheon.consensus.ibft.blockcreation.IbftBlockCreator; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Commit; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Prepare; +import tech.pegasys.pantheon.consensus.ibft.messagewrappers.Proposal; import tech.pegasys.pantheon.consensus.ibft.network.IbftMessageTransmitter; import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory; import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate; @@ -137,7 +140,7 @@ public class IbftRoundTest { transmitter); round.handleProposalMessage( - messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); + new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock))); verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash()); verify(transmitter, never()).multicastCommit(any(), any(), any()); } @@ -203,7 +206,7 @@ public class IbftRoundTest { // Receive Proposal Message round.handleProposalMessage( - messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); + new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock))); verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash()); verify(transmitter, times(1)) .multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal); @@ -212,8 +215,9 @@ public class IbftRoundTest { // Receive Commit Message round.handleCommitMessage( - messageFactory.createSignedCommitPayload( - roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); + new Commit( + messageFactory.createSignedCommitPayload( + roundIdentifier, proposedBlock.getHash(), remoteCommitSeal))); // Should import block when both commit seals are available. ArgumentCaptor capturedBlock = ArgumentCaptor.forClass(Block.class); @@ -249,15 +253,17 @@ public class IbftRoundTest { verify(blockImporter, never()).importBlock(any(), any(), any()); round.handlePrepareMessage( - messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())); + new Prepare( + messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash()))); verify(transmitter, times(1)) .multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal); verify(blockImporter, never()).importBlock(any(), any(), any()); round.handleCommitMessage( - messageFactory.createSignedCommitPayload( - roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); + new Commit( + messageFactory.createSignedCommitPayload( + roundIdentifier, proposedBlock.getHash(), remoteCommitSeal))); verify(blockImporter, times(1)).importBlock(any(), any(), any()); } @@ -324,7 +330,8 @@ public class IbftRoundTest { // 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( - messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())); + new Prepare( + messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash()))); assertThat(roundState.isPrepared()).isTrue(); } @@ -355,7 +362,8 @@ public class IbftRoundTest { // 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( - messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())); + new Prepare( + messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash()))); assertThat(roundState.isPrepared()).isTrue(); } @@ -393,11 +401,12 @@ public class IbftRoundTest { transmitter); round.handleCommitMessage( - messageFactory.createSignedCommitPayload( - roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); + new Commit( + messageFactory.createSignedCommitPayload( + roundIdentifier, proposedBlock.getHash(), remoteCommitSeal))); round.handleProposalMessage( - messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); + new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock))); verify(blockImporter, times(1)).importBlock(any(), any(), any()); } @@ -418,11 +427,12 @@ public class IbftRoundTest { transmitter); round.handleCommitMessage( - messageFactory.createSignedCommitPayload( - roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); + new Commit( + messageFactory.createSignedCommitPayload( + roundIdentifier, proposedBlock.getHash(), remoteCommitSeal))); round.handleProposalMessage( - messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); + new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock))); verify(blockImporter, times(1)).importBlock(any(), any(), any()); } diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundStateTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundStateTest.java index 234f9a2703..3001c34a67 100644 --- a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundStateTest.java +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundStateTest.java @@ -19,10 +19,11 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; -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.messagewrappers.Proposal; import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory; import tech.pegasys.pantheon.consensus.ibft.payload.PreparePayload; -import tech.pegasys.pantheon.consensus.ibft.payload.ProposalPayload; import tech.pegasys.pantheon.consensus.ibft.payload.SignedData; import tech.pegasys.pantheon.consensus.ibft.validation.MessageValidator; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; @@ -80,8 +81,9 @@ public class RoundStateTest { when(messageValidator.addSignedProposalPayload(any())).thenReturn(false); final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator); - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); assertThat(roundState.setProposedBlock(proposal)).isFalse(); assertThat(roundState.isPrepared()).isFalse(); @@ -94,15 +96,16 @@ public class RoundStateTest { when(messageValidator.addSignedProposalPayload(any())).thenReturn(true); final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator); - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isCommitted()).isFalse(); assertThat(roundState.constructPreparedCertificate()).isNotEmpty(); assertThat(roundState.constructPreparedCertificate().get().getProposalPayload()) - .isEqualTo(proposal); + .isEqualTo(proposal.getSignedPayload()); } @Test @@ -112,20 +115,22 @@ public class RoundStateTest { final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator); - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isCommitted()).isFalse(); - final SignedData commit = - validatorMessageFactories - .get(0) - .createSignedCommitPayload( - roundIdentifier, - block.getHash(), - Signature.create(BigInteger.ONE, BigInteger.ONE, (byte) 1)); + final Commit commit = + new Commit( + validatorMessageFactories + .get(0) + .createSignedCommitPayload( + roundIdentifier, + block.getHash(), + Signature.create(BigInteger.ONE, BigInteger.ONE, (byte) 1))); roundState.addCommitMessage(commit); assertThat(roundState.isPrepared()).isTrue(); @@ -140,15 +145,17 @@ public class RoundStateTest { final RoundState roundState = new RoundState(roundIdentifier, 3, messageValidator); - final SignedData firstPrepare = - validatorMessageFactories - .get(1) - .createSignedPreparePayload(roundIdentifier, block.getHash()); + final Prepare firstPrepare = + new Prepare( + validatorMessageFactories + .get(1) + .createSignedPreparePayload(roundIdentifier, block.getHash())); - final SignedData secondPrepare = - validatorMessageFactories - .get(2) - .createSignedPreparePayload(roundIdentifier, block.getHash()); + final Prepare secondPrepare = + new Prepare( + validatorMessageFactories + .get(2) + .createSignedPreparePayload(roundIdentifier, block.getHash())); roundState.addPrepareMessage(firstPrepare); assertThat(roundState.isPrepared()).isFalse(); @@ -160,8 +167,9 @@ public class RoundStateTest { assertThat(roundState.isCommitted()).isFalse(); assertThat(roundState.constructPreparedCertificate()).isEmpty(); - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isCommitted()).isFalse(); @@ -188,12 +196,13 @@ public class RoundStateTest { when(messageValidator.validatePrepareMessage(firstPrepare)).thenReturn(true); when(messageValidator.validatePrepareMessage(secondPrepare)).thenReturn(false); - roundState.addPrepareMessage(firstPrepare); - roundState.addPrepareMessage(secondPrepare); + roundState.addPrepareMessage(new Prepare(firstPrepare)); + roundState.addPrepareMessage(new Prepare(secondPrepare)); verify(messageValidator, never()).validatePrepareMessage(any()); - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.isPrepared()).isFalse(); @@ -215,8 +224,9 @@ public class RoundStateTest { .get(2) .createSignedPreparePayload(roundIdentifier, block.getHash()); - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); when(messageValidator.addSignedProposalPayload(any())).thenReturn(true); when(messageValidator.validatePrepareMessage(firstPrepare)).thenReturn(false); @@ -225,10 +235,10 @@ public class RoundStateTest { roundState.setProposedBlock(proposal); assertThat(roundState.isPrepared()).isFalse(); - roundState.addPrepareMessage(firstPrepare); + roundState.addPrepareMessage(new Prepare(firstPrepare)); assertThat(roundState.isPrepared()).isFalse(); - roundState.addPrepareMessage(secondPrepare); + roundState.addPrepareMessage(new Prepare(secondPrepare)); assertThat(roundState.isPrepared()).isTrue(); } @@ -239,24 +249,27 @@ public class RoundStateTest { final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator); - final SignedData firstCommit = - validatorMessageFactories - .get(1) - .createSignedCommitPayload( - roundIdentifier, - block.getHash(), - Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 1)); - - final SignedData secondCommit = - validatorMessageFactories - .get(2) - .createSignedCommitPayload( - roundIdentifier, - block.getHash(), - Signature.create(BigInteger.TEN, BigInteger.TEN, (byte) 1)); - - final SignedData proposal = - validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); + final Commit firstCommit = + new Commit( + validatorMessageFactories + .get(1) + .createSignedCommitPayload( + roundIdentifier, + block.getHash(), + Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 1))); + + final Commit secondCommit = + new Commit( + validatorMessageFactories + .get(2) + .createSignedCommitPayload( + roundIdentifier, + block.getHash(), + Signature.create(BigInteger.TEN, BigInteger.TEN, (byte) 1))); + + final Proposal proposal = + new Proposal( + validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block)); roundState.setProposedBlock(proposal); roundState.addCommitMessage(firstCommit); @@ -266,6 +279,7 @@ public class RoundStateTest { assertThat(roundState.getCommitSeals()) .containsOnly( - firstCommit.getPayload().getCommitSeal(), secondCommit.getPayload().getCommitSeal()); + firstCommit.getSignedPayload().getPayload().getCommitSeal(), + secondCommit.getSignedPayload().getPayload().getCommitSeal()); } }