Search in sources :

Example 1 with BlockProcessingException

use of tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException 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 2 with BlockProcessingException

use of tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException in project teku by ConsenSys.

the class BlockProcessorAltair method processSyncAggregate.

@Override
public void processSyncAggregate(final MutableBeaconState baseState, final SyncAggregate aggregate, final BLSSignatureVerifier signatureVerifier) throws BlockProcessingException {
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(baseState);
    final List<BLSPublicKey> participantPubkeys = new ArrayList<>();
    final List<BLSPublicKey> idlePubkeys = new ArrayList<>();
    for (int i = 0; i < specConfigAltair.getSyncCommitteeSize(); i++) {
        final BLSPublicKey publicKey = syncCommitteeUtil.getCurrentSyncCommitteeParticipantPubKey(state, i);
        if (aggregate.getSyncCommitteeBits().getBit(i)) {
            participantPubkeys.add(publicKey);
        } else {
            idlePubkeys.add(publicKey);
        }
    }
    final UInt64 previousSlot = state.getSlot().minusMinZero(1);
    final Bytes32 domain = beaconStateAccessors.getDomain(state.getForkInfo(), Domain.SYNC_COMMITTEE, miscHelpers.computeEpochAtSlot(previousSlot));
    final Bytes32 signingRoot = miscHelpersAltair.computeSigningRoot(beaconStateAccessors.getBlockRootAtSlot(state, previousSlot), domain);
    if (!eth2FastAggregateVerify(signatureVerifier, participantPubkeys, signingRoot, aggregate.getSyncCommitteeSignature().getSignature())) {
        throw new BlockProcessingException("Invalid sync committee signature in " + aggregate);
    }
    // Compute participant and proposer rewards
    final UInt64 totalActiveIncrements = beaconStateAccessors.getTotalActiveBalance(state).dividedBy(specConfig.getEffectiveBalanceIncrement());
    final UInt64 totalBaseRewards = beaconStateAccessorsAltair.getBaseRewardPerIncrement(state).times(totalActiveIncrements);
    final UInt64 maxParticipantRewards = totalBaseRewards.times(SYNC_REWARD_WEIGHT).dividedBy(WEIGHT_DENOMINATOR).dividedBy(specConfig.getSlotsPerEpoch());
    final UInt64 participantReward = maxParticipantRewards.dividedBy(specConfigAltair.getSyncCommitteeSize());
    final UInt64 proposerReward = participantReward.times(PROPOSER_WEIGHT).dividedBy(WEIGHT_DENOMINATOR.minus(PROPOSER_WEIGHT));
    // Apply participant and proposer rewards
    participantPubkeys.stream().map(pubkey -> validatorsUtil.getValidatorIndex(state, pubkey).orElseThrow()).forEach(participantIndex -> beaconStateMutators.increaseBalance(state, participantIndex, participantReward));
    UInt64 totalProposerReward = proposerReward.times(participantPubkeys.size());
    beaconStateMutators.increaseBalance(state, beaconStateAccessors.getBeaconProposerIndex(state), totalProposerReward);
    // impose penalties for any idle validators
    idlePubkeys.stream().map(pubkey -> validatorsUtil.getValidatorIndex(state, pubkey).orElseThrow()).forEach(participantIndex -> beaconStateMutators.decreaseBalance(state, participantIndex, participantReward));
}
Also used : BeaconBlockBodyAltair(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodyAltair) MiscHelpersAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair) AbstractBlockProcessor(tech.pegasys.teku.spec.logic.common.block.AbstractBlockProcessor) PARTICIPATION_FLAG_WEIGHTS(tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair.PARTICIPATION_FLAG_WEIGHTS) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) ArrayList(java.util.ArrayList) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) ExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload) SyncCommitteeUtil(tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil) SYNC_REWARD_WEIGHT(tech.pegasys.teku.spec.constants.IncentivizationWeights.SYNC_REWARD_WEIGHT) WEIGHT_DENOMINATOR(tech.pegasys.teku.spec.constants.IncentivizationWeights.WEIGHT_DENOMINATOR) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) OperationValidator(tech.pegasys.teku.spec.logic.common.operations.validation.OperationValidator) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) Domain(tech.pegasys.teku.spec.constants.Domain) BeaconStateAccessorsAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) OptimisticExecutionPayloadExecutor(tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) ValidatorsUtil(tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Predicates(tech.pegasys.teku.spec.logic.common.helpers.Predicates) MutableBeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState) List(java.util.List) IndexedAttestationCache(tech.pegasys.teku.spec.cache.IndexedAttestationCache) AttestationUtil(tech.pegasys.teku.spec.logic.common.util.AttestationUtil) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) BeaconStateMutators(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators) SszMutableList(tech.pegasys.teku.infrastructure.ssz.SszMutableList) SpecConfigAltair(tech.pegasys.teku.spec.config.SpecConfigAltair) PROPOSER_WEIGHT(tech.pegasys.teku.spec.constants.IncentivizationWeights.PROPOSER_WEIGHT) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) SyncAggregate(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregate) OperationSignatureVerifier(tech.pegasys.teku.spec.logic.common.operations.OperationSignatureVerifier) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) ArrayList(java.util.ArrayList) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 3 with BlockProcessingException

