Ejemplo n.º 1
0
 void addToBalanceAndUnconfirmedBalanceNQT(long amountNQT) {
   if (amountNQT == 0) {
     return;
   }
   this.balanceNQT = Convert.safeAdd(this.balanceNQT, amountNQT);
   this.unconfirmedBalanceNQT = Convert.safeAdd(this.unconfirmedBalanceNQT, amountNQT);
   addToGuaranteedBalanceNQT(amountNQT);
   checkBalance(this.id, this.balanceNQT, this.unconfirmedBalanceNQT);
   accountTable.insert(this);
   listeners.notify(this, Event.BALANCE);
   listeners.notify(this, Event.UNCONFIRMED_BALANCE);
 }
Ejemplo n.º 2
0
 void addToForgedBalanceNQT(long amountNQT) {
   if (amountNQT == 0) {
     return;
   }
   this.forgedBalanceNQT = Convert.safeAdd(this.forgedBalanceNQT, amountNQT);
   accountTable.insert(this);
 }
Ejemplo n.º 3
0
 private void addToGuaranteedBalanceNQT(long amountNQT) {
   if (amountNQT <= 0) {
     return;
   }
   int blockchainHeight = Nxt.getBlockchain().getHeight();
   try (Connection con = Db.getConnection();
       PreparedStatement pstmtSelect =
           con.prepareStatement(
               "SELECT additions FROM account_guaranteed_balance "
                   + "WHERE account_id = ? and height = ?");
       PreparedStatement pstmtUpdate =
           con.prepareStatement(
               "MERGE INTO account_guaranteed_balance (account_id, "
                   + " additions, height) KEY (account_id, height) VALUES(?, ?, ?)")) {
     pstmtSelect.setLong(1, this.id);
     pstmtSelect.setInt(2, blockchainHeight);
     try (ResultSet rs = pstmtSelect.executeQuery()) {
       long additions = amountNQT;
       if (rs.next()) {
         additions = Convert.safeAdd(additions, rs.getLong("additions"));
       }
       pstmtUpdate.setLong(1, this.id);
       pstmtUpdate.setLong(2, additions);
       pstmtUpdate.setInt(3, blockchainHeight);
       pstmtUpdate.executeUpdate();
     }
   } catch (SQLException e) {
     throw new RuntimeException(e.toString(), e);
   }
 }
Ejemplo n.º 4
0
 void addToAssetAndUnconfirmedAssetBalanceQNT(long assetId, long quantityQNT) {
   if (quantityQNT == 0) {
     return;
   }
   AccountAsset accountAsset;
   accountAsset = accountAssetTable.get(accountAssetDbKeyFactory.newKey(this.id, assetId));
   long assetBalance = accountAsset == null ? 0 : accountAsset.quantityQNT;
   assetBalance = Convert.safeAdd(assetBalance, quantityQNT);
   long unconfirmedAssetBalance = accountAsset == null ? 0 : accountAsset.unconfirmedQuantityQNT;
   unconfirmedAssetBalance = Convert.safeAdd(unconfirmedAssetBalance, quantityQNT);
   if (accountAsset == null) {
     accountAsset = new AccountAsset(this.id, assetId, assetBalance, unconfirmedAssetBalance);
   } else {
     accountAsset.quantityQNT = assetBalance;
     accountAsset.unconfirmedQuantityQNT = unconfirmedAssetBalance;
   }
   accountAsset.save();
   listeners.notify(this, Event.ASSET_BALANCE);
   listeners.notify(this, Event.UNCONFIRMED_ASSET_BALANCE);
   assetListeners.notify(accountAsset, Event.ASSET_BALANCE);
   assetListeners.notify(accountAsset, Event.UNCONFIRMED_ASSET_BALANCE);
 }
