Search in sources :

Example 1 with EpochProcessingException

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

the class ChainBuilder method appendNewBlockToChain.

private SignedBlockAndState appendNewBlockToChain(final UInt64 slot, final BlockOptions options) {
    final SignedBlockAndState latestBlockAndState = getLatestBlockAndState();
    final BeaconState preState = latestBlockAndState.getState();
    final Bytes32 parentRoot = latestBlockAndState.getBlock().getMessage().hashTreeRoot();
    int proposerIndex = blockProposalTestUtil.getProposerIndexForSlot(preState, slot);
    if (options.getWrongProposer()) {
        proposerIndex = (proposerIndex == 0 ? 1 : proposerIndex - 1);
    }
    final Signer signer = getSigner(proposerIndex);
    final SignedBlockAndState nextBlockAndState;
    try {
        SszList<Attestation> attestations = BeaconBlockBodyLists.ofSpec(spec).createAttestations(options.getAttestations().toArray(new Attestation[0]));
        nextBlockAndState = blockProposalTestUtil.createBlock(signer, slot, preState, parentRoot, Optional.of(attestations), Optional.empty(), Optional.empty(), options.getEth1Data(), options.getTransactions(), options.getTerminalBlockHash(), options.getExecutionPayload(), options.getSkipStateTransition());
        trackBlock(nextBlockAndState);
        return nextBlockAndState;
    } catch (StateTransitionException | EpochProcessingException | SlotProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Signer(tech.pegasys.teku.core.signatures.Signer) LocalSigner(tech.pegasys.teku.core.signatures.LocalSigner) SlotProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) StateTransitionException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException)

Example 2 with EpochProcessingException

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

the class StateTransition method processSlots.

public BeaconState processSlots(BeaconState preState, UInt64 slot) throws SlotProcessingException, EpochProcessingException {
    try {
        checkArgument(preState.getSlot().compareTo(slot) < 0, "process_slots: State slot %s higher than given slot %s", preState.getSlot(), slot);
        BeaconState state = preState;
        SpecVersion currentSpec = specProvider.getSpec(state.getSlot());
        while (state.getSlot().compareTo(slot) < 0) {
            // Transition from current to new slot (advance by 1)
            final UInt64 currentSlot = state.getSlot();
            final UInt64 newSlot = currentSlot.plus(1);
            final boolean isEpochTransition = newSlot.mod(currentSpec.getSlotsPerEpoch()).equals(UInt64.ZERO);
            state = processSlot(currentSpec, state);
            // Process epoch on the start slot of the next epoch
            if (isEpochTransition) {
                state = currentSpec.getEpochProcessor().processEpoch(state);
            }
            state = state.updated(s -> s.setSlot(newSlot));
            // Update spec, perform state upgrades on epoch boundaries
            if (isEpochTransition) {
                final SpecVersion newSpec = specProvider.getSpec(newSlot);
                if (!newSpec.getMilestone().equals(currentSpec.getMilestone())) {
                    // We've just transition to a new milestone - upgrade the state if necessary
                    final BeaconState prevMilestoneState = state;
                    state = newSpec.getStateUpgrade().map(u -> (BeaconState) u.upgrade(prevMilestoneState)).orElse(prevMilestoneState);
                    // Update spec
                    currentSpec = newSpec;
                }
            }
        }
        return state;
    } catch (IllegalArgumentException e) {
        LOG.warn(e.getMessage(), e);
        throw new SlotProcessingException(e);
    }
}
Also used : Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Logger(org.apache.logging.log4j.Logger) SlotProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) LogManager(org.apache.logging.log4j.LogManager) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconBlockHeader(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader) SpecVersion(tech.pegasys.teku.spec.SpecVersion) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) SpecVersion(tech.pegasys.teku.spec.SpecVersion) SlotProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Example 3 with EpochProcessingException

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

the class TransitionCommand method processStateTransition.

private int processStateTransition(final InAndOutParams params, final StateTransitionFunction transition) {
    final Spec spec = params.eth2NetworkOptions.getNetworkConfiguration().getSpec();
    try (final InputStream in = selectInputStream(params);
        final OutputStream out = selectOutputStream(params)) {
        final Bytes inData = Bytes.wrap(ByteStreams.toByteArray(in));
        BeaconState state = readState(spec, inData);
        try {
            BeaconState result = transition.applyTransition(spec, state);
            out.write(result.sszSerialize().toArrayUnsafe());
            return 0;
        } catch (final StateTransitionException | EpochProcessingException | SlotProcessingException e) {
            SUB_COMMAND_LOG.error("State transition failed", e);
            return 1;
        }
    } catch (final SSZException e) {
        SUB_COMMAND_LOG.error(e.getMessage());
        return 1;
    } catch (final IOException e) {
        SUB_COMMAND_LOG.error("I/O error: " + e.toString());
        return 1;
    } catch (final Throwable t) {
        t.printStackTrace();
        return 2;
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) SlotProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) SSZException(org.apache.tuweni.ssz.SSZException) IOException(java.io.IOException) Spec(tech.pegasys.teku.spec.Spec) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) StateTransitionException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException)

