Search in sources :

Example 1 with ValidatorProvider

use of tech.pegasys.teku.validator.client.loader.ValidatorSource.ValidatorProvider in project teku by ConsenSys.

the class MultithreadedValidatorLoader method loadValidators.

public static void loadValidators(final OwnedValidators ownedValidators, final Map<BLSPublicKey, ValidatorProvider> providers, final GraffitiProvider graffitiProvider) {
    final int totalValidatorCount = providers.size();
    STATUS_LOG.loadingValidators(totalValidatorCount);
    final ExecutorService executorService = Executors.newFixedThreadPool(Math.min(4, Runtime.getRuntime().availableProcessors()));
    try {
        final AtomicInteger numberOfLoadedKeys = new AtomicInteger(0);
        final List<Future<Validator>> futures = providers.values().stream().map(provider -> executorService.submit(() -> {
            final Validator validator = new Validator(provider.getPublicKey(), new DeletableSigner(provider.createSigner()), graffitiProvider, provider.isReadOnly());
            int loadedValidatorCount = numberOfLoadedKeys.incrementAndGet();
            if (loadedValidatorCount % 10 == 0) {
                STATUS_LOG.atLoadedValidatorNumber(loadedValidatorCount, totalValidatorCount);
            }
            return validator;
        })).collect(toList());
        final List<Validator> addedValidators = new ArrayList<>();
        for (Future<Validator> future : futures) {
            final Validator validator = future.get();
            addedValidators.add(validator);
        }
        // Only start adding validators once we've successfully loaded all keys
        addedValidators.forEach(ownedValidators::addValidator);
        STATUS_LOG.validatorsInitialised(addedValidators.stream().map(validator -> validator.getPublicKey().toAbbreviatedString()).collect(toList()));
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while attempting to load validator key files", e);
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw new RuntimeException("Unable to load validator key files", e);
    } finally {
        executorService.shutdownNow();
    }
}
Also used : GraffitiProvider(tech.pegasys.teku.validator.api.GraffitiProvider) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) DeletableSigner(tech.pegasys.teku.core.signatures.DeletableSigner) Executors(java.util.concurrent.Executors) ValidatorProvider(tech.pegasys.teku.validator.client.loader.ValidatorSource.ValidatorProvider) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) STATUS_LOG(tech.pegasys.teku.infrastructure.logging.StatusLogger.STATUS_LOG) ExecutorService(java.util.concurrent.ExecutorService) Validator(tech.pegasys.teku.validator.client.Validator) DeletableSigner(tech.pegasys.teku.core.signatures.DeletableSigner) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Validator(tech.pegasys.teku.validator.client.Validator)

Example 2 with ValidatorProvider

use of tech.pegasys.teku.validator.client.loader.ValidatorSource.ValidatorProvider in project teku by ConsenSys.

the class LocalValidatorSourceTest method shouldLoadKeysFromKeyStores.

@Test
void shouldLoadKeysFromKeyStores(@TempDir final Path tempDir) throws Exception {
    // load keystores from resources
    final Path scryptKeystore = Path.of(Resources.getResource("scryptTestVector.json").toURI());
    final Path pbkdf2Keystore = Path.of(Resources.getResource("pbkdf2TestVector.json").toURI());
    // create password file
    final Path tempPasswordFile = createTempFile(tempDir, "pass", ".txt");
    writeString(tempPasswordFile, EXPECTED_PASSWORD);
    final List<Pair<Path, Path>> keystorePasswordFilePairs = List.of(Pair.of(scryptKeystore, tempPasswordFile), Pair.of(pbkdf2Keystore, tempPasswordFile));
    when(keyStoreFilesLocator.parse()).thenReturn(keystorePasswordFilePairs);
    final List<ValidatorProvider> availableValidators = validatorSource.getAvailableValidators();
    assertThat(availableValidators).hasSize(2);
    // Both keystores encyrpt the same key.
    assertProviderMatchesKey(availableValidators.get(0), EXPECTED_BLS_KEY_PAIR);
    assertProviderMatchesKey(availableValidators.get(1), EXPECTED_BLS_KEY_PAIR);
}
Also used : Path(java.nio.file.Path) ValidatorProvider(tech.pegasys.teku.validator.client.loader.ValidatorSource.ValidatorProvider) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.jupiter.api.Test)

Aggregations

ValidatorProvider (tech.pegasys.teku.validator.client.loader.ValidatorSource.ValidatorProvider)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1 Pair (org.apache.commons.lang3.tuple.Pair)1 Test (org.junit.jupiter.api.Test)1 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)1 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)1 DeletableSigner (tech.pegasys.teku.core.signatures.DeletableSigner)1 STATUS_LOG (tech.pegasys.teku.infrastructure.logging.StatusLogger.STATUS_LOG)1 GraffitiProvider (tech.pegasys.teku.validator.api.GraffitiProvider)1 Validator (tech.pegasys.teku.validator.client.Validator)1