Ejemplo n.º 5
0
  public static void main(String[] args) {
    String fileName = args.length == 1 ? args[0] : "nxt-trace.csv";
    try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
      String line = reader.readLine();
      String[] headers = unquote(line.split(DebugTrace.SEPARATOR));

      Map<String, Map<String, Long>> totals = new HashMap<>();
      Map<String, Map<String, Map<String, Long>>> accountAssetTotals = new HashMap<>();
      Map<String, Long> issuedAssetQuantities = new HashMap<>();
      Map<String, Long> accountAssetQuantities = new HashMap<>();

      while ((line = reader.readLine()) != null) {
        String[] values = unquote(line.split(DebugTrace.SEPARATOR));
        Map<String, String> valueMap = new HashMap<>();
        for (int i = 0; i < headers.length; i++) {
          valueMap.put(headers[i], values[i]);
        }
        String accountId = valueMap.get("account");
        Map<String, Long> accountTotals = totals.get(accountId);
        if (accountTotals == null) {
          accountTotals = new HashMap<>();
          totals.put(accountId, accountTotals);
        }
        Map<String, Map<String, Long>> accountAssetMap = accountAssetTotals.get(accountId);
        if (accountAssetMap == null) {
          accountAssetMap = new HashMap<>();
          accountAssetTotals.put(accountId, accountAssetMap);
        }
        if ("asset issuance".equals(valueMap.get("event"))) {
          String assetId = valueMap.get("asset");
          issuedAssetQuantities.put(assetId, Long.parseLong(valueMap.get("asset quantity")));
        }
        for (Map.Entry<String, String> mapEntry : valueMap.entrySet()) {
          String header = mapEntry.getKey();
          String value = mapEntry.getValue();
          if (value == null || "".equals(value.trim())) {
            continue;
          }
          if (isBalance(header)) {
            accountTotals.put(header, Long.parseLong(value));
          } else if (isDelta(header)) {
            long previousValue = Convert.nullToZero(accountTotals.get(header));
            accountTotals.put(header, Convert.safeAdd(previousValue, Long.parseLong(value)));
          } else if (isAssetQuantity(header)) {
            String assetId = valueMap.get("asset");
            Map<String, Long> assetTotals = accountAssetMap.get(assetId);
            if (assetTotals == null) {
              assetTotals = new HashMap<>();
              accountAssetMap.put(assetId, assetTotals);
            }
            assetTotals.put(header, Long.parseLong(value));
          } else if (isDeltaAssetQuantity(header)) {
            String assetId = valueMap.get("asset");
            Map<String, Long> assetTotals = accountAssetMap.get(assetId);
            if (assetTotals == null) {
              assetTotals = new HashMap<>();
              accountAssetMap.put(assetId, assetTotals);
            }
            long previousValue = Convert.nullToZero(assetTotals.get(header));
            assetTotals.put(header, Convert.safeAdd(previousValue, Long.parseLong(value)));
          }
        }
      }

      Set<String> failed = new HashSet<>();
      for (Map.Entry<String, Map<String, Long>> mapEntry : totals.entrySet()) {
        String accountId = mapEntry.getKey();
        Map<String, Long> accountValues = mapEntry.getValue();
        System.out.println("account: " + accountId);
        for (String balanceHeader : balanceHeaders) {
          System.out.println(
              balanceHeader + ": " + Convert.nullToZero(accountValues.get(balanceHeader)));
        }
        System.out.println("totals:");
        long totalDelta = 0;
        for (String header : deltaHeaders) {
          long delta = Convert.nullToZero(accountValues.get(header));
          totalDelta = Convert.safeAdd(totalDelta, delta);
          System.out.println(header + ": " + delta);
        }
        System.out.println("total confirmed balance change: " + totalDelta);
        long balance = Convert.nullToZero(accountValues.get("balance"));
        if (balance != totalDelta) {
          System.out.println("ERROR: balance doesn't match total change!!!");
          failed.add(accountId);
        }
        Map<String, Map<String, Long>> accountAssetMap = accountAssetTotals.get(accountId);
        for (Map.Entry<String, Map<String, Long>> assetMapEntry : accountAssetMap.entrySet()) {
          String assetId = assetMapEntry.getKey();
          Map<String, Long> assetValues = assetMapEntry.getValue();
          System.out.println("asset: " + assetId);
          for (Map.Entry<String, Long> assetValueEntry : assetValues.entrySet()) {
            System.out.println(assetValueEntry.getKey() + ": " + assetValueEntry.getValue());
          }
          long totalAssetDelta = 0;
          for (String header : deltaAssetQuantityHeaders) {
            long delta = Convert.nullToZero(assetValues.get(header));
            totalAssetDelta = Convert.safeAdd(totalAssetDelta, delta);
          }
          System.out.println("total confirmed asset quantity change: " + totalAssetDelta);
          long assetBalance = assetValues.get("asset balance");
          if (assetBalance != totalAssetDelta) {
            System.out.println("ERROR: asset balance doesn't match total asset quantity change!!!");
            failed.add(accountId);
          }
          Long previousAssetQuantity = Convert.nullToZero(accountAssetQuantities.get(assetId));
          accountAssetQuantities.put(assetId, Convert.safeAdd(previousAssetQuantity, assetBalance));
        }
        System.out.println();
      }
      Set<String> failedAssets = new HashSet<>();
      for (Map.Entry<String, Long> assetEntry : issuedAssetQuantities.entrySet()) {
        String assetId = assetEntry.getKey();
        long issuedAssetQuantity = assetEntry.getValue();
        if (issuedAssetQuantity != Convert.nullToZero(accountAssetQuantities.get(assetId))) {
          System.out.println(
              "ERROR: asset "
                  + assetId
                  + " balances don't match, issued: "
                  + issuedAssetQuantity
                  + ", total of account balances: "
                  + accountAssetQuantities.get(assetId));
          failedAssets.add(assetId);
        }
      }
      if (failed.size() > 0) {
        System.out.println("ERROR: " + failed.size() + " accounts have incorrect balances");
        System.out.println(failed);
      } else {
        System.out.println(
            "SUCCESS: all "
                + totals.size()
                + " account balances and asset balances match the transaction and trade totals!");
      }
      if (failedAssets.size() > 0) {
        System.out.println("ERROR: " + failedAssets.size() + " assets have incorrect balances");
        System.out.println(failedAssets);
      } else {
        System.out.println(
            "SUCCESS: all " + issuedAssetQuantities.size() + " assets quantities are correct!");
      }

    } catch (IOException e) {
      System.out.println(e.toString());
      throw new RuntimeException(e);
    }
  }