Search in sources :

Example 1 with SlotProcessingException

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

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

use of tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException 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)

Aggregations

BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)3 EpochProcessingException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException)3 SlotProcessingException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException)3 Bytes32 (org.apache.tuweni.bytes.Bytes32)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 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 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 Spec (tech.pegasys.teku.spec.Spec)1 SpecVersion (tech.pegasys.teku.spec.SpecVersion)1 BeaconBlockHeader (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader)1 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)1