Search in sources :

Example 1 with EthScheduler

use of org.hyperledger.besu.ethereum.eth.manager.EthScheduler in project besu by hyperledger.

the class PendingTransactionsSenderTest method testSendEth65PeersOnly.

@Test
public void testSendEth65PeersOnly() {
    PeerPendingTransactionTracker peerPendingTransactionTracker = mock(PeerPendingTransactionTracker.class);
    PendingTransactionsMessageSender pendingTransactionsMessageSender = mock(PendingTransactionsMessageSender.class);
    EthContext ethContext = mock(EthContext.class);
    EthScheduler ethScheduler = mock(EthScheduler.class);
    when(ethContext.getScheduler()).thenReturn(ethScheduler);
    PendingTransactionSender sender = new PendingTransactionSender(peerPendingTransactionTracker, pendingTransactionsMessageSender, ethContext);
    EthPeer peer1 = mock(EthPeer.class);
    EthPeer peer2 = mock(EthPeer.class);
    Transaction tx = mock(Transaction.class);
    Hash hash = Hash.wrap(Bytes32.random());
    when(tx.getHash()).thenReturn(hash);
    EthPeers ethPeers = mock(EthPeers.class);
    when(ethContext.getEthPeers()).thenReturn(ethPeers);
    when(ethPeers.streamAvailablePeers()).thenReturn(Arrays.asList(peer1, peer2).stream());
    when(peerPendingTransactionTracker.isPeerSupported(peer1, EthProtocol.ETH65)).thenReturn(true);
    when(peerPendingTransactionTracker.isPeerSupported(peer2, EthProtocol.ETH65)).thenReturn(false);
    sender.onTransactionsAdded(Collections.singleton(tx));
    verify(peerPendingTransactionTracker, times(1)).addToPeerSendQueue(peer1, hash);
    verify(peerPendingTransactionTracker, never()).addToPeerSendQueue(peer2, hash);
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) Transaction(org.hyperledger.besu.ethereum.core.Transaction) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) Hash(org.hyperledger.besu.datatypes.Hash) Test(org.junit.Test)

Example 2 with EthScheduler

use of org.hyperledger.besu.ethereum.eth.manager.EthScheduler in project besu by hyperledger.

the class WebSocketMessageHandler method handle.

public void handle(final ServerWebSocket websocket, final Buffer buffer, final Optional<User> user) {
    if (buffer.length() == 0) {
        replyToClient(websocket, errorResponse(null, JsonRpcError.INVALID_REQUEST));
    } else {
        try {
            final JsonObject jsonRpcRequest = buffer.toJsonObject();
            vertx.<JsonRpcResponse>executeBlocking(promise -> {
                try {
                    final JsonRpcResponse jsonRpcResponse = jsonRpcExecutor.execute(user, null, null, new IsAliveHandler(ethScheduler, timeoutSec), jsonRpcRequest, req -> {
                        final WebSocketRpcRequest websocketRequest = req.mapTo(WebSocketRpcRequest.class);
                        websocketRequest.setConnectionId(websocket.textHandlerID());
                        return websocketRequest;
                    });
                    promise.complete(jsonRpcResponse);
                } catch (RuntimeException e) {
                    promise.fail(e);
                }
            }).onSuccess(jsonRpcResponse -> replyToClient(websocket, jsonRpcResponse)).onFailure(throwable -> {
                try {
                    final Integer id = jsonRpcRequest.getInteger("id", null);
                    replyToClient(websocket, errorResponse(id, JsonRpcError.INTERNAL_ERROR));
                } catch (ClassCastException idNotIntegerException) {
                    replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR));
                }
            });
        } catch (DecodeException jsonObjectDecodeException) {
            try {
                final JsonArray batchJsonRpcRequest = buffer.toJsonArray();
                vertx.<List<JsonRpcResponse>>executeBlocking(promise -> {
                    List<JsonRpcResponse> responses = new ArrayList<>();
                    for (int i = 0; i < batchJsonRpcRequest.size(); i++) {
                        final JsonObject jsonRequest;
                        try {
                            jsonRequest = batchJsonRpcRequest.getJsonObject(i);
                        } catch (ClassCastException e) {
                            responses.add(new JsonRpcErrorResponse(null, INVALID_REQUEST));
                            continue;
                        }
                        responses.add(jsonRpcExecutor.execute(user, null, null, new IsAliveHandler(ethScheduler, timeoutSec), jsonRequest, req -> {
                            final WebSocketRpcRequest websocketRequest = req.mapTo(WebSocketRpcRequest.class);
                            websocketRequest.setConnectionId(websocket.textHandlerID());
                            return websocketRequest;
                        }));
                    }
                    promise.complete(responses);
                }).onSuccess(jsonRpcBatchResponse -> {
                    final JsonRpcResponse[] completed = jsonRpcBatchResponse.stream().filter(jsonRpcResponse -> jsonRpcResponse.getType() != JsonRpcResponseType.NONE).toArray(JsonRpcResponse[]::new);
                    replyToClient(websocket, completed);
                }).onFailure(throwable -> replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR)));
            } catch (RuntimeException jsonArrayDecodeException) {
                replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR));
            }
        }
    }
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ServerWebSocket(io.vertx.core.http.ServerWebSocket) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) JsonRpcResponseType(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType) ArrayList(java.util.ArrayList) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) JsonObject(io.vertx.core.json.JsonObject) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) Logger(org.slf4j.Logger) INVALID_REQUEST(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_REQUEST) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Feature(com.fasterxml.jackson.core.JsonGenerator.Feature) User(io.vertx.ext.auth.User) Buffer(io.vertx.core.buffer.Buffer) Optional(java.util.Optional) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor) JsonObject(io.vertx.core.json.JsonObject) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) DecodeException(io.vertx.core.json.DecodeException) JsonArray(io.vertx.core.json.JsonArray) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) ArrayList(java.util.ArrayList) List(java.util.List) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 3 with EthScheduler

