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

Refactor QbftExtraDataCodec from new instance to injection #2645

Merged
merged 4 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected MiningCoordinator createMiningCoordinator(
final UniqueMessageMulticaster uniqueMessageMulticaster =
new UniqueMessageMulticaster(peers, qbftConfig.getGossipedHistoryLimit());

final QbftGossip gossiper = new QbftGossip(uniqueMessageMulticaster);
final QbftGossip gossiper = new QbftGossip(uniqueMessageMulticaster, bftExtraDataCodec().get());

final BftFinalState finalState =
new BftFinalState(
Expand Down Expand Up @@ -217,7 +217,8 @@ protected MiningCoordinator createMiningCoordinator(
gossiper,
duplicateMessageTracker,
futureMessageBuffer,
new EthSynchronizerUpdater(ethProtocolManager.ethContext().getEthPeers()));
new EthSynchronizerUpdater(ethProtocolManager.ethContext().getEthPeers()),
bftExtraDataCodec().get());

final EventMultiplexer eventMultiplexer = new EventMultiplexer(qbftController);
final BftProcessor bftProcessor = new BftProcessor(bftEventQueue, eventMultiplexer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
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;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
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 @@ -47,6 +49,7 @@
import com.google.common.collect.Lists;

public class RoundSpecificPeers {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be better to pass in the constructor from TestContext so we can write PKI integration tests


private final ValidatorPeer proposer;
private final Collection<ValidatorPeer> peers;
Expand Down Expand Up @@ -196,7 +199,7 @@ private void verifyMessage(final MessageData actual, final BftMessage<?> expecte

switch (expectedMessage.getMessageType()) {
case QbftV1.PROPOSAL:
actualSignedPayload = ProposalMessageData.fromMessageData(actual).decode();
actualSignedPayload = ProposalMessageData.fromMessageData(actual).decode(bftExtraDataCodec);
break;
case QbftV1.PREPARE:
actualSignedPayload = PrepareMessageData.fromMessageData(actual).decode();
Expand All @@ -205,7 +208,8 @@ private void verifyMessage(final MessageData actual, final BftMessage<?> expecte
actualSignedPayload = CommitMessageData.fromMessageData(actual).decode();
break;
case QbftV1.ROUND_CHANGE:
actualSignedPayload = RoundChangeMessageData.fromMessageData(actual).decode();
actualSignedPayload =
RoundChangeMessageData.fromMessageData(actual).decode(bftExtraDataCodec);
break;
default:
fail("Illegal QBFTV1 message type.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ public TestContext build() {
final UniqueMessageMulticaster uniqueMulticaster =
new UniqueMessageMulticaster(multicaster, GOSSIPED_HISTORY_LIMIT);

final Gossiper gossiper = useGossip ? new QbftGossip(uniqueMulticaster) : mock(Gossiper.class);
final Gossiper gossiper =
useGossip
? new QbftGossip(uniqueMulticaster, BFT_EXTRA_DATA_ENCODER)
: mock(Gossiper.class);

final StubbedSynchronizerUpdater synchronizerUpdater = new StubbedSynchronizerUpdater();

Expand Down Expand Up @@ -469,7 +472,8 @@ private static ControllerAndState createControllerAndFinalState(
gossiper,
duplicateMessageTracker,
futureMessageBuffer,
synchronizerUpdater);
synchronizerUpdater,
BFT_EXTRA_DATA_ENCODER);

final EventMultiplexer eventMultiplexer = new EventMultiplexer(qbftController);
//////////////////////////// END IBFT BesuController ////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.consensus.qbft;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.Gossiper;
import org.hyperledger.besu.consensus.common.bft.network.ValidatorMulticaster;
import org.hyperledger.besu.consensus.common.bft.payload.Authored;
Expand All @@ -34,14 +35,17 @@
public class QbftGossip implements Gossiper {

private final ValidatorMulticaster multicaster;
private final BftExtraDataCodec bftExtraDataCodec;

/**
* Constructor that attaches gossip logic to a set of multicaster
*
* @param multicaster Network connections to the remote validators
*/
public QbftGossip(final ValidatorMulticaster multicaster) {
public QbftGossip(
final ValidatorMulticaster multicaster, final BftExtraDataCodec bftExtraDataCodec) {
this.multicaster = multicaster;
this.bftExtraDataCodec = bftExtraDataCodec;
}

/**
Expand All @@ -55,7 +59,7 @@ public void send(final Message message) {
final Authored decodedMessage;
switch (messageData.getCode()) {
case QbftV1.PROPOSAL:
decodedMessage = ProposalMessageData.fromMessageData(messageData).decode();
decodedMessage = ProposalMessageData.fromMessageData(messageData).decode(bftExtraDataCodec);
break;
case QbftV1.PREPARE:
decodedMessage = PrepareMessageData.fromMessageData(messageData).decode();
Expand All @@ -64,7 +68,8 @@ public void send(final Message message) {
decodedMessage = CommitMessageData.fromMessageData(messageData).decode();
break;
case QbftV1.ROUND_CHANGE:
decodedMessage = RoundChangeMessageData.fromMessageData(messageData).decode();
decodedMessage =
RoundChangeMessageData.fromMessageData(messageData).decode(bftExtraDataCodec);
break;
default:
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.consensus.qbft.messagedata;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.messagedata.AbstractBftMessageData;
import org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
Expand All @@ -33,8 +34,8 @@ public static ProposalMessageData fromMessageData(final MessageData messageData)
messageData, MESSAGE_CODE, ProposalMessageData.class, ProposalMessageData::new);
}

public Proposal decode() {
return Proposal.decode(data);
public Proposal decode(final BftExtraDataCodec bftExtraDataCodec) {
return Proposal.decode(data, bftExtraDataCodec);
}

public static ProposalMessageData create(final Proposal proposal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.consensus.qbft.messagedata;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.messagedata.AbstractBftMessageData;
import org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
Expand All @@ -33,8 +34,8 @@ public static RoundChangeMessageData fromMessageData(final MessageData messageDa
messageData, MESSAGE_CODE, RoundChangeMessageData.class, RoundChangeMessageData::new);
}

public RoundChange decode() {
return RoundChange.decode(data);
public RoundChange decode(final BftExtraDataCodec bftExtraDataCodec) {
return RoundChange.decode(data, bftExtraDataCodec);
}

public static RoundChangeMessageData create(final RoundChange signedPayload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.consensus.qbft.messagewrappers;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.messagewrappers.BftMessage;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
Expand Down Expand Up @@ -69,10 +70,11 @@ public Bytes encode() {
return rlpOut.encoded();
}

public static Proposal decode(final Bytes data) {
public static Proposal decode(final Bytes data, final BftExtraDataCodec bftExtraDataCodec) {
final RLPInput rlpIn = RLP.input(data);
rlpIn.enterList();
final SignedData<ProposalPayload> payload = readPayload(rlpIn, ProposalPayload::readFrom);
final SignedData<ProposalPayload> payload =
readPayload(rlpIn, rlpInput -> ProposalPayload.readFrom(rlpInput, bftExtraDataCodec));

rlpIn.enterList();
final List<SignedData<RoundChangePayload>> roundChanges =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package org.hyperledger.besu.consensus.qbft.messagewrappers;

import org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions;
import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.messagewrappers.BftMessage;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
import org.hyperledger.besu.consensus.qbft.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.payload.PreparedRoundMetadata;
import org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload;
Expand All @@ -33,7 +33,6 @@

public class RoundChange extends BftMessage<RoundChangePayload> {

private static final QbftExtraDataCodec QBFT_EXTRA_DATA_ENCODER = new QbftExtraDataCodec();
private final Optional<Block> proposedBlock;
private final List<SignedData<PreparePayload>> prepares;

Expand Down Expand Up @@ -73,7 +72,7 @@ public Bytes encode() {
return rlpOut.encoded();
}

public static RoundChange decode(final Bytes data) {
public static RoundChange decode(final Bytes data, final BftExtraDataCodec bftExtraDataCodec) {

final RLPInput rlpIn = RLP.input(data);
rlpIn.enterList();
Expand All @@ -86,8 +85,7 @@ public static RoundChange decode(final Bytes data) {
} else {
block =
Optional.of(
Block.readFrom(
rlpIn, BftBlockHeaderFunctions.forCommittedSeal(QBFT_EXTRA_DATA_ENCODER)));
Block.readFrom(rlpIn, BftBlockHeaderFunctions.forCommittedSeal(bftExtraDataCodec)));
}

final List<SignedData<PreparePayload>> prepares =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package org.hyperledger.besu.consensus.qbft.payload;

import org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions;
import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
import org.hyperledger.besu.consensus.qbft.messagedata.QbftV1;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
Expand All @@ -38,12 +38,12 @@ public ProposalPayload(
this.proposedBlock = proposedBlock;
}

public static ProposalPayload readFrom(final RLPInput rlpInput) {
public static ProposalPayload readFrom(
final RLPInput rlpInput, final BftExtraDataCodec bftExtraDataCodec) {
rlpInput.enterList();
final ConsensusRoundIdentifier roundIdentifier = readConsensusRound(rlpInput);
final Block proposedBlock =
Block.readFrom(
rlpInput, BftBlockHeaderFunctions.forCommittedSeal(new QbftExtraDataCodec()));
Block.readFrom(rlpInput, BftBlockHeaderFunctions.forCommittedSeal(bftExtraDataCodec));
rlpInput.leaveList();

return new ProposalPayload(roundIdentifier, proposedBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.consensus.qbft.statemachine;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.Gossiper;
import org.hyperledger.besu.consensus.common.bft.MessageTracker;
import org.hyperledger.besu.consensus.common.bft.SynchronizerUpdater;
Expand All @@ -35,6 +36,7 @@ public class QbftController extends BaseBftController {

private BaseQbftBlockHeightManager currentHeightManager;
private final QbftBlockHeightManagerFactory qbftBlockHeightManagerFactory;
private final BftExtraDataCodec bftExtraDataCodec;

public QbftController(
final Blockchain blockchain,
Expand All @@ -43,7 +45,8 @@ public QbftController(
final Gossiper gossiper,
final MessageTracker duplicateMessageTracker,
final FutureMessageBuffer futureMessageBuffer,
final SynchronizerUpdater sychronizerUpdater) {
final SynchronizerUpdater sychronizerUpdater,
final BftExtraDataCodec bftExtraDataCodec) {

super(
blockchain,
Expand All @@ -53,6 +56,7 @@ public QbftController(
futureMessageBuffer,
sychronizerUpdater);
this.qbftBlockHeightManagerFactory = qbftBlockHeightManagerFactory;
this.bftExtraDataCodec = bftExtraDataCodec;
}

@Override
Expand All @@ -63,7 +67,7 @@ protected void handleMessage(final Message message) {
case QbftV1.PROPOSAL:
consumeMessage(
message,
ProposalMessageData.fromMessageData(messageData).decode(),
ProposalMessageData.fromMessageData(messageData).decode(bftExtraDataCodec),
currentHeightManager::handleProposalPayload);
break;

Expand All @@ -84,7 +88,7 @@ protected void handleMessage(final Message message) {
case QbftV1.ROUND_CHANGE:
consumeMessage(
message,
RoundChangeMessageData.fromMessageData(messageData).decode(),
RoundChangeMessageData.fromMessageData(messageData).decode(bftExtraDataCodec),
currentHeightManager::handleRoundChangePayload);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.consensus.qbt.support;

import org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions;
import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
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.SignedData;
Expand All @@ -37,6 +38,8 @@
import org.apache.tuweni.bytes.Bytes;

public class ProposalMessage implements RlpTestCaseMessage {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();

private final SignedProposal signedProposal;
private final List<SignedRoundChange> roundChanges;

Expand All @@ -54,7 +57,7 @@ public ProposalMessage(

@Override
public BftMessage<ProposalPayload> fromRlp(final Bytes rlp) {
return Proposal.decode(rlp);
return Proposal.decode(rlp, bftExtraDataCodec);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions.forCommittedSeal;

import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
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.SignedData;
Expand All @@ -40,6 +41,8 @@
import org.apache.tuweni.bytes.Bytes;

public class RoundChangeMessage implements RlpTestCaseMessage {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();

private final SignedRoundChange signedRoundChange;
private final Optional<String> block;

Expand All @@ -57,7 +60,7 @@ public RoundChangeMessage(

@Override
public BftMessage<RoundChangePayload> fromRlp(final Bytes rlp) {
return RoundChange.decode(rlp);
return RoundChange.decode(rlp, bftExtraDataCodec);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.consensus.common.bft.BftExtraData;
import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
Expand All @@ -41,16 +42,15 @@
import org.junit.Test;

public class ProposalTest {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();
jframe marked this conversation as resolved.
Show resolved Hide resolved

private static final BftExtraData extraData =
new BftExtraData(
Bytes32.ZERO, Collections.emptyList(), Optional.empty(), 1, Collections.emptyList());

private static final Block BLOCK =
new Block(
new BlockHeaderTestFixture()
.extraData(new QbftExtraDataCodec().encode(extraData))
.buildHeader(),
new BlockHeaderTestFixture().extraData(bftExtraDataCodec.encode(extraData)).buildHeader(),
new BlockBody(Collections.emptyList(), Collections.emptyList()));

@Test
Expand Down Expand Up @@ -78,7 +78,7 @@ public void canRoundTripProposalMessage() {

final Proposal proposal = new Proposal(signedPayload, List.of(roundChange), List.of(prepare));

final Proposal decodedProposal = Proposal.decode(proposal.encode());
final Proposal decodedProposal = Proposal.decode(proposal.encode(), bftExtraDataCodec);

assertThat(decodedProposal.getAuthor()).isEqualTo(addr);
assertThat(decodedProposal.getMessageType()).isEqualTo(QbftV1.PROPOSAL);
Expand Down
Loading