use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.
the class ExternalValidatorSource method addValidator.
@Override
public AddValidatorResult addValidator(final BLSPublicKey publicKey, final Optional<URL> signerUrl) {
if (!canUpdateValidators()) {
return new AddValidatorResult(PostKeyResult.error("Cannot add validator to a read only source."), Optional.empty());
}
final DataDirLayout dataDirLayout = maybeDataDirLayout.orElseThrow();
final String fileName = publicKey.toBytesCompressed().toUnprefixedHexString();
final Path path = ValidatorClientService.getManagedRemoteKeyPath(dataDirLayout).resolve(fileName + ".json");
try {
ensureDirectoryExists(ValidatorClientService.getManagedRemoteKeyPath(dataDirLayout));
if (path.toFile().exists()) {
return new AddValidatorResult(PostKeyResult.duplicate(), Optional.empty());
}
Files.write(path, serialize(new ExternalValidator(publicKey, signerUrl), ValidatorTypes.EXTERNAL_VALIDATOR_STORE).getBytes(UTF_8));
final URL url = signerUrl.orElse(config.getValidatorExternalSignerUrl());
final ValidatorProvider provider = new ExternalValidatorProvider(spec, externalSignerHttpClientFactory, url, publicKey, config.getValidatorExternalSignerTimeout(), externalSignerTaskQueue, metricsSystem, readOnly);
externalValidatorSourceMap.put(publicKey, url);
return new AddValidatorResult(PostKeyResult.success(), Optional.of(provider.createSigner()));
} catch (InvalidConfigurationException | IOException ex) {
cleanupIncompleteSave(path);
return new AddValidatorResult(PostKeyResult.error(ex.getMessage()), Optional.empty());
}
}
use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.
the class ActiveKeyManagerTest method shouldReturnActiveRemoteValidatorsList.
@Test
void shouldReturnActiveRemoteValidatorsList() throws MalformedURLException {
final BLSKeyPair keyPair1 = BLSTestUtil.randomKeyPair(1);
final BLSKeyPair keyPair2 = BLSTestUtil.randomKeyPair(2);
final BLSKeyPair keyPair3 = BLSTestUtil.randomKeyPair(3);
final Validator validator1 = new Validator(keyPair1.getPublicKey(), NO_OP_REMOTE_SIGNER, Optional::empty, true);
final Validator validator2 = new Validator(keyPair2.getPublicKey(), NO_OP_REMOTE_SIGNER, Optional::empty, false);
final Validator validator3 = new Validator(keyPair3.getPublicKey(), NO_OP_SIGNER, Optional::empty, false);
List<Validator> activeValidators = Arrays.asList(validator1, validator2, validator3);
when(ownedValidators.getActiveValidators()).thenReturn(activeValidators);
when(validatorLoader.getOwnedValidators()).thenReturn(ownedValidators);
final List<ExternalValidator> result = keyManager.getActiveRemoteValidatorKeys();
List<ExternalValidator> externalValidators = Arrays.asList(new ExternalValidator(keyPair1.getPublicKey(), Optional.of(new URL("http://example.com/")), true), new ExternalValidator(keyPair2.getPublicKey(), Optional.of(new URL("http://example.com/")), false));
assertThat(result).isEqualTo(externalValidators);
}
use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.
the class PostRemoteKeys method handle.
@Override
public void handle(final RestApiRequest request) throws JsonProcessingException {
final PostRemoteKeysRequest body = request.getRequestBody();
if (body.getExternalValidators().isEmpty()) {
request.respondOk(Collections.emptyList());
return;
}
List<ExternalValidator> validators = body.getExternalValidators();
request.respondOk(keyManager.importExternalValidators(validators));
}
use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.
the class ActiveKeyManager method importExternalValidators.
@Override
public List<PostKeyResult> importExternalValidators(final List<ExternalValidator> validators) {
final List<PostKeyResult> importResults = new ArrayList<>();
boolean reloadRequired = false;
for (ExternalValidator v : validators) {
try {
importResults.add(validatorLoader.loadExternalMutableValidator(v.getPublicKey(), v.getUrl()));
if (importResults.get(importResults.size() - 1).getImportStatus() == ImportStatus.IMPORTED) {
reloadRequired = true;
}
} catch (Exception e) {
importResults.add(PostKeyResult.error(e.getMessage()));
}
}
if (reloadRequired) {
validatorTimingChannel.onValidatorsAdded();
}
return importResults;
}
use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.
the class ValidatorTypesTest method postRemoteKeysRequest_shouldFormatPostRemoteKeysRequestOptionalUrl.
@SuppressWarnings("unchecked")
@Test
void postRemoteKeysRequest_shouldFormatPostRemoteKeysRequestOptionalUrl() throws Exception {
final BLSPublicKey publicKey1 = dataStructureUtil.randomPublicKey();
final BLSPublicKey publicKey2 = dataStructureUtil.randomPublicKey();
final List<ExternalValidator> externalValidators = List.of(new ExternalValidator(publicKey1, Optional.empty()), new ExternalValidator(publicKey2, Optional.of(new URL("http://host.com"))));
final PostRemoteKeysRequest request = new PostRemoteKeysRequest(externalValidators);
final Map<String, Object> result = JsonTestUtil.parse(JsonUtil.serialize(request, ValidatorTypes.POST_REMOTE_KEYS_REQUEST));
assertThat(result).containsOnlyKeys("remote_keys").isInstanceOf(HashMap.class);
final List<Map<String, Object>> remoteKeys = (List<Map<String, Object>>) result.get("remote_keys");
assertThat(remoteKeys).containsExactly(Map.of("pubkey", publicKey1.toString()), Map.of("pubkey", publicKey2.toString(), "url", new URL("http://host.com").toString()));
}
Aggregations