Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qbft rework artifacts #1781

Merged
merged 7 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up artifacts
Signed-off-by: Trent Mohay <trent.mohay@consensys.net>
  • Loading branch information
Trent Mohay committed Jan 11, 2021
commit aa7cf7c8a2b7455fc3bd07f5afb63e759d1a3fd4
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 @@ -18,14 +18,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.common.bft.messagewrappers.BftMessage;
import org.hyperledger.besu.consensus.common.bft.payload.Payload;
Expand All @@ -43,6 +35,16 @@
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class RoundSpecificPeers {

private final ValidatorPeer proposer;
Expand Down Expand Up @@ -112,8 +114,7 @@ public List<SignedData<RoundChangePayload>> createSignedRoundChangePayload(
.map(
p ->
p.getMessageFactory()
.createRoundChange(
roundId, Optional.of(preparedCertificate))
.createRoundChange(roundId, Optional.of(preparedCertificate))
.getSignedPayload())
.collect(Collectors.toList());
}
Expand All @@ -130,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 @@ -14,9 +14,6 @@
*/
package org.hyperledger.besu.consensus.qbft.support;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.common.bft.EventMultiplexer;
import org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer;
Expand All @@ -38,6 +35,10 @@
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.
public class ValidatorPeer extends DefaultValidatorPeer {

Expand Down Expand Up @@ -85,20 +86,15 @@ public Proposal injectProposalForFutureRound(
final Block blockToPropose) {

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

public RoundChange injectRoundChange(
final ConsensusRoundIdentifier rId,
final Optional<PreparedCertificate> preparedRoundArtifacts) {
final RoundChange payload =
messageFactory.createRoundChange(rId, 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 @@ -77,8 +77,9 @@ public RoundChange createRoundChange(
payload =
new RoundChangePayload(
roundIdentifier,
Optional.of(new PreparedRoundMetadata(preparedBlock.getHash(),
preparedRoundData.get().getRound())));
Optional.of(
new PreparedRoundMetadata(
preparedBlock.getHash(), preparedRoundData.get().getRound())));

return new RoundChange(
createSignedMessage(payload), Optional.of(preparedBlock), Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class PreparedCertificate {
private final List<SignedData<PreparePayload>> prepares;
private final int round;

public PreparedCertificate(final Block block, final List<SignedData<PreparePayload>> prepares,
final int round) {
public PreparedCertificate(
final Block block, final List<SignedData<PreparePayload>> prepares, final int round) {
this.block = block;
this.prepares = prepares;
this.round = round;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@
*/
package org.hyperledger.besu.consensus.qbft.statemachine;

import com.google.common.collect.Maps;
import java.time.Clock;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hyperledger.besu.consensus.common.bft.BlockTimer;
import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.common.bft.events.RoundExpiry;
Expand All @@ -42,6 +31,19 @@
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;

import java.time.Clock;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import com.google.common.collect.Maps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* Responsible for starting/clearing Consensus rounds at a given block height. One of these is
* created when a new block is imported to the chain. It immediately then creates a Round-0 object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ public Optional<PreparedCertificate> constructPreparedCertificate() {
return Optional.of(
new PreparedCertificate(
proposalMessage.get().getSignedPayload().getPayload().getProposedBlock(),
prepareMessages.stream()
.map(Prepare::getSignedPayload)
.collect(Collectors.toList()), roundIdentifier.getRoundNumber()));
prepareMessages.stream().map(Prepare::getSignedPayload).collect(Collectors.toList()),
roundIdentifier.getRoundNumber()));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.qbft.network.QbftMessageTransmitter;
import org.hyperledger.besu.consensus.qbft.payload.MessageFactory;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangeMetadata;
import org.hyperledger.besu.consensus.qbft.validation.FutureRoundProposalMessageValidator;
import org.hyperledger.besu.consensus.qbft.validation.MessageValidator;
import org.hyperledger.besu.consensus.qbft.validation.MessageValidatorFactory;
Expand Down Expand Up @@ -266,8 +265,8 @@ public void whenSufficientRoundChangesAreReceivedAProposalMessageIsTransmitted()
final ConsensusRoundIdentifier futureRoundIdentifier = createFrom(roundIdentifier, 0, +2);
final RoundChange roundChange =
messageFactory.createRoundChange(futureRoundIdentifier, Optional.empty());
final RoundChangeMetadata roundChangMetadata =
RoundChangeMetadata.create(singletonList(roundChange));
final RoundChangeArtifacts roundChangArtifacts =
RoundChangeArtifacts.create(singletonList(roundChange));

when(roundChangeManager.appendRoundChangeMessage(any()))
.thenReturn(Optional.of(singletonList(roundChange)));
Expand All @@ -290,8 +289,8 @@ public void whenSufficientRoundChangesAreReceivedAProposalMessageIsTransmitted()
.multicastProposal(
eq(futureRoundIdentifier),
any(),
eq(roundChangMetadata.getRoundChangePayloads()),
eq(roundChangMetadata.getPrepares()));
eq(roundChangArtifacts.getRoundChanges()),
eq(roundChangArtifacts.getBestPreparedPeer().get().getPrepares()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void aProposalWithAnewBlockIsSentUponReceptionOfARoundChangeWithNoCertifi
transmitter,
roundTimer);

round.startRoundWith(new RoundChangeMetadata(empty(), emptyList(), emptyList()), 15);
round.startRoundWith(new RoundChangeArtifacts(emptyList(), Optional.empty()), 15);
verify(transmitter, times(1))
.multicastProposal(eq(roundIdentifier), any(), eq(emptyList()), eq(emptyList()));
}
Expand All @@ -327,8 +327,7 @@ public void aProposalMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWit
final RoundChange roundChange =
messageFactory.createRoundChange(
roundIdentifier,
Optional.of(
new PreparedCertificate(proposedBlock, singletonList(preparedPayload), 2)));
Optional.of(new PreparedCertificate(proposedBlock, singletonList(preparedPayload), 2)));

final RoundChangeArtifacts roundChangeMetadata =
RoundChangeArtifacts.create(singletonList(roundChange));
Expand Down Expand Up @@ -369,10 +368,10 @@ public void creatingNewBlockFromEmptyPreparedCertificateUpdatesInternalState() {
final RoundChange roundChange =
messageFactory.createRoundChange(roundIdentifier, Optional.empty());

final RoundChangeMetadata roundChangeMetadata =
RoundChangeMetadata.create(List.of(roundChange));
final RoundChangeArtifacts roundChangeArtifacts =
RoundChangeArtifacts.create(List.of(roundChange));

round.startRoundWith(roundChangeMetadata, 15);
round.startRoundWith(roundChangeArtifacts, 15);
verify(transmitter, times(1))
.multicastProposal(
eq(roundIdentifier),
Expand Down Expand Up @@ -483,7 +482,7 @@ public void exceptionDuringNodeKeySigningDoesNotEscape() {
roundIdentifier, proposedBlock, Collections.emptyList(), Collections.emptyList()));

// Verify that no prepare message was constructed by the QbftRound
assertThat(roundState.constructPreparedRoundCertificate().get().getPrepares()).isEmpty();
assertThat(roundState.constructPreparedCertificate().get().getPrepares()).isEmpty();

verifyNoInteractions(transmitter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void defaultRoundIsNotPreparedOrCommittedAndHasNoPreparedCertificate() {

assertThat(roundState.isPrepared()).isFalse();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isEmpty();
assertThat(roundState.constructPreparedCertificate()).isEmpty();
}

@Test
Expand All @@ -92,7 +92,7 @@ public void ifProposalMessageFailsValidationMethodReturnsFalse() {
assertThat(roundState.setProposedBlock(proposal)).isFalse();
assertThat(roundState.isPrepared()).isFalse();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isEmpty();
assertThat(roundState.constructPreparedCertificate()).isEmpty();
}

@Test
Expand All @@ -108,8 +108,8 @@ public void singleValidatorIsPreparedWithJustProposal() {
assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isTrue();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isNotEmpty();
assertThat(roundState.constructPreparedRoundCertificate().get().getPreparedBlock())
assertThat(roundState.constructPreparedCertificate()).isNotEmpty();
assertThat(roundState.constructPreparedCertificate().get().getBlock())
.isEqualTo(proposal.getBlock());
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public void singleValidatorRequiresCommitMessageToBeCommitted() {
roundState.addCommitMessage(commit);
assertThat(roundState.isPrepared()).isTrue();
assertThat(roundState.isCommitted()).isTrue();
assertThat(roundState.constructPreparedRoundCertificate()).isNotEmpty();
assertThat(roundState.constructPreparedCertificate()).isNotEmpty();
}

@Test
Expand All @@ -160,12 +160,12 @@ public void prepareMessagesCanBeReceivedPriorToProposal() {
roundState.addPrepareMessage(firstPrepare);
assertThat(roundState.isPrepared()).isFalse();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isEmpty();
assertThat(roundState.constructPreparedCertificate()).isEmpty();

roundState.addPrepareMessage(secondPrepare);
assertThat(roundState.isPrepared()).isFalse();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isEmpty();
assertThat(roundState.constructPreparedCertificate()).isEmpty();

final Proposal proposal =
validatorMessageFactories
Expand All @@ -175,7 +175,7 @@ public void prepareMessagesCanBeReceivedPriorToProposal() {
assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isTrue();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isNotEmpty();
assertThat(roundState.constructPreparedCertificate()).isNotEmpty();
}

@Test
Expand Down Expand Up @@ -207,7 +207,7 @@ public void invalidPriorPrepareMessagesAreDiscardedUponSubsequentProposal() {
assertThat(roundState.setProposedBlock(proposal)).isTrue();
assertThat(roundState.isPrepared()).isFalse();
assertThat(roundState.isCommitted()).isFalse();
assertThat(roundState.constructPreparedRoundCertificate()).isEmpty();
assertThat(roundState.constructPreparedCertificate()).isEmpty();
}

@Test
Expand Down