Updated IbftRound and RoudnState APIs to use wrapped messages (#740)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
tmohay 6 years ago committed by GitHub
parent bdb5e8367f
commit e388de7b4e
  1. 11
      consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftBlockHeightManager.java
  2. 56
      consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRound.java
  3. 41
      consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundState.java
  4. 32
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRoundTest.java
  5. 78
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/RoundStateTest.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.Payload;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate; import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate;
import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangeCertificate; 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.MessageValidatorFactory;
import tech.pegasys.pantheon.consensus.ibft.validation.NewRoundMessageValidator; import tech.pegasys.pantheon.consensus.ibft.validation.NewRoundMessageValidator;
import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeader;
@ -177,18 +176,18 @@ public class IbftBlockHeightManager implements BlockHeightManager {
private <P extends Payload, M extends IbftMessage<P>> void actionOrBufferMessage( private <P extends Payload, M extends IbftMessage<P>> void actionOrBufferMessage(
final M ibftMessage, final M ibftMessage,
final Consumer<SignedData<P>> inRoundHandler, final Consumer<M> inRoundHandler,
final BiConsumer<RoundState, SignedData<P>> buffer) { final BiConsumer<RoundState, M> buffer) {
final MessageAge messageAge = final MessageAge messageAge =
determineAgeOfPayload(ibftMessage.getRoundIdentifier().getRoundNumber()); determineAgeOfPayload(ibftMessage.getRoundIdentifier().getRoundNumber());
if (messageAge == CURRENT_ROUND) { if (messageAge == CURRENT_ROUND) {
inRoundHandler.accept(ibftMessage.getSignedPayload()); inRoundHandler.accept(ibftMessage);
} else if (messageAge == FUTURE_ROUND) { } else if (messageAge == FUTURE_ROUND) {
final ConsensusRoundIdentifier msgRoundId = ibftMessage.getRoundIdentifier(); final ConsensusRoundIdentifier msgRoundId = ibftMessage.getRoundIdentifier();
final RoundState roundstate = final RoundState roundstate =
futureRoundStateBuffer.computeIfAbsent( futureRoundStateBuffer.computeIfAbsent(
msgRoundId.getRoundNumber(), k -> roundStateCreator.apply(msgRoundId)); 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) { if (messageAge == FUTURE_ROUND) {
startNewRound(newRound.getRoundIdentifier().getRoundNumber()); startNewRound(newRound.getRoundIdentifier().getRoundNumber());
} }
currentRound.handleProposalFromNewRound(newRound.getSignedPayload()); currentRound.handleProposalFromNewRound(newRound);
} }
} }

@ -20,15 +20,14 @@ 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.IbftHelpers; import tech.pegasys.pantheon.consensus.ibft.IbftHelpers;
import tech.pegasys.pantheon.consensus.ibft.blockcreation.IbftBlockCreator; 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.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.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.PreparedCertificate;
import tech.pegasys.pantheon.consensus.ibft.payload.ProposalPayload;
import tech.pegasys.pantheon.consensus.ibft.payload.RoundChangeCertificate; 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;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.crypto.SECP256K1.Signature; import tech.pegasys.pantheon.crypto.SECP256K1.Signature;
@ -94,7 +93,8 @@ public class IbftRound {
transmitter.multicastProposal(roundState.getRoundIdentifier(), block); transmitter.multicastProposal(roundState.getRoundIdentifier(), block);
updateStateWithProposedBlock( updateStateWithProposedBlock(
messageFactory.createSignedProposalPayload(roundState.getRoundIdentifier(), block)); new Proposal(
messageFactory.createSignedProposalPayload(roundState.getRoundIdentifier(), block)));
} }
public void startRoundWith( public void startRoundWith(
@ -102,22 +102,24 @@ public class IbftRound {
final Optional<PreparedCertificate> latestCertificate = final Optional<PreparedCertificate> latestCertificate =
findLatestPreparedCertificate(roundChangeCertificate.getRoundChangePayloads()); findLatestPreparedCertificate(roundChangeCertificate.getRoundChangePayloads());
SignedData<ProposalPayload> proposal; Proposal proposal;
if (!latestCertificate.isPresent()) { if (!latestCertificate.isPresent()) {
LOG.trace("Multicasting NewRound with new block. round={}", roundState.getRoundIdentifier()); LOG.trace("Multicasting NewRound with new block. round={}", roundState.getRoundIdentifier());
final Block block = blockCreator.createBlock(headerTimestamp); final Block block = blockCreator.createBlock(headerTimestamp);
proposal = messageFactory.createSignedProposalPayload(getRoundIdentifier(), block); proposal =
new Proposal(messageFactory.createSignedProposalPayload(getRoundIdentifier(), block));
} else { } else {
LOG.trace( LOG.trace(
"Multicasting NewRound from PreparedCertificate. round={}", "Multicasting NewRound from PreparedCertificate. round={}",
roundState.getRoundIdentifier()); roundState.getRoundIdentifier());
proposal = createProposalFromPreparedCertificate(latestCertificate.get()); proposal = createProposalFromPreparedCertificate(latestCertificate.get());
} }
transmitter.multicastNewRound(getRoundIdentifier(), roundChangeCertificate, proposal); transmitter.multicastNewRound(
getRoundIdentifier(), roundChangeCertificate, proposal.getSignedPayload());
updateStateWithProposedBlock(proposal); updateStateWithProposedBlock(proposal);
} }
private SignedData<ProposalPayload> createProposalFromPreparedCertificate( private Proposal createProposalFromPreparedCertificate(
final PreparedCertificate preparedCertificate) { final PreparedCertificate preparedCertificate) {
final Block block = preparedCertificate.getProposalPayload().getPayload().getBlock(); final Block block = preparedCertificate.getProposalPayload().getPayload().getBlock();
@ -143,10 +145,10 @@ public class IbftRound {
"Created proposal from prepared certificate blockHeader={} extraData={}", "Created proposal from prepared certificate blockHeader={} extraData={}",
block.getHeader(), block.getHeader(),
extraDataToPublish); extraDataToPublish);
return messageFactory.createSignedProposalPayload(getRoundIdentifier(), newBlock); return new Proposal(messageFactory.createSignedProposalPayload(getRoundIdentifier(), newBlock));
} }
public void handleProposalMessage(final SignedData<ProposalPayload> msg) { public void handleProposalMessage(final Proposal msg) {
LOG.info("Handling a Proposal message."); LOG.info("Handling a Proposal message.");
if (getRoundIdentifier().getRoundNumber() != 0) { if (getRoundIdentifier().getRoundNumber() != 0) {
@ -156,35 +158,36 @@ public class IbftRound {
actionReceivedProposal(msg); actionReceivedProposal(msg);
} }
public void handleProposalFromNewRound(final SignedData<NewRoundPayload> msg) { public void handleProposalFromNewRound(final NewRound msg) {
LOG.info("Handling a New Round Proposal."); LOG.info("Handling a New Round Proposal.");
if (getRoundIdentifier().getRoundNumber() == 0) { if (getRoundIdentifier().getRoundNumber() == 0) {
LOG.error("Illegally received a NewRound message when in Round 0."); LOG.error("Illegally received a NewRound message when in Round 0.");
return; return;
} }
actionReceivedProposal(msg.getPayload().getProposalPayload()); actionReceivedProposal(new Proposal(msg.getSignedPayload().getPayload().getProposalPayload()));
} }
private void actionReceivedProposal(final SignedData<ProposalPayload> msg) { private void actionReceivedProposal(final Proposal msg) {
final Block block = msg.getPayload().getBlock(); final Block block = msg.getSignedPayload().getPayload().getBlock();
if (updateStateWithProposedBlock(msg)) { if (updateStateWithProposedBlock(msg)) {
LOG.info("Sending prepare message."); LOG.info("Sending prepare message.");
transmitter.multicastPrepare(getRoundIdentifier(), block.getHash()); transmitter.multicastPrepare(getRoundIdentifier(), block.getHash());
final SignedData<PreparePayload> localPrepareMessage = final Prepare localPrepareMessage =
new Prepare(
messageFactory.createSignedPreparePayload( messageFactory.createSignedPreparePayload(
roundState.getRoundIdentifier(), block.getHash()); roundState.getRoundIdentifier(), block.getHash()));
peerIsPrepared(localPrepareMessage); peerIsPrepared(localPrepareMessage);
} }
} }
public void handlePrepareMessage(final SignedData<PreparePayload> msg) { public void handlePrepareMessage(final Prepare msg) {
LOG.debug("Received a prepare message. round={}", roundState.getRoundIdentifier()); LOG.debug("Received a prepare message. round={}", roundState.getRoundIdentifier());
peerIsPrepared(msg); peerIsPrepared(msg);
} }
public void handleCommitMessage(final SignedData<CommitPayload> msg) { public void handleCommitMessage(final Commit msg) {
LOG.debug("Received a commit message. round={}", roundState.getRoundIdentifier()); LOG.debug("Received a commit message. round={}", roundState.getRoundIdentifier());
peerIsCommitted(msg); peerIsCommitted(msg);
} }
@ -193,7 +196,7 @@ public class IbftRound {
return roundState.constructPreparedCertificate(); return roundState.constructPreparedCertificate();
} }
private boolean updateStateWithProposedBlock(final SignedData<ProposalPayload> msg) { private boolean updateStateWithProposedBlock(final Proposal msg) {
final boolean wasPrepared = roundState.isPrepared(); final boolean wasPrepared = roundState.isPrepared();
final boolean wasCommitted = roundState.isCommitted(); final boolean wasCommitted = roundState.isCommitted();
final boolean blockAccepted = roundState.setProposedBlock(msg); final boolean blockAccepted = roundState.setProposedBlock(msg);
@ -208,18 +211,19 @@ public class IbftRound {
importBlockToChain(); importBlockToChain();
} }
final SignedData<CommitPayload> localCommitMessage = final Commit localCommitMessage =
new Commit(
messageFactory.createSignedCommitPayload( messageFactory.createSignedCommitPayload(
roundState.getRoundIdentifier(), roundState.getRoundIdentifier(),
msg.getPayload().getBlock().getHash(), msg.getSignedPayload().getPayload().getBlock().getHash(),
createCommitSeal(roundState.getProposedBlock().get())); createCommitSeal(roundState.getProposedBlock().get())));
peerIsCommitted(localCommitMessage); peerIsCommitted(localCommitMessage);
} }
return blockAccepted; return blockAccepted;
} }
private void peerIsPrepared(final SignedData<PreparePayload> msg) { private void peerIsPrepared(final Prepare msg) {
final boolean wasPrepared = roundState.isPrepared(); final boolean wasPrepared = roundState.isPrepared();
roundState.addPrepareMessage(msg); roundState.addPrepareMessage(msg);
if (wasPrepared != roundState.isPrepared()) { if (wasPrepared != roundState.isPrepared()) {
@ -229,7 +233,7 @@ public class IbftRound {
} }
} }
private void peerIsCommitted(final SignedData<CommitPayload> msg) { private void peerIsCommitted(final Commit msg) {
final boolean wasCommitted = roundState.isCommitted(); final boolean wasCommitted = roundState.isCommitted();
roundState.addCommitMessage(msg); roundState.addCommitMessage(msg);
if (wasCommitted != roundState.isCommitted()) { if (wasCommitted != roundState.isCommitted()) {

@ -15,11 +15,10 @@ package tech.pegasys.pantheon.consensus.ibft.statemachine;
import static tech.pegasys.pantheon.consensus.ibft.IbftHelpers.prepareMessageCountForQuorum; import static tech.pegasys.pantheon.consensus.ibft.IbftHelpers.prepareMessageCountForQuorum;
import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; 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.payload.PreparePayload; 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.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.consensus.ibft.validation.MessageValidator;
import tech.pegasys.pantheon.crypto.SECP256K1.Signature; import tech.pegasys.pantheon.crypto.SECP256K1.Signature;
import tech.pegasys.pantheon.ethereum.core.Block; import tech.pegasys.pantheon.ethereum.core.Block;
@ -41,12 +40,12 @@ public class RoundState {
private final MessageValidator validator; private final MessageValidator validator;
private final long quorum; private final long quorum;
private Optional<SignedData<ProposalPayload>> proposalMessage = Optional.empty(); private Optional<Proposal> proposalMessage = Optional.empty();
// Must track the actual Prepare message, not just the sender, as these may need to be reused // Must track the actual Prepare message, not just the sender, as these may need to be reused
// to send out in a PrepareCertificate. // to send out in a PrepareCertificate.
private final Set<SignedData<PreparePayload>> preparePayloads = Sets.newLinkedHashSet(); private final Set<Prepare> preparePayloads = Sets.newLinkedHashSet();
private final Set<SignedData<CommitPayload>> commitPayloads = Sets.newLinkedHashSet(); private final Set<Commit> commitPayloads = Sets.newLinkedHashSet();
private boolean prepared = false; private boolean prepared = false;
private boolean committed = false; private boolean committed = false;
@ -64,13 +63,13 @@ public class RoundState {
return roundIdentifier; return roundIdentifier;
} }
public boolean setProposedBlock(final SignedData<ProposalPayload> msg) { public boolean setProposedBlock(final Proposal msg) {
if (!proposalMessage.isPresent()) { if (!proposalMessage.isPresent()) {
if (validator.addSignedProposalPayload(msg)) { if (validator.addSignedProposalPayload(msg.getSignedPayload())) {
proposalMessage = Optional.of(msg); proposalMessage = Optional.of(msg);
preparePayloads.removeIf(p -> !validator.validatePrepareMessage(p)); preparePayloads.removeIf(p -> !validator.validatePrepareMessage(p.getSignedPayload()));
commitPayloads.removeIf(p -> !validator.validateCommmitMessage(p)); commitPayloads.removeIf(p -> !validator.validateCommmitMessage(p.getSignedPayload()));
updateState(); updateState();
return true; return true;
} }
@ -79,16 +78,16 @@ public class RoundState {
return false; return false;
} }
public void addPrepareMessage(final SignedData<PreparePayload> msg) { public void addPrepareMessage(final Prepare msg) {
if (!proposalMessage.isPresent() || validator.validatePrepareMessage(msg)) { if (!proposalMessage.isPresent() || validator.validatePrepareMessage(msg.getSignedPayload())) {
preparePayloads.add(msg); preparePayloads.add(msg);
LOG.debug("Round state added prepare message prepare={}", msg); LOG.debug("Round state added prepare message prepare={}", msg);
} }
updateState(); updateState();
} }
public void addCommitMessage(final SignedData<CommitPayload> msg) { public void addCommitMessage(final Commit msg) {
if (!proposalMessage.isPresent() || validator.validateCommmitMessage(msg)) { if (!proposalMessage.isPresent() || validator.validateCommmitMessage(msg.getSignedPayload())) {
commitPayloads.add(msg); commitPayloads.add(msg);
LOG.debug("Round state added commit message commit={}", msg); LOG.debug("Round state added commit message commit={}", msg);
} }
@ -111,7 +110,7 @@ public class RoundState {
} }
public Optional<Block> getProposedBlock() { public Optional<Block> getProposedBlock() {
return proposalMessage.map(p -> p.getPayload().getBlock()); return proposalMessage.map(p -> p.getSignedPayload().getPayload().getBlock());
} }
public boolean isPrepared() { public boolean isPrepared() {
@ -125,13 +124,19 @@ public class RoundState {
public Collection<Signature> getCommitSeals() { public Collection<Signature> getCommitSeals() {
return commitPayloads return commitPayloads
.stream() .stream()
.map(cp -> cp.getPayload().getCommitSeal()) .map(cp -> cp.getSignedPayload().getPayload().getCommitSeal())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public Optional<PreparedCertificate> constructPreparedCertificate() { public Optional<PreparedCertificate> constructPreparedCertificate() {
if (isPrepared()) { 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(); return Optional.empty();
} }

@ -28,6 +28,9 @@ import tech.pegasys.pantheon.consensus.ibft.IbftBlockHashing;
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.blockcreation.IbftBlockCreator; 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.network.IbftMessageTransmitter;
import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory; import tech.pegasys.pantheon.consensus.ibft.payload.MessageFactory;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate; import tech.pegasys.pantheon.consensus.ibft.payload.PreparedCertificate;
@ -137,7 +140,7 @@ public class IbftRoundTest {
transmitter); transmitter);
round.handleProposalMessage( round.handleProposalMessage(
messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)));
verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash()); verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash());
verify(transmitter, never()).multicastCommit(any(), any(), any()); verify(transmitter, never()).multicastCommit(any(), any(), any());
} }
@ -203,7 +206,7 @@ public class IbftRoundTest {
// Receive Proposal Message // Receive Proposal Message
round.handleProposalMessage( round.handleProposalMessage(
messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)));
verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash()); verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash());
verify(transmitter, times(1)) verify(transmitter, times(1))
.multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal); .multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal);
@ -212,8 +215,9 @@ public class IbftRoundTest {
// Receive Commit Message // Receive Commit Message
round.handleCommitMessage( round.handleCommitMessage(
new Commit(
messageFactory.createSignedCommitPayload( messageFactory.createSignedCommitPayload(
roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)));
// Should import block when both commit seals are available. // Should import block when both commit seals are available.
ArgumentCaptor<Block> capturedBlock = ArgumentCaptor.forClass(Block.class); ArgumentCaptor<Block> capturedBlock = ArgumentCaptor.forClass(Block.class);
@ -249,15 +253,17 @@ public class IbftRoundTest {
verify(blockImporter, never()).importBlock(any(), any(), any()); verify(blockImporter, never()).importBlock(any(), any(), any());
round.handlePrepareMessage( round.handlePrepareMessage(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())); new Prepare(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())));
verify(transmitter, times(1)) verify(transmitter, times(1))
.multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal); .multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal);
verify(blockImporter, never()).importBlock(any(), any(), any()); verify(blockImporter, never()).importBlock(any(), any(), any());
round.handleCommitMessage( round.handleCommitMessage(
new Commit(
messageFactory.createSignedCommitPayload( messageFactory.createSignedCommitPayload(
roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)));
verify(blockImporter, times(1)).importBlock(any(), any(), any()); 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 // 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) // indicates the block has entered the roundState (note: all msgs are deemed valid due to mocks)
round.handlePrepareMessage( round.handlePrepareMessage(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())); new Prepare(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())));
assertThat(roundState.isPrepared()).isTrue(); 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 // 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) // indicates the block has entered the roundState (note: all msgs are deemed valid due to mocks)
round.handlePrepareMessage( round.handlePrepareMessage(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())); new Prepare(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash())));
assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isPrepared()).isTrue();
} }
@ -393,11 +401,12 @@ public class IbftRoundTest {
transmitter); transmitter);
round.handleCommitMessage( round.handleCommitMessage(
new Commit(
messageFactory.createSignedCommitPayload( messageFactory.createSignedCommitPayload(
roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)));
round.handleProposalMessage( round.handleProposalMessage(
messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)));
verify(blockImporter, times(1)).importBlock(any(), any(), any()); verify(blockImporter, times(1)).importBlock(any(), any(), any());
} }
@ -418,11 +427,12 @@ public class IbftRoundTest {
transmitter); transmitter);
round.handleCommitMessage( round.handleCommitMessage(
new Commit(
messageFactory.createSignedCommitPayload( messageFactory.createSignedCommitPayload(
roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)); roundIdentifier, proposedBlock.getHash(), remoteCommitSeal)));
round.handleProposalMessage( round.handleProposalMessage(
messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)); new Proposal(messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock)));
verify(blockImporter, times(1)).importBlock(any(), any(), any()); verify(blockImporter, times(1)).importBlock(any(), any(), any());
} }