use of org.hyperledger.besu.ethereum.eth.manager.EthScheduler in project besu by hyperledger.

the class Istanbul99ProtocolManagerTest method respondToEth65GetHeadersUsingIstanbul99.

@Test
public void respondToEth65GetHeadersUsingIstanbul99() throws ExecutionException, InterruptedException, TimeoutException {
    final CompletableFuture<Void> done = new CompletableFuture<>();
    final EthScheduler ethScheduler = new DeterministicEthScheduler(() -> false);
    EthPeers peers = new EthPeers(Istanbul99Protocol.NAME, TestClock.fixed(), new NoOpMetricsSystem(), 25);
    EthMessages messages = new EthMessages();
    final BigInteger networkId = BigInteger.ONE;
    try (final EthProtocolManager ethManager = new Istanbul99ProtocolManager(blockchain, networkId, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig(), peers, messages, new EthContext(peers, messages, ethScheduler), Collections.emptyList(), false, ethScheduler)) {
        final long startBlock = blockchain.getChainHeadBlockNumber() + 1;
        final int blockCount = 5;
        final MessageData messageData = GetBlockHeadersMessage.create(startBlock, blockCount, 0, false);
        final PeerSendHandler onSend = (cap, message, conn) -> {
            if (message.getCode() == EthPV62.STATUS) {
                // Ignore status message
                return;
            }
            assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_HEADERS);
            final BlockHeadersMessage headersMsg = BlockHeadersMessage.readFrom(message);
            final List<BlockHeader> headers = Lists.newArrayList(headersMsg.getHeaders(protocolSchedule));
            assertThat(headers.size()).isEqualTo(0);
            done.complete(null);
        };
        final PeerConnection peer = setupPeer(ethManager, onSend);
        ethManager.processMessage(Istanbul99Protocol.ISTANBUL99, new DefaultMessage(peer, messageData));
        done.get(10, TimeUnit.SECONDS);
    }
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection) StatusMessage(org.hyperledger.besu.ethereum.eth.messages.StatusMessage) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) EthProtocol(org.hyperledger.besu.ethereum.eth.EthProtocol) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) HashSet(java.util.HashSet) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) Lists(com.google.common.collect.Lists) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) BigInteger(java.math.BigInteger) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) Set(java.util.Set) Test(org.junit.Test) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) List(java.util.List) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) MockPeerConnection(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) CompletableFuture(java.util.concurrent.CompletableFuture) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) BigInteger(java.math.BigInteger) List(java.util.List) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) Test(org.junit.Test)

Example 4 with EthScheduler

use of org.hyperledger.besu.ethereum.eth.manager.EthScheduler in project besu by hyperledger.

the class TransactionPoolTest method setUp.

