Added "txTypes" parameter to GET /transactions/unconfirmed, to allow optional filtering of unconfirmed transactions by one or more types

This commit is contained in:
CalDescent 2022-05-13 11:28:21 +01:00
parent 0a419cb105
commit 659431ebfd
4 changed files with 33 additions and 8 deletions

View File

@ -253,6 +253,8 @@ public class TransactionsResource {
ApiError.REPOSITORY_ISSUE ApiError.REPOSITORY_ISSUE
}) })
public List<TransactionData> getUnconfirmedTransactions(@Parameter( public List<TransactionData> getUnconfirmedTransactions(@Parameter(
description = "A list of transaction types"
) @QueryParam("txType") List<TransactionType> txTypes, @Parameter(
description = "Transaction creator's base58 encoded public key" description = "Transaction creator's base58 encoded public key"
) @QueryParam("creator") String creatorPublicKey58, @Parameter( ) @QueryParam("creator") String creatorPublicKey58, @Parameter(
ref = "limit" ref = "limit"
@ -273,7 +275,7 @@ public class TransactionsResource {
} }
try (final Repository repository = RepositoryManager.getRepository()) { 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) { } catch (ApiException e) {
throw e; throw e;
} catch (DataException e) { } catch (DataException e) {

View File

@ -257,7 +257,8 @@ public interface TransactionRepository {
* @return list of transactions, or empty if none. * @return list of transactions, or empty if none.
* @throws DataException * @throws DataException
*/ */
public List<TransactionData> getUnconfirmedTransactions(byte[] creatorPublicKey, Integer limit, Integer offset, Boolean reverse) throws DataException; public List<TransactionData> getUnconfirmedTransactions(List<TransactionType> txTypes, byte[] creatorPublicKey,
Integer limit, Integer offset, Boolean reverse) throws DataException;
/** /**
* Returns list of unconfirmed transactions in timestamp-else-signature order. * Returns list of unconfirmed transactions in timestamp-else-signature order.
@ -266,7 +267,7 @@ public interface TransactionRepository {
* @throws DataException * @throws DataException
*/ */
public default List<TransactionData> getUnconfirmedTransactions() throws DataException { public default List<TransactionData> getUnconfirmedTransactions() throws DataException {
return getUnconfirmedTransactions(null, null, null, null); return getUnconfirmedTransactions(null, null, null, null, null);
} }
/** /**

View File

@ -1213,21 +1213,43 @@ public class HSQLDBTransactionRepository implements TransactionRepository {
} }
@Override @Override
public List<TransactionData> getUnconfirmedTransactions(byte[] creatorPublicKey, Integer limit, Integer offset, Boolean reverse) throws DataException { public List<TransactionData> getUnconfirmedTransactions(List<TransactionType> txTypes, byte[] creatorPublicKey,
Integer limit, Integer offset, Boolean reverse) throws DataException {
List<String> whereClauses = new ArrayList<>(); List<String> whereClauses = new ArrayList<>();
List<Object> bindParams = new ArrayList<>(); List<Object> bindParams = new ArrayList<>();
boolean hasCreatorPublicKey = creatorPublicKey != null;
boolean hasTxTypes = txTypes != null && !txTypes.isEmpty();
if (creatorPublicKey != null) { if (creatorPublicKey != null) {
whereClauses.add("creator = ?"); whereClauses.add("Transactions.creator = ?");
bindParams.add(creatorPublicKey); bindParams.add(creatorPublicKey);
} }
StringBuilder sql = new StringBuilder(256); StringBuilder sql = new StringBuilder(256);
sql.append("SELECT signature FROM UnconfirmedTransactions"); sql.append("SELECT signature FROM UnconfirmedTransactions");
if (creatorPublicKey != null) { if (hasCreatorPublicKey || hasTxTypes) {
sql.append(" JOIN Transactions USING (signature) "); 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()) { if (!whereClauses.isEmpty()) {
sql.append(" WHERE "); sql.append(" WHERE ");

View File

@ -36,8 +36,8 @@ public class TransactionsApiTests extends ApiCommon {
@Test @Test
public void testGetUnconfirmedTransactions() { public void testGetUnconfirmedTransactions() {
assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, null, null, null)); assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, null, null, null, null));
assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, 1, 1, true)); assertNotNull(this.transactionsResource.getUnconfirmedTransactions(null, null, 1, 1, true));
} }
@Test @Test