Skip to content

Commit

Permalink
Remove dependency in tests on main qbft module by using stub BftExtra…
Browse files Browse the repository at this point in the history
…DataCodec

Signed-off-by: Jason Frame <[email protected]>
  • Loading branch information
jframe committed Dec 18, 2024
1 parent fe9341a commit 3212b83
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,20 @@ public static <T extends BftContext> T setupContextWithBftExtraDataEncoder(
final Class<T> contextClazz,
final Collection<Address> validators,
final BftExtraDataCodec bftExtraDataCodec) {
return setupContextWithBftBlockInterface(
contextClazz, validators, new BftBlockInterface(bftExtraDataCodec));
}

public static <T extends BftContext> T setupContextWithBftBlockInterface(
final Class<T> contextClazz,
final Collection<Address> validators,
final BftBlockInterface bftBlockInterface) {
final T bftContext = mock(contextClazz, withSettings().strictness(Strictness.LENIENT));
final ValidatorProvider mockValidatorProvider =
mock(ValidatorProvider.class, withSettings().strictness(Strictness.LENIENT));
when(bftContext.getValidatorProvider()).thenReturn(mockValidatorProvider);
when(mockValidatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators);
when(bftContext.getBlockInterface()).thenReturn(new BftBlockInterface(bftExtraDataCodec));
when(bftContext.getBlockInterface()).thenReturn(bftBlockInterface);
when(bftContext.as(any())).thenReturn(bftContext);

return bftContext;
Expand Down
1 change: 0 additions & 1 deletion consensus/qbft-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ dependencies {
testImplementation project(path: ':config', configuration: 'testSupportArtifacts')
testImplementation project(path: ':consensus:common', configuration: 'testArtifacts')
testImplementation project(path: ':consensus:common', configuration: 'testSupportArtifacts')
testImplementation project(':consensus:qbft')
testImplementation project(':ethereum:core')
testImplementation project(path: ':ethereum:core', configuration: 'testSupportArtifacts')
testImplementation project(':crypto:algorithms')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

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;
import org.hyperledger.besu.consensus.qbft.core.messagedata.QbftV1;
import org.hyperledger.besu.consensus.qbft.core.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.core.payload.PreparedRoundMetadata;
Expand All @@ -38,19 +36,18 @@
import java.util.List;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class ProposalTest {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();

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

private static final Block BLOCK =
new Block(
new BlockHeaderTestFixture().extraData(bftExtraDataCodec.encode(extraData)).buildHeader(),
new BlockHeaderTestFixture().buildHeader(),
new BlockBody(
Collections.emptyList(),
Collections.emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

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;
import org.hyperledger.besu.consensus.qbft.core.messagedata.QbftV1;
import org.hyperledger.besu.consensus.qbft.core.payload.PreparePayload;
import org.hyperledger.besu.consensus.qbft.core.payload.PreparedRoundMetadata;
Expand All @@ -37,20 +35,18 @@
import java.util.List;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class RoundChangeTest {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();
private static final BftExtraData extraData =
new BftExtraData(
Bytes32.ZERO, Collections.emptyList(), Optional.empty(), 1, Collections.emptyList());
@Mock private BftExtraDataCodec bftExtraDataCodec;

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.hyperledger.besu.consensus.common.bft.events.RoundExpiry;
import org.hyperledger.besu.consensus.common.bft.network.ValidatorMulticaster;
import org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
import org.hyperledger.besu.consensus.qbft.core.messagedata.RoundChangeMessageData;
import org.hyperledger.besu.consensus.qbft.core.messagewrappers.Commit;
import org.hyperledger.besu.consensus.qbft.core.messagewrappers.Prepare;
Expand Down Expand Up @@ -107,7 +106,6 @@ public class QbftBlockHeightManagerTest {
private final NodeKey nodeKey = NodeKeyUtils.generate();
private final MessageFactory messageFactory = new MessageFactory(nodeKey);
private final BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture();
private final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();

@Mock private BftFinalState finalState;
@Mock private QbftMessageTransmitter messageTransmitter;
Expand All @@ -122,6 +120,7 @@ public class QbftBlockHeightManagerTest {
@Mock private FutureRoundProposalMessageValidator futureRoundProposalMessageValidator;
@Mock private ValidatorMulticaster validatorMulticaster;
@Mock private BlockHeader parentHeader;
@Mock private BftExtraDataCodec bftExtraDataCodec;

@Captor private ArgumentCaptor<MessageData> sentMessageArgCaptor;

Expand All @@ -133,11 +132,6 @@ public class QbftBlockHeightManagerTest {
private Block createdBlock;

private void buildCreatedBlock() {

final BftExtraData extraData =
new BftExtraData(Bytes.wrap(new byte[32]), emptyList(), Optional.empty(), 0, validators);

headerTestFixture.extraData(bftExtraDataCodec.encode(extraData));
final BlockHeader header = headerTestFixture.buildHeader();
createdBlock = new Block(header, new BlockBody(emptyList(), emptyList()));
}
Expand Down Expand Up @@ -175,8 +169,7 @@ public void setup() {
new ProtocolContext(
blockchain,
null,
setupContextWithBftExtraDataEncoder(
BftContext.class, validators, new QbftExtraDataCodec()),
setupContextWithBftExtraDataEncoder(BftContext.class, validators, bftExtraDataCodec),
new BadBlockManager());

final ProtocolScheduleBuilder protocolScheduleBuilder =
Expand Down Expand Up @@ -234,6 +227,12 @@ BftContext.class, validators, new QbftExtraDataCodec()),
bftExtraDataCodec,
parentHeader);
});

when(bftExtraDataCodec.decode(any()))
.thenReturn(
new BftExtraData(
Bytes.wrap(new byte[32]), emptyList(), Optional.empty(), 0, validators));
when(bftExtraDataCodec.encode(any())).thenReturn(Bytes.EMPTY);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.hyperledger.besu.consensus.common.bft.events.RoundExpiry;
import org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState;
import org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
import org.hyperledger.besu.consensus.qbft.core.messagedata.CommitMessageData;
import org.hyperledger.besu.consensus.qbft.core.messagedata.PrepareMessageData;
import org.hyperledger.besu.consensus.qbft.core.messagedata.ProposalMessageData;
Expand Down Expand Up @@ -68,14 +67,13 @@
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class QbftControllerTest {
private static final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();

@Mock private Blockchain blockChain;
@Mock private BftFinalState bftFinalState;
@Mock private QbftBlockHeightManagerFactory blockHeightManagerFactory;
@Mock private BlockHeader chainHeadBlockHeader;
@Mock private BlockHeader nextBlock;
@Mock private BaseQbftBlockHeightManager blockHeightManager;
@Mock private BftExtraDataCodec bftExtraDataCodec;

@Mock private Proposal proposal;
private Message proposalMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.consensus.common.bft.BftContextBuilder.setupContextWithBftExtraDataEncoder;
import static org.hyperledger.besu.consensus.common.bft.BftContextBuilder.setupContextWithBftBlockInterface;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -29,6 +29,7 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.consensus.common.bft.BftBlockInterface;
import org.hyperledger.besu.consensus.common.bft.BftContext;
import org.hyperledger.besu.consensus.common.bft.BftExtraData;
import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;
Expand All @@ -37,7 +38,6 @@
import org.hyperledger.besu.consensus.common.bft.RoundTimer;
import org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreator;
import org.hyperledger.besu.consensus.common.bft.payload.SignedData;
import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec;
import org.hyperledger.besu.consensus.qbft.core.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.qbft.core.network.QbftMessageTransmitter;
import org.hyperledger.besu.consensus.qbft.core.payload.MessageFactory;
Expand Down Expand Up @@ -91,7 +91,6 @@ public class QbftRoundTest {
private final MessageFactory messageFactory = new MessageFactory(nodeKey);
private final MessageFactory messageFactory2 = new MessageFactory(nodeKey2);
private final Subscribers<MinedBlockObserver> subscribers = Subscribers.create();
private final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();
private ProtocolContext protocolContext;

@Mock private BftProtocolSchedule protocolSchedule;
Expand All @@ -105,11 +104,12 @@ public class QbftRoundTest {
@Mock private ProtocolSpec protocolSpec;
@Mock private BlockImporter blockImporter;
@Mock private BlockHeader parentHeader;
@Mock private BftExtraDataCodec bftExtraDataCodec;
@Mock private BftBlockInterface bftBlockInteface;

@Captor private ArgumentCaptor<Block> blockCaptor;

private Block proposedBlock;
private BftExtraData proposedExtraData;

private final SECPSignature remoteCommitSeal =
SignatureAlgorithmFactory.getInstance()
Expand All @@ -121,18 +121,14 @@ public void setup() {
new ProtocolContext(
blockChain,
worldStateArchive,
setupContextWithBftExtraDataEncoder(
BftContext.class, emptyList(), new QbftExtraDataCodec()),
setupContextWithBftBlockInterface(BftContext.class, emptyList(), bftBlockInteface),
new BadBlockManager());

when(messageValidator.validateProposal(any())).thenReturn(true);
when(messageValidator.validatePrepare(any())).thenReturn(true);
when(messageValidator.validateCommit(any())).thenReturn(true);

proposedExtraData =
new BftExtraData(Bytes.wrap(new byte[32]), emptyList(), empty(), 0, emptyList());
final BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture();
headerTestFixture.extraData(new QbftExtraDataCodec().encode(proposedExtraData));
headerTestFixture.number(1);

final BlockHeader header = headerTestFixture.buildHeader();
Expand All @@ -149,6 +145,16 @@ BftContext.class, emptyList(), new QbftExtraDataCodec()),
when(blockImporter.importBlock(any(), any(), any()))
.thenReturn(new BlockImportResult(BlockImportResult.BlockImportStatus.IMPORTED));

BftExtraData bftExtraData =
new BftExtraData(Bytes.wrap(new byte[32]), emptyList(), empty(), 0, emptyList());
when(bftExtraDataCodec.decode(any())).thenReturn(bftExtraData);
when(bftExtraDataCodec.encode(any())).thenReturn(Bytes.EMPTY);
when(bftExtraDataCodec.encodeWithoutCommitSeals(any())).thenReturn(Bytes.EMPTY);
when(bftExtraDataCodec.encodeWithoutCommitSealsAndRoundNumber(any())).thenReturn(Bytes.EMPTY);
when(bftBlockInteface.replaceRoundInBlock(
eq(proposedBlock), eq(roundIdentifier.getRoundNumber()), any()))
.thenReturn(proposedBlock);

subscribers.subscribe(minedBlockObserver);
}

Expand Down Expand Up @@ -187,6 +193,9 @@ public void onReceptionOfValidProposalSendsAPrepareToNetworkPeers() {
bftExtraDataCodec,
parentHeader);

when(bftBlockInteface.replaceRoundInBlock(eq(proposedBlock), eq(0), any()))
.thenReturn(proposedBlock);

round.handleProposalMessage(
messageFactory.createProposal(
roundIdentifier, proposedBlock, Collections.emptyList(), Collections.emptyList()));
Expand Down Expand Up @@ -256,10 +265,6 @@ public void aProposalMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWit
verify(transmitter, times(1))
.multicastPrepare(eq(roundIdentifier), eq(blockCaptor.getValue().getHash()));

final BftExtraData proposedExtraData =
new QbftExtraDataCodec().decode(blockCaptor.getValue().getHeader());
assertThat(proposedExtraData.getRound()).isEqualTo(roundIdentifier.getRoundNumber());

// Inject a single Prepare message, and confirm the roundState has gone to Prepared (which
// indicates the block has entered the roundState (note: all msgs are deemed valid due to mocks)
round.handlePrepareMessage(
Expand Down Expand Up @@ -326,6 +331,9 @@ public void blockIsOnlyImportedOnceWhenCommitsAreReceivedBeforeProposal() {
bftExtraDataCodec,
parentHeader);

when(bftBlockInteface.replaceRoundInBlock(eq(proposedBlock), eq(0), any()))
.thenReturn(proposedBlock);

round.handleCommitMessage(
messageFactory.createCommit(roundIdentifier, proposedBlock.getHash(), remoteCommitSeal));

Expand All @@ -352,12 +360,17 @@ public void blockIsImportedOnlyOnceIfQuorumCommitsAreReceivedPriorToProposal() {
bftExtraDataCodec,
parentHeader);

when(bftBlockInteface.replaceRoundInBlock(eq(proposedBlock), eq(0), any()))
.thenReturn(proposedBlock);

round.handleCommitMessage(
messageFactory.createCommit(roundIdentifier, proposedBlock.getHash(), remoteCommitSeal));

round.handleProposalMessage(
messageFactory.createProposal(
roundIdentifier, proposedBlock, Collections.emptyList(), Collections.emptyList()));

// TODO add assertions
}

@Test
Expand All @@ -382,6 +395,9 @@ public void exceptionDuringNodeKeySigningDoesNotEscape() {
bftExtraDataCodec,
parentHeader);

when(bftBlockInteface.replaceRoundInBlock(eq(proposedBlock), eq(0), any()))
.thenReturn(proposedBlock);

round.handleProposalMessage(
messageFactory.createProposal(
roundIdentifier, proposedBlock, Collections.emptyList(), Collections.emptyList()));
Expand Down
Loading

0 comments on commit 3212b83

Please sign in to comment.