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