Ibft Round to update internal state on reception of NewRound Message (#451)

tmohay 6 years ago committed by GitHub
parent cae7c9011b
commit 6375caf389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRound.java
  2. 47
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/statemachine/IbftRoundTest.java

@ -94,18 +94,18 @@ public class IbftRound {
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(
@ -134,7 +134,7 @@ public class IbftRound {
}
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();

@ -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() {
@ -105,11 +106,11 @@ public class IbftRoundTest {
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()));
@ -281,9 +282,6 @@ public class IbftRoundTest {
@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 =
@ -319,5 +317,42 @@ public class IbftRoundTest {
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();
}
}

Loading…
Cancel
Save