From 7891132dd6daf9ec1ff4ac410a601c30cfcab989 Mon Sep 17 00:00:00 2001 From: tmohay <37158202+rain-on@users.noreply.github.com> Date: Fri, 7 Dec 2018 09:42:55 +1100 Subject: [PATCH] Renamed IBFT message classes (#333) The classes used around the IBFT Messaging classes have all been renamed to make their use less cumbersome throughout the codebase. Other issues are yet to be found (not all variable names and function names have been captured in this sweep). --- .../pantheon/consensus/ibft/IbftMessages.java | 26 +-- .../ibft/blockcreation/ProposerSelector.java | 2 +- .../ibft/ibftmessage/AbstractIbftMessage.java | 4 +- ...tCommitMessage.java => CommitMessage.java} | 21 ++- .../ibftmessage/IbftPrePrepareMessage.java | 49 ------ .../consensus/ibft/ibftmessage/IbftV2.java | 2 +- ...RoundMessage.java => NewRoundMessage.java} | 21 ++- ...repareMessage.java => PrepareMessage.java} | 21 ++- .../ibft/ibftmessage/ProposalMessage.java | 47 ++++++ ...geMessage.java => RoundChangeMessage.java} | 22 ++- ...dMessageData.java => AbstractPayload.java} | 2 +- ...mitMessageData.java => CommitPayload.java} | 8 +- .../ibftmessagedata/IbftMessageFactory.java | 108 ------------- .../IbftPreparedCertificate.java | 62 ------- .../IbftRoundChangeCertificate.java | 53 ------ ...ndMessageData.java => InRoundPayload.java} | 5 +- .../ibft/ibftmessagedata/MessageFactory.java | 98 +++++++++++ ...dMessageData.java => NewRoundPayload.java} | 34 ++-- ...reMessageData.java => PreparePayload.java} | 9 +- .../ibftmessagedata/PreparedCertificate.java | 58 +++++++ ...eMessageData.java => ProposalPayload.java} | 13 +- .../RoundChangeCertificate.java | 48 ++++++ ...ssageData.java => RoundChangePayload.java} | 18 +-- ...SignedMessageData.java => SignedData.java} | 55 +++---- .../ibft/protocol/IbftSubProtocol.java | 2 +- .../ibft/validation/MessageValidator.java | 101 ++++++------ .../RoundChangeMessageValidator.java | 53 +++--- .../ibft/validation/MessageValidatorTest.java | 153 +++++++++--------- .../RoundChangeMessageValidatorTest.java | 119 +++++++------- 29 files changed, 570 insertions(+), 644 deletions(-) rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/{IbftCommitMessage.java => CommitMessage.java} (54%) delete mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrePrepareMessage.java rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/{IbftNewRoundMessage.java => NewRoundMessage.java} (53%) rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/{IbftPrepareMessage.java => PrepareMessage.java} (53%) create mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/ProposalMessage.java rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/{IbftRoundChangeMessage.java => RoundChangeMessage.java} (52%) rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{AbstractIbftUnsignedMessageData.java => AbstractPayload.java} (95%) rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{IbftUnsignedCommitMessageData.java => CommitPayload.java} (87%) delete mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftMessageFactory.java delete mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftPreparedCertificate.java delete mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftRoundChangeCertificate.java rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{AbstractIbftUnsignedInRoundMessageData.java => InRoundPayload.java} (81%) create mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/MessageFactory.java rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{IbftUnsignedNewRoundMessageData.java => NewRoundPayload.java} (59%) rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{IbftUnsignedPrepareMessageData.java => PreparePayload.java} (81%) create mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparedCertificate.java rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{IbftUnsignedPrePrepareMessageData.java => ProposalPayload.java} (73%) create mode 100644 consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangeCertificate.java rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{IbftUnsignedRoundChangeMessageData.java => RoundChangePayload.java} (77%) rename consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/{IbftSignedMessageData.java => SignedData.java} (56%) diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/IbftMessages.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/IbftMessages.java index d729077a57..3eebbb8635 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/IbftMessages.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/IbftMessages.java @@ -12,36 +12,36 @@ */ package tech.pegasys.pantheon.consensus.ibft; -import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftCommitMessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftNewRoundMessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftPrePrepareMessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftPrepareMessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftRoundChangeMessage; +import tech.pegasys.pantheon.consensus.ibft.ibftmessage.CommitMessage; import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftV2; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessage.NewRoundMessage; +import tech.pegasys.pantheon.consensus.ibft.ibftmessage.PrepareMessage; +import tech.pegasys.pantheon.consensus.ibft.ibftmessage.ProposalMessage; +import tech.pegasys.pantheon.consensus.ibft.ibftmessage.RoundChangeMessage; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.p2p.api.Message; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; public class IbftMessages { - public static IbftSignedMessageData fromMessage(final Message message) { + public static SignedData fromMessage(final Message message) { final MessageData messageData = message.getData(); switch (messageData.getCode()) { - case IbftV2.PRE_PREPARE: - return IbftPrePrepareMessage.fromMessage(messageData).decode(); + case IbftV2.PROPOSAL: + return ProposalMessage.fromMessage(messageData).decode(); case IbftV2.PREPARE: - return IbftPrepareMessage.fromMessage(messageData).decode(); + return PrepareMessage.fromMessage(messageData).decode(); case IbftV2.COMMIT: - return IbftCommitMessage.fromMessage(messageData).decode(); + return CommitMessage.fromMessage(messageData).decode(); case IbftV2.ROUND_CHANGE: - return IbftRoundChangeMessage.fromMessage(messageData).decode(); + return RoundChangeMessage.fromMessage(messageData).decode(); case IbftV2.NEW_ROUND: - return IbftNewRoundMessage.fromMessage(messageData).decode(); + return NewRoundMessage.fromMessage(messageData).decode(); default: throw new IllegalArgumentException( diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/blockcreation/ProposerSelector.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/blockcreation/ProposerSelector.java index 43426ccb74..98b96dc483 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/blockcreation/ProposerSelector.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/blockcreation/ProposerSelector.java @@ -33,7 +33,7 @@ import org.apache.logging.log4j.Logger; /** * Responsible for determining which member of the validator pool should propose the next block - * (i.e. send the Preprepare message). + * (i.e. send the Proposal message). * *

It does this by extracting the previous block's proposer from the ProposerSeal (stored in the * Blocks ExtraData) then iterating through the validator list (stored in {@link diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/AbstractIbftMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/AbstractIbftMessage.java index 0943aefa57..212dc85bcc 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/AbstractIbftMessage.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/AbstractIbftMessage.java @@ -12,7 +12,7 @@ */ package tech.pegasys.pantheon.consensus.ibft.ibftmessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import tech.pegasys.pantheon.ethereum.p2p.wire.AbstractMessageData; import tech.pegasys.pantheon.util.bytes.BytesValue; @@ -24,7 +24,7 @@ public abstract class AbstractIbftMessage extends AbstractMessageData { super(data); } - public abstract IbftSignedMessageData decode(); + public abstract SignedData decode(); protected static T fromMessage( final MessageData message, diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftCommitMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessage.java similarity index 54% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftCommitMessage.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessage.java index 91449d6406..18aa65601e 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftCommitMessage.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessage.java @@ -12,33 +12,32 @@ */ package tech.pegasys.pantheon.consensus.ibft.ibftmessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedCommitMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.CommitPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import tech.pegasys.pantheon.ethereum.rlp.RLP; import tech.pegasys.pantheon.util.bytes.BytesValue; -public class IbftCommitMessage extends AbstractIbftMessage { +public class CommitMessage extends AbstractIbftMessage { private static final int MESSAGE_CODE = IbftV2.COMMIT; - private IbftCommitMessage(final BytesValue data) { + private CommitMessage(final BytesValue data) { super(data); } - public static IbftCommitMessage fromMessage(final MessageData message) { - return fromMessage(message, MESSAGE_CODE, IbftCommitMessage.class, IbftCommitMessage::new); + public static CommitMessage fromMessage(final MessageData message) { + return fromMessage(message, MESSAGE_CODE, CommitMessage.class, CommitMessage::new); } @Override - public IbftSignedMessageData decode() { - return IbftSignedMessageData.readIbftSignedCommitMessageDataFrom(RLP.input(data)); + public SignedData decode() { + return SignedData.readSignedCommitPayloadFrom(RLP.input(data)); } - public static IbftCommitMessage create( - final IbftSignedMessageData ibftPrepareMessageDecoded) { + public static CommitMessage create(final SignedData signedPayload) { - return new IbftCommitMessage(ibftPrepareMessageDecoded.encode()); + return new CommitMessage(signedPayload.encode()); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrePrepareMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrePrepareMessage.java deleted file mode 100644 index 6d9328dbe9..0000000000 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrePrepareMessage.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2018 ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package tech.pegasys.pantheon.consensus.ibft.ibftmessage; - -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrePrepareMessageData; -import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; -import tech.pegasys.pantheon.ethereum.rlp.RLP; -import tech.pegasys.pantheon.util.bytes.BytesValue; - -public class IbftPrePrepareMessage extends AbstractIbftMessage { - - private static final int MESSAGE_CODE = IbftV2.PRE_PREPARE; - - private IbftPrePrepareMessage(final BytesValue data) { - super(data); - } - - public static IbftPrePrepareMessage fromMessage(final MessageData message) { - return fromMessage( - message, MESSAGE_CODE, IbftPrePrepareMessage.class, IbftPrePrepareMessage::new); - } - - @Override - public IbftSignedMessageData decode() { - return IbftSignedMessageData.readIbftSignedPrePrepareMessageDataFrom(RLP.input(data)); - } - - public static IbftPrePrepareMessage create( - final IbftSignedMessageData ibftPrepareMessageDecoded) { - - return new IbftPrePrepareMessage(ibftPrepareMessageDecoded.encode()); - } - - @Override - public int getCode() { - return MESSAGE_CODE; - } -} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftV2.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftV2.java index 5ed334e2d7..e00f7c84b9 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftV2.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftV2.java @@ -14,7 +14,7 @@ package tech.pegasys.pantheon.consensus.ibft.ibftmessage; /** Message codes for iBFT v2 messages */ public class IbftV2 { - public static final int PRE_PREPARE = 0; + public static final int PROPOSAL = 0; public static final int PREPARE = 1; public static final int COMMIT = 2; public static final int ROUND_CHANGE = 3; diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftNewRoundMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessage.java similarity index 53% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftNewRoundMessage.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessage.java index ddfe381f81..5a39f15726 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftNewRoundMessage.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessage.java @@ -12,33 +12,32 @@ */ package tech.pegasys.pantheon.consensus.ibft.ibftmessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedNewRoundMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.NewRoundPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import tech.pegasys.pantheon.ethereum.rlp.RLP; import tech.pegasys.pantheon.util.bytes.BytesValue; -public class IbftNewRoundMessage extends AbstractIbftMessage { +public class NewRoundMessage extends AbstractIbftMessage { private static final int MESSAGE_CODE = IbftV2.NEW_ROUND; - private IbftNewRoundMessage(final BytesValue data) { + private NewRoundMessage(final BytesValue data) { super(data); } - public static IbftNewRoundMessage fromMessage(final MessageData message) { - return fromMessage(message, MESSAGE_CODE, IbftNewRoundMessage.class, IbftNewRoundMessage::new); + public static NewRoundMessage fromMessage(final MessageData message) { + return fromMessage(message, MESSAGE_CODE, NewRoundMessage.class, NewRoundMessage::new); } @Override - public IbftSignedMessageData decode() { - return IbftSignedMessageData.readIbftSignedNewRoundMessageDataFrom(RLP.input(data)); + public SignedData decode() { + return SignedData.readSignedNewRoundPayloadFrom(RLP.input(data)); } - public static IbftNewRoundMessage create( - final IbftSignedMessageData ibftPrepareMessageDecoded) { + public static NewRoundMessage create(final SignedData signedPayload) { - return new IbftNewRoundMessage(ibftPrepareMessageDecoded.encode()); + return new NewRoundMessage(signedPayload.encode()); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrepareMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessage.java similarity index 53% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrepareMessage.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessage.java index 5707af605f..5549d85fe4 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftPrepareMessage.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessage.java @@ -12,33 +12,32 @@ */ package tech.pegasys.pantheon.consensus.ibft.ibftmessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrepareMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import tech.pegasys.pantheon.ethereum.rlp.RLP; import tech.pegasys.pantheon.util.bytes.BytesValue; -public class IbftPrepareMessage extends AbstractIbftMessage { +public class PrepareMessage extends AbstractIbftMessage { private static final int MESSAGE_CODE = IbftV2.PREPARE; - private IbftPrepareMessage(final BytesValue data) { + private PrepareMessage(final BytesValue data) { super(data); } - public static IbftPrepareMessage fromMessage(final MessageData message) { - return fromMessage(message, MESSAGE_CODE, IbftPrepareMessage.class, IbftPrepareMessage::new); + public static PrepareMessage fromMessage(final MessageData message) { + return fromMessage(message, MESSAGE_CODE, PrepareMessage.class, PrepareMessage::new); } @Override - public IbftSignedMessageData decode() { - return IbftSignedMessageData.readIbftSignedPrepareMessageDataFrom(RLP.input(data)); + public SignedData decode() { + return SignedData.readSignedPreparePayloadFrom(RLP.input(data)); } - public static IbftPrepareMessage create( - final IbftSignedMessageData ibftPrepareMessageDecoded) { + public static PrepareMessage create(final SignedData signedPayload) { - return new IbftPrepareMessage(ibftPrepareMessageDecoded.encode()); + return new PrepareMessage(signedPayload.encode()); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/ProposalMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/ProposalMessage.java new file mode 100644 index 0000000000..951e0562ad --- /dev/null +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/ProposalMessage.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessage; + +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; +import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; +import tech.pegasys.pantheon.ethereum.rlp.RLP; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +public class ProposalMessage extends AbstractIbftMessage { + + private static final int MESSAGE_CODE = IbftV2.PROPOSAL; + + private ProposalMessage(final BytesValue data) { + super(data); + } + + public static ProposalMessage fromMessage(final MessageData message) { + return fromMessage(message, MESSAGE_CODE, ProposalMessage.class, ProposalMessage::new); + } + + @Override + public SignedData decode() { + return SignedData.readSignedProposalPayloadFrom(RLP.input(data)); + } + + public static ProposalMessage create(final SignedData signedPayload) { + + return new ProposalMessage(signedPayload.encode()); + } + + @Override + public int getCode() { + return MESSAGE_CODE; + } +} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftRoundChangeMessage.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessage.java similarity index 52% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftRoundChangeMessage.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessage.java index 5a65b95dcb..6fcf934c91 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/IbftRoundChangeMessage.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessage.java @@ -12,34 +12,32 @@ */ package tech.pegasys.pantheon.consensus.ibft.ibftmessage; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedRoundChangeMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import tech.pegasys.pantheon.ethereum.rlp.RLP; import tech.pegasys.pantheon.util.bytes.BytesValue; -public class IbftRoundChangeMessage extends AbstractIbftMessage { +public class RoundChangeMessage extends AbstractIbftMessage { private static final int MESSAGE_CODE = IbftV2.ROUND_CHANGE; - private IbftRoundChangeMessage(final BytesValue data) { + private RoundChangeMessage(final BytesValue data) { super(data); } - public static IbftRoundChangeMessage fromMessage(final MessageData message) { - return fromMessage( - message, MESSAGE_CODE, IbftRoundChangeMessage.class, IbftRoundChangeMessage::new); + public static RoundChangeMessage fromMessage(final MessageData message) { + return fromMessage(message, MESSAGE_CODE, RoundChangeMessage.class, RoundChangeMessage::new); } @Override - public IbftSignedMessageData decode() { - return IbftSignedMessageData.readIbftSignedRoundChangeMessageDataFrom(RLP.input(data)); + public SignedData decode() { + return SignedData.readSignedRoundChangePayloadFrom(RLP.input(data)); } - public static IbftRoundChangeMessage create( - final IbftSignedMessageData ibftPrepareMessageDecoded) { + public static RoundChangeMessage create(final SignedData signedPayload) { - return new IbftRoundChangeMessage(ibftPrepareMessageDecoded.encode()); + return new RoundChangeMessage(signedPayload.encode()); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractIbftUnsignedMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractPayload.java similarity index 95% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractIbftUnsignedMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractPayload.java index d178643bfd..e8f9a566b3 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractIbftUnsignedMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractPayload.java @@ -18,7 +18,7 @@ import tech.pegasys.pantheon.ethereum.rlp.RLPInput; import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; import tech.pegasys.pantheon.util.bytes.BytesValue; -public abstract class AbstractIbftUnsignedMessageData { +public abstract class AbstractPayload { public abstract void writeTo(final RLPOutput rlpOutput); diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedCommitMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/CommitPayload.java similarity index 87% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedCommitMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/CommitPayload.java index 314ebf62ec..e7c48e1594 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedCommitMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/CommitPayload.java @@ -19,12 +19,12 @@ import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.rlp.RLPInput; import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; -public class IbftUnsignedCommitMessageData extends AbstractIbftUnsignedInRoundMessageData { +public class CommitPayload extends InRoundPayload { private static final int TYPE = IbftV2.COMMIT; private final Hash digest; private final Signature commitSeal; - public IbftUnsignedCommitMessageData( + public CommitPayload( final ConsensusRoundIdentifier roundIdentifier, final Hash digest, final Signature commitSeal) { @@ -33,7 +33,7 @@ public class IbftUnsignedCommitMessageData extends AbstractIbftUnsignedInRoundMe this.commitSeal = commitSeal; } - public static IbftUnsignedCommitMessageData readFrom(final RLPInput rlpInput) { + public static CommitPayload readFrom(final RLPInput rlpInput) { rlpInput.enterList(); final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); @@ -41,7 +41,7 @@ public class IbftUnsignedCommitMessageData extends AbstractIbftUnsignedInRoundMe final Signature commitSeal = rlpInput.readBytesValue(Signature::decode); rlpInput.leaveList(); - return new IbftUnsignedCommitMessageData(roundIdentifier, digest, commitSeal); + return new CommitPayload(roundIdentifier, digest, commitSeal); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftMessageFactory.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftMessageFactory.java deleted file mode 100644 index 09396a8193..0000000000 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftMessageFactory.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2018 ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; - -import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; -import tech.pegasys.pantheon.crypto.SECP256K1; -import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; -import tech.pegasys.pantheon.crypto.SECP256K1.Signature; -import tech.pegasys.pantheon.ethereum.core.Block; -import tech.pegasys.pantheon.ethereum.core.Hash; -import tech.pegasys.pantheon.ethereum.core.Util; -import tech.pegasys.pantheon.util.bytes.BytesValues; - -import java.util.Optional; - -public class IbftMessageFactory { - private final KeyPair validatorKeyPair; - - public IbftMessageFactory(final KeyPair validatorKeyPair) { - this.validatorKeyPair = validatorKeyPair; - } - - public IbftSignedMessageData - createIbftSignedPrePrepareMessageData( - final ConsensusRoundIdentifier roundIdentifier, final Block block) { - - IbftUnsignedPrePrepareMessageData prePrepareUnsignedMessageData = - new IbftUnsignedPrePrepareMessageData(roundIdentifier, block); - - return createSignedMessage(prePrepareUnsignedMessageData); - } - - public IbftSignedMessageData createIbftSignedPrepareMessageData( - final ConsensusRoundIdentifier roundIdentifier, final Hash digest) { - - IbftUnsignedPrepareMessageData prepareUnsignedMessageData = - new IbftUnsignedPrepareMessageData(roundIdentifier, digest); - - return createSignedMessage(prepareUnsignedMessageData); - } - - public IbftSignedMessageData createIbftSignedCommitMessageData( - final ConsensusRoundIdentifier roundIdentifier, - final Hash digest, - final Signature commitSeal) { - - IbftUnsignedCommitMessageData commitUnsignedMessageData = - new IbftUnsignedCommitMessageData(roundIdentifier, digest, commitSeal); - - return createSignedMessage(commitUnsignedMessageData); - } - - public IbftSignedMessageData - createIbftSignedRoundChangeMessageData( - final ConsensusRoundIdentifier roundIdentifier, - final Optional preparedCertificate) { - - IbftUnsignedRoundChangeMessageData prepareUnsignedMessageData = - new IbftUnsignedRoundChangeMessageData(roundIdentifier, preparedCertificate); - - return createSignedMessage(prepareUnsignedMessageData); - } - - public IbftSignedMessageData - createIbftSignedNewRoundChangeMessageData( - final ConsensusRoundIdentifier roundIdentifier, - final IbftRoundChangeCertificate roundChangeCertificate, - final IbftSignedMessageData - ibftPrePrepareMessageData) { - - IbftUnsignedNewRoundMessageData newRoundUnsignedMessageData = - new IbftUnsignedNewRoundMessageData( - roundIdentifier, roundChangeCertificate, ibftPrePrepareMessageData); - - return createSignedMessage(newRoundUnsignedMessageData); - } - - private IbftSignedMessageData createSignedMessage( - final M ibftUnsignedMessage) { - final Signature signature = sign(ibftUnsignedMessage, validatorKeyPair); - - return new IbftSignedMessageData<>( - ibftUnsignedMessage, Util.publicKeyToAddress(validatorKeyPair.getPublicKey()), signature); - } - - static Hash hashForSignature(final AbstractIbftUnsignedMessageData unsignedMessageData) { - return Hash.hash( - BytesValues.concatenate( - BytesValues.ofUnsignedByte(unsignedMessageData.getMessageType()), - unsignedMessageData.encoded())); - } - - private static Signature sign( - final AbstractIbftUnsignedMessageData unsignedMessageData, final KeyPair nodeKeys) { - - return SECP256K1.sign(hashForSignature(unsignedMessageData), nodeKeys); - } -} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftPreparedCertificate.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftPreparedCertificate.java deleted file mode 100644 index f99d70ed7b..0000000000 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftPreparedCertificate.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2018 ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; - -import tech.pegasys.pantheon.ethereum.rlp.RLPInput; -import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; - -import java.util.Collection; - -public class IbftPreparedCertificate { - - private final IbftSignedMessageData ibftPrePrepareMessage; - private final Collection> - ibftPrepareMessages; - - public IbftPreparedCertificate( - final IbftSignedMessageData ibftPrePrepareMessage, - final Collection> ibftPrepareMessages) { - this.ibftPrePrepareMessage = ibftPrePrepareMessage; - this.ibftPrepareMessages = ibftPrepareMessages; - } - - public static IbftPreparedCertificate readFrom(final RLPInput rlpInput) { - final IbftSignedMessageData ibftPrePreparedMessage; - final Collection> ibftPrepareMessages; - - rlpInput.enterList(); - ibftPrePreparedMessage = - IbftSignedMessageData.readIbftSignedPrePrepareMessageDataFrom(rlpInput); - ibftPrepareMessages = - rlpInput.readList(IbftSignedMessageData::readIbftSignedPrepareMessageDataFrom); - rlpInput.leaveList(); - - return new IbftPreparedCertificate(ibftPrePreparedMessage, ibftPrepareMessages); - } - - public void writeTo(final RLPOutput rlpOutput) { - rlpOutput.startList(); - ibftPrePrepareMessage.writeTo(rlpOutput); - rlpOutput.writeList(ibftPrepareMessages, IbftSignedMessageData::writeTo); - rlpOutput.endList(); - } - - public IbftSignedMessageData getIbftPrePrepareMessage() { - return ibftPrePrepareMessage; - } - - public Collection> - getIbftPrepareMessages() { - return ibftPrepareMessages; - } -} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftRoundChangeCertificate.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftRoundChangeCertificate.java deleted file mode 100644 index cdda90b9c3..0000000000 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftRoundChangeCertificate.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2018 ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; - -import tech.pegasys.pantheon.ethereum.rlp.RLPInput; -import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; - -import java.util.Collection; - -public class IbftRoundChangeCertificate { - - private final Collection> - ibftRoundChangeMessages; - - public IbftRoundChangeCertificate( - final Collection> - ibftRoundChangeMessages) { - this.ibftRoundChangeMessages = ibftRoundChangeMessages; - } - - public static IbftRoundChangeCertificate readFrom(final RLPInput rlpInput) { - final Collection> - ibftRoundChangeMessages; - - rlpInput.enterList(); - ibftRoundChangeMessages = - rlpInput.readList(IbftSignedMessageData::readIbftSignedRoundChangeMessageDataFrom); - rlpInput.leaveList(); - - return new IbftRoundChangeCertificate(ibftRoundChangeMessages); - } - - public void writeTo(final RLPOutput rlpOutput) { - rlpOutput.startList(); - rlpOutput.writeList(ibftRoundChangeMessages, IbftSignedMessageData::writeTo); - rlpOutput.endList(); - } - - public Collection> - getIbftRoundChangeMessages() { - return ibftRoundChangeMessages; - } -} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractIbftUnsignedInRoundMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/InRoundPayload.java similarity index 81% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractIbftUnsignedInRoundMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/InRoundPayload.java index de4a8f8007..119fc50fa9 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/AbstractIbftUnsignedInRoundMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/InRoundPayload.java @@ -14,11 +14,10 @@ package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; -public abstract class AbstractIbftUnsignedInRoundMessageData - extends AbstractIbftUnsignedMessageData { +public abstract class InRoundPayload extends AbstractPayload { protected final ConsensusRoundIdentifier roundIdentifier; - protected AbstractIbftUnsignedInRoundMessageData(final ConsensusRoundIdentifier roundIdentifier) { + protected InRoundPayload(final ConsensusRoundIdentifier roundIdentifier) { this.roundIdentifier = roundIdentifier; } diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/MessageFactory.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/MessageFactory.java new file mode 100644 index 0000000000..839504aa57 --- /dev/null +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/MessageFactory.java @@ -0,0 +1,98 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; + +import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; +import tech.pegasys.pantheon.crypto.SECP256K1; +import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; +import tech.pegasys.pantheon.crypto.SECP256K1.Signature; +import tech.pegasys.pantheon.ethereum.core.Block; +import tech.pegasys.pantheon.ethereum.core.Hash; +import tech.pegasys.pantheon.ethereum.core.Util; +import tech.pegasys.pantheon.util.bytes.BytesValues; + +import java.util.Optional; + +public class MessageFactory { + + private final KeyPair validatorKeyPair; + + public MessageFactory(final KeyPair validatorKeyPair) { + this.validatorKeyPair = validatorKeyPair; + } + + public SignedData createSignedProposalPayload( + final ConsensusRoundIdentifier roundIdentifier, final Block block) { + + final ProposalPayload payload = new ProposalPayload(roundIdentifier, block); + + return createSignedMessage(payload); + } + + public SignedData createSignedPreparePayload( + final ConsensusRoundIdentifier roundIdentifier, final Hash digest) { + + final PreparePayload payload = new PreparePayload(roundIdentifier, digest); + + return createSignedMessage(payload); + } + + public SignedData createSignedCommitPayload( + final ConsensusRoundIdentifier roundIdentifier, + final Hash digest, + final Signature commitSeal) { + + final CommitPayload payload = new CommitPayload(roundIdentifier, digest, commitSeal); + + return createSignedMessage(payload); + } + + public SignedData createSignedRoundChangePayload( + final ConsensusRoundIdentifier roundIdentifier, + final Optional preparedCertificate) { + + final RoundChangePayload payload = new RoundChangePayload(roundIdentifier, preparedCertificate); + + return createSignedMessage(payload); + } + + public SignedData createSignedNewRoundPayload( + final ConsensusRoundIdentifier roundIdentifier, + final RoundChangeCertificate roundChangeCertificate, + final SignedData proposalPayload) { + + final NewRoundPayload payload = + new NewRoundPayload(roundIdentifier, roundChangeCertificate, proposalPayload); + + return createSignedMessage(payload); + } + + private SignedData createSignedMessage(final M payload) { + final Signature signature = sign(payload, validatorKeyPair); + + return new SignedData<>( + payload, Util.publicKeyToAddress(validatorKeyPair.getPublicKey()), signature); + } + + static Hash hashForSignature(final AbstractPayload unsignedMessageData) { + return Hash.hash( + BytesValues.concatenate( + BytesValues.ofUnsignedByte(unsignedMessageData.getMessageType()), + unsignedMessageData.encoded())); + } + + private static Signature sign(final AbstractPayload unsignedMessageData, final KeyPair nodeKeys) { + + return SECP256K1.sign(hashForSignature(unsignedMessageData), nodeKeys); + } +} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedNewRoundMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/NewRoundPayload.java similarity index 59% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedNewRoundMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/NewRoundPayload.java index cf451f4f92..894dba7b7a 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedNewRoundMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/NewRoundPayload.java @@ -17,35 +17,35 @@ import tech.pegasys.pantheon.consensus.ibft.ibftmessage.IbftV2; import tech.pegasys.pantheon.ethereum.rlp.RLPInput; import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; -public class IbftUnsignedNewRoundMessageData extends AbstractIbftUnsignedMessageData { +public class NewRoundPayload extends AbstractPayload { private static final int TYPE = IbftV2.NEW_ROUND; private final ConsensusRoundIdentifier roundChangeIdentifier; - private final IbftRoundChangeCertificate roundChangeCertificate; + private final RoundChangeCertificate roundChangeCertificate; - private final IbftSignedMessageData ibftPrePrepareMessage; + private final SignedData proposalPayload; - public IbftUnsignedNewRoundMessageData( + public NewRoundPayload( final ConsensusRoundIdentifier roundIdentifier, - final IbftRoundChangeCertificate roundChangeCertificate, - final IbftSignedMessageData ibftPrePrepareMessage) { + final RoundChangeCertificate roundChangeCertificate, + final SignedData proposalPayload) { this.roundChangeIdentifier = roundIdentifier; this.roundChangeCertificate = roundChangeCertificate; - this.ibftPrePrepareMessage = ibftPrePrepareMessage; + this.proposalPayload = proposalPayload; } public ConsensusRoundIdentifier getRoundChangeIdentifier() { return roundChangeIdentifier; } - public IbftRoundChangeCertificate getRoundChangeCertificate() { + public RoundChangeCertificate getRoundChangeCertificate() { return roundChangeCertificate; } - public IbftSignedMessageData getIbftPrePrepareMessage() { - return ibftPrePrepareMessage; + public SignedData getProposalPayload() { + return proposalPayload; } @Override @@ -54,22 +54,20 @@ public class IbftUnsignedNewRoundMessageData extends AbstractIbftUnsignedMessage rlpOutput.startList(); roundChangeIdentifier.writeTo(rlpOutput); roundChangeCertificate.writeTo(rlpOutput); - ibftPrePrepareMessage.writeTo(rlpOutput); + proposalPayload.writeTo(rlpOutput); rlpOutput.endList(); } - public static IbftUnsignedNewRoundMessageData readFrom(final RLPInput rlpInput) { + public static NewRoundPayload readFrom(final RLPInput rlpInput) { rlpInput.enterList(); final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); - final IbftRoundChangeCertificate roundChangeCertificate = - IbftRoundChangeCertificate.readFrom(rlpInput); - final IbftSignedMessageData ibftPrePrepareMessage = - IbftSignedMessageData.readIbftSignedPrePrepareMessageDataFrom(rlpInput); + final RoundChangeCertificate roundChangeCertificate = RoundChangeCertificate.readFrom(rlpInput); + final SignedData proposalPayload = + SignedData.readSignedProposalPayloadFrom(rlpInput); rlpInput.leaveList(); - return new IbftUnsignedNewRoundMessageData( - roundIdentifier, roundChangeCertificate, ibftPrePrepareMessage); + return new NewRoundPayload(roundIdentifier, roundChangeCertificate, proposalPayload); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedPrepareMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparePayload.java similarity index 81% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedPrepareMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparePayload.java index 6d28cf270d..02310b929b 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedPrepareMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparePayload.java @@ -18,24 +18,23 @@ import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.rlp.RLPInput; import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; -public class IbftUnsignedPrepareMessageData extends AbstractIbftUnsignedInRoundMessageData { +public class PreparePayload extends InRoundPayload { private static final int TYPE = IbftV2.PREPARE; private final Hash digest; - public IbftUnsignedPrepareMessageData( - final ConsensusRoundIdentifier roundIdentifier, final Hash digest) { + public PreparePayload(final ConsensusRoundIdentifier roundIdentifier, final Hash digest) { super(roundIdentifier); this.digest = digest; } - public static IbftUnsignedPrepareMessageData readFrom(final RLPInput rlpInput) { + public static PreparePayload readFrom(final RLPInput rlpInput) { rlpInput.enterList(); final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); final Hash digest = readDigest(rlpInput); rlpInput.leaveList(); - return new IbftUnsignedPrepareMessageData(roundIdentifier, digest); + return new PreparePayload(roundIdentifier, digest); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparedCertificate.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparedCertificate.java new file mode 100644 index 0000000000..796952a450 --- /dev/null +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/PreparedCertificate.java @@ -0,0 +1,58 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; + +import tech.pegasys.pantheon.ethereum.rlp.RLPInput; +import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; + +import java.util.Collection; + +public class PreparedCertificate { + + private final SignedData proposalPayload; + private final Collection> preparePayloads; + + public PreparedCertificate( + final SignedData proposalPayload, + final Collection> preparePayloads) { + this.proposalPayload = proposalPayload; + this.preparePayloads = preparePayloads; + } + + public static PreparedCertificate readFrom(final RLPInput rlpInput) { + final SignedData proposalMessage; + final Collection> prepareMessages; + + rlpInput.enterList(); + proposalMessage = SignedData.readSignedProposalPayloadFrom(rlpInput); + prepareMessages = rlpInput.readList(SignedData::readSignedPreparePayloadFrom); + rlpInput.leaveList(); + + return new PreparedCertificate(proposalMessage, prepareMessages); + } + + public void writeTo(final RLPOutput rlpOutput) { + rlpOutput.startList(); + proposalPayload.writeTo(rlpOutput); + rlpOutput.writeList(preparePayloads, SignedData::writeTo); + rlpOutput.endList(); + } + + public SignedData getProposalPayload() { + return proposalPayload; + } + + public Collection> getPreparePayloads() { + return preparePayloads; + } +} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedPrePrepareMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/ProposalPayload.java similarity index 73% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedPrePrepareMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/ProposalPayload.java index 11c2541132..b82490f39a 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedPrePrepareMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/ProposalPayload.java @@ -19,20 +19,17 @@ import tech.pegasys.pantheon.ethereum.core.Block; import tech.pegasys.pantheon.ethereum.rlp.RLPInput; import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; -// 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 { +public class ProposalPayload extends InRoundPayload { - private static final int TYPE = IbftV2.PRE_PREPARE; + private static final int TYPE = IbftV2.PROPOSAL; private final Block block; - public IbftUnsignedPrePrepareMessageData( - final ConsensusRoundIdentifier roundIdentifier, final Block block) { + public ProposalPayload(final ConsensusRoundIdentifier roundIdentifier, final Block block) { super(roundIdentifier); this.block = block; } - public static IbftUnsignedPrePrepareMessageData readFrom(final RLPInput rlpInput) { + public static ProposalPayload readFrom(final RLPInput rlpInput) { rlpInput.enterList(); final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); @@ -40,7 +37,7 @@ public class IbftUnsignedPrePrepareMessageData extends AbstractIbftUnsignedInRou Block.readFrom(rlpInput, IbftBlockHashing::calculateDataHashForCommittedSeal); rlpInput.leaveList(); - return new IbftUnsignedPrePrepareMessageData(roundIdentifier, block); + return new ProposalPayload(roundIdentifier, block); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangeCertificate.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangeCertificate.java new file mode 100644 index 0000000000..ef170b2d28 --- /dev/null +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangeCertificate.java @@ -0,0 +1,48 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessagedata; + +import tech.pegasys.pantheon.ethereum.rlp.RLPInput; +import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; + +import java.util.Collection; + +public class RoundChangeCertificate { + + private final Collection> roundChangePayloads; + + public RoundChangeCertificate( + final Collection> roundChangePayloads) { + this.roundChangePayloads = roundChangePayloads; + } + + public static RoundChangeCertificate readFrom(final RLPInput rlpInput) { + final Collection> roundChangePayloads; + + rlpInput.enterList(); + roundChangePayloads = rlpInput.readList(SignedData::readSignedRoundChangePayloadFrom); + rlpInput.leaveList(); + + return new RoundChangeCertificate(roundChangePayloads); + } + + public void writeTo(final RLPOutput rlpOutput) { + rlpOutput.startList(); + rlpOutput.writeList(roundChangePayloads, SignedData::writeTo); + rlpOutput.endList(); + } + + public Collection> getRoundChangePayloads() { + return roundChangePayloads; + } +} diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedRoundChangeMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangePayload.java similarity index 77% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedRoundChangeMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangePayload.java index 5701cc9eb4..317c2b6e43 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftUnsignedRoundChangeMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/RoundChangePayload.java @@ -20,18 +20,18 @@ import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; import java.util.Optional; -public class IbftUnsignedRoundChangeMessageData extends AbstractIbftUnsignedMessageData { +public class RoundChangePayload extends AbstractPayload { private static final int TYPE = IbftV2.PREPARE; private final ConsensusRoundIdentifier roundChangeIdentifier; // The validator may not hae any prepared certificate - private final Optional preparedCertificate; + private final Optional preparedCertificate; - public IbftUnsignedRoundChangeMessageData( + public RoundChangePayload( final ConsensusRoundIdentifier roundIdentifier, - final Optional preparedCertificate) { + final Optional preparedCertificate) { this.roundChangeIdentifier = roundIdentifier; this.preparedCertificate = preparedCertificate; } @@ -40,7 +40,7 @@ public class IbftUnsignedRoundChangeMessageData extends AbstractIbftUnsignedMess return roundChangeIdentifier; } - public Optional getPreparedCertificate() { + public Optional getPreparedCertificate() { return preparedCertificate; } @@ -59,21 +59,21 @@ public class IbftUnsignedRoundChangeMessageData extends AbstractIbftUnsignedMess ibftMessage.endList(); } - public static IbftUnsignedRoundChangeMessageData readFrom(final RLPInput rlpInput) { + public static RoundChangePayload readFrom(final RLPInput rlpInput) { rlpInput.enterList(); final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); - final Optional preparedCertificate; + final Optional preparedCertificate; if (rlpInput.nextIsNull()) { rlpInput.skipNext(); preparedCertificate = Optional.empty(); } else { - preparedCertificate = Optional.of(IbftPreparedCertificate.readFrom(rlpInput)); + preparedCertificate = Optional.of(PreparedCertificate.readFrom(rlpInput)); } rlpInput.leaveList(); - return new IbftUnsignedRoundChangeMessageData(roundIdentifier, preparedCertificate); + return new RoundChangePayload(roundIdentifier, preparedCertificate); } @Override diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftSignedMessageData.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/SignedData.java similarity index 56% rename from consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftSignedMessageData.java rename to consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/SignedData.java index 209d02d889..6d8bb1a26f 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/IbftSignedMessageData.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/ibftmessagedata/SignedData.java @@ -20,15 +20,14 @@ import tech.pegasys.pantheon.ethereum.rlp.RLPInput; import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; import tech.pegasys.pantheon.util.bytes.BytesValue; -public class IbftSignedMessageData { +public class SignedData { protected final Address sender; protected final Signature signature; - protected final M ibftUnsignedMessageData; + protected final M unsignedPayload; - public IbftSignedMessageData( - final M ibftUnsignedMessageData, final Address sender, final Signature signature) { - this.ibftUnsignedMessageData = ibftUnsignedMessageData; + public SignedData(final M unsignedPayload, final Address sender, final Signature signature) { + this.unsignedPayload = unsignedPayload; this.sender = sender; this.signature = signature; } @@ -41,14 +40,14 @@ public class IbftSignedMessageData { return signature; } - public M getUnsignedMessageData() { - return ibftUnsignedMessageData; + public M getPayload() { + return unsignedPayload; } public void writeTo(final RLPOutput output) { output.startList(); - ibftUnsignedMessageData.writeTo(output); + unsignedPayload.writeTo(output); output.writeBytesValue(getSignature().encodedBytes()); output.endList(); } @@ -59,72 +58,63 @@ public class IbftSignedMessageData { return rlpEncode.encoded(); } - public static IbftSignedMessageData - readIbftSignedPrePrepareMessageDataFrom(final RLPInput rlpInput) { + public static SignedData readSignedProposalPayloadFrom(final RLPInput rlpInput) { rlpInput.enterList(); - final IbftUnsignedPrePrepareMessageData unsignedMessageData = - IbftUnsignedPrePrepareMessageData.readFrom(rlpInput); + final ProposalPayload unsignedMessageData = ProposalPayload.readFrom(rlpInput); final Signature signature = readSignature(rlpInput); rlpInput.leaveList(); return from(unsignedMessageData, signature); } - public static IbftSignedMessageData - readIbftSignedPrepareMessageDataFrom(final RLPInput rlpInput) { + public static SignedData readSignedPreparePayloadFrom(final RLPInput rlpInput) { rlpInput.enterList(); - final IbftUnsignedPrepareMessageData unsignedMessageData = - IbftUnsignedPrepareMessageData.readFrom(rlpInput); + final PreparePayload unsignedMessageData = PreparePayload.readFrom(rlpInput); final Signature signature = readSignature(rlpInput); rlpInput.leaveList(); return from(unsignedMessageData, signature); } - public static IbftSignedMessageData - readIbftSignedCommitMessageDataFrom(final RLPInput rlpInput) { + public static SignedData readSignedCommitPayloadFrom(final RLPInput rlpInput) { rlpInput.enterList(); - final IbftUnsignedCommitMessageData unsignedMessageData = - IbftUnsignedCommitMessageData.readFrom(rlpInput); + final CommitPayload unsignedMessageData = CommitPayload.readFrom(rlpInput); final Signature signature = readSignature(rlpInput); rlpInput.leaveList(); return from(unsignedMessageData, signature); } - public static IbftSignedMessageData - readIbftSignedRoundChangeMessageDataFrom(final RLPInput rlpInput) { + public static SignedData readSignedRoundChangePayloadFrom( + final RLPInput rlpInput) { rlpInput.enterList(); - final IbftUnsignedRoundChangeMessageData unsignedMessageData = - IbftUnsignedRoundChangeMessageData.readFrom(rlpInput); + final RoundChangePayload unsignedMessageData = RoundChangePayload.readFrom(rlpInput); final Signature signature = readSignature(rlpInput); rlpInput.leaveList(); return from(unsignedMessageData, signature); } - public static IbftSignedMessageData - readIbftSignedNewRoundMessageDataFrom(final RLPInput rlpInput) { + public static SignedData readSignedNewRoundPayloadFrom(final RLPInput rlpInput) { rlpInput.enterList(); - final IbftUnsignedNewRoundMessageData unsignedMessageData = - IbftUnsignedNewRoundMessageData.readFrom(rlpInput); + final NewRoundPayload unsignedMessageData = NewRoundPayload.readFrom(rlpInput); final Signature signature = readSignature(rlpInput); rlpInput.leaveList(); return from(unsignedMessageData, signature); } - protected static IbftSignedMessageData from( + protected static SignedData from( final M unsignedMessageData, final Signature signature) { final Address sender = recoverSender(unsignedMessageData, signature); - return new IbftSignedMessageData<>(unsignedMessageData, sender, signature); + return new SignedData<>(unsignedMessageData, sender, signature); } protected static Signature readSignature(final RLPInput signedMessage) { @@ -132,9 +122,8 @@ public class IbftSignedMessageData { } protected static Address recoverSender( - final AbstractIbftUnsignedMessageData unsignedMessageData, final Signature signature) { + final AbstractPayload unsignedMessageData, final Signature signature) { - return Util.signatureToAddress( - signature, IbftMessageFactory.hashForSignature(unsignedMessageData)); + return Util.signatureToAddress(signature, MessageFactory.hashForSignature(unsignedMessageData)); } } diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/protocol/IbftSubProtocol.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/protocol/IbftSubProtocol.java index 37ad419c97..d140b08980 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/protocol/IbftSubProtocol.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/protocol/IbftSubProtocol.java @@ -40,7 +40,7 @@ public class IbftSubProtocol implements SubProtocol { @Override public boolean isValidMessageCode(final int protocolVersion, final int code) { switch (code) { - case IbftV2.PRE_PREPARE: + case IbftV2.PROPOSAL: case IbftV2.PREPARE: case IbftV2.COMMIT: case IbftV2.ROUND_CHANGE: diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidator.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidator.java index 5aa66f52f3..4cc2ba3c99 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidator.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidator.java @@ -17,11 +17,11 @@ import static tech.pegasys.pantheon.ethereum.mainnet.HeaderValidationMode.FULL; import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; import tech.pegasys.pantheon.consensus.ibft.IbftContext; import tech.pegasys.pantheon.consensus.ibft.IbftExtraData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.AbstractIbftUnsignedInRoundMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedCommitMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrePrepareMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrepareMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.CommitPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.InRoundPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.Block; @@ -47,8 +47,7 @@ public class MessageValidator { private final ProtocolContext protocolContext; private final BlockHeader parentHeader; - private Optional> preprepareMessage = - Optional.empty(); + private Optional> proposal = Optional.empty(); public MessageValidator( final Collection

validators, @@ -65,41 +64,39 @@ public class MessageValidator { this.parentHeader = parentHeader; } - public boolean addPreprepareMessage( - final IbftSignedMessageData msg) { + public boolean addSignedProposalPayload(final SignedData msg) { - if (preprepareMessage.isPresent()) { - return handleSubsequentPreprepareMessage(preprepareMessage.get(), msg); + if (proposal.isPresent()) { + return handleSubsequentProposal(proposal.get(), msg); } - if (!validatePreprepareMessage(msg)) { + if (!validateSignedProposalPayload(msg)) { return false; } - if (!validateBlocKMatchesPrepareMessageRound(msg.getUnsignedMessageData())) { + if (!validateBlockMatchesProposalRound(msg.getPayload())) { return false; } - preprepareMessage = Optional.of(msg); + proposal = Optional.of(msg); return true; } - private boolean validatePreprepareMessage( - final IbftSignedMessageData msg) { + private boolean validateSignedProposalPayload(final SignedData msg) { - if (!msg.getUnsignedMessageData().getRoundIdentifier().equals(roundIdentifier)) { - LOG.info("Invalid Preprepare message, does not match current round."); + if (!msg.getPayload().getRoundIdentifier().equals(roundIdentifier)) { + LOG.info("Invalid Proposal message, does not match current round."); return false; } if (!msg.getSender().equals(expectedProposer)) { LOG.info( - "Invalid Preprepare message, was not created by the proposer expected for the " + "Invalid Proposal message, was not created by the proposer expected for the " + "associated round."); return false; } - final Block proposedBlock = msg.getUnsignedMessageData().getBlock(); + final Block proposedBlock = msg.getPayload().getBlock(); if (!headerValidator.validateHeader( proposedBlock.getHeader(), parentHeader, protocolContext, FULL)) { LOG.info("Invalid Prepare message, block did not pass header validation."); @@ -109,30 +106,28 @@ public class MessageValidator { return true; } - private boolean handleSubsequentPreprepareMessage( - final IbftSignedMessageData existingMsg, - final IbftSignedMessageData newMsg) { + private boolean handleSubsequentProposal( + final SignedData existingMsg, final SignedData newMsg) { if (!existingMsg.getSender().equals(newMsg.getSender())) { - LOG.debug("Received subsequent invalid Preprepare message; sender differs from original."); + LOG.debug("Received subsequent invalid Proposal message; sender differs from original."); return false; } - final IbftUnsignedPrePrepareMessageData existingData = existingMsg.getUnsignedMessageData(); - final IbftUnsignedPrePrepareMessageData newData = newMsg.getUnsignedMessageData(); + final ProposalPayload existingData = existingMsg.getPayload(); + final ProposalPayload newData = newMsg.getPayload(); - if (!preprepareMessagesAreIdentical(existingData, newData)) { - LOG.debug("Received subsequent invalid Preprepare message; content differs from original."); + if (!proposalMessagesAreIdentical(existingData, newData)) { + LOG.debug("Received subsequent invalid Proposal message; content differs from original."); return false; } return true; } - public boolean validatePrepareMessage( - final IbftSignedMessageData msg) { + public boolean validatePrepareMessage(final SignedData msg) { final String msgType = "Prepare"; - if (!isMessageForCurrentRoundFromValidatorAndPreprareMessageAvailable(msg, msgType)) { + if (!isMessageForCurrentRoundFromValidatorAndProposalAvailable(msg, msgType)) { return false; } @@ -141,35 +136,32 @@ public class MessageValidator { return false; } - return validateDigestMatchesPreprepareBlock(msg.getUnsignedMessageData().getDigest(), msgType); + return validateDigestMatchesProposal(msg.getPayload().getDigest(), msgType); } - public boolean validateCommmitMessage( - final IbftSignedMessageData msg) { + public boolean validateCommmitMessage(final SignedData msg) { final String msgType = "Commit"; - if (!isMessageForCurrentRoundFromValidatorAndPreprareMessageAvailable(msg, msgType)) { + if (!isMessageForCurrentRoundFromValidatorAndProposalAvailable(msg, msgType)) { return false; } - final Block proposedBlock = preprepareMessage.get().getUnsignedMessageData().getBlock(); + final Block proposedBlock = proposal.get().getPayload().getBlock(); final Address commitSealCreator = - Util.signatureToAddress( - msg.getUnsignedMessageData().getCommitSeal(), proposedBlock.getHash()); + Util.signatureToAddress(msg.getPayload().getCommitSeal(), proposedBlock.getHash()); if (!commitSealCreator.equals(msg.getSender())) { LOG.info("Invalid Commit message. Seal was not created by the message transmitter."); return false; } - return validateDigestMatchesPreprepareBlock(msg.getUnsignedMessageData().getDigest(), msgType); + return validateDigestMatchesProposal(msg.getPayload().getDigest(), msgType); } - private boolean isMessageForCurrentRoundFromValidatorAndPreprareMessageAvailable( - final IbftSignedMessageData msg, - final String msgType) { + private boolean isMessageForCurrentRoundFromValidatorAndProposalAvailable( + final SignedData msg, final String msgType) { - if (!msg.getUnsignedMessageData().getRoundIdentifier().equals(roundIdentifier)) { + if (!msg.getPayload().getRoundIdentifier().equals(roundIdentifier)) { LOG.info("Invalid {} message, does not match current round.", msgType); return false; } @@ -181,18 +173,18 @@ public class MessageValidator { return false; } - if (!preprepareMessage.isPresent()) { + if (!proposal.isPresent()) { LOG.info( - "Unable to validate {} message. No Preprepare message exists against " - + "which to validate block digest.", + "Unable to validate {} message. No Proposal exists against which to validate " + + "block digest.", msgType); return false; } return true; } - private boolean validateDigestMatchesPreprepareBlock(final Hash digest, final String msgType) { - final Block proposedBlock = preprepareMessage.get().getUnsignedMessageData().getBlock(); + private boolean validateDigestMatchesProposal(final Hash digest, final String msgType) { + final Block proposedBlock = proposal.get().getPayload().getBlock(); if (!digest.equals(proposedBlock.getHash())) { LOG.info( "Illegal {} message, digest does not match the block in the Prepare Message.", msgType); @@ -201,19 +193,18 @@ public class MessageValidator { return true; } - private boolean preprepareMessagesAreIdentical( - final IbftUnsignedPrePrepareMessageData right, final IbftUnsignedPrePrepareMessageData left) { + private boolean proposalMessagesAreIdentical( + final ProposalPayload right, final ProposalPayload left) { return right.getBlock().getHash().equals(left.getBlock().getHash()) && right.getRoundIdentifier().equals(left.getRoundIdentifier()); } - private boolean validateBlocKMatchesPrepareMessageRound( - final IbftUnsignedPrePrepareMessageData msgData) { - final ConsensusRoundIdentifier msgRound = msgData.getRoundIdentifier(); + private boolean validateBlockMatchesProposalRound(final ProposalPayload payload) { + final ConsensusRoundIdentifier msgRound = payload.getRoundIdentifier(); final IbftExtraData extraData = - IbftExtraData.decode(msgData.getBlock().getHeader().getExtraData()); + IbftExtraData.decode(payload.getBlock().getHeader().getExtraData()); if (extraData.getRound() != msgRound.getRoundNumber()) { - LOG.info("Invalid Preprepare message, round number in block does not match that in message."); + LOG.info("Invalid Proposal message, round number in block does not match that in message."); return false; } return true; diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidator.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidator.java index 72cf1aa00c..3812cd7efb 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidator.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidator.java @@ -13,11 +13,11 @@ package tech.pegasys.pantheon.consensus.ibft.validation; import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftPreparedCertificate; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrePrepareMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrepareMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedRoundChangeMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparedCertificate; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.ethereum.core.Address; import java.util.Collection; @@ -45,8 +45,7 @@ public class RoundChangeMessageValidator { this.currentRound = currentRound; } - public boolean validateMessage( - final IbftSignedMessageData msg) { + public boolean validateMessage(final SignedData msg) { if (!validators.contains(msg.getSender())) { LOG.info( @@ -55,33 +54,30 @@ public class RoundChangeMessageValidator { return false; } - final ConsensusRoundIdentifier roundChangeTarget = - msg.getUnsignedMessageData().getRoundChangeIdentifier(); + final ConsensusRoundIdentifier targetRound = msg.getPayload().getRoundChangeIdentifier(); - if (roundChangeTarget.getSequenceNumber() != currentRound.getSequenceNumber()) { + if (targetRound.getSequenceNumber() != currentRound.getSequenceNumber()) { LOG.info("Invalid RoundChange message, not valid for local chain height."); return false; } - if (msg.getUnsignedMessageData().getPreparedCertificate().isPresent()) { - final IbftPreparedCertificate certificate = - msg.getUnsignedMessageData().getPreparedCertificate().get(); + if (msg.getPayload().getPreparedCertificate().isPresent()) { + final PreparedCertificate certificate = msg.getPayload().getPreparedCertificate().get(); - return validatePrepareCertificate(certificate, roundChangeTarget); + return validatePrepareCertificate(certificate, targetRound); } return true; } private boolean validatePrepareCertificate( - final IbftPreparedCertificate certificate, final ConsensusRoundIdentifier roundChangeTarget) { - final IbftSignedMessageData preprepareMessage = - certificate.getIbftPrePrepareMessage(); + final PreparedCertificate certificate, final ConsensusRoundIdentifier roundChangeTarget) { + final SignedData proposalMessage = certificate.getProposalPayload(); final ConsensusRoundIdentifier prepareCertRound = - preprepareMessage.getUnsignedMessageData().getRoundIdentifier(); + proposalMessage.getPayload().getRoundIdentifier(); - if (!validatePreprepareCertificateRound(prepareCertRound, roundChangeTarget)) { + if (!validatePreparedCertificateRound(prepareCertRound, roundChangeTarget)) { return false; } @@ -90,22 +86,21 @@ public class RoundChangeMessageValidator { } private boolean validateConsistencyOfPrepareCertificateMessages( - final IbftPreparedCertificate certificate, final MessageValidator messageValidator) { + final PreparedCertificate certificate, final MessageValidator messageValidator) { - if (!messageValidator.addPreprepareMessage(certificate.getIbftPrePrepareMessage())) { - LOG.info("Invalid RoundChange message, embedded Preprepare message failed validation."); + if (!messageValidator.addSignedProposalPayload(certificate.getProposalPayload())) { + LOG.info("Invalid RoundChange message, embedded Proposal message failed validation."); return false; } - if (certificate.getIbftPrepareMessages().size() < minimumPrepareMessages) { + if (certificate.getPreparePayloads().size() < minimumPrepareMessages) { LOG.info( - "Invalid RoundChange message, insufficient prepare messages exist to justify " + "Invalid RoundChange message, insufficient Prepare messages exist to justify " + "prepare certificate."); return false; } - for (final IbftSignedMessageData prepareMsg : - certificate.getIbftPrepareMessages()) { + for (final SignedData prepareMsg : certificate.getPreparePayloads()) { if (!messageValidator.validatePrepareMessage(prepareMsg)) { LOG.info("Invalid RoundChange message, embedded Prepare message failed validation."); return false; @@ -115,18 +110,18 @@ public class RoundChangeMessageValidator { return true; } - private boolean validatePreprepareCertificateRound( + private boolean validatePreparedCertificateRound( final ConsensusRoundIdentifier prepareCertRound, final ConsensusRoundIdentifier roundChangeTarget) { if (prepareCertRound.getSequenceNumber() != roundChangeTarget.getSequenceNumber()) { - LOG.info("Invalid RoundChange message, PreprepareCertificate is not for local chain height."); + LOG.info("Invalid RoundChange message, PreparedCertificate is not for local chain height."); return false; } if (prepareCertRound.getRoundNumber() >= roundChangeTarget.getRoundNumber()) { LOG.info( - "Invalid RoundChange message, PreprepareCertificate is newer than RoundChange target."); + "Invalid RoundChange message, PreparedCertificate is newer than RoundChange target."); return false; } return true; diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidatorTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidatorTest.java index 79bbbf1113..7a4b594fff 100644 --- a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidatorTest.java +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/MessageValidatorTest.java @@ -20,11 +20,11 @@ import static org.mockito.Mockito.when; import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier; import tech.pegasys.pantheon.consensus.ibft.IbftContext; import tech.pegasys.pantheon.consensus.ibft.IbftExtraData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftMessageFactory; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedCommitMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrePrepareMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrepareMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.CommitPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.MessageFactory; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.crypto.SECP256K1; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.ethereum.ProtocolContext; @@ -55,10 +55,9 @@ public class MessageValidatorTest { private final KeyPair proposerKey = KeyPair.generate(); private final KeyPair validatorKey = KeyPair.generate(); private final KeyPair nonValidatorKey = KeyPair.generate(); - private final IbftMessageFactory proposerMessageFactory = new IbftMessageFactory(proposerKey); - private final IbftMessageFactory validatorMessageFactory = new IbftMessageFactory(validatorKey); - private final IbftMessageFactory nonValidatorMessageFactory = - new IbftMessageFactory(nonValidatorKey); + private final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey); + private final MessageFactory validatorMessageFactory = new MessageFactory(validatorKey); + private final MessageFactory nonValidatorMessageFactory = new MessageFactory(nonValidatorKey); private final List
validators = Lists.newArrayList(); @@ -107,16 +106,16 @@ public class MessageValidatorTest { @Test public void receivingAPrepareMessageBeforePrePrepareFails() { - final IbftSignedMessageData prepareMsg = - proposerMessageFactory.createIbftSignedPrepareMessageData(roundIdentifier, Hash.ZERO); + final SignedData prepareMsg = + proposerMessageFactory.createSignedPreparePayload(roundIdentifier, Hash.ZERO); assertThat(validator.validatePrepareMessage(prepareMsg)).isFalse(); } @Test public void receivingACommitMessageBeforePreprepareFails() { - final IbftSignedMessageData commitMsg = - proposerMessageFactory.createIbftSignedCommitMessageData( + final SignedData commitMsg = + proposerMessageFactory.createSignedCommitPayload( roundIdentifier, Hash.ZERO, SECP256K1.sign(Hash.ZERO, proposerKey)); assertThat(validator.validateCommmitMessage(commitMsg)).isFalse(); @@ -124,48 +123,46 @@ public class MessageValidatorTest { @Test public void receivingPreprepareMessageFromNonProposerFails() { - final IbftSignedMessageData preprepareMsg = - validatorMessageFactory.createIbftSignedPrePrepareMessageData( - roundIdentifier, mock(Block.class)); + final SignedData preprepareMsg = + validatorMessageFactory.createSignedProposalPayload(roundIdentifier, mock(Block.class)); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isFalse(); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isFalse(); } @Test public void receivingPreprepareMessageWithIllegalBlockFails() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(false); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData( - roundIdentifier, mock(Block.class)); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, mock(Block.class)); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isFalse(); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isFalse(); } @Test public void receivingPrepareFromProposerFails() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); - final IbftSignedMessageData prepareMsg = - proposerMessageFactory.createIbftSignedPrepareMessageData(roundIdentifier, block.getHash()); + final SignedData prepareMsg = + proposerMessageFactory.createSignedPreparePayload(roundIdentifier, block.getHash()); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); assertThat(validator.validatePrepareMessage(prepareMsg)).isFalse(); } @Test public void receivingPrepareFromNonValidatorFails() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - final IbftSignedMessageData prepareMsg = - nonValidatorMessageFactory.createIbftSignedPrepareMessageData( - roundIdentifier, block.getHash()); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + final SignedData prepareMsg = + nonValidatorMessageFactory.createSignedPreparePayload(roundIdentifier, block.getHash()); + + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); assertThat(validator.validatePrepareMessage(prepareMsg)).isFalse(); } @@ -173,20 +170,19 @@ public class MessageValidatorTest { public void receivingMessagesWithDifferentRoundIdFromPreprepareFails() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); final ConsensusRoundIdentifier invalidRoundIdentifier = new ConsensusRoundIdentifier( roundIdentifier.getSequenceNumber(), roundIdentifier.getRoundNumber() + 1); - final IbftSignedMessageData prepareMsg = - validatorMessageFactory.createIbftSignedPrepareMessageData( - invalidRoundIdentifier, block.getHash()); - final IbftSignedMessageData commitMsg = - validatorMessageFactory.createIbftSignedCommitMessageData( + final SignedData prepareMsg = + validatorMessageFactory.createSignedPreparePayload(invalidRoundIdentifier, block.getHash()); + final SignedData commitMsg = + validatorMessageFactory.createSignedCommitPayload( 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.validateCommmitMessage(commitMsg)).isFalse(); } @@ -195,13 +191,12 @@ public class MessageValidatorTest { public void receivingPrepareNonProposerValidatorWithCorrectRoundIsSuccessful() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - final IbftSignedMessageData prepareMsg = - validatorMessageFactory.createIbftSignedPrepareMessageData( - roundIdentifier, block.getHash()); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); + final SignedData prepareMsg = + validatorMessageFactory.createSignedPreparePayload(roundIdentifier, block.getHash()); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); assertThat(validator.validatePrepareMessage(prepareMsg)).isTrue(); } @@ -209,14 +204,14 @@ public class MessageValidatorTest { public void receivingACommitMessageWithAnInvalidCommitSealFails() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); - final IbftSignedMessageData commitMsg = - proposerMessageFactory.createIbftSignedCommitMessageData( + final SignedData commitMsg = + proposerMessageFactory.createSignedCommitPayload( roundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), nonValidatorKey)); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); assertThat(validator.validateCommmitMessage(commitMsg)).isFalse(); } @@ -224,18 +219,18 @@ public class MessageValidatorTest { public void commitMessageContainingValidSealFromValidatorIsSuccessful() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); - final IbftSignedMessageData proposerCommitMsg = - proposerMessageFactory.createIbftSignedCommitMessageData( + final SignedData proposerCommitMsg = + proposerMessageFactory.createSignedCommitPayload( roundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), proposerKey)); - final IbftSignedMessageData validatorCommitMsg = - validatorMessageFactory.createIbftSignedCommitMessageData( + final SignedData validatorCommitMsg = + validatorMessageFactory.createSignedCommitPayload( 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(validatorCommitMsg)).isTrue(); } @@ -244,39 +239,39 @@ public class MessageValidatorTest { public void subsequentPreprepareHasDifferentSenderFails() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); - final IbftSignedMessageData secondPreprepareMsg = - validatorMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(secondPreprepareMsg)).isFalse(); + final SignedData secondPreprepareMsg = + validatorMessageFactory.createSignedProposalPayload(roundIdentifier, block); + assertThat(validator.addSignedProposalPayload(secondPreprepareMsg)).isFalse(); } @Test public void subsequentPreprepareHasDifferentContentFails() { - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); final ConsensusRoundIdentifier newRoundIdentifier = new ConsensusRoundIdentifier(3, 0); - final IbftSignedMessageData secondPreprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(newRoundIdentifier, block); - assertThat(validator.addPreprepareMessage(secondPreprepareMsg)).isFalse(); + final SignedData secondPreprepareMsg = + proposerMessageFactory.createSignedProposalPayload(newRoundIdentifier, block); + assertThat(validator.addSignedProposalPayload(secondPreprepareMsg)).isFalse(); } @Test public void subsequentPreprepareHasIdenticalSenderAndContentIsSuccessful() { when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isTrue(); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isTrue(); - final IbftSignedMessageData secondPreprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(secondPreprepareMsg)).isTrue(); + final SignedData secondPreprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); + assertThat(validator.addSignedProposalPayload(secondPreprepareMsg)).isTrue(); } @Test @@ -284,9 +279,9 @@ public class MessageValidatorTest { insertRoundToBlockHeader(roundIdentifier.getRoundNumber() + 1); when(headerValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); - final IbftSignedMessageData preprepareMsg = - proposerMessageFactory.createIbftSignedPrePrepareMessageData(roundIdentifier, block); + final SignedData preprepareMsg = + proposerMessageFactory.createSignedProposalPayload(roundIdentifier, block); - assertThat(validator.addPreprepareMessage(preprepareMsg)).isFalse(); + assertThat(validator.addSignedProposalPayload(preprepareMsg)).isFalse(); } } diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidatorTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidatorTest.java index 404b1ae504..d2f04bd725 100644 --- a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidatorTest.java +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/validation/RoundChangeMessageValidatorTest.java @@ -21,11 +21,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.ibftmessagedata.IbftMessageFactory; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftPreparedCertificate; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedPrepareMessageData; -import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedRoundChangeMessageData; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.MessageFactory; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparedCertificate; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorFactory; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.ethereum.core.Address; @@ -46,10 +46,9 @@ public class RoundChangeMessageValidatorTest { private final KeyPair proposerKey = KeyPair.generate(); private final KeyPair validatorKey = KeyPair.generate(); private final KeyPair nonValidatorKey = KeyPair.generate(); - private final IbftMessageFactory proposerMessageFactory = new IbftMessageFactory(proposerKey); - private final IbftMessageFactory validatorMessageFactory = new IbftMessageFactory(validatorKey); - private final IbftMessageFactory nonValidatorMessageFactory = - new IbftMessageFactory(nonValidatorKey); + private final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey); + private final MessageFactory validatorMessageFactory = new MessageFactory(validatorKey); + private final MessageFactory nonValidatorMessageFactory = new MessageFactory(nonValidatorKey); private final ConsensusRoundIdentifier currentRound = new ConsensusRoundIdentifier(2, 3); private final ConsensusRoundIdentifier targetRound = new ConsensusRoundIdentifier(2, 4); @@ -73,82 +72,76 @@ public class RoundChangeMessageValidatorTest { // By default, have all basic messages being valid thus any failures are attributed to logic // in the RoundChangeMessageValidator - when(basicValidator.addPreprepareMessage(any())).thenReturn(true); + when(basicValidator.addSignedProposalPayload(any())).thenReturn(true); when(basicValidator.validatePrepareMessage(any())).thenReturn(true); } @Test public void roundChangeSentByNonValidatorFails() { - final IbftSignedMessageData msg = - nonValidatorMessageFactory.createIbftSignedRoundChangeMessageData( - targetRound, Optional.empty()); + final SignedData msg = + nonValidatorMessageFactory.createSignedRoundChangePayload(targetRound, Optional.empty()); assertThat(validator.validateMessage(msg)).isFalse(); } @Test public void roundChangeContainingNoCertificateIsSuccessful() { - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( - targetRound, Optional.empty()); + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload(targetRound, Optional.empty()); assertThat(validator.validateMessage(msg)).isTrue(); } @Test public void roundChangeContainingInvalidPreprepareFails() { - final IbftPreparedCertificate prepareCertificate = - new IbftPreparedCertificate( - proposerMessageFactory.createIbftSignedPrePrepareMessageData(currentRound, block), + final PreparedCertificate prepareCertificate = + new PreparedCertificate( + proposerMessageFactory.createSignedProposalPayload(currentRound, block), Collections.emptyList()); - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload( targetRound, Optional.of(prepareCertificate)); - when(basicValidator.addPreprepareMessage(any())).thenReturn(false); + when(basicValidator.addSignedProposalPayload(any())).thenReturn(false); assertThat(validator.validateMessage(msg)).isFalse(); verify(validatorFactory, times(1)) - .createAt( - prepareCertificate - .getIbftPrePrepareMessage() - .getUnsignedMessageData() - .getRoundIdentifier()); + .createAt(prepareCertificate.getProposalPayload().getPayload().getRoundIdentifier()); verify(basicValidator, times(1)) - .addPreprepareMessage(prepareCertificate.getIbftPrePrepareMessage()); + .addSignedProposalPayload(prepareCertificate.getProposalPayload()); verify(basicValidator, never()).validatePrepareMessage(any()); verify(basicValidator, never()).validateCommmitMessage(any()); } @Test public void roundChangeContainingValidPreprepareButNoPrepareMessagesFails() { - final IbftPreparedCertificate prepareCertificate = - new IbftPreparedCertificate( - proposerMessageFactory.createIbftSignedPrePrepareMessageData(currentRound, block), + final PreparedCertificate prepareCertificate = + new PreparedCertificate( + proposerMessageFactory.createSignedProposalPayload(currentRound, block), Collections.emptyList()); - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload( targetRound, Optional.of(prepareCertificate)); - when(basicValidator.addPreprepareMessage(any())).thenReturn(true); + when(basicValidator.addSignedProposalPayload(any())).thenReturn(true); assertThat(validator.validateMessage(msg)).isFalse(); } @Test public void roundChangeInvalidPrepareMessageFromProposerFails() { - final IbftSignedMessageData prepareMsg = - validatorMessageFactory.createIbftSignedPrepareMessageData(currentRound, block.getHash()); - final IbftPreparedCertificate prepareCertificate = - new IbftPreparedCertificate( - proposerMessageFactory.createIbftSignedPrePrepareMessageData(currentRound, block), + final SignedData prepareMsg = + validatorMessageFactory.createSignedPreparePayload(currentRound, block.getHash()); + final PreparedCertificate prepareCertificate = + new PreparedCertificate( + proposerMessageFactory.createSignedProposalPayload(currentRound, block), Lists.newArrayList(prepareMsg)); - when(basicValidator.addPreprepareMessage(any())).thenReturn(true); + when(basicValidator.addSignedProposalPayload(any())).thenReturn(true); when(basicValidator.validatePrepareMessage(any())).thenReturn(false); - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload( targetRound, Optional.of(prepareCertificate)); assertThat(validator.validateMessage(msg)).isFalse(); @@ -162,8 +155,8 @@ public class RoundChangeMessageValidatorTest { final ConsensusRoundIdentifier latterRoundIdentifier = new ConsensusRoundIdentifier(currentRound.getSequenceNumber() + 1, 1); - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload( latterRoundIdentifier, Optional.empty()); assertThat(validator.validateMessage(msg)).isFalse(); @@ -176,15 +169,15 @@ public class RoundChangeMessageValidatorTest { new ConsensusRoundIdentifier( currentRound.getSequenceNumber(), currentRound.getRoundNumber() + 2); - final IbftSignedMessageData prepareMsg = - validatorMessageFactory.createIbftSignedPrepareMessageData(futureRound, block.getHash()); - final IbftPreparedCertificate prepareCertificate = - new IbftPreparedCertificate( - proposerMessageFactory.createIbftSignedPrePrepareMessageData(futureRound, block), + final SignedData prepareMsg = + validatorMessageFactory.createSignedPreparePayload(futureRound, block.getHash()); + final PreparedCertificate prepareCertificate = + new PreparedCertificate( + proposerMessageFactory.createSignedProposalPayload(futureRound, block), Lists.newArrayList(prepareMsg)); - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload( targetRound, Optional.of(prepareCertificate)); assertThat(validator.validateMessage(msg)).isFalse(); @@ -195,30 +188,26 @@ public class RoundChangeMessageValidatorTest { @Test public void roudnChangeWithPastPreprepareForCurrentHeightIsSuccessful() { - final IbftSignedMessageData prepareMsg = - validatorMessageFactory.createIbftSignedPrepareMessageData(currentRound, block.getHash()); - final IbftPreparedCertificate prepareCertificate = - new IbftPreparedCertificate( - proposerMessageFactory.createIbftSignedPrePrepareMessageData(currentRound, block), + final SignedData prepareMsg = + validatorMessageFactory.createSignedPreparePayload(currentRound, block.getHash()); + final PreparedCertificate prepareCertificate = + new PreparedCertificate( + proposerMessageFactory.createSignedProposalPayload(currentRound, block), Lists.newArrayList(prepareMsg)); - final IbftSignedMessageData msg = - proposerMessageFactory.createIbftSignedRoundChangeMessageData( + final SignedData msg = + proposerMessageFactory.createSignedRoundChangePayload( targetRound, Optional.of(prepareCertificate)); - when(basicValidator.addPreprepareMessage(prepareCertificate.getIbftPrePrepareMessage())) + when(basicValidator.addSignedProposalPayload(prepareCertificate.getProposalPayload())) .thenReturn(true); when(basicValidator.validatePrepareMessage(prepareMsg)).thenReturn(true); assertThat(validator.validateMessage(msg)).isTrue(); verify(validatorFactory, times(1)) - .createAt( - prepareCertificate - .getIbftPrePrepareMessage() - .getUnsignedMessageData() - .getRoundIdentifier()); + .createAt(prepareCertificate.getProposalPayload().getPayload().getRoundIdentifier()); verify(basicValidator, times(1)) - .addPreprepareMessage(prepareCertificate.getIbftPrePrepareMessage()); + .addSignedProposalPayload(prepareCertificate.getProposalPayload()); verify(basicValidator, times(1)).validatePrepareMessage(prepareMsg); } }