Search in sources :

Example 16 with KeyPair

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

the class GoQuorumSendRawPrivateTransactionTest method rlpEncodeTransaction.

private static String rlpEncodeTransaction(final PrivateTransaction.Builder privateTransactionBuilder) {
    final KeyPair keyPair = SIGNATURE_ALGORITHM.get().createKeyPair(SIGNATURE_ALGORITHM.get().createPrivateKey(new BigInteger("8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16)));
    final PrivateTransaction privateTransaction = privateTransactionBuilder.signAndBuild(keyPair);
    final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput();
    privateTransaction.writeTo(bvrlp);
    return bvrlp.encoded().toHexString();
}
Also used : PrivateTransaction(org.hyperledger.besu.ethereum.privacy.PrivateTransaction) GoQuorumSendRawPrivateTransaction(org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.GoQuorumSendRawPrivateTransaction) KeyPair(org.hyperledger.besu.crypto.KeyPair) BigInteger(java.math.BigInteger) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)

Example 17 with KeyPair

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

the class IbftBlockHeaderValidationRulesetFactoryTest method ibftValidateHeaderFails.

@Test
public void ibftValidateHeaderFails() {
    final KeyPair proposerKeyPair = SIGNATURE_ALGORITHM.get().generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = singletonList(proposerAddress);
    final BlockHeader parentHeader = buildBlockHeader(1, proposerKeyPair, validators, null);
    final BlockHeader blockHeader = buildBlockHeader(2, proposerKeyPair, validators, null);
    final BlockHeaderValidator validator = IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5, 0).build();
    assertThat(validator.validateHeader(blockHeader, parentHeader, setupContextWithValidators(validators), HeaderValidationMode.FULL)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 18 with KeyPair

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

the class BftBlockCreatorTest method headerProducedPassesValidationRules.

@Test
public void headerProducedPassesValidationRules() {
    // 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 KeyPair nodeKeys = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    // Add the local node as a validator (can't propose a block if node is not a validator).
    final Address localAddr = Address.extract(Hash.hash(nodeKeys.getPublicKey().getEncodedBytes()));
    final List<Address> initialValidatorList = Arrays.asList(Address.fromHexString(String.format("%020d", 1)), Address.fromHexString(String.format("%020d", 2)), Address.fromHexString(String.format("%020d", 3)), Address.fromHexString(String.format("%020d", 4)), localAddr);
    final ProtocolSchedule protocolSchedule = IbftProtocolSchedule.create(GenesisConfigFile.fromConfig("{\"config\": {\"spuriousDragonBlock\":0}}").getConfigOptions(), false, EvmConfiguration.DEFAULT);
    final ProtocolContext protContext = new ProtocolContext(blockchain, createInMemoryWorldStateArchive(), setupContextWithValidators(initialValidatorList));
    final IbftBlockCreator blockCreator = new IbftBlockCreator(Address.fromHexString(String.format("%020d", 0)), () -> Optional.of(10_000_000L), parent -> new IbftExtraData(Bytes.wrap(new byte[32]), Lists.newArrayList(), null, initialValidatorList).encode(), new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, blockchain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP), protContext, protocolSchedule, nodeKeys, Wei.ZERO, 0.8, parentHeader);
    final Block block = blockCreator.createBlock(Instant.now().getEpochSecond());
    final BlockHeaderValidator rules = IbftBlockHeaderValidationRulesetFactory.ibftProposedBlockValidator(0).build();
    final boolean validationResult = rules.validateHeader(block.getHeader(), parentHeader, protContext, HeaderValidationMode.FULL);
    assertThat(validationResult).isTrue();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) IbftProtocolSchedule(org.hyperledger.besu.consensus.ibftlegacy.IbftProtocolSchedule) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Test(org.junit.Test)

Example 19 with KeyPair

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

the class BftExtraDataValidationRuleTest method outOfOrderValidatorListFailsValidation.

@Test
public void outOfOrderValidatorListFailsValidation() {
    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(AddressHelpers.calculateAddressWithRespectTo(proposerAddress, 1), proposerAddress);
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // 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 20 with KeyPair

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

the class BftExtraDataValidationRuleTest method correctlyConstructedHeaderPassesValidation.

@Test
public void correctlyConstructedHeaderPassesValidation() {
    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);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // 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)).isTrue();
}
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)

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