Search in sources :

Example 1 with NodeParams

use of org.hyperledger.besu.consensus.common.bft.inttest.NodeParams in project besu by hyperledger.

the class SpuriousBehaviourTest method nonValidatorsCannotTriggerResponses.

@Test
public void nonValidatorsCannotTriggerResponses() {
    final NodeKey nonValidatorNodeKey = NodeKeyUtils.generate();
    final NodeParams nonValidatorParams = new NodeParams(Util.publicKeyToAddress(nonValidatorNodeKey.getPublicKey()), nonValidatorNodeKey);
    final ValidatorPeer nonvalidator = new ValidatorPeer(nonValidatorParams, new MessageFactory(nonValidatorParams.getNodeKey()), context.getEventMultiplexer());
    nonvalidator.injectProposal(new ConsensusRoundIdentifier(1, 0), proposedBlock);
    peers.verifyNoMessagesReceived();
}
Also used : ValidatorPeer(org.hyperledger.besu.consensus.qbft.support.ValidatorPeer) ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) MessageFactory(org.hyperledger.besu.consensus.qbft.payload.MessageFactory) NodeKey(org.hyperledger.besu.crypto.NodeKey) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Test(org.junit.jupiter.api.Test)

Example 2 with NodeParams

use of org.hyperledger.besu.consensus.common.bft.inttest.NodeParams 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 3 with NodeParams

use of org.hyperledger.besu.consensus.common.bft.inttest.NodeParams 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 4 with NodeParams

use of org.hyperledger.besu.consensus.common.bft.inttest.NodeParams in project besu by hyperledger.

the class TestContextBuilder method build.

public TestContext build() {
    final NetworkLayout networkNodes = NetworkLayout.createNetworkLayout(validatorCount, indexOfFirstLocallyProposedBlock);
    final Block genesisBlock = createGenesisBlock(networkNodes.getValidatorAddresses());
    final MutableBlockchain blockChain = createInMemoryBlockchain(genesisBlock, BftBlockHeaderFunctions.forOnchainBlock(IBFT_EXTRA_DATA_ENCODER));
    // Use a stubbed version of the multicaster, to prevent creating PeerConnections etc.
    final StubValidatorMulticaster multicaster = new StubValidatorMulticaster();
    final UniqueMessageMulticaster uniqueMulticaster = new UniqueMessageMulticaster(multicaster, GOSSIPED_HISTORY_LIMIT);
    final Gossiper gossiper = useGossip ? new IbftGossip(uniqueMulticaster) : mock(Gossiper.class);
    final StubbedSynchronizerUpdater synchronizerUpdater = new StubbedSynchronizerUpdater();
    final ControllerAndState controllerAndState = createControllerAndFinalState(blockChain, multicaster, networkNodes.getLocalNode().getNodeKey(), clock, bftEventQueue, gossiper, synchronizerUpdater, bftForks);
    // Add each networkNode to the Multicaster (such that each can receive msgs from local node).
    // NOTE: the remotePeers needs to be ordered based on Address (as this is used to determine
    // the proposer order which must be managed in test).
    final Map<Address, ValidatorPeer> remotePeers = networkNodes.getRemotePeers().stream().collect(Collectors.toMap(NodeParams::getAddress, nodeParams -> new ValidatorPeer(nodeParams, new MessageFactory(nodeParams.getNodeKey()), controllerAndState.getEventMultiplexer()), (u, v) -> {
        throw new IllegalStateException(String.format("Duplicate key %s", u));
    }, LinkedHashMap::new));
    final List<DefaultValidatorPeer> peerCollection = new ArrayList<>(remotePeers.values());
    multicaster.addNetworkPeers(peerCollection);
    synchronizerUpdater.addNetworkPeers(peerCollection);
    return new TestContext(remotePeers, blockChain, controllerAndState.getIbftExecutors(), controllerAndState.getEventHandler(), controllerAndState.getFinalState(), controllerAndState.getEventMultiplexer(), controllerAndState.getMessageFactory());
}
Also used : BftFinalState(org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState) BftEventQueue(org.hyperledger.besu.consensus.common.bft.BftEventQueue) Subscribers(org.hyperledger.besu.util.Subscribers) BftBlockHeaderFunctions(org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) IbftBlockHeightManagerFactory(org.hyperledger.besu.consensus.ibft.statemachine.IbftBlockHeightManagerFactory) BftContext(org.hyperledger.besu.consensus.common.bft.BftContext) StubbedSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.inttest.StubbedSynchronizerUpdater) ProposerSelector(org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) MessageValidatorFactory(org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory) Map(java.util.Map) BlockTimer(org.hyperledger.besu.consensus.common.bft.BlockTimer) Block(org.hyperledger.besu.ethereum.core.Block) BftFork(org.hyperledger.besu.config.BftFork) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) RoundTimer(org.hyperledger.besu.consensus.common.bft.RoundTimer) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BftEventHandler(org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler) MessageTracker(org.hyperledger.besu.consensus.common.bft.MessageTracker) Set(java.util.Set) Instant(java.time.Instant) EventMultiplexer(org.hyperledger.besu.consensus.common.bft.EventMultiplexer) BlockValidatorProvider(org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) IbftController(org.hyperledger.besu.consensus.ibft.statemachine.IbftController) BftExecutors(org.hyperledger.besu.consensus.common.bft.BftExecutors) List(java.util.List) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) Optional(java.util.Optional) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) Mockito.mock(org.mockito.Mockito.mock) NodeKey(org.hyperledger.besu.crypto.NodeKey) Hash(org.hyperledger.besu.datatypes.Hash) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) IbftProtocolSchedule(org.hyperledger.besu.consensus.ibft.IbftProtocolSchedule) Iterables(com.google.common.collect.Iterables) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) BftBlockCreatorFactory(org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreatorFactory) IbftRoundFactory(org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) Util(org.hyperledger.besu.ethereum.core.Util) ArrayList(java.util.ArrayList) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) LinkedHashMap(java.util.LinkedHashMap) InMemoryKeyValueStorageProvider.createInMemoryBlockchain(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain) FutureMessageBuffer(org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) NetworkLayout(org.hyperledger.besu.consensus.common.bft.inttest.NetworkLayout) IbftGossip(org.hyperledger.besu.consensus.ibft.IbftGossip) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) BftBlockInterface(org.hyperledger.besu.consensus.common.bft.BftBlockInterface) Wei(org.hyperledger.besu.datatypes.Wei) EpochManager(org.hyperledger.besu.consensus.common.EpochManager) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) StubValidatorMulticaster(org.hyperledger.besu.consensus.common.bft.inttest.StubValidatorMulticaster) IbftForksSchedulesFactory(org.hyperledger.besu.consensus.ibft.IbftForksSchedulesFactory) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) AddressHelpers(org.hyperledger.besu.ethereum.core.AddressHelpers) TestTransitions(org.hyperledger.besu.consensus.common.bft.inttest.TestTransitions) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Gossiper(org.hyperledger.besu.consensus.common.bft.Gossiper) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) BftConfigOptions(org.hyperledger.besu.config.BftConfigOptions) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) Clock(java.time.Clock) BftHelpers(org.hyperledger.besu.consensus.common.bft.BftHelpers) SynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.SynchronizerUpdater) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) StubValidatorMulticaster(org.hyperledger.besu.consensus.common.bft.inttest.StubValidatorMulticaster) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) Address(org.hyperledger.besu.datatypes.Address) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) ArrayList(java.util.ArrayList) NetworkLayout(org.hyperledger.besu.consensus.common.bft.inttest.NetworkLayout) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) Gossiper(org.hyperledger.besu.consensus.common.bft.Gossiper) StubbedSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.inttest.StubbedSynchronizerUpdater) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) IbftGossip(org.hyperledger.besu.consensus.ibft.IbftGossip)

