Search in sources :

Example 21 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method insufficientCommitSealsFailsValidation.

@Test
public void insufficientCommitSealsFailsValidation() {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = singletonList(proposerAddress);
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    final BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // Note that no committer seals are in the header's IBFT extra data.
    final IbftExtraData headerExtraData = IbftExtraData.decode(header);
    Assertions.assertThat(headerExtraData.getSeals().size()).isEqualTo(0);
    assertThat(extraDataValidationRule.validate(header, null, context)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 22 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method committerNotInValidatorListFailsValidation.

@Test
public void committerNotInValidatorListFailsValidation() {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = singletonList(proposerAddress);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // Insert an extraData block with committer seals.
    final KeyPair nonValidatorKeyPair = signatureAlgorithm.generateKeyPair();
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, singletonList(nonValidatorKeyPair));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    assertThat(extraDataValidationRule.validate(header, null, context)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 23 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method mismatchingReportedValidatorsVsLocallyStoredListFailsValidation.

@Test
public void mismatchingReportedValidatorsVsLocallyStoredListFailsValidation() {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = Lists.newArrayList(proposerAddress);
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    // Add another validator to the list reported in the IbftExtraData (note, as the
    final List<Address> extraDataValidators = Lists.newArrayList(proposerAddress, AddressHelpers.calculateAddressWithRespectTo(proposerAddress, 1));
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, extraDataValidators);
    // Insert an extraData block with committer seals.
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, singletonList(proposerKeyPair));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    assertThat(extraDataValidationRule.validate(header, null, context)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 24 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class DebugGetBadBlockTest method shouldReturnCorrectResponse.

@Test
public void shouldReturnCorrectResponse() {
    final KeyPair keyPair = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    final List<Transaction> transactions = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        transactions.add(transactionTestFixture.createTransaction(keyPair));
    }
    final Block parentBlock = new BlockDataGenerator().block(BlockDataGenerator.BlockOptions.create().setBlockHeaderFunctions(new MainnetBlockHeaderFunctions()));
    final Block badBlockWithTransaction = new BlockDataGenerator().block(BlockDataGenerator.BlockOptions.create().addTransaction(transactions).setBlockNumber(1).setBlockHeaderFunctions(new MainnetBlockHeaderFunctions()).setParentHash(parentBlock.getHash()));
    final Block badBlockWoTransaction = new BlockDataGenerator().block(BlockDataGenerator.BlockOptions.create().setBlockNumber(2).hasTransactions(false).setBlockHeaderFunctions(new MainnetBlockHeaderFunctions()).setParentHash(parentBlock.getHash()));
    badBlockManager.addBadBlock(badBlockWithTransaction);
    badBlockManager.addBadBlock(badBlockWoTransaction);
    final ProtocolSpec protocolSpec = mock(ProtocolSpec.class);
    when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
    when(protocolSpec.getBadBlocksManager()).thenReturn(badBlockManager);
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_traceBlock", new Object[] {}));
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) debugGetBadBlocks.response(request);
    final Collection<BadBlockResult> result = (Collection<BadBlockResult>) response.getResult();
    assertThat(result).hasSize(2);
    for (BadBlockResult badBlockResult : result) {
        if (badBlockResult.getBlockResult().getNumber().equals("0x1")) {
            assertThat(badBlockResult.getBlockResult().getTransactions().size()).isEqualTo(3);
        } else if (badBlockResult.getBlockResult().getNumber().equals("0x2")) {
            assertThat(badBlockResult.getBlockResult().getTransactions()).isEmpty();
        } else {
            fail("Invalid response");
        }
        assertThat(badBlockResult.getRlp()).isNotEmpty();
        assertThat(badBlockResult.getHash()).isNotEmpty();
        assertThat(badBlockResult.getBlockResult().getNonce()).isNotEmpty();
    }
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) ArrayList(java.util.ArrayList) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) BadBlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BadBlockResult) Transaction(org.hyperledger.besu.ethereum.core.Transaction) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Block(org.hyperledger.besu.ethereum.core.Block) Collection(java.util.Collection) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) Test(org.junit.Test)

Example 25 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class EthGetUncleByBlockHashAndIndexTest method blockWithMetadata.

public BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata(final BlockHeader header) {
    final KeyPair keyPair = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    final List<TransactionWithMetadata> transactions = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        final Transaction transaction = transactionTestFixture.createTransaction(keyPair);
        transactions.add(new TransactionWithMetadata(transaction, header.getNumber(), header.getBaseFee(), header.getHash(), 0));
    }
    final List<Hash> ommers = new ArrayList<>();
    ommers.add(Hash.ZERO);
    return new BlockWithMetadata<>(header, transactions, ommers, header.getDifficulty(), 0);
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) ArrayList(java.util.ArrayList) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) Hash(org.hyperledger.besu.datatypes.Hash)

Aggregations

KeyPair (org.hyperledger.besu.crypto.KeyPair)34 Test (org.junit.Test)21 Address (org.hyperledger.besu.datatypes.Address)14 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)13 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)10 IbftExtraData (org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData)9 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)9 Transaction (org.hyperledger.besu.ethereum.core.Transaction)7 SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)5 ArrayList (java.util.ArrayList)4 SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)4 BigInteger (java.math.BigInteger)3 Bytes (org.apache.tuweni.bytes.Bytes)3 Path (java.nio.file.Path)2 Collection (java.util.Collection)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)2 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)2 Hash (org.hyperledger.besu.datatypes.Hash)2 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)2