use of tech.pegasys.teku.infrastructure.async.SafeFuture in project teku by ConsenSys.
the class TekuDepositSender method sendValidatorDeposits.
public void sendValidatorDeposits(final BesuNode eth1Node, final List<ValidatorKeys> validatorKeys, UInt64 amount) throws InterruptedException, ExecutionException, TimeoutException {
final Eth1Address eth1Address = Eth1Address.fromHexString(eth1Node.getDepositContractAddress());
final Credentials eth1Credentials = Credentials.create(eth1Node.getRichBenefactorKey());
final DepositSenderService depositSenderService = new DepositSenderService(spec, eth1Node.getExternalJsonRpcUrl(), eth1Credentials, eth1Address, amount);
final List<SafeFuture<TransactionReceipt>> transactionReceipts = validatorKeys.stream().map(depositSenderService::sendDeposit).collect(Collectors.toList());
final SafeFuture<Void> future = SafeFuture.allOf(transactionReceipts.toArray(SafeFuture[]::new));
Waiter.waitFor(future, Duration.ofMinutes(2));
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture in project teku by ConsenSys.
the class SyncController method startSync.
private InProgressSync startSync(final SyncTarget syncTarget) {
eventThread.checkOnEventThread();
final TargetChain chain = syncTarget.getTargetChain();
if (currentSync.map(current -> !current.isFailed() && current.hasSameTarget(chain)).orElse(false)) {
LOG.trace("Not starting new sync because it has the same target as current sync");
return currentSync.get();
}
LOG.debug("Starting new sync to {} with {} peers finalized: {}, speculative: {}", chain.getChainHead(), chain.getPeerCount(), syncTarget.isFinalizedSync(), syncTarget.isSpeculativeSync());
final UInt64 startSlot = recentChainData.getHeadSlot();
final SafeFuture<SyncResult> syncResult = sync.syncToChain(chain);
syncResult.finishAsync(this::onSyncComplete, error -> {
LOG.error("Error encountered during sync", error);
onSyncComplete(SyncResult.FAILED);
}, eventThread);
return new InProgressSync(startSlot, syncTarget, syncResult);
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture in project teku by ConsenSys.
the class BatchImporter method importBatch.
/**
* Import the blocks in the specified batch.
*
* <p>Guaranteed to return immediately and perform the import on worker threads.
*
* @param batch the batch to import
* @return a future reporting the result of the import
*/
public SafeFuture<BatchImportResult> importBatch(final Batch batch) {
// Copy the data from batch as we're going to use them from off the event thread.
final List<SignedBeaconBlock> blocks = new ArrayList<>(batch.getBlocks());
final Optional<SyncSource> source = batch.getSource();
checkState(!blocks.isEmpty(), "Batch has no blocks to import");
return asyncRunner.runAsync(() -> {
SafeFuture<BlockImportResult> importResult = importBlock(blocks.get(0), source.orElseThrow());
for (int i = 1; i < blocks.size(); i++) {
final SignedBeaconBlock block = blocks.get(i);
importResult = importResult.thenCompose(previousResult -> {
if (previousResult.isSuccessful()) {
return importBlock(block, source.orElseThrow());
} else {
return SafeFuture.completedFuture(previousResult);
}
});
}
return importResult.thenApply(lastBlockImportResult -> {
if (lastBlockImportResult.isSuccessful()) {
return BatchImportResult.IMPORTED_ALL_BLOCKS;
} else if (lastBlockImportResult.getFailureReason() == BlockImportResult.FailureReason.FAILED_EXECUTION_PAYLOAD_EXECUTION) {
return BatchImportResult.SERVICE_OFFLINE;
}
LOG.debug("Failed to import batch {}: {}", batch, lastBlockImportResult.getFailureReason(), lastBlockImportResult.getFailureCause().orElse(null));
return BatchImportResult.IMPORT_FAILED;
});
});
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture in project teku by ConsenSys.
the class SyncControllerTest method shouldNotNotifySubscribersWhenRunningSpeculativeTarget.
@Test
void shouldNotNotifySubscribersWhenRunningSpeculativeTarget() {
final SyncSubscriber subscriber = mock(SyncSubscriber.class);
syncController.subscribeToSyncChanges(subscriber);
final SafeFuture<SyncResult> syncResult = new SafeFuture<>();
when(syncTargetSelector.selectSyncTarget(any())).thenReturn(Optional.of(SyncTarget.speculativeTarget(targetChain)));
when(sync.syncToChain(targetChain)).thenReturn(syncResult);
onTargetChainsUpdated();
syncResult.complete(SyncResult.COMPLETE);
verify(subscriberExecutor, never()).execute(any());
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture in project teku by ConsenSys.
the class PeerSyncTest method sync_stopSyncIfPeerSendsBlocksInWrongOrder.
@Test
void sync_stopSyncIfPeerSendsBlocksInWrongOrder() {
final UInt64 startSlot = UInt64.ONE;
UInt64 peerHeadSlot = UInt64.valueOf(1000000);
withPeerHeadSlot(peerHeadSlot);
final SafeFuture<Void> requestFuture = new SafeFuture<>();
when(peer.requestBlocksByRange(any(), any(), any(), any())).thenReturn(requestFuture);
final SafeFuture<PeerSyncResult> syncFuture = peerSync.sync(peer);
assertThat(syncFuture).isNotDone();
verify(peer).requestBlocksByRange(eq(startSlot), eq(MAX_BLOCK_BY_RANGE_REQUEST_SIZE), eq(UInt64.ONE), responseListenerArgumentCaptor.capture());
requestFuture.completeExceptionally(new BlocksByRangeResponseInvalidResponseException(peer, BlocksByRangeResponseInvalidResponseException.InvalidResponseType.BLOCK_SLOT_NOT_GREATER_THAN_PREVIOUS_BLOCK_SLOT));
// Peer returns some blocks but they are not ordered
assertThat(syncFuture).isCompletedWithValue(PeerSyncResult.INVALID_RESPONSE);
verify(peer).disconnectCleanly(any());
}
Aggregations