Search in sources :

Example 1 with BeaconBlock

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");
        }
    });
}
Also used : Fork(tech.pegasys.teku.spec.datastructures.state.Fork) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) ArrayList(java.util.ArrayList) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) ArrayList(java.util.ArrayList) List(java.util.List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) BLSSignature(tech.pegasys.teku.bls.BLSSignature)

Example 2 with BeaconBlock

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));
}
Also used : Optional(java.util.Optional) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 3 with BeaconBlock

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);
    }
}
Also used : BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) BeaconBlockBody(tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody) BeaconBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockAndState) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) StateTransitionException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException)

Example 4 with BeaconBlock

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);
}
Also used : BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BeaconBlockBody(tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody) BeaconBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockAndState) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconBlockHeader(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader) SignedBeaconBlockHeader(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockHeader) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Example 5 with BeaconBlock

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);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions)

Aggregations

BeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock)59 Test (org.junit.jupiter.api.Test)35 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)30 BLSSignature (tech.pegasys.teku.bls.BLSSignature)19 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)19 Bytes32 (org.apache.tuweni.bytes.Bytes32)18 BeaconBlockBody (tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody)10 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)9 Optional (java.util.Optional)8 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)6 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)5 ArrayList (java.util.ArrayList)4 Bytes (org.apache.tuweni.bytes.Bytes)4 ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)4 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)4 ForkInfo (tech.pegasys.teku.spec.datastructures.state.ForkInfo)4 DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)4 Collection (java.util.Collection)3 List (java.util.List)3 LogManager (org.apache.logging.log4j.LogManager)3