Example 5 with NodeParams

use of org.hyperledger.besu.consensus.common.bft.inttest.NodeParams in project besu by hyperledger.

the class SpuriousBehaviourTest method nonValidatorsCannotTriggerResponses.

@Test
public void nonValidatorsCannotTriggerResponses() {
    final NodeKey nonValidatorNodeKey = NodeKeyUtils.generate();
    final NodeParams nonValidatorParams = new NodeParams(Util.publicKeyToAddress(nonValidatorNodeKey.getPublicKey()), nonValidatorNodeKey);
    final ValidatorPeer nonvalidator = new ValidatorPeer(nonValidatorParams, new MessageFactory(nonValidatorParams.getNodeKey()), context.getEventMultiplexer());
    nonvalidator.injectProposal(new ConsensusRoundIdentifier(1, 0), proposedBlock);
    peers.verifyNoMessagesReceived();
}
Also used : ValidatorPeer(org.hyperledger.besu.consensus.ibft.support.ValidatorPeer) ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) NodeKey(org.hyperledger.besu.crypto.NodeKey) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Test(org.junit.jupiter.api.Test)

Aggregations

NodeParams (org.hyperledger.besu.consensus.common.bft.inttest.NodeParams)10 ValidatorProvider (org.hyperledger.besu.consensus.common.validator.ValidatorProvider)8 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)8 Test (org.junit.jupiter.api.Test)8 Address (org.hyperledger.besu.datatypes.Address)7 QbftFork (org.hyperledger.besu.config.QbftFork)6 TestContext (org.hyperledger.besu.consensus.qbft.support.TestContext)6 TestContextBuilder (org.hyperledger.besu.consensus.qbft.support.TestContextBuilder)6 NodeKey (org.hyperledger.besu.crypto.NodeKey)3 Iterables (com.google.common.collect.Iterables)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2 Clock (java.time.Clock)2 Instant (java.time.Instant)2 ZoneId (java.time.ZoneId)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2