mirror of https://github.com/hyperledger/besu
[EC-183] Added RLP enc/dec for PrePrepare, Commit and NewRound messages (#200)
parent
448e465f3f
commit
14a8f3780a
@ -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.ibftmessage; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; |
||||||
|
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedCommitMessageData; |
||||||
|
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 { |
||||||
|
|
||||||
|
private static final int MESSAGE_CODE = IbftV2.COMMIT; |
||||||
|
|
||||||
|
private IbftCommitMessage(final BytesValue data) { |
||||||
|
super(data); |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftCommitMessage fromMessage(final MessageData message) { |
||||||
|
return fromMessage(message, MESSAGE_CODE, IbftCommitMessage.class, IbftCommitMessage::new); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public IbftSignedMessageData<IbftUnsignedCommitMessageData> decode() { |
||||||
|
return IbftSignedMessageData.readIbftSignedCommitMessageDataFrom(RLP.input(data)); |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftCommitMessage create( |
||||||
|
final IbftSignedMessageData<IbftUnsignedCommitMessageData> ibftPrepareMessageDecoded) { |
||||||
|
|
||||||
|
return new IbftCommitMessage(ibftPrepareMessageDecoded.encode()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getCode() { |
||||||
|
return MESSAGE_CODE; |
||||||
|
} |
||||||
|
} |
@ -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.ibftmessage; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftSignedMessageData; |
||||||
|
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.IbftUnsignedNewRoundMessageData; |
||||||
|
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 { |
||||||
|
|
||||||
|
private static final int MESSAGE_CODE = IbftV2.NEW_ROUND; |
||||||
|
|
||||||
|
private IbftNewRoundMessage(final BytesValue data) { |
||||||
|
super(data); |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftNewRoundMessage fromMessage(final MessageData message) { |
||||||
|
return fromMessage(message, MESSAGE_CODE, IbftNewRoundMessage.class, IbftNewRoundMessage::new); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public IbftSignedMessageData<IbftUnsignedNewRoundMessageData> decode() { |
||||||
|
return IbftSignedMessageData.readIbftSignedNewRoundMessageDataFrom(RLP.input(data)); |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftNewRoundMessage create( |
||||||
|
final IbftSignedMessageData<IbftUnsignedNewRoundMessageData> ibftPrepareMessageDecoded) { |
||||||
|
|
||||||
|
return new IbftNewRoundMessage(ibftPrepareMessageDecoded.encode()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getCode() { |
||||||
|
return MESSAGE_CODE; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
/* |
||||||
|
* 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<IbftSignedMessageData<IbftUnsignedRoundChangeMessageData>> |
||||||
|
ibftRoundChangeMessages; |
||||||
|
|
||||||
|
public IbftRoundChangeCertificate( |
||||||
|
final Collection<IbftSignedMessageData<IbftUnsignedRoundChangeMessageData>> |
||||||
|
ibftRoundChangeMessages) { |
||||||
|
this.ibftRoundChangeMessages = ibftRoundChangeMessages; |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftRoundChangeCertificate readFrom(final RLPInput rlpInput) { |
||||||
|
final Collection<IbftSignedMessageData<IbftUnsignedRoundChangeMessageData>> |
||||||
|
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<IbftSignedMessageData<IbftUnsignedRoundChangeMessageData>> |
||||||
|
getIbftRoundChangeMessages() { |
||||||
|
return ibftRoundChangeMessages; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,69 @@ |
|||||||
|
/* |
||||||
|
* 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.consensus.ibft.ibftmessage.IbftV2; |
||||||
|
import tech.pegasys.pantheon.crypto.SECP256K1.Signature; |
||||||
|
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 { |
||||||
|
private static final int TYPE = IbftV2.COMMIT; |
||||||
|
private final Hash digest; |
||||||
|
private final Signature commitSeal; |
||||||
|
|
||||||
|
public IbftUnsignedCommitMessageData( |
||||||
|
final ConsensusRoundIdentifier roundIdentifier, |
||||||
|
final Hash digest, |
||||||
|
final Signature commitSeal) { |
||||||
|
super(roundIdentifier); |
||||||
|
this.digest = digest; |
||||||
|
this.commitSeal = commitSeal; |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftUnsignedCommitMessageData readFrom(final RLPInput rlpInput) { |
||||||
|
|
||||||
|
rlpInput.enterList(); |
||||||
|
final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); |
||||||
|
final Hash digest = readDigest(rlpInput); |
||||||
|
final Signature commitSeal = rlpInput.readBytesValue(Signature::decode); |
||||||
|
rlpInput.leaveList(); |
||||||
|
|
||||||
|
return new IbftUnsignedCommitMessageData(roundIdentifier, digest, commitSeal); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void writeTo(final RLPOutput rlpOutput) { |
||||||
|
|
||||||
|
rlpOutput.startList(); |
||||||
|
roundIdentifier.writeTo(rlpOutput); |
||||||
|
rlpOutput.writeBytesValue(digest); |
||||||
|
rlpOutput.writeBytesValue(commitSeal.encodedBytes()); |
||||||
|
rlpOutput.endList(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getMessageType() { |
||||||
|
return TYPE; |
||||||
|
} |
||||||
|
|
||||||
|
public Hash getDigest() { |
||||||
|
return digest; |
||||||
|
} |
||||||
|
|
||||||
|
public Signature getCommitSeal() { |
||||||
|
return commitSeal; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
/* |
||||||
|
* 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.consensus.ibft.ibftmessage.IbftV2; |
||||||
|
import tech.pegasys.pantheon.ethereum.rlp.RLPInput; |
||||||
|
import tech.pegasys.pantheon.ethereum.rlp.RLPOutput; |
||||||
|
|
||||||
|
public class IbftUnsignedNewRoundMessageData extends AbstractIbftUnsignedMessageData { |
||||||
|
|
||||||
|
private static final int TYPE = IbftV2.NEW_ROUND; |
||||||
|
|
||||||
|
private final ConsensusRoundIdentifier roundChangeIdentifier; |
||||||
|
|
||||||
|
private final IbftRoundChangeCertificate roundChangeCertificate; |
||||||
|
|
||||||
|
private final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> ibftPrePrepareMessage; |
||||||
|
|
||||||
|
public IbftUnsignedNewRoundMessageData( |
||||||
|
final ConsensusRoundIdentifier roundIdentifier, |
||||||
|
final IbftRoundChangeCertificate roundChangeCertificate, |
||||||
|
final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> ibftPrePrepareMessage) { |
||||||
|
this.roundChangeIdentifier = roundIdentifier; |
||||||
|
this.roundChangeCertificate = roundChangeCertificate; |
||||||
|
this.ibftPrePrepareMessage = ibftPrePrepareMessage; |
||||||
|
} |
||||||
|
|
||||||
|
public ConsensusRoundIdentifier getRoundChangeIdentifier() { |
||||||
|
return roundChangeIdentifier; |
||||||
|
} |
||||||
|
|
||||||
|
public IbftRoundChangeCertificate getRoundChangeCertificate() { |
||||||
|
return roundChangeCertificate; |
||||||
|
} |
||||||
|
|
||||||
|
public IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> getIbftPrePrepareMessage() { |
||||||
|
return ibftPrePrepareMessage; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void writeTo(final RLPOutput rlpOutput) { |
||||||
|
// RLP encode of the message data content (round identifier and prepared certificate)
|
||||||
|
rlpOutput.startList(); |
||||||
|
roundChangeIdentifier.writeTo(rlpOutput); |
||||||
|
roundChangeCertificate.writeTo(rlpOutput); |
||||||
|
ibftPrePrepareMessage.writeTo(rlpOutput); |
||||||
|
rlpOutput.endList(); |
||||||
|
} |
||||||
|
|
||||||
|
public static IbftUnsignedNewRoundMessageData readFrom(final RLPInput rlpInput) { |
||||||
|
|
||||||
|
rlpInput.enterList(); |
||||||
|
final ConsensusRoundIdentifier roundIdentifier = ConsensusRoundIdentifier.readFrom(rlpInput); |
||||||
|
final IbftRoundChangeCertificate roundChangeCertificate = |
||||||
|
IbftRoundChangeCertificate.readFrom(rlpInput); |
||||||
|
final IbftSignedMessageData<IbftUnsignedPrePrepareMessageData> ibftPrePrepareMessage = |
||||||
|
IbftSignedMessageData.readIbftSignedPrePrepareMessageDataFrom(rlpInput); |
||||||
|
rlpInput.leaveList(); |
||||||
|
|
||||||
|
return new IbftUnsignedNewRoundMessageData( |
||||||
|
roundIdentifier, roundChangeCertificate, ibftPrePrepareMessage); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getMessageType() { |
||||||
|
return TYPE; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue