Search in sources :

Example 1 with IbftExtraDataCodec

use of org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec in project besu by hyperledger.

the class IbftRoundTest method twoValidatorNetworkSendsPrepareOnProposalReceptionThenSendsCommitOnCommitReceive.

@Test
public void twoValidatorNetworkSendsPrepareOnProposalReceptionThenSendsCommitOnCommitReceive() {
    final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
    final IbftRound round = new IbftRound(roundState, blockCreator, protocolContext, blockImporter, subscribers, nodeKey, messageFactory, transmitter, roundTimer, bftExtraDataCodec);
    final Hash commitSealHash = new BftBlockHashing(new IbftExtraDataCodec()).calculateDataHashForCommittedSeal(proposedBlock.getHeader(), proposedExtraData);
    final SECPSignature localCommitSeal = nodeKey.sign(commitSealHash);
    // Receive Proposal Message
    round.handleProposalMessage(messageFactory.createProposal(roundIdentifier, proposedBlock, Optional.empty()));
    verify(transmitter, times(1)).multicastPrepare(roundIdentifier, proposedBlock.getHash());
    verify(transmitter, times(1)).multicastCommit(roundIdentifier, proposedBlock.getHash(), localCommitSeal);
    verify(blockImporter, never()).importBlock(any(), any(), any());
    // Receive Commit Message
    round.handleCommitMessage(messageFactory.createCommit(roundIdentifier, proposedBlock.getHash(), remoteCommitSeal));
    // Should import block when both commit seals are available.
    final ArgumentCaptor<Block> capturedBlock = ArgumentCaptor.forClass(Block.class);
    verify(blockImporter, times(1)).importBlock(any(), capturedBlock.capture(), any());
    // Ensure imported block contains both commit seals.
    final BftExtraData importedExtraData = new IbftExtraDataCodec().decode(capturedBlock.getValue().getHeader());
    assertThat(importedExtraData.getSeals()).containsOnly(remoteCommitSeal, localCommitSeal);
}
Also used : SECPSignature(org.hyperledger.besu.crypto.SECPSignature) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) BftBlockHashing(org.hyperledger.besu.consensus.common.bft.BftBlockHashing) Block(org.hyperledger.besu.ethereum.core.Block) Hash(org.hyperledger.besu.datatypes.Hash) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Example 2 with IbftExtraDataCodec

use of org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec in project besu by hyperledger.

the class IbftRoundTest method aProposalMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWithCertificate.

@Test
public void aProposalMessageWithTheSameBlockIsSentUponReceptionOfARoundChangeWithCertificate() {
    final ConsensusRoundIdentifier priorRoundChange = new ConsensusRoundIdentifier(1, 0);
    final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
    final IbftRound round = new IbftRound(roundState, blockCreator, protocolContext, blockImporter, subscribers, nodeKey, messageFactory, transmitter, roundTimer, bftExtraDataCodec);
    final RoundChangeArtifacts roundChangeArtifacts = RoundChangeArtifacts.create(Collections.singletonList(messageFactory.createRoundChange(roundIdentifier, Optional.of(new PreparedRoundArtifacts(messageFactory.createProposal(priorRoundChange, proposedBlock, Optional.empty()), emptyList())))));
    // NOTE: IbftRound assumes the prepare's are valid
    round.startRoundWith(roundChangeArtifacts, 15);
    verify(transmitter, times(1)).multicastProposal(eq(roundIdentifier), blockCaptor.capture(), eq(Optional.of(roundChangeArtifacts.getRoundChangeCertificate())));
    final BftExtraData proposedExtraData = new IbftExtraDataCodec().decode(blockCaptor.getValue().getHeader());
    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.createPrepare(roundIdentifier, proposedBlock.getHash()));
    assertThat(roundState.isPrepared()).isTrue();
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Example 3 with IbftExtraDataCodec

use of org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec in project besu by hyperledger.

the class BftBlockCreatorTest method createdBlockPassesValidationRulesAndHasAppropriateHashAndMixHash.

