Skip to content

Commit

Permalink
wip: getAccountTransactions considering sender and recipient now, #753,
Browse files Browse the repository at this point in the history
  • Loading branch information
ohager committed Dec 29, 2023
1 parent 006839a commit a48e31f
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 102 deletions.
5 changes: 3 additions & 2 deletions src/brs/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public interface Blockchain {

long getBlockReward(int height);

Collection<Transaction> getTransactions(Account account, byte type, byte subtype, int blockImplTimestamp, boolean includeIndirectIncoming);
Collection<Transaction> getTransactions(Account account, byte type, byte subtype, int blockTimestamp, boolean includeIndirectIncoming);

CollectionWithIndex<Transaction> getTransactions(Account account, int numberOfConfirmations, byte type, byte subtype, int blockImplTimestamp, int from, int to, boolean includeIndirectIncoming);
CollectionWithIndex<Transaction> getTransactions(Account account, int numberOfConfirmations, byte type, byte subtype, int blockTimestamp, int from, int to, boolean includeIndirectIncoming);
CollectionWithIndex<Transaction> getTransactions(Long senderId, Long recipientId, int numberOfConfirmations, byte type, byte subtype, int blockTimestamp, int from, int to, boolean includeIndirectIncoming, boolean bidirectional);

Collection<Transaction> getTransactions(long senderId, byte type, byte subtypeStart, byte subtypeEnd, int from, int to);

Expand Down
9 changes: 7 additions & 2 deletions src/brs/BlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,19 @@ public Collection<Transaction> getTransactions(Account account, byte type, byte
@Override
public CollectionWithIndex<Transaction> getTransactions(Account account, int numberOfConfirmations, byte type, byte subtype,
int blockTimestamp, int from, int to, boolean includeIndirectIncoming) {
return new CollectionWithIndex<Transaction>(blockchainStore.getTransactions(account, numberOfConfirmations, type, subtype, blockTimestamp, from, to, includeIndirectIncoming), from, to);
return new CollectionWithIndex<>(blockchainStore.getTransactions(account, numberOfConfirmations, type, subtype, blockTimestamp, from, to, includeIndirectIncoming), from, to);
}

@Override
public CollectionWithIndex<Transaction> getTransactions(Long senderId, Long recipientId, int numberOfConfirmations, byte type, byte subtype, int blockTimestamp, int from, int to, boolean includeIndirectIncoming, boolean bidirectional) {
return new CollectionWithIndex<>(blockchainStore.getTransactions(senderId, recipientId, numberOfConfirmations, type, subtype, blockTimestamp, from, to, includeIndirectIncoming, bidirectional), from, to);
}

@Override
public Collection<Transaction> getTransactions(long senderId, byte type, byte subtypeStart, byte subtypeEnd, int from, int to) {
return blockchainStore.getTransactions(senderId, type, subtypeStart, subtypeEnd, from, to);
}

@Override
public int countTransactions(byte type, byte subtypeStart, byte subtypeEnd) {
return blockchainStore.countTransactions(type, subtypeStart, subtypeEnd);
Expand Down
213 changes: 139 additions & 74 deletions src/brs/db/sql/SqlBlockchainStore.java

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/brs/db/store/BlockchainStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface BlockchainStore {
Collection<Transaction> getTransactions(Account account, int numberOfConfirmations, byte type, byte subtype,
int blockTimestamp, int from, int to, boolean includeIndirectIncoming);

Collection<Transaction> getTransactions(Long senderId, Long recipientId, int numberOfConfirmations, byte type, byte subtype,
int blockTimestamp, int from, int to, boolean includeIndirectIncoming, boolean bidirectional);
Collection<Transaction> getTransactions(long senderId, byte type, byte subtypeStart, byte subtypeEnd, int from, int to);

int countTransactions(byte type, byte subtypeStart, byte subtypeEnd);
Expand Down
91 changes: 75 additions & 16 deletions src/brs/http/GetAccountTransactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import brs.services.ParameterService;
import brs.util.CollectionWithIndex;

import brs.util.Convert;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import signumj.entity.SignumAddress;

import javax.servlet.http.HttpServletRequest;

import static brs.http.JSONResponses.MISSING_ACCOUNT;
import static brs.http.common.Parameters.*;
import static brs.http.common.ResultFields.NEXT_INDEX_RESPONSE;
import static brs.http.common.ResultFields.TRANSACTIONS_RESPONSE;

import java.util.ArrayList;
import java.util.Collection;

final class GetAccountTransactions extends APIServlet.JsonRequestHandler {
Expand All @@ -25,53 +29,108 @@ final class GetAccountTransactions extends APIServlet.JsonRequestHandler {
private final Blockchain blockchain;

GetAccountTransactions(ParameterService parameterService, Blockchain blockchain) {
super(new APITag[] {APITag.ACCOUNTS}, ACCOUNT_PARAMETER, TIMESTAMP_PARAMETER, TYPE_PARAMETER, SUBTYPE_PARAMETER, FIRST_INDEX_PARAMETER, LAST_INDEX_PARAMETER, NUMBER_OF_CONFIRMATIONS_PARAMETER, INCLUDE_INDIRECT_PARAMETER);
super(new APITag[]{APITag.ACCOUNTS},
ACCOUNT_PARAMETER,
SENDER_PARAMETER,
RECIPIENT_PARAMETER,
TIMESTAMP_PARAMETER,
TYPE_PARAMETER,
SUBTYPE_PARAMETER,
FIRST_INDEX_PARAMETER,
LAST_INDEX_PARAMETER,
NUMBER_OF_CONFIRMATIONS_PARAMETER,
INCLUDE_INDIRECT_PARAMETER,
BIDIRECTIONAL_PARAMETER
);
this.parameterService = parameterService;
this.blockchain = blockchain;
}

private static Long getAccountIdParameter(String idParameter) {
String accountId = Convert.emptyToNull(idParameter);
if (accountId == null) {
return null;
}
SignumAddress accountAddress = Convert.parseAddress(accountId);
if (accountAddress == null) {
return null;
}
return accountAddress.getSignedLongId();
}

@Override
protected
JsonElement processRequest(HttpServletRequest req) throws BurstException {
Account account = parameterService.getAccount(req);
int timestamp = ParameterParser.getTimestamp(req);
int numberOfConfirmations = parameterService.getNumberOfConfirmations(req);
protected JsonElement processRequest(HttpServletRequest req) throws BurstException {
Account account = parameterService.getAccount(req, false);

Long senderId = null, recipientId = null;
if (account == null) {
recipientId = getAccountIdParameter(req.getParameter(RECIPIENT_PARAMETER));
senderId = getAccountIdParameter(req.getParameter(SENDER_PARAMETER));
if (senderId == null && recipientId == null) {
// account is pre-dominantly required if sender and recipient are not provided
throw new ParameterException(MISSING_ACCOUNT);
}
}

byte type;
byte subtype;
try {
type = Byte.parseByte(req.getParameter(TYPE_PARAMETER));
}
catch (NumberFormatException e) {
} catch (NumberFormatException e) {
type = -1;
}
try {
subtype = Byte.parseByte(req.getParameter(SUBTYPE_PARAMETER));
}
catch (NumberFormatException e) {
} catch (NumberFormatException e) {
subtype = -1;
}

int firstIndex = ParameterParser.getFirstIndex(req);
int lastIndex = ParameterParser.getLastIndex(req);
int lastIndex = ParameterParser.getLastIndex(req);

if(lastIndex < firstIndex) {
if (lastIndex < firstIndex) {
throw new IllegalArgumentException("lastIndex must be greater or equal to firstIndex");
}

JsonArray transactions = new JsonArray();
CollectionWithIndex<Transaction> accountTransactions = blockchain.getTransactions(account, numberOfConfirmations, type, subtype, timestamp, firstIndex, lastIndex, parameterService.getIncludeIndirect(req));
CollectionWithIndex<Transaction> accountTransactions;
int timestamp = ParameterParser.getTimestamp(req);
int numberOfConfirmations = parameterService.getNumberOfConfirmations(req);
boolean includeIndirect = parameterService.getIncludeIndirect(req);
accountTransactions = account != null
? blockchain.getTransactions(
account,
numberOfConfirmations,
type,
subtype,
timestamp,
firstIndex,
lastIndex,
includeIndirect)
: blockchain.getTransactions(
senderId,
recipientId,
numberOfConfirmations,
type,
subtype,
timestamp,
firstIndex,
lastIndex,
includeIndirect,
parameterService.getBidirectional(req));


for (Transaction transaction : accountTransactions) {
transactions.add(JSONData.transaction(transaction, blockchain.getHeight()));
}

JsonObject response = new JsonObject();
response.add(TRANSACTIONS_RESPONSE, transactions);
if(accountTransactions.hasNextIndex()) {

if (accountTransactions.hasNextIndex()) {
response.addProperty(NEXT_INDEX_RESPONSE, accountTransactions.nextIndex());
}

return response;
}
}
2 changes: 2 additions & 0 deletions src/brs/http/common/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private Parameters() {
public static final String PAYLOAD_PARAMETER = "payload";
public static final String API_KEY_PARAMETER = "apiKey";

public static final String BIDIRECTIONAL_PARAMETER = "bidirectional";

public static boolean isFalse(String text) {
return "false".equalsIgnoreCase(text);
}
Expand Down
4 changes: 3 additions & 1 deletion src/brs/services/ParameterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public interface ParameterService {
AT getAT(HttpServletRequest req) throws ParameterException;

boolean getIncludeIndirect(HttpServletRequest req);

boolean getEstimateCommitment(HttpServletRequest req);

boolean getAmountCommitted(HttpServletRequest req);

boolean getBidirectional(HttpServletRequest req);
}
19 changes: 12 additions & 7 deletions src/brs/services/impl/ParameterServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ParameterServiceImpl(AccountService accountService, AliasService aliasSer
public Account getAccount(HttpServletRequest req) throws BurstException {
return getAccount(req, true);
}

@Override
public Account getAccount(HttpServletRequest req, boolean checkPresent) throws BurstException {
String accountId = Convert.emptyToNull(req.getParameter(ACCOUNT_PARAMETER));
Expand All @@ -77,12 +77,12 @@ public Account getAccount(HttpServletRequest req, boolean checkPresent) throws B
throw new ParameterException(INCORRECT_HEIGHT);
}
}

try {
SignumAddress accountAddress = Convert.parseAddress(accountId);
Account account = height >= 0 ? accountService.getAccount(accountAddress.getSignedLongId(), height)
: accountService.getAccount(accountAddress.getSignedLongId());

if(account == null && accountAddress.getPublicKey() == null) {
throw new ParameterException(UNKNOWN_ACCOUNT);
}
Expand All @@ -93,11 +93,11 @@ public Account getAccount(HttpServletRequest req, boolean checkPresent) throws B
if(account.getPublicKey() == null && accountAddress.getPublicKey() != null) {
account.setPublicKey(accountAddress.getPublicKey());
}

if(accountAddress.getPublicKey() != null && account.getPublicKey() != null && !Arrays.equals(account.getPublicKey(), accountAddress.getPublicKey())) {
throw new ParameterException(INCORRECT_ACCOUNT);
}

return account;
} catch (RuntimeException e) {
throw new ParameterException(INCORRECT_ACCOUNT);
Expand Down Expand Up @@ -407,12 +407,17 @@ public AT getAT(HttpServletRequest req) throws ParameterException {
public boolean getIncludeIndirect(HttpServletRequest req) {
return Boolean.parseBoolean(req.getParameter(INCLUDE_INDIRECT_PARAMETER));
}


@Override
public boolean getBidirectional(HttpServletRequest req) {
return Boolean.parseBoolean(req.getParameter(BIDIRECTIONAL_PARAMETER));
}

@Override
public boolean getAmountCommitted(HttpServletRequest req) {
return Boolean.parseBoolean(req.getParameter(GET_COMMITTED_AMOUNT_PARAMETER));
}

@Override
public boolean getEstimateCommitment(HttpServletRequest req) {
return Boolean.parseBoolean(req.getParameter(ESTIMATE_COMMITMENT_PARAMETER));
Expand Down

0 comments on commit a48e31f

Please sign in to comment.