From a530b64ae74b963f21be49610aa08414532f8817 Mon Sep 17 00:00:00 2001 From: kennycud Date: Mon, 23 Sep 2024 16:24:24 -0700 Subject: [PATCH] added transfer privs inquiries to sponsorship analytics --- .../data/account/SponsorshipReport.java | 18 ++- .../hsqldb/HSQLDBAccountRepository.java | 103 +++++++++--------- 2 files changed, 67 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/qortal/data/account/SponsorshipReport.java b/src/main/java/org/qortal/data/account/SponsorshipReport.java index 47470b6a..b08051ef 100644 --- a/src/main/java/org/qortal/data/account/SponsorshipReport.java +++ b/src/main/java/org/qortal/data/account/SponsorshipReport.java @@ -18,6 +18,8 @@ public class SponsorshipReport { private int penalties; + private boolean transfer; + private String[] names; private int sponseeCount; @@ -30,6 +32,8 @@ public class SponsorshipReport { private int transferAssetCount; + private int transferPrivsCount; + private int sellCount; private int sellAmount; @@ -44,18 +48,20 @@ public class SponsorshipReport { protected SponsorshipReport() { } - public SponsorshipReport(String address, int level, int blocksMinted, int adjustments, int penalties, String[] names, int sponseeCount, int nonRegisteredCount, int avgBalance, int arbitraryCount, int transferAssetCount, int sellCount, int sellAmount, int buyCount, int buyAmount) { + public SponsorshipReport(String address, int level, int blocksMinted, int adjustments, int penalties, boolean transfer, String[] names, int sponseeCount, int nonRegisteredCount, int avgBalance, int arbitraryCount, int transferAssetCount, int transferPrivsCount, int sellCount, int sellAmount, int buyCount, int buyAmount) { this.address = address; this.level = level; this.blocksMinted = blocksMinted; this.adjustments = adjustments; this.penalties = penalties; + this.transfer = transfer; this.names = names; this.sponseeCount = sponseeCount; this.nonRegisteredCount = nonRegisteredCount; this.avgBalance = avgBalance; this.arbitraryCount = arbitraryCount; this.transferAssetCount = transferAssetCount; + this.transferPrivsCount = transferPrivsCount; this.sellCount = sellCount; this.sellAmount = sellAmount; this.buyCount = buyCount; @@ -85,6 +91,10 @@ public class SponsorshipReport { return penalties; } + public boolean isTransfer() { + return transfer; + } + public String[] getNames() { return names; } @@ -109,6 +119,10 @@ public class SponsorshipReport { return transferAssetCount; } + public int getTransferPrivsCount() { + return transferPrivsCount; + } + public int getSellCount() { return sellCount; } @@ -133,12 +147,14 @@ public class SponsorshipReport { ", blocksMinted=" + blocksMinted + ", adjustments=" + adjustments + ", penalties=" + penalties + + ", transfer=" + transfer + ", names=" + Arrays.toString(names) + ", sponseeCount=" + sponseeCount + ", nonRegisteredCount=" + nonRegisteredCount + ", avgBalance=" + avgBalance + ", arbitraryCount=" + arbitraryCount + ", transferAssetCount=" + transferAssetCount + + ", transferPrivsCount=" + transferPrivsCount + ", sellCount=" + sellCount + ", sellAmount=" + sellAmount + ", buyCount=" + buyCount + diff --git a/src/main/java/org/qortal/repository/hsqldb/HSQLDBAccountRepository.java b/src/main/java/org/qortal/repository/hsqldb/HSQLDBAccountRepository.java index 374c3a99..ede1ddf9 100644 --- a/src/main/java/org/qortal/repository/hsqldb/HSQLDBAccountRepository.java +++ b/src/main/java/org/qortal/repository/hsqldb/HSQLDBAccountRepository.java @@ -1168,14 +1168,15 @@ public class HSQLDBAccountRepository implements AccountRepository { int blocksMinted = accountResultSet.getInt(3); int adjustments = accountResultSet.getInt(4); int penalties = accountResultSet.getInt(5); + boolean transferPrivs = accountResultSet.getBoolean(6); List sponseeAddresses = getSponseeAddresses(account); if( sponseeAddresses.isEmpty() ){ - return new SponsorshipReport(account, level, blocksMinted, adjustments, penalties, new String[0], 0, 0, 0, 0, 0, 0, 0, 0, 0); + return new SponsorshipReport(account, level, blocksMinted, adjustments, penalties, transferPrivs, new String[0], 0, 0,0, 0, 0, 0, 0, 0, 0, 0); } else { - return produceSponsorShipReport(account, level, blocksMinted, adjustments, penalties, sponseeAddresses); + return produceSponsorShipReport(account, level, blocksMinted, adjustments, penalties, sponseeAddresses, transferPrivs); } } catch (Exception e) { @@ -1243,15 +1244,14 @@ public class HSQLDBAccountRepository implements AccountRepository { /** * Produce Sponsorship Report * - * @param address the account address for the sponsor - * @param level the sponsor's level - * @param blocksMinted the blocks minted by the sponsor + * @param address the account address for the sponsor + * @param level the sponsor's level + * @param blocksMinted the blocks minted by the sponsor * @param blocksMintedAdjustment * @param blocksMintedPenalty * @param sponseeAddresses - * + * @param transferPrivs true if this account was involved in a TRANSFER_PRIVS transaction * @return the report - * * @throws SQLException */ private SponsorshipReport produceSponsorShipReport( @@ -1260,7 +1260,8 @@ public class HSQLDBAccountRepository implements AccountRepository { int blocksMinted, int blocksMintedAdjustment, int blocksMintedPenalty, - List sponseeAddresses) throws SQLException { + List sponseeAddresses, + boolean transferPrivs) throws SQLException, DataException { int sponseeCount = sponseeAddresses.size(); @@ -1275,32 +1276,41 @@ public class HSQLDBAccountRepository implements AccountRepository { // count the arbitrary and transfer asset transactions for all sponsees ResultSet txTypeResultSet = getTxTypeResultSet(sponseeAddresses, sponseeCount); - int arbitraryCount = 0; - int transferAssetCount = 0; + int arbitraryCount; + int transferAssetCount; + int transferPrivsCount; if( txTypeResultSet != null) { - int txType = txTypeResultSet.getInt(1); - // if arbitrary transaction type, then get the count and move to the next result - if (txType == 10) { - arbitraryCount = txTypeResultSet.getInt(2); + Map countsByType = new HashMap<>(2); - // if there is another result, then get - if (txTypeResultSet.next()) - txType = txTypeResultSet.getInt(1); - } + do{ + Integer type = txTypeResultSet.getInt(1); - // if asset transfer type, then get the count and move to the next result - if (txType == 12) { - transferAssetCount = txTypeResultSet.getInt(2); - txTypeResultSet.next(); - } + if( type != null ) { + countsByType.put(type, txTypeResultSet.getInt(2)); + } + } while( txTypeResultSet.next()); + + arbitraryCount = countsByType.getOrDefault(10, 0); + transferAssetCount = countsByType.getOrDefault(12, 0); + transferPrivsCount = countsByType.getOrDefault(40, 0); } + else { + throw new DataException("trouble fetching counts for transaction types"); + } + // count up the each the buy and sell foreign coin exchanges for all sponsees // also sum up the balances of these exchanges ResultSet buySellResultSet = getBuySellResultSet(sponseeAddresses, sponseeCount); + int sellCount; + int sellAmount; + + int buyCount; + int buyAmount; + // if there are results, then fill in the buy/sell amount/counts if( buySellResultSet != null ) { @@ -1317,31 +1327,15 @@ public class HSQLDBAccountRepository implements AccountRepository { } while( buySellResultSet.next()); - int sellCount = countsByDirection.getOrDefault(SELL, 0); - int sellAmount = amountsByDirection.getOrDefault(SELL, 0); - - int buyCount = countsByDirection.getOrDefault(BUY, 0); - int buyAmount = amountsByDirection.getOrDefault(BUY, 0); - - return new SponsorshipReport( - address, - level, - blocksMinted, - blocksMintedAdjustment, - blocksMintedPenalty, - sponseeNames.toArray(new String[sponseeNames.size()]), - sponseeCount, - sponseeCount - sponseeNames.size(), - avgBalance, - arbitraryCount, - transferAssetCount, - sellCount, - sellAmount, - buyCount, - buyAmount); + sellCount = countsByDirection.getOrDefault(SELL, 0); + sellAmount = amountsByDirection.getOrDefault(SELL, 0); + buyCount = countsByDirection.getOrDefault(BUY, 0); + buyAmount = amountsByDirection.getOrDefault(BUY, 0); + } + else { + throw new DataException("trouble fetching counts for buy/sell transactions"); } - // otherwise use zeros for the counts and amounts return new SponsorshipReport( address, @@ -1349,16 +1343,18 @@ public class HSQLDBAccountRepository implements AccountRepository { blocksMinted, blocksMintedAdjustment, blocksMintedPenalty, + transferPrivs, sponseeNames.toArray(new String[sponseeNames.size()]), sponseeCount, sponseeCount - sponseeNames.size(), avgBalance, arbitraryCount, transferAssetCount, - 0, - 0, - 0, - 0); + transferPrivsCount, + sellCount, + sellAmount, + buyCount, + buyAmount); } private ResultSet getBuySellResultSet(List sponseeAddresses, int sponseeCount) throws SQLException { @@ -1392,8 +1388,9 @@ public class HSQLDBAccountRepository implements AccountRepository { StringBuffer accountSql = new StringBuffer(); - accountSql.append( "SELECT DISTINCT account, level, blocks_minted, blocks_minted_adjustment, blocks_minted_penalty "); - accountSql.append( "FROM ACCOUNTS "); + accountSql.append( "SELECT DISTINCT a.account, a.level, a.blocks_minted, a.blocks_minted_adjustment, a.blocks_minted_penalty, tx.sender IS NOT NULL as transfer "); + accountSql.append( "FROM ACCOUNTS a "); + accountSql.append( "LEFT JOIN TRANSFERPRIVSTRANSACTIONS tx on a.public_key = tx.sender or a.account = tx.recipient "); accountSql.append( "WHERE account = ? "); ResultSet accountResultSet = this.repository.checkedExecute( accountSql.toString(), account); @@ -1413,7 +1410,7 @@ public class HSQLDBAccountRepository implements AccountRepository { txTypeTotalsSql.append("INNER JOIN TRANSACTIONS USING (signature) "); txTypeTotalsSql.append("where participant in ( "); txTypeTotalsSql.append(String.join(", ", Collections.nCopies(sponseeCount, "?"))); - txTypeTotalsSql.append(") and type in (10, 12) "); + txTypeTotalsSql.append(") and type in (10, 12, 40) "); txTypeTotalsSql.append("group by type order by type"); String[] sponsees = sponseeAddresses.toArray(new String[sponseeCount]);