Search in sources :

Example 1 with TestContext

use of org.hyperledger.besu.consensus.qbft.support.TestContext in project besu by hyperledger.

the class ValidatorContractTest method transitionsFromValidatorContractModeToValidatorContractMode.

@Test
public void transitionsFromValidatorContractModeToValidatorContractMode() {
    final List<QbftFork> qbftForks = List.of(createContractFork(1, NEW_VALIDATOR_CONTRACT_ADDRESS));
    final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(1).nodeParams(List.of(new NodeParams(NODE_ADDRESS, NodeKeyUtils.createFrom(NODE_PRIVATE_KEY)), new NodeParams(NODE_2_ADDRESS, NodeKeyUtils.createFrom(NODE_2_PRIVATE_KEY)))).clock(clock).genesisFile(Resources.getResource("genesis_validator_contract.json").getFile()).useValidatorContract(true).qbftForks(qbftForks).buildAndStart();
    // block 0 uses validator contract with 1 validator
    // block 1 uses validator contract with 2 validators
    final List<Address> block0Addresses = List.of(NODE_ADDRESS);
    final List<Address> block1Addresses = Stream.of(NODE_ADDRESS, NODE_2_ADDRESS).sorted().collect(Collectors.toList());
    remotePeerProposesNewBlock(context, 1L);
    final ValidatorProvider validatorProvider = context.getValidatorProvider();
    final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
    final BlockHeader block1 = context.getBlockchain().getBlockHeader(1).get();
    // contract block extra data cannot contain validators or vote
    assertThat(validatorProvider.getValidatorsForBlock(genesisBlock)).isEqualTo(block0Addresses);
    assertThat(extraDataCodec.decode(genesisBlock).getValidators()).isEmpty();
    assertThat(extraDataCodec.decode(genesisBlock).getVote()).isEmpty();
    // contract block extra data cannot contain validators or vote
    assertThat(validatorProvider.getValidatorsForBlock(block1)).isEqualTo(block1Addresses);
    assertThat(extraDataCodec.decode(block1).getValidators()).isEmpty();
    assertThat(extraDataCodec.decode(block1).getVote()).isEmpty();
}
Also used : Address(org.hyperledger.besu.datatypes.Address) QbftFork(org.hyperledger.besu.config.QbftFork) TestContext(org.hyperledger.besu.consensus.qbft.support.TestContext) TestContextBuilder(org.hyperledger.besu.consensus.qbft.support.TestContextBuilder) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Test(org.junit.jupiter.api.Test)

Example 2 with TestContext

use of org.hyperledger.besu.consensus.qbft.support.TestContext in project besu by hyperledger.

the class ValidatorContractTest method transitionsFromValidatorContractModeToBlockHeaderMode.

@Test
public void transitionsFromValidatorContractModeToBlockHeaderMode() {
    final List<QbftFork> qbftForks = List.of(createBlockHeaderFork(1, List.of(NODE_ADDRESS, NODE_2_ADDRESS)));
    final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).nodeParams(List.of(new NodeParams(NODE_ADDRESS, NodeKeyUtils.createFrom(NODE_PRIVATE_KEY)), new NodeParams(NODE_2_ADDRESS, NodeKeyUtils.createFrom(NODE_2_PRIVATE_KEY)))).clock(clock).genesisFile(Resources.getResource("genesis_validator_contract.json").getFile()).useValidatorContract(true).qbftForks(qbftForks).buildAndStart();
    // block 0 uses validator contract with 1 validator
    // block 1 onwards uses block header voting with 2 overridden validators
    // block 2 uses block header voting with the cached validators
    final List<Address> block0Addresses = Stream.of(NODE_ADDRESS).collect(Collectors.toList());
    final List<Address> block1Addresses = Stream.of(NODE_ADDRESS, NODE_2_ADDRESS).sorted().collect(Collectors.toList());
    createNewBlockAsProposer(context, 1);
    clock.step(TestContextBuilder.BLOCK_TIMER_SEC, SECONDS);
    remotePeerProposesNewBlock(context, 2L);
    final ValidatorProvider validatorProvider = context.getValidatorProvider();
    final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
    final BlockHeader block1 = context.getBlockchain().getBlockHeader(1).get();
    final BlockHeader block2 = context.getBlockchain().getBlockHeader(2).get();
    // contract block extra data cannot contain validators or vote
    assertThat(validatorProvider.getValidatorsForBlock(genesisBlock)).isEqualTo(block0Addresses);
    assertThat(extraDataCodec.decode(genesisBlock).getValidators()).isEmpty();
    assertThat(extraDataCodec.decode(genesisBlock).getVote()).isEmpty();
    // uses overridden validators
    assertThat(validatorProvider.getValidatorsForBlock(block1)).isEqualTo(block1Addresses);
    assertThat(extraDataCodec.decode(block1).getValidators()).containsExactly(NODE_2_ADDRESS, NODE_ADDRESS);
    // uses cached validators
    assertThat(validatorProvider.getValidatorsForBlock(block2)).isEqualTo(block1Addresses);
    assertThat(extraDataCodec.decode(block2).getValidators()).containsExactly(NODE_2_ADDRESS, NODE_ADDRESS);
}
Also used : Address(org.hyperledger.besu.datatypes.Address) QbftFork(org.hyperledger.besu.config.QbftFork) TestContext(org.hyperledger.besu.consensus.qbft.support.TestContext) TestContextBuilder(org.hyperledger.besu.consensus.qbft.support.TestContextBuilder) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Test(org.junit.jupiter.api.Test)