@ -19,10 +19,11 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; 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.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.MessageFactory;
import tech.pegasys.pantheon.consensus.ibft.payload.PreparePayload; 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.payload.SignedData;
import tech.pegasys.pantheon.consensus.ibft.validation.MessageValidator; import tech.pegasys.pantheon.consensus.ibft.validation.MessageValidator;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
@ -80,8 +81,9 @@ public class RoundStateTest {
when(messageValidator.addSignedProposalPayload(any())).thenReturn(false); when(messageValidator.addSignedProposalPayload(any())).thenReturn(false);
final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator); final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator);
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
assertThat(roundState.setProposedBlock(proposal)).isFalse(); assertThat(roundState.setProposedBlock(proposal)).isFalse();
assertThat(roundState.isPrepared()).isFalse(); assertThat(roundState.isPrepared()).isFalse();
@ -94,15 +96,16 @@ public class RoundStateTest {
when(messageValidator.addSignedProposalPayload(any())).thenReturn(true); when(messageValidator.addSignedProposalPayload(any())).thenReturn(true);
final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator); final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator);
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isPrepared()).isTrue();
assertThat(roundState.isCommitted()).isFalse(); assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedCertificate()).isNotEmpty(); assertThat(roundState.constructPreparedCertificate()).isNotEmpty();
assertThat(roundState.constructPreparedCertificate().get().getProposalPayload()) assertThat(roundState.constructPreparedCertificate().get().getProposalPayload())
.isEqualTo(proposal); .isEqualTo(proposal.getSignedPayload());
} }
@Test @Test
@ -112,20 +115,22 @@ public class RoundStateTest {
final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator); final RoundState roundState = new RoundState(roundIdentifier, 1, messageValidator);
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isPrepared()).isTrue();
assertThat(roundState.isCommitted()).isFalse(); assertThat(roundState.isCommitted()).isFalse();
final SignedData<CommitPayload> commit = final Commit commit =
new Commit(
validatorMessageFactories validatorMessageFactories
.get(0) .get(0)
.createSignedCommitPayload( .createSignedCommitPayload(
roundIdentifier, roundIdentifier,
block.getHash(), block.getHash(),
Signature.create(BigInteger.ONE, BigInteger.ONE, (byte) 1)); Signature.create(BigInteger.ONE, BigInteger.ONE, (byte) 1)));
roundState.addCommitMessage(commit); roundState.addCommitMessage(commit);
assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isPrepared()).isTrue();
@ -140,15 +145,17 @@ public class RoundStateTest {
final RoundState roundState = new RoundState(roundIdentifier, 3, messageValidator); final RoundState roundState = new RoundState(roundIdentifier, 3, messageValidator);
final SignedData<PreparePayload> firstPrepare = final Prepare firstPrepare =
new Prepare(
validatorMessageFactories validatorMessageFactories
.get(1) .get(1)
.createSignedPreparePayload(roundIdentifier, block.getHash()); .createSignedPreparePayload(roundIdentifier, block.getHash()));
final SignedData<PreparePayload> secondPrepare = final Prepare secondPrepare =
new Prepare(
validatorMessageFactories validatorMessageFactories
.get(2) .get(2)
.createSignedPreparePayload(roundIdentifier, block.getHash()); .createSignedPreparePayload(roundIdentifier, block.getHash()));
roundState.addPrepareMessage(firstPrepare); roundState.addPrepareMessage(firstPrepare);
assertThat(roundState.isPrepared()).isFalse(); assertThat(roundState.isPrepared()).isFalse();
@ -160,8 +167,9 @@ public class RoundStateTest {
assertThat(roundState.isCommitted()).isFalse(); assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedCertificate()).isEmpty(); assertThat(roundState.constructPreparedCertificate()).isEmpty();
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isPrepared()).isTrue();
assertThat(roundState.isCommitted()).isFalse(); assertThat(roundState.isCommitted()).isFalse();
@ -188,12 +196,13 @@ public class RoundStateTest {
when(messageValidator.validatePrepareMessage(firstPrepare)).thenReturn(true); when(messageValidator.validatePrepareMessage(firstPrepare)).thenReturn(true);
when(messageValidator.validatePrepareMessage(secondPrepare)).thenReturn(false); when(messageValidator.validatePrepareMessage(secondPrepare)).thenReturn(false);
roundState.addPrepareMessage(firstPrepare); roundState.addPrepareMessage(new Prepare(firstPrepare));
roundState.addPrepareMessage(secondPrepare); roundState.addPrepareMessage(new Prepare(secondPrepare));
verify(messageValidator, never()).validatePrepareMessage(any()); verify(messageValidator, never()).validatePrepareMessage(any());
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
assertThat(roundState.setProposedBlock(proposal)).isTrue(); assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isFalse(); assertThat(roundState.isPrepared()).isFalse();
@ -215,8 +224,9 @@ public class RoundStateTest {
.get(2) .get(2)
.createSignedPreparePayload(roundIdentifier, block.getHash()); .createSignedPreparePayload(roundIdentifier, block.getHash());
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
when(messageValidator.addSignedProposalPayload(any())).thenReturn(true); when(messageValidator.addSignedProposalPayload(any())).thenReturn(true);
when(messageValidator.validatePrepareMessage(firstPrepare)).thenReturn(false); when(messageValidator.validatePrepareMessage(firstPrepare)).thenReturn(false);
@ -225,10 +235,10 @@ public class RoundStateTest {
roundState.setProposedBlock(proposal); roundState.setProposedBlock(proposal);
assertThat(roundState.isPrepared()).isFalse(); assertThat(roundState.isPrepared()).isFalse();
roundState.addPrepareMessage(firstPrepare); roundState.addPrepareMessage(new Prepare(firstPrepare));
assertThat(roundState.isPrepared()).isFalse(); assertThat(roundState.isPrepared()).isFalse();
roundState.addPrepareMessage(secondPrepare); roundState.addPrepareMessage(new Prepare(secondPrepare));
assertThat(roundState.isPrepared()).isTrue(); assertThat(roundState.isPrepared()).isTrue();
} }
@ -239,24 +249,27 @@ public class RoundStateTest {
final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator); final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
final SignedData<CommitPayload> firstCommit = final Commit firstCommit =
new Commit(
validatorMessageFactories validatorMessageFactories
.get(1) .get(1)
.createSignedCommitPayload( .createSignedCommitPayload(
roundIdentifier, roundIdentifier,
block.getHash(), block.getHash(),
Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 1)); Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 1)));
final SignedData<CommitPayload> secondCommit = final Commit secondCommit =
new Commit(
validatorMessageFactories validatorMessageFactories
.get(2) .get(2)
.createSignedCommitPayload( .createSignedCommitPayload(
roundIdentifier, roundIdentifier,
block.getHash(), block.getHash(),
Signature.create(BigInteger.TEN, BigInteger.TEN, (byte) 1)); Signature.create(BigInteger.TEN, BigInteger.TEN, (byte) 1)));
final SignedData<ProposalPayload> proposal = final Proposal proposal =
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block); new Proposal(
validatorMessageFactories.get(0).createSignedProposalPayload(roundIdentifier, block));
roundState.setProposedBlock(proposal); roundState.setProposedBlock(proposal);
roundState.addCommitMessage(firstCommit); roundState.addCommitMessage(firstCommit);
@ -266,6 +279,7 @@ public class RoundStateTest {
assertThat(roundState.getCommitSeals()) assertThat(roundState.getCommitSeals())
.containsOnly( .containsOnly(
firstCommit.getPayload().getCommitSeal(), secondCommit.getPayload().getCommitSeal()); firstCommit.getSignedPayload().getPayload().getCommitSeal(),
secondCommit.getSignedPayload().getPayload().getCommitSeal());
} }
} }

Loading…
Cancel
Save