From 659431ebfd430438fe40caf38f4e296b6bae2a1b Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 13 May 2022 11:28:21 +0100 Subject: [PATCH] Added "txTypes" parameter to GET /transactions/unconfirmed, to allow optional filtering of unconfirmed transactions by one or more types --- .../api/resource/TransactionsResource.java | 4 ++- .../repository/TransactionRepository.java | 5 ++-- .../HSQLDBTransactionRepository.java | 28 +++++++++++++++++-- .../qortal/test/api/TransactionsApiTests.java | 4 +-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/qortal/api/resource/TransactionsResource.java b/src/main/java/org/qortal/api/resource/TransactionsResource.java index a35a5cf5..4c440304 100644 --- a/src/main/java/org/qortal/api/resource/TransactionsResource.java +++ b/src/main/java/org/qortal/api/resource/TransactionsResource.java @@ -253,6 +253,8 @@ public class TransactionsResource { ApiError.REPOSITORY_ISSUE }) public List getUnconfirmedTransactions(@Parameter( + description = "A list of transaction types" + ) @QueryParam("txType") List txTypes, @Parameter( description = "Transaction creator's base58 encoded public key" ) @QueryParam("creator") String creatorPublicKey58, @Parameter( ref = "limit" @@ -273,7 +275,7 @@ public class TransactionsResource { } try (final Repository repository = RepositoryManager.getRepository()) { - return repository.getTransactionRepository().getUnconfirmedTransactions(creatorPublicKey, limit, offset, reverse); + return repository.getTransactionRepository().getUnconfirmedTransactions(txTypes, creatorPublicKey, limit, offset, reverse); } catch (ApiException e) { throw e; } catch (DataException e) { diff --git a/src/main/java/org/qortal/repository/TransactionRepository.java b/src/main/java/org/qortal/repository/TransactionRepository.java index 645ca32c..4fb9bb12 100644 --- a/src/main/java/org/qortal/repository/TransactionRepository.java +++ b/src/main/java/org/qortal/repository/TransactionRepository.java @@ -257,7 +257,8 @@ public interface TransactionRepository { * @return list of transactions, or empty if none. * @throws DataException */ - public List getUnconfirmedTransactions(byte[] creatorPublicKey, Integer limit, Integer offset, Boolean reverse) throws DataException; + public List getUnconfirmedTransactions(List txTypes, byte[] creatorPublicKey, + Integer limit, Integer offset, Boolean reverse) throws DataException; /** * Returns list of unconfirmed transactions in timestamp-else-signature order. @@ -266,7 +267,7 @@ public interface TransactionRepository { * @throws DataException */ public default List getUnconfirmedTransactions() throws DataException { - return getUnconfirmedTransactions(null, null, null, null); + return getUnconfirmedTransactions(null, null, null, null, null); } /** diff --git a/src/main/java/org/qortal/repository/hsqldb/transaction/HSQLDBTransactionRepository.java b/src/main/java/org/qortal/repository/hsqldb/transaction/HSQLDBTransactionRepository.java index 6ba4154a..24174abb 100644 --- a/src/main/java/org/qortal/repository/hsqldb/transaction/HSQLDBTransactionRepository.java +++ b/src/main/java/org/qortal/repository/hsqldb/transaction/HSQLDBTransactionRepository.java @@ -1213,21 +1213,43 @@ public class HSQLDBTransactionRepository implements TransactionRepository { } @Override - public List getUnconfirmedTransactions(byte[] creatorPublicKey, Integer limit, Integer offset, Boolean reverse) throws DataException { + public List getUnconfirmedTransactions(List txTypes, byte[] creatorPublicKey, + Integer limit, Integer offset, Boolean reverse) throws DataException { List whereClauses = new ArrayList<>(); List bindParams = new ArrayList<>(); + boolean hasCreatorPublicKey = creatorPublicKey != null; + boolean hasTxTypes = txTypes != null && !txTypes.isEmpty(); + if (creatorPublicKey != null) { - whereClauses.add("creator = ?"); + whereClauses.add("Transactions.creator = ?"); bindParams.add(creatorPublicKey); } StringBuilder sql = new StringBuilder(256); sql.append("SELECT signature FROM UnconfirmedTransactions"); - if (creatorPublicKey != null) { + if (hasCreatorPublicKey || hasTxTypes) { sql.append(" JOIN Transactions USING (signature) "); } + if (hasTxTypes) { + StringBuilder txTypesIn = new StringBuilder(256); + txTypesIn.append("Transactions.type IN ("); + + // ints are safe enough to use literally + final int txTypesSize = txTypes.size(); + for (int tti = 0; tti < txTypesSize; ++tti) { + if (tti != 0) + txTypesIn.append(", "); + + txTypesIn.append(txTypes.get(tti).value); + } + + txTypesIn.append(")"); + + whereClauses.add(txTypesIn.toString()); + } + if (!whereClauses.isEmpty()) { sql.append(" WHERE "); diff --git a/src/test/java/org/qortal/test/api/TransactionsApiTests.java b/src/test/java/org/qortal/test/api/TransactionsApiTests.java index bceb94ac..102cac34 100644 --- a/src/test/java/org/qortal/test/api/TransactionsApiTests.java +++ b/src/test/java/org/qortal/test/api/TransactionsApiTests.java @@ -36,8 +36,8 @@ public class TransactionsApiTests extends ApiCommon { @Test public void testGetUnconfirmedTransactions() { - assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, null, null, null)); - assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, 1, 1, true)); + assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, null, null, null, null)); + assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, null, 1, 1, true)); } @Test