|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; |
| 21 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
| 22 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; |
| 23 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPoolStatusResult; |
| 24 | +import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions; |
| 25 | +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | + |
| 33 | +@ExtendWith(MockitoExtension.class) |
| 34 | +public class TxPoolStatusTest { |
| 35 | + |
| 36 | + @Mock private TransactionPool transactionPool; |
| 37 | + private TxPoolStatus method; |
| 38 | + private static final String JSON_RPC_VERSION = "2.0"; |
| 39 | + private static final String TXPOOL_STATUS_METHOD = "txpool_status"; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public void setUp() { |
| 43 | + method = new TxPoolStatus(transactionPool); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void returnsCorrectMethodName() { |
| 48 | + assertThat(method.getName()).isEqualTo(TXPOOL_STATUS_METHOD); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void shouldReturnZeroCountsWhenPoolIsEmpty() { |
| 53 | + when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(0, 0)); |
| 54 | + |
| 55 | + final JsonRpcRequestContext request = buildRequest(); |
| 56 | + final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); |
| 57 | + final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); |
| 58 | + |
| 59 | + assertThat(result.getPending()).isEqualTo("0x0"); |
| 60 | + assertThat(result.getQueued()).isEqualTo("0x0"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldReturnCorrectCountsWithPendingAndQueuedTransactions() { |
| 65 | + when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(10, 7)); |
| 66 | + |
| 67 | + final JsonRpcRequestContext request = buildRequest(); |
| 68 | + final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); |
| 69 | + final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); |
| 70 | + |
| 71 | + assertThat(result.getPending()).isEqualTo("0xa"); |
| 72 | + assertThat(result.getQueued()).isEqualTo("0x7"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldReturnCorrectCountsWithOnlyPendingTransactions() { |
| 77 | + when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(5, 0)); |
| 78 | + |
| 79 | + final JsonRpcRequestContext request = buildRequest(); |
| 80 | + final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); |
| 81 | + final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); |
| 82 | + |
| 83 | + assertThat(result.getPending()).isEqualTo("0x5"); |
| 84 | + assertThat(result.getQueued()).isEqualTo("0x0"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void shouldReturnCorrectCountsWithOnlyQueuedTransactions() { |
| 89 | + when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(0, 3)); |
| 90 | + |
| 91 | + final JsonRpcRequestContext request = buildRequest(); |
| 92 | + final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); |
| 93 | + final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); |
| 94 | + |
| 95 | + assertThat(result.getPending()).isEqualTo("0x0"); |
| 96 | + assertThat(result.getQueued()).isEqualTo("0x3"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void shouldReturnHexEncodedLargeValues() { |
| 101 | + when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(256, 4096)); |
| 102 | + |
| 103 | + final JsonRpcRequestContext request = buildRequest(); |
| 104 | + final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); |
| 105 | + final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); |
| 106 | + |
| 107 | + assertThat(result.getPending()).isEqualTo("0x100"); |
| 108 | + assertThat(result.getQueued()).isEqualTo("0x1000"); |
| 109 | + } |
| 110 | + |
| 111 | + private JsonRpcRequestContext buildRequest() { |
| 112 | + return new JsonRpcRequestContext( |
| 113 | + new JsonRpcRequest(JSON_RPC_VERSION, TXPOOL_STATUS_METHOD, new Object[] {})); |
| 114 | + } |
| 115 | +} |
0 commit comments