@Test
public void createdBlockPassesValidationRulesAndHasAppropriateHashAndMixHash() {
    // Construct a parent block.
    final BlockHeaderTestFixture blockHeaderBuilder = new BlockHeaderTestFixture();
    // required to pass validation rule checks.
    blockHeaderBuilder.gasLimit(5000);
    final BlockHeader parentHeader = blockHeaderBuilder.buildHeader();
    final Optional<BlockHeader> optionalHeader = Optional.of(parentHeader);
    // Construct a block chain and world state
    final MutableBlockchain blockchain = mock(MutableBlockchain.class);
    when(blockchain.getChainHeadHash()).thenReturn(parentHeader.getHash());
    when(blockchain.getBlockHeader(any())).thenReturn(optionalHeader);
    final BlockHeader blockHeader = mock(BlockHeader.class);
    when(blockHeader.getBaseFee()).thenReturn(Optional.empty());
    when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
    final List<Address> initialValidatorList = Lists.newArrayList();
    for (int i = 0; i < 4; i++) {
        initialValidatorList.add(AddressHelpers.ofValue(i));
    }
    final IbftExtraDataCodec bftExtraDataEncoder = new IbftExtraDataCodec();
    final BaseBftProtocolSchedule bftProtocolSchedule = new BaseBftProtocolSchedule() {

        @Override
        public BlockHeaderValidator.Builder createBlockHeaderRuleset(final BftConfigOptions config, final FeeMarket feeMarket) {
            return IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator(5, Optional.empty());
        }
    };
    final GenesisConfigOptions configOptions = GenesisConfigFile.fromConfig("{\"config\": {\"spuriousDragonBlock\":0}}").getConfigOptions();
    final ForksSchedule<BftConfigOptions> forksSchedule = new ForksSchedule<>(List.of(new ForkSpec<>(0, configOptions.getBftConfigOptions())));
    final ProtocolSchedule protocolSchedule = bftProtocolSchedule.createProtocolSchedule(configOptions, forksSchedule, PrivacyParameters.DEFAULT, false, bftExtraDataEncoder, EvmConfiguration.DEFAULT);
    final ProtocolContext protContext = new ProtocolContext(blockchain, createInMemoryWorldStateArchive(), setupContextWithBftExtraDataEncoder(initialValidatorList, bftExtraDataEncoder));
    final GasPricePendingTransactionsSorter pendingTransactions = new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, blockchain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final BftBlockCreator blockCreator = new BftBlockCreator(forksSchedule, initialValidatorList.get(0), () -> Optional.of(10_000_000L), parent -> bftExtraDataEncoder.encode(new BftExtraData(Bytes.wrap(new byte[32]), Collections.emptyList(), Optional.empty(), 0, initialValidatorList)), pendingTransactions, protContext, protocolSchedule, Wei.ZERO, 0.8, parentHeader, bftExtraDataEncoder);
    final int secondsBetweenBlocks = 1;
    final Block block = blockCreator.createBlock(parentHeader.getTimestamp() + 1);
    final BlockHeaderValidator rules = IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator(secondsBetweenBlocks, Optional.empty()).build();
    // NOTE: The header will not contain commit seals, so can only do light validation on header.
    final boolean validationResult = rules.validateHeader(block.getHeader(), parentHeader, protContext, HeaderValidationMode.LIGHT);
    assertThat(validationResult).isTrue();
    final BlockHeader header = block.getHeader();
    final BftExtraData extraData = bftExtraDataEncoder.decode(header);
    assertThat(block.getHash()).isEqualTo(new BftBlockHashing(bftExtraDataEncoder).calculateDataHashForCommittedSeal(header, extraData));
}
Also used : Address(org.hyperledger.besu.datatypes.Address) ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) BftConfigOptions(org.hyperledger.besu.config.BftConfigOptions) BftBlockCreator(org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreator) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) FeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BaseBftProtocolSchedule(org.hyperledger.besu.consensus.common.bft.BaseBftProtocolSchedule) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BaseBftProtocolSchedule(org.hyperledger.besu.consensus.common.bft.BaseBftProtocolSchedule) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) BftBlockHashing(org.hyperledger.besu.consensus.common.bft.BftBlockHashing) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Test(org.junit.Test)

Example 4 with IbftExtraDataCodec

use of org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec in project besu by hyperledger.

the class IntegrationTestHelpers method createSignedCommitPayload.

public static SignedData<CommitPayload> createSignedCommitPayload(final ConsensusRoundIdentifier roundId, final Block block, final NodeKey nodeKey) {
    final IbftExtraDataCodec ibftExtraDataEncoder = new IbftExtraDataCodec();
    final BftExtraData extraData = ibftExtraDataEncoder.decode(block.getHeader());
    final SECPSignature commitSeal = nodeKey.sign(new BftBlockHashing(ibftExtraDataEncoder).calculateDataHashForCommittedSeal(block.getHeader(), extraData));
    final MessageFactory messageFactory = new MessageFactory(nodeKey);
    return messageFactory.createCommit(roundId, block.getHash(), commitSeal).getSignedPayload();
}
Also used : SECPSignature(org.hyperledger.besu.crypto.SECPSignature) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) BftBlockHashing(org.hyperledger.besu.consensus.common.bft.BftBlockHashing) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData)

Example 5 with IbftExtraDataCodec

use of org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec in project besu by hyperledger.

the class IbftBlockHeightManagerTest method buildCreatedBlock.

private void buildCreatedBlock() {
    final BftExtraData extraData = new BftExtraData(Bytes.wrap(new byte[32]), emptyList(), Optional.empty(), 0, validators);
    headerTestFixture.extraData(new IbftExtraDataCodec().encode(extraData));
    final BlockHeader header = headerTestFixture.buildHeader();
    createdBlock = new Block(header, new BlockBody(emptyList(), emptyList()));
}
Also used : BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) Block(org.hyperledger.besu.ethereum.core.Block) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData)

Aggregations

IbftExtraDataCodec (org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec)9 BftExtraData (org.hyperledger.besu.consensus.common.bft.BftExtraData)6 Block (org.hyperledger.besu.ethereum.core.Block)6 Test (org.junit.Test)5 BftBlockHashing (org.hyperledger.besu.consensus.common.bft.BftBlockHashing)4 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)2 Hash (org.hyperledger.besu.datatypes.Hash)2 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)2 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)2 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)2 BftConfigOptions (org.hyperledger.besu.config.BftConfigOptions)1 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)1 ForkSpec (org.hyperledger.besu.consensus.common.ForkSpec)1 ForksSchedule (org.hyperledger.besu.consensus.common.ForksSchedule)1 BaseBftProtocolSchedule (org.hyperledger.besu.consensus.common.bft.BaseBftProtocolSchedule)1 BftBlockCreator (org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreator)1 NewChainHead (org.hyperledger.besu.consensus.common.bft.events.NewChainHead)1 Prepare (org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare)1