Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Ibft Round to update internal state on reception of NewRound Message (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rain-on authored Dec 19, 2018
1 parent cae7c90 commit 6375caf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ public void startRoundWith(
final RoundChangeCertificate roundChangeCertificate, final long headerTimestamp) {
final Optional<PreparedCertificate> latestCertificate =
findLatestPreparedCertificate(roundChangeCertificate.getRoundChangePayloads());

SignedData<ProposalPayload> proposal;
if (!latestCertificate.isPresent()) {
LOG.info("Multicasting NewRound with new block.");
final Block block = blockCreator.createBlock(headerTimestamp);
transmitter.multicastNewRound(
getRoundIdentifier(),
roundChangeCertificate,
messageFactory.createSignedProposalPayload(getRoundIdentifier(), block));
proposal = messageFactory.createSignedProposalPayload(getRoundIdentifier(), block);
} else {
final SignedData<ProposalPayload> proposal =
createProposalFromPreparedCertificate(latestCertificate.get());
transmitter.multicastNewRound(getRoundIdentifier(), roundChangeCertificate, proposal);
updateStateWithProposedBlock(proposal);
LOG.info("Multicasting NewRound from PreparedCertificate.");
proposal = createProposalFromPreparedCertificate(latestCertificate.get());
}
transmitter.multicastNewRound(getRoundIdentifier(), roundChangeCertificate, proposal);
updateStateWithProposedBlock(proposal);
}

private SignedData<ProposalPayload> createProposalFromPreparedCertificate(
Expand Down Expand Up @@ -134,7 +134,7 @@ private SignedData<ProposalPayload> createProposalFromPreparedCertificate(
}

public void handleProposalMessage(final SignedData<ProposalPayload> msg) {
LOG.info("Received a Proposal message.");
LOG.info("Handling a Proposal message.");
final Block block = msg.getPayload().getBlock();
final boolean wasCommitted = roundState.isCommitted();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public class IbftRoundTest {
private Block proposedBlock;
private IbftExtraData proposedExtraData;

final Signature remoteCommitSeal = Signature.create(BigInteger.ONE, BigInteger.ONE, (byte) 1);
private final Signature remoteCommitSeal =
Signature.create(BigInteger.ONE, BigInteger.ONE, (byte) 1);

@Before
public void setup() {
Expand All @@ -105,11 +106,11 @@ public void setup() {
Optional.empty(),
0,
Collections.emptyList());
BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture();
final BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture();
headerTestFixture.extraData(proposedExtraData.encode());
headerTestFixture.number(1);

BlockHeader header = headerTestFixture.buildHeader();
final BlockHeader header = headerTestFixture.buildHeader();
proposedBlock =
new Block(header, new BlockBody(Collections.emptyList(), Collections.emptyList()));

Expand Down Expand Up @@ -281,9 +282,6 @@ public void aNewRoundMessageWithAnewBlockIsSentUponReceptionOfARoundChangeWithNo

@Test
public void aNewRoundMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWithCertificate() {
SignedData<ProposalPayload> mockedSentMessage =
messageFactory.createSignedProposalPayload(roundIdentifier, proposedBlock);

final ConsensusRoundIdentifier priorRoundChange = new ConsensusRoundIdentifier(1, 0);
final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
final IbftRound round =
Expand Down Expand Up @@ -319,5 +317,42 @@ public void aNewRoundMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWit
IbftExtraData.decode(
payloadArgCaptor.getValue().getPayload().getBlock().getHeader().getExtraData());
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(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash()));
assertThat(roundState.isPrepared()).isTrue();
}

@Test
public void creatingNewBlockFromEmptyPreparedCertificateUpdatesInternalState() {
final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
final IbftRound round =
new IbftRound(
roundState,
blockCreator,
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
messageFactory,
transmitter);

final RoundChangeCertificate roundChangeCertificate =
new RoundChangeCertificate(
Collections.singletonList(
messageFactory.createSignedRoundChangePayload(roundIdentifier, Optional.empty())));

round.startRoundWith(roundChangeCertificate, 15);
verify(transmitter, times(1))
.multicastNewRound(
eq(roundIdentifier), eq(roundChangeCertificate), payloadArgCaptor.capture());

// 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(
messageFactory.createSignedPreparePayload(roundIdentifier, proposedBlock.getHash()));
assertThat(roundState.isPrepared()).isTrue();
}
}

0 comments on commit 6375caf

Please sign in to comment.