Search in sources :

Example 1 with BlockTimerExpiry

use of org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry in project besu by hyperledger.

the class BlockTimer method startTimer.

/**
 * Starts a timer for the supplied round cancelling any previously active block timer
 *
 * @param round The round identifier which this timer is tracking
 * @param chainHeadHeader The header of the chain head
 */
public synchronized void startTimer(final ConsensusRoundIdentifier round, final BlockHeader chainHeadHeader) {
    cancelTimer();
    final long now = clock.millis();
    // absolute time when the timer is supposed to expire
    final int blockPeriodSeconds = forksSchedule.getFork(round.getSequenceNumber()).getValue().getBlockPeriodSeconds();
    final long minimumTimeBetweenBlocksMillis = blockPeriodSeconds * 1000L;
    final long expiryTime = chainHeadHeader.getTimestamp() * 1_000 + minimumTimeBetweenBlocksMillis;
    if (expiryTime > now) {
        final long delay = expiryTime - now;
        final Runnable newTimerRunnable = () -> queue.add(new BlockTimerExpiry(round));
        final ScheduledFuture<?> newTimerTask = bftExecutors.scheduleTask(newTimerRunnable, delay, TimeUnit.MILLISECONDS);
        currentTimerTask = Optional.of(newTimerTask);
    } else {
        queue.add(new BlockTimerExpiry(round));
    }
}
Also used : BlockTimerExpiry(org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry)

Example 2 with BlockTimerExpiry

use of org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry in project besu by hyperledger.

the class ValidatorContractTest method createNewBlockAsProposer.

private void createNewBlockAsProposer(final TestContext context, final long blockNumber) {
    ConsensusRoundIdentifier roundId = new ConsensusRoundIdentifier(blockNumber, 0);
    // trigger proposal
    context.getController().handleBlockTimerExpiry(new BlockTimerExpiry(roundId));
    // peers commit proposed block
    Block proposedBlock = context.createBlockForProposalFromChainHead(clock.instant().getEpochSecond());
    RoundSpecificPeers peers = context.roundSpecificPeers(roundId);
    peers.commitForNonProposing(roundId, proposedBlock);
    assertThat(context.getCurrentChainHeight()).isEqualTo(blockNumber);
    context.getController().handleNewBlockEvent(new NewChainHead(context.getBlockchain().getChainHeadHeader()));
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) BlockTimerExpiry(org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry) RoundSpecificPeers(org.hyperledger.besu.consensus.qbft.support.RoundSpecificPeers) Block(org.hyperledger.besu.ethereum.core.Block) NewChainHead(org.hyperledger.besu.consensus.common.bft.events.NewChainHead)

Example 3 with BlockTimerExpiry

use of org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry in project besu by hyperledger.

the class LocalNodeIsProposerTest method setup.

@BeforeEach
public void setup() {
    expectedProposedBlock = context.createBlockForProposalFromChainHead(blockTimeStamp);
    expectedTxProposal = localNodeMessageFactory.createProposal(roundId, expectedProposedBlock, Collections.emptyList(), Collections.emptyList());
    expectedTxPrepare = localNodeMessageFactory.createPrepare(roundId, expectedProposedBlock.getHash());
    expectedTxCommit = new Commit(createSignedCommitPayload(roundId, expectedProposedBlock, context.getLocalNodeParams().getNodeKey()));
    // Trigger "block timer" to send proposal.
    context.getController().handleBlockTimerExpiry(new BlockTimerExpiry(roundId));
}
Also used : BlockTimerExpiry(org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry) Commit(org.hyperledger.besu.consensus.qbft.messagewrappers.Commit) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with BlockTimerExpiry

use of org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry in project besu by hyperledger.

the class IbftControllerTest method handlesBlockTimerExpiry.

@Test
public void handlesBlockTimerExpiry() {
    final BlockTimerExpiry blockTimerExpiry = new BlockTimerExpiry(roundIdentifier);
    constructIbftController();
    ibftController.start();
    ibftController.handleBlockTimerExpiry(blockTimerExpiry);
    verify(blockHeightManager).handleBlockTimerExpiry(roundIdentifier);
}
Also used : BlockTimerExpiry(org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry) Test(org.junit.Test)

Example 5 with BlockTimerExpiry

use of org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry in project besu by hyperledger.

the class BlockTimerTest method eventIsImmediatelyAddedToTheQueueIfAbsoluteExpiryIsEqualToNow.

@Test
public void eventIsImmediatelyAddedToTheQueueIfAbsoluteExpiryIsEqualToNow() {
    final int MINIMAL_TIME_BETWEEN_BLOCKS_SECONDS = 15;
    final long NOW_MILLIS = 515_000L;
    final long BLOCK_TIME_STAMP = 500;
    when(mockForksSchedule.getFork(anyLong())).thenReturn(new ForkSpec<>(0, createBftFork(MINIMAL_TIME_BETWEEN_BLOCKS_SECONDS)));
    final BlockTimer timer = new BlockTimer(mockQueue, mockForksSchedule, bftExecutors, mockClock);
    when(mockClock.millis()).thenReturn(NOW_MILLIS);
    final BlockHeader header = new BlockHeaderTestFixture().timestamp(BLOCK_TIME_STAMP).buildHeader();
    final ConsensusRoundIdentifier round = new ConsensusRoundIdentifier(0xFEDBCA9876543210L, 0x12345678);
    timer.startTimer(round, header);
    verify(bftExecutors, never()).scheduleTask(any(Runnable.class), anyLong(), any());
    final ArgumentCaptor<BftEvent> bftEventCaptor = ArgumentCaptor.forClass(BftEvent.class);
    verify(mockQueue).add(bftEventCaptor.capture());
    assertThat(bftEventCaptor.getValue() instanceof BlockTimerExpiry).isTrue();
    assertThat(((BlockTimerExpiry) bftEventCaptor.getValue()).getRoundIndentifier()).usingRecursiveComparison().isEqualTo(round);
}
Also used : BlockTimerExpiry(org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) BftEvent(org.hyperledger.besu.consensus.common.bft.events.BftEvent) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

BlockTimerExpiry (org.hyperledger.besu.consensus.common.bft.events.BlockTimerExpiry)12 Test (org.junit.Test)6 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)2 BftEvent (org.hyperledger.besu.consensus.common.bft.events.BftEvent)2 NewChainHead (org.hyperledger.besu.consensus.common.bft.events.NewChainHead)2 RoundExpiry (org.hyperledger.besu.consensus.common.bft.events.RoundExpiry)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 BftReceivedMessageEvent (org.hyperledger.besu.consensus.common.bft.events.BftReceivedMessageEvent)1 Commit (org.hyperledger.besu.consensus.ibft.messagewrappers.Commit)1 Commit (org.hyperledger.besu.consensus.qbft.messagewrappers.Commit)1 RoundChange (org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange)1 RoundSpecificPeers (org.hyperledger.besu.consensus.qbft.support.RoundSpecificPeers)1 Block (org.hyperledger.besu.ethereum.core.Block)1 Test (org.junit.jupiter.api.Test)1