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);
}
}
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);
}
}
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;
}
}
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);
}
}
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);
}
}
Aggregations