Example 4 with EpochProcessingException

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

the class AbstractEpochProcessor method processJustificationAndFinalization.

/**
 * Processes justification and finalization
 */
@Override
public void processJustificationAndFinalization(MutableBeaconState state, TotalBalances totalBalances) throws EpochProcessingException {
    try {
        UInt64 currentEpoch = beaconStateAccessors.getCurrentEpoch(state);
        if (currentEpoch.isLessThanOrEqualTo(SpecConfig.GENESIS_EPOCH.plus(1))) {
            return;
        }
        final UInt64 totalActiveBalance = totalBalances.getCurrentEpochActiveValidators();
        final UInt64 previousEpochTargetBalance = totalBalances.getPreviousEpochTargetAttesters();
        final UInt64 currentEpochTargetBalance = totalBalances.getCurrentEpochTargetAttesters();
        weighJustificationAndFinalization(state, currentEpoch, totalActiveBalance, previousEpochTargetBalance, currentEpochTargetBalance);
    } catch (IllegalArgumentException e) {
        throw new EpochProcessingException(e);
    }
}
Also used : EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 5 with EpochProcessingException

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

the class AbstractEpochProcessor method processRegistryUpdates.

/**
 * Processes validator registry updates
 */
@Override
public void processRegistryUpdates(MutableBeaconState state, List<ValidatorStatus> statuses) throws EpochProcessingException {
    try {
        // Process activation eligibility and ejections
        SszMutableList<Validator> validators = state.getValidators();
        final UInt64 currentEpoch = beaconStateAccessors.getCurrentEpoch(state);
        for (int index = 0; index < validators.size(); index++) {
            final ValidatorStatus status = statuses.get(index);
            // confirm it isn't already in the queue.
            if (!status.isActiveInCurrentEpoch() && status.getCurrentEpochEffectiveBalance().equals(specConfig.getMaxEffectiveBalance())) {
                final Validator validator = validators.get(index);
                if (validator.getActivation_eligibility_epoch().equals(SpecConfig.FAR_FUTURE_EPOCH)) {
                    validators.set(index, validator.withActivation_eligibility_epoch(currentEpoch.plus(UInt64.ONE)));
                }
            }
            if (status.isActiveInCurrentEpoch() && status.getCurrentEpochEffectiveBalance().isLessThanOrEqualTo(specConfig.getEjectionBalance())) {
                beaconStateMutators.initiateValidatorExit(state, index);
            }
        }
        // Queue validators eligible for activation and not yet dequeued for activation
        List<Integer> activationQueue = IntStream.range(0, state.getValidators().size()).filter(index -> !statuses.get(index).isActiveInCurrentEpoch()).filter(index -> {
            Validator validator = state.getValidators().get(index);
            return validatorsUtil.isEligibleForActivation(state, validator);
        }).boxed().sorted((index1, index2) -> {
            int comparisonResult = state.getValidators().get(index1).getActivation_eligibility_epoch().compareTo(state.getValidators().get(index2).getActivation_eligibility_epoch());
            if (comparisonResult == 0) {
                return index1.compareTo(index2);
            } else {
                return comparisonResult;
            }
        }).collect(Collectors.toList());
        // Dequeued validators for activation up to churn limit (without resetting activation epoch)
        int churnLimit = beaconStateAccessors.getValidatorChurnLimit(state).intValue();
        int sublistSize = Math.min(churnLimit, activationQueue.size());
        for (Integer index : activationQueue.subList(0, sublistSize)) {
            state.getValidators().update(index, validator -> validator.withActivation_epoch(miscHelpers.computeActivationExitEpoch(currentEpoch)));
        }
    } catch (IllegalArgumentException e) {
        throw new EpochProcessingException(e);
    }
}
Also used : IntStream(java.util.stream.IntStream) ValidatorStatusFactory(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatusFactory) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) TotalBalances(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances) HistoricalBatch(tech.pegasys.teku.spec.datastructures.state.HistoricalBatch) RewardAndPenalty(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty) SszBitvector(tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) ValidatorStatuses(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatuses) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) SszMutableUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) ValidatorsUtil(tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil) Collectors(java.util.stream.Collectors) MutableBeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState) List(java.util.List) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) BeaconStateMutators(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators) SszMutableList(tech.pegasys.teku.infrastructure.ssz.SszMutableList) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Aggregations

EpochProcessingException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException)6 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)4 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 SlotProcessingException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException)3 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)2 StateTransitionException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 SSZException (org.apache.tuweni.ssz.SSZException)1 LocalSigner (tech.pegasys.teku.core.signatures.LocalSigner)1 Signer (tech.pegasys.teku.core.signatures.Signer)1