use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockAndState 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.datastructures.blocks.BeaconBlockAndState 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.BeaconBlockAndState in project teku by ConsenSys.
the class AnchorPointTest method create_withCheckpointPriorToState.
@Test
public void create_withCheckpointPriorToState() {
final UInt64 epoch = UInt64.valueOf(10);
final UInt64 epochStartSlot = spec.computeStartSlotAtEpoch(epoch);
final BeaconBlockAndState blockAndState = dataStructureUtil.randomBlockAndState(epochStartSlot.plus(1));
final Checkpoint checkpoint = new Checkpoint(epoch, blockAndState.getRoot());
assertThatThrownBy(() -> AnchorPoint.create(spec, checkpoint, blockAndState.getState(), Optional.empty())).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Block must be at or prior to the start of the checkpoint epoch");
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockAndState in project teku by ConsenSys.
the class BlockProposalTestUtil method createNewBlock.
public SignedBlockAndState createNewBlock(final Signer signer, final UInt64 newSlot, final BeaconState state, final Bytes32 parentBlockSigningRoot, final Eth1Data eth1Data, final SszList<Attestation> attestations, final SszList<ProposerSlashing> slashings, final SszList<Deposit> deposits, final SszList<SignedVoluntaryExit> exits, final Optional<List<Bytes>> transactions, final Optional<Bytes32> terminalBlock, final Optional<ExecutionPayload> executionPayload) throws StateTransitionException, EpochProcessingException, SlotProcessingException {
final UInt64 newEpoch = spec.computeEpochAtSlot(newSlot);
final BLSSignature randaoReveal = signer.createRandaoReveal(newEpoch, state.getForkInfo()).join();
final BeaconState blockSlotState = spec.processSlots(state, newSlot);
final BeaconBlockAndState newBlockAndState = spec.createNewUnsignedBlock(newSlot, spec.getBeaconProposerIndex(blockSlotState, newSlot), blockSlotState, parentBlockSigningRoot, builder -> builder.randaoReveal(randaoReveal).eth1Data(eth1Data).graffiti(Bytes32.ZERO).attestations(attestations).proposerSlashings(slashings).attesterSlashings(blockBodyLists.createAttesterSlashings()).deposits(deposits).voluntaryExits(exits).syncAggregate(() -> dataStructureUtil.emptySyncAggregateIfRequiredByState(blockSlotState)).executionPayload(() -> executionPayload.orElseGet(() -> createExecutionPayload(newSlot, state, transactions, terminalBlock))));
// Sign block and set block signature
final BeaconBlock block = newBlockAndState.getBlock();
BLSSignature blockSignature = signer.signBlock(block, state.getForkInfo()).join();
final SignedBeaconBlock signedBlock = SignedBeaconBlock.create(spec, block, blockSignature);
return new SignedBlockAndState(signedBlock, newBlockAndState.getState());
}
use of tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockAndState in project teku by ConsenSys.
the class DatabaseMigraterTest method shouldCopyColumnData.
@Test
void shouldCopyColumnData(@TempDir Path tmpDir) throws IOException, DatabaseMigraterError {
final DataDirLayout dataDirLayout = prepareTempDir(tmpDir, "5");
DatabaseMigrater migrater = getDatabaseMigrater(dataDirLayout);
final BeaconBlockAndState blockAndState = dataStructureUtil.randomBlockAndState(1_000_000);
migrater.openDatabases(DatabaseVersion.V5, DatabaseVersion.LEVELDB2);
TestKvStoreDatabase originalDb = new TestKvStoreDatabase(migrater.getOriginalDatabase());
try (KvStoreHotDao.HotUpdater updater = originalDb.getHotDao().hotUpdater()) {
updater.addHotState(blockAndState.getBlock().getRoot(), blockAndState.getState());
updater.commit();
}
migrater.migrateData();
TestKvStoreDatabase newDb = new TestKvStoreDatabase(migrater.getNewDatabase());
assertThat(newDb.getHotDao().getHotState(blockAndState.getRoot())).contains(blockAndState.getState());
migrater.closeDatabases();
}
Aggregations