Changes to API regarding proxy forging calls

This commit is contained in:
catbref 2019-05-27 12:06:46 +01:00
parent f3c588d90f
commit b2f235f7a5
4 changed files with 26 additions and 13 deletions

View File

@ -294,8 +294,8 @@ public class AddressesResource {
@GET
@Path("/proxying")
@Operation(
summary = "List accounts involved in proxy forging, with reward percentage",
description = "Returns list of accounts. At least one of \"proxiedFor\" or \"proxiedBy\" needs to be supplied.",
summary = "List proxy forging relationships",
description = "Returns list of accounts, with reward share percentage and proxy public key.",
responses = {
@ApiResponse(
content = @Content(mediaType = MediaType.APPLICATION_JSON, array = @ArraySchema(schema = @Schema(implementation = ProxyForgerData.class)))
@ -312,9 +312,6 @@ public class AddressesResource {
) @QueryParam("offset") Integer offset, @Parameter(
ref = "reverse"
) @QueryParam("reverse") Boolean reverse) {
if (recipients.isEmpty() && forgers.isEmpty())
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
try (final Repository repository = RepositoryManager.getRepository()) {
return repository.getAccountRepository().findProxyAccounts(recipients, forgers, limit, offset, reverse);
} catch (DataException e) {

View File

@ -569,6 +569,7 @@ public class BlocksResource {
@Path("/forgers")
@Operation(
summary = "Show summary of block forgers",
description = "Returns count of blocks forged, optionally limited to generators/recipients in passed address(es).",
responses = {
@ApiResponse(
content = @Content(
@ -581,7 +582,8 @@ public class BlocksResource {
)
}
)
public List<BlockForgerSummary> getBlockForgers(@Parameter(
public List<BlockForgerSummary> getBlockForgers(@QueryParam("address") List<String> addresses,
@Parameter(
ref = "limit"
) @QueryParam("limit") Integer limit, @Parameter(
ref = "offset"
@ -589,7 +591,11 @@ public class BlocksResource {
ref = "reverse"
) @QueryParam("reverse") Boolean reverse) {
try (final Repository repository = RepositoryManager.getRepository()) {
return repository.getBlockRepository().getBlockForgers(limit, offset, reverse);
for (String address : addresses)
if (!Crypto.isValidAddress(address))
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_ADDRESS);
return repository.getBlockRepository().getBlockForgers(addresses, limit, offset, reverse);
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}

View File

@ -103,9 +103,9 @@ public interface BlockRepository {
public int countForgedBlocks(byte[] publicKey) throws DataException;
/**
* Returns summaries of block forgers.
* Returns summaries of block forgers, optionally limited to passed addresses.
*/
public List<BlockForgerSummary> getBlockForgers(Integer limit, Integer offset, Boolean reverse) throws DataException;
public List<BlockForgerSummary> getBlockForgers(List<String> addresses, Integer limit, Integer offset, Boolean reverse) throws DataException;
/**
* Returns blocks with passed generator public key.

View File

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.qora.api.model.BlockForgerSummary;
@ -168,19 +169,28 @@ public class HSQLDBBlockRepository implements BlockRepository {
}
@Override
public List<BlockForgerSummary> getBlockForgers(Integer limit, Integer offset, Boolean reverse) throws DataException {
public List<BlockForgerSummary> getBlockForgers(List<String> addresses, Integer limit, Integer offset, Boolean reverse) throws DataException {
String subquerySql = "SELECT generator, COUNT(signature) FROM Blocks GROUP BY generator ORDER BY COUNT(signature) ";
if (reverse != null && reverse)
subquerySql += " DESC";
String sql = "SELECT generator, n_blocks, forger, recipient FROM (" + subquerySql + ") AS Forgers (generator, n_blocks) "
String sql = "SELECT DISTINCT generator, n_blocks, forger, recipient FROM (" + subquerySql + ") AS Forgers (generator, n_blocks) "
+ " LEFT OUTER JOIN ProxyForgers ON proxy_public_key = generator ";
if (addresses != null && !addresses.isEmpty()) {
sql += " LEFT OUTER JOIN Accounts AS GeneratorAccounts ON GeneratorAccounts.public_key = generator "
+ " LEFT OUTER JOIN Accounts AS ForgerAccounts ON ForgerAccounts.public_key = forger "
+ " JOIN (VALUES " + String.join(", ", Collections.nCopies(addresses.size(), "(?)")) + ") AS FilterAccounts (account) "
+ " ON FilterAccounts.account IN (recipient, GeneratorAccounts.account, ForgerAccounts.account) ";
} else {
addresses = Collections.emptyList();
}
sql += HSQLDBRepository.limitOffsetSql(limit, offset);
List<BlockForgerSummary> summaries = new ArrayList<>();
try (ResultSet resultSet = this.repository.checkedExecute(sql)) {
try (ResultSet resultSet = this.repository.checkedExecute(sql, addresses.toArray())) {
if (resultSet == null)
return summaries;