@Before
public void setUp() {
    blockchain = executionContext.getBlockchain();
    transactions = new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, MAX_TRANSACTIONS, TestClock.fixed(), metricsSystem, blockchain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
    when(protocolSpec.getTransactionValidator()).thenReturn(transactionValidator);
    when(protocolSpec.getFeeMarket()).thenReturn(FeeMarket.legacy());
    genesisBlockGasLimit = executionContext.getGenesis().getHeader().getGasLimit();
    ethContext = mock(EthContext.class);
    final EthScheduler ethScheduler = mock(EthScheduler.class);
    syncTaskCapture = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(ethScheduler).scheduleSyncWorkerTask(syncTaskCapture.capture());
    when(ethContext.getScheduler()).thenReturn(ethScheduler);
    ethPeers = mock(EthPeers.class);
    when(ethContext.getEthPeers()).thenReturn(ethPeers);
    peerTransactionTracker = new PeerTransactionTracker();
    transactionBroadcaster = spy(new TransactionBroadcaster(ethContext, transactions, peerTransactionTracker, transactionsMessageSender, newPooledTransactionHashesMessageSender));
    transactionPool = createTransactionPool();
    blockchain.observeBlockAdded(transactionPool);
    when(miningParameters.getMinTransactionGasPrice()).thenReturn(Wei.of(2));
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Before(org.junit.Before)

Example 5 with EthScheduler

use of org.hyperledger.besu.ethereum.eth.manager.EthScheduler in project besu by hyperledger.

the class FastWorldStateDownloaderTest method stalledDownloader.

@Test
public void stalledDownloader() {
    final EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create(new EthScheduler(1, 1, 1, 1, new NoOpMetricsSystem()));
    // Setup "remote" state
    final WorldStateStorage remoteStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStateArchive remoteWorldStateArchive = new DefaultWorldStateArchive(remoteStorage, createPreimageStorage());
    final MutableWorldState remoteWorldState = remoteWorldStateArchive.getMutable();
    // Generate accounts and save corresponding state root
    dataGen.createRandomAccounts(remoteWorldState, 10);
    final Hash stateRoot = remoteWorldState.rootHash();
    // Sanity check
    assertThat(stateRoot).isNotEqualTo(EMPTY_TRIE_ROOT);
    final BlockHeader header = dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
    final InMemoryTasksPriorityQueues<NodeDataRequest> taskCollection = new InMemoryTasksPriorityQueues<>();
    final WorldStateStorage localStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final SynchronizerConfiguration syncConfig = SynchronizerConfiguration.builder().worldStateMaxRequestsWithoutProgress(10).build();
    final WorldStateDownloader downloader = createDownloader(syncConfig, ethProtocolManager.ethContext(), localStorage, taskCollection);
    // Create a peer that can respond
    final RespondingEthPeer peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, header.getNumber());
    // Start downloader (with a state root that's not available anywhere
    final CompletableFuture<Void> result = downloader.run(null, new FastSyncState(new BlockHeaderTestFixture().stateRoot(Hash.hash(Bytes.of(1, 2, 3, 4))).buildHeader()));
    // A second run should return an error without impacting the first result
    final CompletableFuture<?> secondResult = downloader.run(null, new FastSyncState(header));
    assertThat(secondResult).isCompletedExceptionally();
    assertThat(result).isNotCompletedExceptionally();
    final RespondingEthPeer.Responder emptyResponder = RespondingEthPeer.emptyResponder();
    peer.respondWhileOtherThreadsWork(emptyResponder, () -> !result.isDone());
    assertThat(result).isCompletedExceptionally();
    assertThatThrownBy(result::get).hasCauseInstanceOf(StalledDownloadException.class);
    // Finally, check that when we restart the download with state that is available it works
    final CompletableFuture<Void> retryResult = downloader.run(null, new FastSyncState(header));
    final RespondingEthPeer.Responder responder = RespondingEthPeer.blockchainResponder(mock(Blockchain.class), remoteWorldStateArchive);
    peer.respondWhileOtherThreadsWork(responder, () -> !retryResult.isDone());
    assertThat(retryResult).isCompleted();
}
Also used : WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) InMemoryTasksPriorityQueues(org.hyperledger.besu.services.tasks.InMemoryTasksPriorityQueues) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader) FastSyncState(org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncState) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration) Test(org.junit.Test)

Aggregations

EthScheduler (org.hyperledger.besu.ethereum.eth.manager.EthScheduler)24 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)14 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)11 SyncState (org.hyperledger.besu.ethereum.eth.sync.state.SyncState)10 Before (org.junit.Before)10 EthPeers (org.hyperledger.besu.ethereum.eth.manager.EthPeers)9 EthMessages (org.hyperledger.besu.ethereum.eth.manager.EthMessages)7 Test (org.junit.Test)7 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)5 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)4 BlockchainSetupUtil (org.hyperledger.besu.ethereum.core.BlockchainSetupUtil)4 EthProtocolManager (org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager)4 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)4 DefaultWorldStateArchive (org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive)4 Hash (org.hyperledger.besu.datatypes.Hash)3 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)3 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)3 DeterministicEthScheduler (org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler)3 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)3 List (java.util.List)2