use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.
the class BftExtraDataValidationRuleTest method createExtraDataWithCommitSeals.
private IbftExtraData createExtraDataWithCommitSeals(final BlockHeader header, final Collection<KeyPair> committerKeyPairs) {
final IbftExtraData extraDataInHeader = IbftExtraData.decode(header);
final Hash headerHashForCommitters = IbftBlockHashing.calculateDataHashForCommittedSeal(header, extraDataInHeader);
final List<SECPSignature> commitSeals = committerKeyPairs.stream().map(keys -> signatureAlgorithm.sign(headerHashForCommitters, keys)).collect(Collectors.toList());
return new IbftExtraData(extraDataInHeader.getVanityData(), commitSeals, extraDataInHeader.getProposerSeal(), extraDataInHeader.getValidators());
}
use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.
the class BftExtraDataValidationRuleTest method proposerNotInValidatorListFailsValidation.
@Test
public void proposerNotInValidatorListFailsValidation() {
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();
}
use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.
the class BftExtraDataValidationRuleTest method subExecution.
private boolean subExecution(final int validatorCount, final int committerCount, final boolean useTwoThirds) {
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();
final List<KeyPair> committerKeys = Lists.newArrayList();
validators.add(proposerAddress);
committerKeys.add(proposerKeyPair);
for (int i = 0; i < validatorCount - 1; i++) {
// need -1 to account for proposer
final KeyPair committerKeyPair = signatureAlgorithm.generateKeyPair();
committerKeys.add(committerKeyPair);
validators.add(Address.extract(Hash.hash(committerKeyPair.getPublicKey().getEncodedBytes())));
}
Collections.sort(validators);
BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, committerKeys.subList(0, committerCount));
builder.extraData(commitedExtraData.encode());
header = builder.buildHeader();
final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, useTwoThirds ? 0 : 2);
return extraDataValidationRule.validate(header, null, context);
}
use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData 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();
}
use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData 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();
}
Aggregations