use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader in project teku by ConsenSys.
the class OperationSignatureVerifier method verifyProposerSlashingSignature.
public boolean verifyProposerSlashingSignature(Fork fork, BeaconState state, ProposerSlashing proposerSlashing, BLSSignatureVerifier signatureVerifier) {
final BeaconBlockHeader header1 = proposerSlashing.getHeader_1().getMessage();
final BeaconBlockHeader header2 = proposerSlashing.getHeader_2().getMessage();
Optional<BLSPublicKey> maybePublicKey = beaconStateAccessors.getValidatorPubKey(state, header1.getProposerIndex());
if (maybePublicKey.isEmpty()) {
return false;
}
BLSPublicKey publicKey = maybePublicKey.get();
if (!signatureVerifier.verify(publicKey, miscHelpers.computeSigningRoot(header1, beaconStateAccessors.getDomain(Domain.BEACON_PROPOSER, miscHelpers.computeEpochAtSlot(header1.getSlot()), fork, state.getGenesis_validators_root())), proposerSlashing.getHeader_1().getSignature())) {
LOG.trace("Header1 signature is invalid {}", header1);
return false;
}
if (!signatureVerifier.verify(publicKey, miscHelpers.computeSigningRoot(header2, beaconStateAccessors.getDomain(Domain.BEACON_PROPOSER, miscHelpers.computeEpochAtSlot(header2.getSlot()), fork, state.getGenesis_validators_root())), proposerSlashing.getHeader_2().getSignature())) {
LOG.trace("Header2 signature is invalid {}", header1);
return false;
}
return true;
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader in project teku by ConsenSys.
the class DataStructureUtil method randomBlockAndState.
private BeaconBlockAndState randomBlockAndState(final UInt64 slot, final BeaconState state, final Bytes32 parentRoot) {
final BeaconBlockBody body = randomBeaconBlockBody();
final UInt64 proposer_index = randomUInt64();
final BeaconBlockHeader latestHeader = new BeaconBlockHeader(slot, proposer_index, parentRoot, Bytes32.ZERO, body.hashTreeRoot());
final BeaconState matchingState = state.updated(s -> s.setLatest_block_header(latestHeader));
final BeaconBlock block = new BeaconBlock(spec.atSlot(slot).getSchemaDefinitions().getBeaconBlockSchema(), slot, proposer_index, parentRoot, matchingState.hashTreeRoot(), body);
return new BeaconBlockAndState(block, matchingState);
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader in project teku by ConsenSys.
the class StateTransition method processSlot.
private BeaconState processSlot(final SpecVersion spec, final BeaconState preState) {
// Cache state root
Bytes32 previous_state_root = preState.hashTreeRoot();
return preState.updated(state -> {
int index = state.getSlot().mod(spec.getSlotsPerHistoricalRoot()).intValue();
state.getState_roots().setElement(index, previous_state_root);
// Cache latest block header state root
BeaconBlockHeader latest_block_header = state.getLatest_block_header();
if (latest_block_header.getStateRoot().equals(Bytes32.ZERO)) {
BeaconBlockHeader latest_block_header_new = new BeaconBlockHeader(latest_block_header.getSlot(), latest_block_header.getProposerIndex(), latest_block_header.getParentRoot(), previous_state_root, latest_block_header.getBodyRoot());
state.setLatest_block_header(latest_block_header_new);
}
// Cache block root
Bytes32 previous_block_root = state.getLatest_block_header().hashTreeRoot();
state.getBlock_roots().setElement(index, previous_block_root);
});
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader in project teku by ConsenSys.
the class AbstractBlockProcessor method processBlockHeader.
@Override
public void processBlockHeader(MutableBeaconState state, BeaconBlockSummary blockHeader) throws BlockProcessingException {
safelyProcess(() -> {
checkArgument(blockHeader.getSlot().equals(state.getSlot()), "process_block_header: Verify that the slots match");
checkArgument(blockHeader.getProposerIndex().longValue() == beaconStateAccessors.getBeaconProposerIndex(state), "process_block_header: Verify that proposer index is the correct index");
checkArgument(blockHeader.getParentRoot().equals(state.getLatest_block_header().hashTreeRoot()), "process_block_header: Verify that the parent matches");
checkArgument(blockHeader.getSlot().compareTo(state.getLatest_block_header().getSlot()) > 0, "process_block_header: Verify that the block is newer than latest block header");
// Cache the current block as the new latest block
state.setLatest_block_header(new BeaconBlockHeader(blockHeader.getSlot(), blockHeader.getProposerIndex(), blockHeader.getParentRoot(), // Overwritten in the next `process_slot` call
Bytes32.ZERO, blockHeader.getBodyRoot()));
// Only if we are processing blocks (not proposing them)
Validator proposer = state.getValidators().get(toIntExact(blockHeader.getProposerIndex().longValue()));
checkArgument(!proposer.isSlashed(), "process_block_header: Verify proposer is not slashed");
});
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader in project teku by ConsenSys.
the class AnchorPoint method fromInitialState.
public static AnchorPoint fromInitialState(final Spec spec, final BeaconState state) {
if (isGenesisState(state)) {
return fromGenesisState(spec, state);
} else {
final BeaconBlockHeader header = BeaconBlockHeader.fromState(state);
// Calculate closest epoch boundary to use for the checkpoint
final UInt64 epoch = spec.computeNextEpochBoundary(state.getSlot());
final Checkpoint checkpoint = new Checkpoint(epoch, header.hashTreeRoot());
return new AnchorPoint(spec, checkpoint, state, header);
}
}
Aggregations