Search in sources :

Example 1 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class JsonRpcHttpServiceTest method blockWithMetadataAndTxHashes.

public BlockWithMetadata<Hash, Hash> blockWithMetadataAndTxHashes(final Block block) {
    final Difficulty td = block.getHeader().getDifficulty().add(10L);
    final int size = block.calculateSize();
    final List<Hash> txs = block.getBody().getTransactions().stream().map(Transaction::getHash).collect(Collectors.toList());
    final List<Hash> ommers = block.getBody().getOmmers().stream().map(BlockHeader::getHash).collect(Collectors.toList());
    return new BlockWithMetadata<>(block.getHeader(), txs, ommers, td, size);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) Hash(org.hyperledger.besu.datatypes.Hash)

Example 2 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class ConstantFieldValidationRuleTest method difficultyFieldIsValidatedCorrectly.

@Test
public void difficultyFieldIsValidatedCorrectly() {
    final ConstantFieldValidationRule<Difficulty> uut = new ConstantFieldValidationRule<>("Difficulty", BlockHeader::getDifficulty, Difficulty.ONE);
    final BlockHeaderTestFixture blockHeaderBuilder = new BlockHeaderTestFixture();
    blockHeaderBuilder.difficulty(Difficulty.ONE);
    BlockHeader header = blockHeaderBuilder.buildHeader();
    assertThat(uut.validate(header, null)).isTrue();
    blockHeaderBuilder.difficulty(Difficulty.ZERO);
    header = blockHeaderBuilder.buildHeader();
    assertThat(uut.validate(header, null)).isFalse();
}
Also used : BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 3 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class CliqueDifficultyValidationRuleTest method isFalseIfInTurnValidatorSuppliesDifficultyOfOne.

@Test
public void isFalseIfInTurnValidatorSuppliesDifficultyOfOne() {
    final long IN_TURN_BLOCK_NUMBER = validatorList.size();
    final Difficulty REPORTED_DIFFICULTY = Difficulty.ONE;
    blockHeaderBuilder.number(IN_TURN_BLOCK_NUMBER - 1L);
    final BlockHeader parentHeader = TestHelpers.createCliqueSignedBlockHeader(blockHeaderBuilder, proposerKeyPair, validatorList);
    blockHeaderBuilder.number(IN_TURN_BLOCK_NUMBER).difficulty(REPORTED_DIFFICULTY);
    final BlockHeader newBlock = TestHelpers.createCliqueSignedBlockHeader(blockHeaderBuilder, proposerKeyPair, validatorList);
    final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule();
    assertThat(diffValidationRule.validate(newBlock, parentHeader, cliqueProtocolContext)).isFalse();
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 4 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class CliqueDifficultyValidationRuleTest method isFalseIfOutTurnValidatorSuppliesDifficultyOfTwo.

@Test
public void isFalseIfOutTurnValidatorSuppliesDifficultyOfTwo() {
    final long OUT_OF_TURN_BLOCK_NUMBER = validatorList.size() - 1L;
    final Difficulty REPORTED_DIFFICULTY = Difficulty.of(2);
    blockHeaderBuilder.number(OUT_OF_TURN_BLOCK_NUMBER - 1L);
    final BlockHeader parentHeader = TestHelpers.createCliqueSignedBlockHeader(blockHeaderBuilder, proposerKeyPair, validatorList);
    blockHeaderBuilder.number(OUT_OF_TURN_BLOCK_NUMBER).difficulty(REPORTED_DIFFICULTY);
    final BlockHeader newBlock = TestHelpers.createCliqueSignedBlockHeader(blockHeaderBuilder, proposerKeyPair, validatorList);
    final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule();
    assertThat(diffValidationRule.validate(newBlock, parentHeader, cliqueProtocolContext)).isFalse();
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 5 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class CliqueDifficultyValidationRuleTest method isTrueIfInTurnValidatorSuppliesDifficultyOfTwo.

@Test
public void isTrueIfInTurnValidatorSuppliesDifficultyOfTwo() {
    // i.e. proposer is 'in turn'
    final long IN_TURN_BLOCK_NUMBER = validatorList.size();
    final Difficulty REPORTED_DIFFICULTY = Difficulty.of(2);
    blockHeaderBuilder.number(IN_TURN_BLOCK_NUMBER - 1L);
    final BlockHeader parentHeader = TestHelpers.createCliqueSignedBlockHeader(blockHeaderBuilder, proposerKeyPair, validatorList);
    blockHeaderBuilder.number(IN_TURN_BLOCK_NUMBER).difficulty(REPORTED_DIFFICULTY);
    final BlockHeader newBlock = TestHelpers.createCliqueSignedBlockHeader(blockHeaderBuilder, proposerKeyPair, validatorList);
    final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule();
    assertThat(diffValidationRule.validate(newBlock, parentHeader, cliqueProtocolContext)).isTrue();
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)51 Test (org.junit.Test)29 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)20 Block (org.hyperledger.besu.ethereum.core.Block)16 Hash (org.hyperledger.besu.datatypes.Hash)14 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)9 BigInteger (java.math.BigInteger)8 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)8 Transaction (org.hyperledger.besu.ethereum.core.Transaction)7 List (java.util.List)6 Optional (java.util.Optional)6 Wei (org.hyperledger.besu.datatypes.Wei)6 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)6 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)6 ArrayList (java.util.ArrayList)5 Bytes (org.apache.tuweni.bytes.Bytes)5 Address (org.hyperledger.besu.datatypes.Address)5 Collections (java.util.Collections)4 MergeContext (org.hyperledger.besu.consensus.merge.MergeContext)4 NewBlockMessage (org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage)4