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