Esempio n. 1
0
 public long getGuaranteedBalanceNQT(final int numberOfConfirmations, final int currentHeight) {
   if (numberOfConfirmations >= Nxt.getBlockchain().getHeight()) {
     return 0;
   }
   if (numberOfConfirmations > 2880 || numberOfConfirmations < 0) {
     throw new IllegalArgumentException(
         "Number of required confirmations must be between 0 and " + 2880);
   }
   int height = currentHeight - numberOfConfirmations;
   try (Connection con = Db.getConnection();
       PreparedStatement pstmt =
           con.prepareStatement(
               "SELECT SUM (additions) AS additions "
                   + "FROM account_guaranteed_balance WHERE account_id = ? AND height > ? AND height <= ?")) {
     pstmt.setLong(1, this.id);
     pstmt.setInt(2, height);
     pstmt.setInt(3, currentHeight);
     try (ResultSet rs = pstmt.executeQuery()) {
       if (!rs.next()) {
         return balanceNQT;
       }
       return Math.max(Convert.safeSubtract(balanceNQT, rs.getLong("additions")), 0);
     }
   } catch (SQLException e) {
     throw new RuntimeException(e.toString(), e);
   }
 }
Esempio n. 2
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);
   }
 }
Esempio n. 3
0
 // returns true iff:
 // this.publicKey is set to null (in which case this.publicKey also gets set to key)
 // or
 // this.publicKey is already set to an array equal to key
 boolean setOrVerify(byte[] key, int height) {
   if (this.publicKey == null) {
     if (Db.isInTransaction()) {
       this.publicKey = key;
       this.keyHeight = -1;
       accountTable.insert(this);
     }
     return true;
   } else if (Arrays.equals(this.publicKey, key)) {
     return true;
   } else if (this.keyHeight == -1) {
     Logger.logMessage("DUPLICATE KEY!!!");
     Logger.logMessage(
         "Account key for "
             + Convert.toUnsignedLong(id)
             + " was already set to a different one at the same height "
             + ", current height is "
             + height
             + ", rejecting new key");
     return false;
   } else if (this.keyHeight >= height) {
     Logger.logMessage("DUPLICATE KEY!!!");
     if (Db.isInTransaction()) {
       Logger.logMessage(
           "Changing key for account "
               + Convert.toUnsignedLong(id)
               + " at height "
               + height
               + ", was previously set to a different one at height "
               + keyHeight);
       this.publicKey = key;
       this.keyHeight = height;
       accountTable.insert(this);
     }
     return true;
   }
   Logger.logMessage("DUPLICATE KEY!!!");
   Logger.logMessage(
       "Invalid key for account "
           + Convert.toUnsignedLong(id)
           + " at height "
           + height
           + ", was already set to a different one at height "
           + keyHeight);
   return false;
 }
Esempio n. 4
0
 @Override
 public void trim(int height) {
   try (Connection con = Db.getConnection();
       PreparedStatement pstmtDelete =
           con.prepareStatement(
               "DELETE FROM account_guaranteed_balance " + "WHERE height < ?")) {
     pstmtDelete.setInt(1, height - 1440);
     pstmtDelete.executeUpdate();
   } catch (SQLException e) {
     throw new RuntimeException(e.toString(), e);
   }
 }
Esempio n. 5
0
 public static int getAssetAccountsCount(long assetId) {
   try (Connection con = Db.getConnection();
       PreparedStatement pstmt =
           con.prepareStatement(
               "SELECT COUNT(*) FROM account_asset WHERE asset_id = ? AND latest = TRUE")) {
     pstmt.setLong(1, assetId);
     try (ResultSet rs = pstmt.executeQuery()) {
       rs.next();
       return rs.getInt(1);
     }
   } catch (SQLException e) {
     throw new RuntimeException(e.toString(), e);
   }
 }