Example 3 with TestContext

use of org.hyperledger.besu.consensus.qbft.support.TestContext 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)

Example 4 with TestContext

use of org.hyperledger.besu.consensus.qbft.support.TestContext in project besu by hyperledger.

the class ValidatorContractTest method retrievesValidatorsFromValidatorContract.

@Test
public void retrievesValidatorsFromValidatorContract() {
    final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).nodeParams(List.of(new NodeParams(NODE_ADDRESS, NodeKeyUtils.createFrom(NODE_PRIVATE_KEY)))).clock(TestClock.fixed()).genesisFile(Resources.getResource("genesis_validator_contract.json").getFile()).useValidatorContract(true).buildAndStart();
    createNewBlockAsProposer(context, 1);
    final ValidatorProvider validatorProvider = context.getValidatorProvider();
    final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
    final BlockHeader block1 = context.getBlockchain().getBlockHeader(1).get();
    assertThat(validatorProvider.getValidatorsForBlock(genesisBlock)).containsExactly(NODE_ADDRESS);
    assertThat(validatorProvider.getValidatorsForBlock(block1)).containsExactly(NODE_ADDRESS);
}
Also used : TestContext(org.hyperledger.besu.consensus.qbft.support.TestContext) TestContextBuilder(org.hyperledger.besu.consensus.qbft.support.TestContextBuilder) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Test(org.junit.jupiter.api.Test)

Example 5 with TestContext

use of org.hyperledger.besu.consensus.qbft.support.TestContext in project besu by hyperledger.

the class ValidatorContractTest method transitionsFromValidatorContractModeToBlockHeaderModeThenBackToNewContract.

@Test
public void transitionsFromValidatorContractModeToBlockHeaderModeThenBackToNewContract() {
    final List<QbftFork> qbftForks = List.of(createBlockHeaderFork(1, List.of(NODE_2_ADDRESS)), createContractFork(2, NEW_VALIDATOR_CONTRACT_ADDRESS));
    final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).nodeParams(List.of(new NodeParams(NODE_ADDRESS, NodeKeyUtils.createFrom(NODE_PRIVATE_KEY)), new NodeParams(NODE_2_ADDRESS, NodeKeyUtils.createFrom(NODE_2_PRIVATE_KEY)))).clock(clock).genesisFile(Resources.getResource("genesis_validator_contract.json").getFile()).useValidatorContract(true).qbftForks(qbftForks).buildAndStart();
    // block 0 uses validator contract with 1 validator
    // block 1 uses block header voting with 1 overridden validator
    // block 2 uses validator contract with 2 validators
    final List<Address> block0Addresses = List.of(NODE_ADDRESS);
    final List<Address> block1Addresses = List.of(NODE_2_ADDRESS);
    final List<Address> block2Addresses = Stream.of(NODE_ADDRESS, NODE_2_ADDRESS).sorted().collect(Collectors.toList());
    createNewBlockAsProposer(context, 1L);
    clock.step(TestContextBuilder.BLOCK_TIMER_SEC, SECONDS);
    remotePeerProposesNewBlock(context, 2L);
    final ValidatorProvider validatorProvider = context.getValidatorProvider();
    final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
    final BlockHeader block1 = context.getBlockchain().getBlockHeader(1).get();
    final BlockHeader block2 = context.getBlockchain().getBlockHeader(2).get();
    // contract block extra data cannot contain validators or vote
    assertThat(validatorProvider.getValidatorsForBlock(genesisBlock)).isEqualTo(block0Addresses);
    assertThat(extraDataCodec.decode(genesisBlock).getValidators()).isEmpty();
    assertThat(extraDataCodec.decode(genesisBlock).getVote()).isEmpty();
    assertThat(validatorProvider.getValidatorsForBlock(block1)).isEqualTo(block1Addresses);
    assertThat(extraDataCodec.decode(block1).getValidators()).containsExactly(NODE_2_ADDRESS);
    // contract block extra data cannot contain validators or vote
    assertThat(validatorProvider.getValidatorsForBlock(block2)).isEqualTo(block2Addresses);
    assertThat(extraDataCodec.decode(block2).getValidators()).isEmpty();
    assertThat(extraDataCodec.decode(block2).getVote()).isEmpty();
}
Also used : Address(org.hyperledger.besu.datatypes.Address) QbftFork(org.hyperledger.besu.config.QbftFork) TestContext(org.hyperledger.besu.consensus.qbft.support.TestContext) TestContextBuilder(org.hyperledger.besu.consensus.qbft.support.TestContextBuilder) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Test(org.junit.jupiter.api.Test)

Aggregations

TestContext (org.hyperledger.besu.consensus.qbft.support.TestContext)7 TestContextBuilder (org.hyperledger.besu.consensus.qbft.support.TestContextBuilder)7 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)7 Test (org.junit.jupiter.api.Test)7 QbftFork (org.hyperledger.besu.config.QbftFork)6 NodeParams (org.hyperledger.besu.consensus.common.bft.inttest.NodeParams)6 ValidatorProvider (org.hyperledger.besu.consensus.common.validator.ValidatorProvider)6 Address (org.hyperledger.besu.datatypes.Address)5 BftEventQueue (org.hyperledger.besu.consensus.common.bft.BftEventQueue)1 NewChainHead (org.hyperledger.besu.consensus.common.bft.events.NewChainHead)1 TestClock (org.hyperledger.besu.testutil.TestClock)1