use of tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException in project teku by ConsenSys.

the class BlockProcessorTest method processDepositHelper.

private BeaconState processDepositHelper(BeaconState beaconState, DepositData depositData) throws BlockProcessingException {
    // Add the deposit to a Merkle tree so that we can get the root to put into the state Eth1 data
    MerkleTree depositMerkleTree = new OptimizedMerkleTree(specConfig.getDepositContractTreeDepth());
    depositMerkleTree.add(depositData.hashTreeRoot());
    beaconState = beaconState.updated(state -> state.setEth1_data(new Eth1Data(depositMerkleTree.getRoot(), UInt64.valueOf(1), Bytes32.ZERO)));
    SszListSchema<Deposit, ?> schema = SszListSchema.create(DepositWithIndex.SSZ_SCHEMA, specConfig.getMaxDeposits());
    SszBytes32Vector proof = Deposit.SSZ_SCHEMA.getProofSchema().of(depositMerkleTree.getProof(0));
    SszList<Deposit> deposits = schema.of(new DepositWithIndex(proof, depositData, UInt64.valueOf(0)));
    // Attempt to process deposit with above data.
    return beaconState.updated(state -> blockProcessor.processDeposits(state, deposits));
}
Also used : BouncyCastleExtension(org.apache.tuweni.junit.BouncyCastleExtension) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) Bytes(org.apache.tuweni.bytes.Bytes) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) SpecVersion(tech.pegasys.teku.spec.SpecVersion) ArrayList(java.util.ArrayList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) SszListSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Test(org.junit.jupiter.api.Test) List(java.util.List) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Bytes48(org.apache.tuweni.bytes.Bytes48) DepositMessage(tech.pegasys.teku.spec.datastructures.operations.DepositMessage) DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)

Example 4 with BlockProcessingException

use of tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException in project teku by ConsenSys.

the class AbstractBlockProcessor method processAttestations.

@Override
public void processAttestations(final MutableBeaconState state, final SszList<Attestation> attestations, final BLSSignatureVerifier signatureVerifier) throws BlockProcessingException {
    final CapturingIndexedAttestationCache indexedAttestationCache = IndexedAttestationCache.capturing();
    processAttestationsNoVerification(state, attestations, indexedAttestationCache);
    final BlockValidationResult result = verifyAttestationSignatures(state, attestations, signatureVerifier, indexedAttestationCache);
    if (!result.isValid()) {
        throw new BlockProcessingException(result.getFailureReason());
    }
}
Also used : CapturingIndexedAttestationCache(tech.pegasys.teku.spec.cache.CapturingIndexedAttestationCache) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) BlockValidationResult(tech.pegasys.teku.spec.logic.common.statetransition.blockvalidator.BlockValidationResult)

Example 5 with BlockProcessingException

use of tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException in project teku by ConsenSys.

the class FuzzUtil method fuzzVoluntaryExit.

public Optional<byte[]> fuzzVoluntaryExit(final byte[] input) {
    VoluntaryExitFuzzInput structuredInput = deserialize(input, VoluntaryExitFuzzInput.createSchema(spec.getGenesisSpec()));
    SszList<SignedVoluntaryExit> voluntaryExits = beaconBlockBodySchema.getVoluntaryExitsSchema().of(structuredInput.getExit());
    try {
        BeaconState postState = structuredInput.getState().updated(state -> spec.getBlockProcessor(state.getSlot()).processVoluntaryExits(state, voluntaryExits, signatureVerifier));
        Bytes output = postState.sszSerialize();
        return Optional.of(output.toArrayUnsafe());
    } catch (BlockProcessingException e) {
        // "expected error"
        return Optional.empty();
    }
}
Also used : SignedVoluntaryExit(tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit) Bytes(org.apache.tuweni.bytes.Bytes) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) VoluntaryExitFuzzInput(tech.pegasys.teku.fuzz.input.VoluntaryExitFuzzInput) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Aggregations

BlockProcessingException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException)14 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)9 Bytes (org.apache.tuweni.bytes.Bytes)7 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 Deposit (tech.pegasys.teku.spec.datastructures.operations.Deposit)3 BlockValidationResult (tech.pegasys.teku.spec.logic.common.statetransition.blockvalidator.BlockValidationResult)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)2 BLSSignature (tech.pegasys.teku.bls.BLSSignature)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 BeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock)2 Bytes48 (org.apache.tuweni.bytes.Bytes48)1 BouncyCastleExtension (org.apache.tuweni.junit.BouncyCastleExtension)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Test (org.junit.jupiter.api.Test)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 BLSSignatureVerifier (tech.pegasys.teku.bls.BLSSignatureVerifier)1 AttestationFuzzInput (tech.pegasys.teku.fuzz.input.AttestationFuzzInput)1