use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock in project teku by ConsenSys.
the class HistoricalBatchFetcher method batchVerifyHistoricalBlockSignature.
private SafeFuture<Void> batchVerifyHistoricalBlockSignature(final Collection<SignedBeaconBlock> blocks, final BeaconState bestState) {
List<BLSSignature> signatures = new ArrayList<>();
List<Bytes> signingRoots = new ArrayList<>();
List<List<BLSPublicKey>> proposerPublicKeys = new ArrayList<>();
final Bytes32 genesisValidatorsRoot = bestState.getForkInfo().getGenesisValidatorsRoot();
blocks.forEach(signedBlock -> {
final BeaconBlock block = signedBlock.getMessage();
if (block.getSlot().isGreaterThan(SpecConfig.GENESIS_SLOT)) {
final UInt64 epoch = spec.computeEpochAtSlot(block.getSlot());
final Fork fork = spec.fork(epoch);
final Bytes32 domain = spec.getDomain(Domain.BEACON_PROPOSER, epoch, fork, genesisValidatorsRoot);
signatures.add(signedBlock.getSignature());
signingRoots.add(spec.computeSigningRoot(block, domain));
BLSPublicKey proposerPublicKey = spec.getValidatorPubKey(bestState, block.getProposerIndex()).orElseThrow(() -> new IllegalStateException("Proposer has to be in the state since state is more recent than the block proposed"));
proposerPublicKeys.add(List.of(proposerPublicKey));
}
});
return signatureVerificationService.verify(proposerPublicKeys, signingRoots, signatures).thenAccept(signaturesValid -> {
if (!signaturesValid) {
throw new IllegalArgumentException("Batch signature verification failed");
}
});
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock in project teku by ConsenSys.
the class ValidatorApiHandlerTest method createUnsignedBlock_shouldCreateBlock.
@Test
public void createUnsignedBlock_shouldCreateBlock() throws Exception {
final UInt64 newSlot = UInt64.valueOf(25);
final BeaconState blockSlotState = dataStructureUtil.randomBeaconState(newSlot);
final BLSSignature randaoReveal = dataStructureUtil.randomSignature();
final BeaconBlock createdBlock = dataStructureUtil.randomBeaconBlock(newSlot.longValue());
when(chainDataClient.getStateAtSlotExact(newSlot)).thenReturn(SafeFuture.completedFuture(Optional.of(blockSlotState)));
when(blockFactory.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, Optional.empty())).thenReturn(createdBlock);
final SafeFuture<Optional<BeaconBlock>> result = validatorApiHandler.createUnsignedBlock(newSlot, randaoReveal, Optional.empty());
verify(blockFactory).createUnsignedBlock(blockSlotState, newSlot, randaoReveal, Optional.empty());
assertThat(result).isCompletedWithValue(Optional.of(createdBlock));
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock in project teku by ConsenSys.
the class BlockProposalUtil method createNewUnsignedBlock.
public BeaconBlockAndState createNewUnsignedBlock(final UInt64 newSlot, final int proposerIndex, final BeaconState blockSlotState, final Bytes32 parentBlockSigningRoot, final Consumer<BeaconBlockBodyBuilder> bodyBuilder) throws StateTransitionException {
checkArgument(blockSlotState.getSlot().equals(newSlot), "Block slot state from incorrect slot. Expected %s but got %s", newSlot, blockSlotState.getSlot());
// Create block body
final BeaconBlockBody beaconBlockBody = schemaDefinitions.getBeaconBlockBodySchema().createBlockBody(bodyBuilder);
// Create initial block with some stubs
final Bytes32 tmpStateRoot = Bytes32.ZERO;
BeaconBlock newBlock = schemaDefinitions.getBeaconBlockSchema().create(newSlot, UInt64.valueOf(proposerIndex), parentBlockSigningRoot, tmpStateRoot, beaconBlockBody);
// Skip verifying signatures as all operations are coming from our own pools.
try {
final BeaconState newState = blockProcessor.processUnsignedBlock(blockSlotState, newBlock, IndexedAttestationCache.NOOP, BLSSignatureVerifier.NO_OP, OptimisticExecutionPayloadExecutor.NOOP);
Bytes32 stateRoot = newState.hashTreeRoot();
BeaconBlock newCompleteBlock = newBlock.withStateRoot(stateRoot);
return new BeaconBlockAndState(newCompleteBlock, newState);
} catch (final BlockProcessingException e) {
throw new StateTransitionException(e);
}
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock in project teku by ConsenSys.
the class DataStructureUtil method randomBlockAndState.
private BeaconBlockAndState randomBlockAndState(final UInt64 slot, final BeaconState state, final Bytes32 parentRoot) {
final BeaconBlockBody body = randomBeaconBlockBody();
final UInt64 proposer_index = randomUInt64();
final BeaconBlockHeader latestHeader = new BeaconBlockHeader(slot, proposer_index, parentRoot, Bytes32.ZERO, body.hashTreeRoot());
final BeaconState matchingState = state.updated(s -> s.setLatest_block_header(latestHeader));
final BeaconBlock block = new BeaconBlock(spec.atSlot(slot).getSchemaDefinitions().getBeaconBlockSchema(), slot, proposer_index, parentRoot, matchingState.hashTreeRoot(), body);
return new BeaconBlockAndState(block, matchingState);
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock in project teku by ConsenSys.
the class DataStructureUtil method createAnchorFromState.
public AnchorPoint createAnchorFromState(final BeaconState anchorState) {
// Create corresponding block
final SchemaDefinitions schemaDefinitions = spec.atSlot(anchorState.getSlot()).getSchemaDefinitions();
final BeaconBlock anchorBlock = new BeaconBlock(schemaDefinitions.getBeaconBlockSchema(), anchorState.getSlot(), UInt64.ZERO, anchorState.getLatest_block_header().getParentRoot(), anchorState.hashTreeRoot(), spec.getGenesisSpec().getSchemaDefinitions().getBeaconBlockBodySchema().createEmpty());
final SignedBeaconBlock signedAnchorBlock = SignedBeaconBlock.create(spec, anchorBlock, BLSSignature.empty());
final Bytes32 anchorRoot = anchorBlock.hashTreeRoot();
final UInt64 anchorEpoch = spec.getCurrentEpoch(anchorState);
final Checkpoint anchorCheckpoint = new Checkpoint(anchorEpoch, anchorRoot);
return AnchorPoint.create(spec, anchorCheckpoint, signedAnchorBlock, anchorState);
}
Aggregations