use of tech.pegasys.teku.api.schema.SignedBeaconBlock in project teku by ConsenSys.
the class TekuNode method waitForAttestationBeingGossiped.
public void waitForAttestationBeingGossiped(int validatorSeparationIndex, int totalValidatorCount) {
List<UInt64> node1Validators = IntStream.range(0, validatorSeparationIndex).mapToObj(UInt64::valueOf).collect(toList());
List<UInt64> node2Validators = IntStream.range(validatorSeparationIndex, totalValidatorCount).mapToObj(UInt64::valueOf).collect(toList());
waitFor(() -> {
final Optional<SignedBlock> maybeBlock = fetchHeadBlock();
final Optional<BeaconState> maybeState = fetchHeadState();
assertThat(maybeBlock).isPresent();
assertThat(maybeState).isPresent();
SignedBeaconBlock block = (SignedBeaconBlock) maybeBlock.get();
BeaconState state = maybeState.get();
// Check that the fetched block and state are in sync
assertThat(state.latest_block_header.parent_root).isEqualTo(block.getMessage().parent_root);
tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState internalBeaconState = state.asInternalBeaconState(spec);
UInt64 proposerIndex = block.getMessage().proposer_index;
Set<UInt64> attesterIndicesInAttestations = block.getMessage().getBody().attestations.stream().map(a -> spec.getAttestingIndices(internalBeaconState, a.asInternalAttestation(spec).getData(), a.asInternalAttestation(spec).getAggregationBits())).flatMap(Collection::stream).map(UInt64::valueOf).collect(toSet());
if (node1Validators.contains(proposerIndex)) {
assertThat(attesterIndicesInAttestations.stream().anyMatch(node2Validators::contains)).isTrue();
} else if (node2Validators.contains(proposerIndex)) {
assertThat(attesterIndicesInAttestations.stream().anyMatch(node1Validators::contains)).isTrue();
} else {
throw new IllegalStateException("Proposer index greater than total validator count");
}
}, 2, MINUTES);
}
use of tech.pegasys.teku.api.schema.SignedBeaconBlock in project teku by ConsenSys.
the class PostBlock method handle.
@OpenApi(path = ROUTE, method = HttpMethod.POST, summary = "Publish a signed block", tags = { TAG_BEACON, TAG_VALIDATOR_REQUIRED }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = SignedBlock.class) }), description = "Submit a signed beacon block to the beacon node to be imported." + " The beacon node performs the required validation.", responses = { @OpenApiResponse(status = RES_OK, description = "Block has been successfully broadcast, validated and imported."), @OpenApiResponse(status = RES_ACCEPTED, description = "Block has been successfully broadcast, but failed validation and has not been imported."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Unable to parse request body."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node experienced an internal error."), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = "Beacon node is currently syncing.") })
@Override
public void handle(final Context ctx) throws Exception {
try {
if (syncDataProvider.isSyncing()) {
ctx.status(SC_SERVICE_UNAVAILABLE);
ctx.json(BadRequest.serviceUnavailable(jsonProvider));
return;
}
final SignedBeaconBlock signedBeaconBlock = validatorDataProvider.parseBlock(jsonProvider, ctx.body());
ctx.future(validatorDataProvider.submitSignedBlock(signedBeaconBlock).thenApplyChecked(validatorBlockResult -> handleResponseContext(ctx, validatorBlockResult)));
} catch (final JsonProcessingException ex) {
ctx.status(SC_BAD_REQUEST);
ctx.json(BadRequest.badRequest(jsonProvider, ex.getMessage()));
} catch (final Exception ex) {
LOG.error("Failed to post block due to internal error", ex);
ctx.status(SC_INTERNAL_SERVER_ERROR);
ctx.json(BadRequest.internalError(jsonProvider, ex.getMessage()));
}
}
use of tech.pegasys.teku.api.schema.SignedBeaconBlock in project teku by ConsenSys.
the class GetBlockIntegrationTest method shouldGetBlock.
@Test
public void shouldGetBlock() throws IOException {
startRestAPIAtGenesis();
final List<SignedBlockAndState> created = createBlocksAtSlots(10);
final Response response = get("head");
final GetBlockResponse body = jsonProvider.jsonToObject(response.body().string(), GetBlockResponse.class);
final SignedBeaconBlock data = body.data;
final SignedBlockAndState block = created.get(0);
assertThat(data).isEqualTo(SignedBeaconBlock.create(block.getBlock()));
}
use of tech.pegasys.teku.api.schema.SignedBeaconBlock in project teku by ConsenSys.
the class OkHttpValidatorRestApiClientTest method sendSignedBlock_MakesExpectedRequest.
@Test
public void sendSignedBlock_MakesExpectedRequest() throws Exception {
final Bytes32 blockRoot = Bytes32.fromHexStringLenient("0x1234");
final SignedBeaconBlock signedBeaconBlock = schemaObjects.signedBeaconBlock();
// Block has been successfully broadcast, validated and imported
mockWebServer.enqueue(new MockResponse().setResponseCode(SC_OK).setBody(asJson(blockRoot)));
apiClient.sendSignedBlock(signedBeaconBlock);
RecordedRequest request = mockWebServer.takeRequest();
assertThat(request.getMethod()).isEqualTo("POST");
assertThat(request.getPath()).contains(ValidatorApiMethod.SEND_SIGNED_BLOCK.getPath(emptyMap()));
assertThat(request.getBody().readString(StandardCharsets.UTF_8)).isEqualTo(asJson(signedBeaconBlock));
}
use of tech.pegasys.teku.api.schema.SignedBeaconBlock in project teku by ConsenSys.
the class OkHttpValidatorRestApiClientTest method sendSignedBlock_WhenServerError_ThrowsRuntimeException.
@Test
public void sendSignedBlock_WhenServerError_ThrowsRuntimeException() {
final SignedBeaconBlock signedBeaconBlock = schemaObjects.signedBeaconBlock();
mockWebServer.enqueue(new MockResponse().setResponseCode(SC_INTERNAL_SERVER_ERROR));
assertThatThrownBy(() -> apiClient.sendSignedBlock(signedBeaconBlock)).isInstanceOf(RuntimeException.class).hasMessageContaining("Unexpected response from Beacon Node API");
}
Aggregations