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();
}
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();
}
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();
}
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();
}
}
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);
}
Aggregations