Skip to content

Commit

Permalink
Qbft rework artifacts (#1781)
Browse files Browse the repository at this point in the history
Refactor how Prepared/RoundChangeArtifacts are used in QBFT business logic.

Signed-off-by: Trent Mohay <trent.mohay@consensys.net>
  • Loading branch information
rain-on authored Jan 11, 2021
1 parent 6f23ac1 commit 70b37c7
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@
import org.hyperledger.besu.consensus.common.bft.BftExtraData;
import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.qbft.payload.CommitPayload;
import org.hyperledger.besu.consensus.qbft.payload.MessageFactory;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;

import java.util.Collections;
import java.util.stream.Collectors;

public class IntegrationTestHelpers {

public static SignedData<CommitPayload> createSignedCommitPayload(
Expand All @@ -45,17 +41,13 @@ public static SignedData<CommitPayload> createSignedCommitPayload(
return messageFactory.createCommit(roundId, block.getHash(), commitSeal).getSignedPayload();
}

public static PreparedRoundArtifacts createValidPreparedRoundArtifacts(
public static PreparedCertificate createValidPreparedRoundArtifacts(
final TestContext context, final ConsensusRoundIdentifier preparedRound, final Block block) {
final RoundSpecificPeers peers = context.roundSpecificPeers(preparedRound);

return new PreparedRoundArtifacts(
peers
.getProposer()
.getMessageFactory()
.createProposal(preparedRound, block, Collections.emptyList(), Collections.emptyList()),
peers.createSignedPreparePayloadOfNonProposing(preparedRound, block.getHash()).stream()
.map(Prepare::new)
.collect(Collectors.toList()));
return new PreparedCertificate(
block,
peers.createSignedPreparePayloadOfNonProposing(preparedRound, block.getHash()),
preparedRound.getRoundNumber());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
Expand Down Expand Up @@ -109,13 +109,12 @@ public List<SignedData<RoundChangePayload>> createSignedRoundChangePayload(
}

public List<SignedData<RoundChangePayload>> createSignedRoundChangePayload(
final ConsensusRoundIdentifier roundId, final PreparedRoundArtifacts preparedRoundArtifacts) {
final ConsensusRoundIdentifier roundId, final PreparedCertificate preparedCertificate) {
return peers.stream()
.map(
p ->
p.getMessageFactory()
.createRoundChange(
roundId, Optional.of(preparedRoundArtifacts.getPreparedCertificate()))
.createRoundChange(roundId, Optional.of(preparedCertificate))
.getSignedPayload())
.collect(Collectors.toList());
}
Expand All @@ -132,7 +131,7 @@ public void forNonProposing(final Consumer<ValidatorPeer> assertion) {
nonProposingPeers.forEach(assertion);
}

public Collection<SignedData<PreparePayload>> createSignedPreparePayloadOfNonProposing(
public List<SignedData<PreparePayload>> createSignedPreparePayloadOfNonProposing(
final ConsensusRoundIdentifier preparedRound, final Hash hash) {
return nonProposingPeers.stream()
.map(role -> role.getMessageFactory().createPrepare(preparedRound, hash).getSignedPayload())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.consensus.common.bft.EventMultiplexer;
import org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer;
import org.hyperledger.besu.consensus.common.bft.inttest.NodeParams;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.messagedata.CommitMessageData;
import org.hyperledger.besu.consensus.qbft.messagedata.PrepareMessageData;
import org.hyperledger.besu.consensus.qbft.messagedata.ProposalMessageData;
Expand All @@ -27,13 +28,15 @@
import org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.qbft.payload.MessageFactory;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangeMetadata;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

// Each "inject" function returns the SignedPayload representation of the transmitted message.
Expand Down Expand Up @@ -78,25 +81,20 @@ public Commit injectCommit(

public Proposal injectProposalForFutureRound(
final ConsensusRoundIdentifier rId,
final RoundChangeMetadata roundChangeMetadata,
final List<SignedData<RoundChangePayload>> roundChanges,
final List<SignedData<PreparePayload>> prepares,
final Block blockToPropose) {

final Proposal payload =
messageFactory.createProposal(
rId,
blockToPropose,
roundChangeMetadata.getRoundChangePayloads(),
roundChangeMetadata.getPrepares());
messageFactory.createProposal(rId, blockToPropose, roundChanges, prepares);
injectMessage(ProposalMessageData.create(payload));
return payload;
}

public RoundChange injectRoundChange(
final ConsensusRoundIdentifier rId,
final Optional<PreparedRoundArtifacts> preparedRoundArtifacts) {
final RoundChange payload =
messageFactory.createRoundChange(
rId, preparedRoundArtifacts.map(PreparedRoundArtifacts::getPreparedCertificate));
final Optional<PreparedCertificate> preparedRoundArtifacts) {
final RoundChange payload = messageFactory.createRoundChange(rId, preparedRoundArtifacts);
injectMessage(RoundChangeMessageData.create(payload));
return payload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
import org.hyperledger.besu.consensus.qbft.payload.PayloadDeserializers;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.payload.ProposalPayload;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangeMetadata;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPInput;

import java.util.List;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;

Expand Down Expand Up @@ -57,14 +55,6 @@ public Block getBlock() {
return getPayload().getProposedBlock();
}

public Optional<RoundChangeMetadata> getRoundChangeMetadata() {
if (!roundChanges.isEmpty() && !prepares.isEmpty()) {
return Optional.of(new RoundChangeMetadata(Optional.of(getBlock()), roundChanges, prepares));
} else {
return Optional.empty();
}
}

@Override
public Bytes encode() {
final BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.qbft.payload.MessageFactory;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.payload.PreparedCertificate;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hyperledger.besu.consensus.qbft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.qbft.statemachine.PreparedCertificate;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;
Expand Down Expand Up @@ -53,19 +54,15 @@ public Proposal createProposal(
}

public Prepare createPrepare(final ConsensusRoundIdentifier roundIdentifier, final Hash digest) {

final PreparePayload payload = new PreparePayload(roundIdentifier, digest);

return new Prepare(createSignedMessage(payload));
}

public Commit createCommit(
final ConsensusRoundIdentifier roundIdentifier,
final Hash digest,
final Signature commitSeal) {

final CommitPayload payload = new CommitPayload(roundIdentifier, digest, commitSeal);

return new Commit(createSignedMessage(payload));
}

Expand All @@ -75,24 +72,18 @@ public RoundChange createRoundChange(

final RoundChangePayload payload;
if (preparedRoundData.isPresent()) {
final int round =
preparedRoundData
.get()
.getPrepares()
.get(0)
.getPayload()
.getRoundIdentifier()
.getRoundNumber();

final Block preparedBlock = preparedRoundData.get().getBlock();
payload =
new RoundChangePayload(
roundIdentifier,
Optional.of(
new PreparedRoundMetadata(
preparedRoundData.get().getPreparedBlock().getHash(), round)));
preparedBlock.getHash(), preparedRoundData.get().getRound())));

return new RoundChange(
createSignedMessage(payload),
Optional.of(preparedRoundData.get().getPreparedBlock()),
Optional.of(preparedBlock),
preparedRoundData.get().getPrepares());

} else {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,33 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.consensus.qbft.payload;
package org.hyperledger.besu.consensus.qbft.statemachine;

import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
import org.hyperledger.besu.ethereum.core.Block;

import java.util.List;

public class PreparedCertificate {
private final Block preparedBlock;

private final Block block;
private final List<SignedData<PreparePayload>> prepares;
private final int round;

public PreparedCertificate(
final Block preparedBlock, final List<SignedData<PreparePayload>> prepares) {
this.preparedBlock = preparedBlock;
final Block block, final List<SignedData<PreparePayload>> prepares, final int round) {
this.block = block;
this.prepares = prepares;
this.round = round;
}

public int getRound() {
return round;
}

public Block getPreparedBlock() {
return preparedBlock;
public Block getBlock() {
return block;
}

public List<SignedData<PreparePayload>> getPrepares() {
Expand Down
Loading

0 comments on commit 70b37c7

Please sign in to comment.