Search in sources :

Example 1 with TestClock

use of org.hyperledger.besu.testutil.TestClock in project besu by hyperledger.

the class TransitionsTest method transitionsBlockPeriod.

@Test
public void transitionsBlockPeriod() throws InterruptedException {
    final TestClock clock = new TestClock(Instant.EPOCH);
    final List<BftFork> bftForks = List.of(new BftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, BftFork.BLOCK_PERIOD_SECONDS_KEY, 10))), new BftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 2, BftFork.BLOCK_PERIOD_SECONDS_KEY, 20))));
    final BftEventQueue bftEventQueue = new BftEventQueue(TestContextBuilder.MESSAGE_QUEUE_LIMIT);
    final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).validatorCount(1).clock(clock).bftForks(bftForks).eventQueue(bftEventQueue).buildAndStart();
    clock.stepMillis(10_000);
    context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
    context.getController().handleNewBlockEvent(new NewChainHead(context.getBlockchain().getChainHeadHeader()));
    clock.stepMillis(20_000);
    context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
    final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
    final BlockHeader blockHeader1 = context.getBlockchain().getBlockHeader(1).get();
    final BlockHeader blockHeader2 = context.getBlockchain().getBlockHeader(2).get();
    assertThat(blockHeader1.getTimestamp()).isEqualTo(genesisBlock.getTimestamp() + 10);
    assertThat(blockHeader2.getTimestamp()).isEqualTo(blockHeader1.getTimestamp() + 20);
}
Also used : TestClock(org.hyperledger.besu.testutil.TestClock) TestContext(org.hyperledger.besu.consensus.ibft.support.TestContext) TestContextBuilder(org.hyperledger.besu.consensus.ibft.support.TestContextBuilder) NewChainHead(org.hyperledger.besu.consensus.common.bft.events.NewChainHead) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BftEventQueue(org.hyperledger.besu.consensus.common.bft.BftEventQueue) BftFork(org.hyperledger.besu.config.BftFork) Test(org.junit.jupiter.api.Test)

Example 2 with TestClock

use of org.hyperledger.besu.testutil.TestClock in project besu by hyperledger.

the class TransitionsTest method transitionsBlockPeriod.

@Test
public void transitionsBlockPeriod() throws InterruptedException {
    final TestClock clock = new TestClock(Instant.EPOCH);
    final List<QbftFork> qbftForks = List.of(new QbftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, BftFork.BLOCK_PERIOD_SECONDS_KEY, 10))), new QbftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 2, BftFork.BLOCK_PERIOD_SECONDS_KEY, 20))));
    final BftEventQueue bftEventQueue = new BftEventQueue(TestContextBuilder.MESSAGE_QUEUE_LIMIT);
    final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).validatorCount(1).clock(clock).qbftForks(qbftForks).eventQueue(bftEventQueue).buildAndStart();
    clock.stepMillis(10_000);
    context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
    context.getController().handleNewBlockEvent(new NewChainHead(context.getBlockchain().getChainHeadHeader()));
    clock.stepMillis(20_000);
    context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
    final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
    final BlockHeader blockHeader1 = context.getBlockchain().getBlockHeader(1).get();
    final BlockHeader blockHeader2 = context.getBlockchain().getBlockHeader(2).get();
    assertThat(blockHeader1.getTimestamp()).isEqualTo(genesisBlock.getTimestamp() + 10);
    assertThat(blockHeader2.getTimestamp()).isEqualTo(blockHeader1.getTimestamp() + 20);
}
Also used : TestClock(org.hyperledger.besu.testutil.TestClock) QbftFork(org.hyperledger.besu.config.QbftFork) TestContext(org.hyperledger.besu.consensus.qbft.support.TestContext) TestContextBuilder(org.hyperledger.besu.consensus.qbft.support.TestContextBuilder) NewChainHead(org.hyperledger.besu.consensus.common.bft.events.NewChainHead) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BftEventQueue(org.hyperledger.besu.consensus.common.bft.BftEventQueue) Test(org.junit.jupiter.api.Test)

Aggregations

BftEventQueue (org.hyperledger.besu.consensus.common.bft.BftEventQueue)2 NewChainHead (org.hyperledger.besu.consensus.common.bft.events.NewChainHead)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 TestClock (org.hyperledger.besu.testutil.TestClock)2 Test (org.junit.jupiter.api.Test)2 BftFork (org.hyperledger.besu.config.BftFork)1 QbftFork (org.hyperledger.besu.config.QbftFork)1 TestContext (org.hyperledger.besu.consensus.ibft.support.TestContext)1 TestContextBuilder (org.hyperledger.besu.consensus.ibft.support.TestContextBuilder)1 TestContext (org.hyperledger.besu.consensus.qbft.support.TestContext)1 TestContextBuilder (org.hyperledger.besu.consensus.qbft.support.TestContextBuilder)1