コード例 #1
1
 public StoredTransactionOutput getTransactionOutput(Sha256Hash hash, long index)
     throws BlockStoreException {
   maybeConnect();
   PreparedStatement s = null;
   try {
     s =
         conn.get()
             .prepareStatement(
                 "SELECT height, value, scriptBytes FROM openOutputs "
                     + "WHERE hash = ? AND index = ?");
     s.setBytes(1, hash.getBytes());
     // index is actually an unsigned int
     s.setInt(2, (int) index);
     ResultSet results = s.executeQuery();
     if (!results.next()) {
       return null;
     }
     // Parse it.
     int height = results.getInt(1);
     BigInteger value = new BigInteger(results.getBytes(2));
     // Tell the StoredTransactionOutput that we are a coinbase, as that is encoded in height
     StoredTransactionOutput txout =
         new StoredTransactionOutput(hash, index, value, height, true, results.getBytes(3));
     return txout;
   } catch (SQLException ex) {
     throw new BlockStoreException(ex);
   } finally {
     if (s != null)
       try {
         s.close();
       } catch (SQLException e) {
         throw new BlockStoreException("Failed to close PreparedStatement");
       }
   }
 }
コード例 #2
0
  public StoredBlock get(Sha256Hash hash, boolean wasUndoableOnly) throws BlockStoreException {
    // Optimize for chain head
    if (chainHeadHash != null && chainHeadHash.equals(hash)) return chainHeadBlock;
    if (verifiedChainHeadHash != null && verifiedChainHeadHash.equals(hash))
      return verifiedChainHeadBlock;
    maybeConnect();
    PreparedStatement s = null;
    try {
      s =
          conn.get()
              .prepareStatement(
                  "SELECT chainWork, height, header, wasUndoable FROM headers WHERE hash = ?");
      // We skip the first 4 bytes because (on prodnet) the minimum target has 4 0-bytes
      byte[] hashBytes = new byte[28];
      System.arraycopy(hash.getBytes(), 3, hashBytes, 0, 28);
      s.setBytes(1, hashBytes);
      ResultSet results = s.executeQuery();
      if (!results.next()) {
        return null;
      }
      // Parse it.

      if (wasUndoableOnly && !results.getBoolean(4)) return null;

      BigInteger chainWork = new BigInteger(results.getBytes(1));
      int height = results.getInt(2);
      Block b = new Block(params, results.getBytes(3));
      b.verifyHeader();
      StoredBlock stored = new StoredBlock(b, chainWork, height);
      return stored;
    } catch (SQLException ex) {
      throw new BlockStoreException(ex);
    } catch (ProtocolException e) {
      // Corrupted database.
      throw new BlockStoreException(e);
    } catch (VerificationException e) {
      // Should not be able to happen unless the database contains bad
      // blocks.
      throw new BlockStoreException(e);
    } finally {
      if (s != null)
        try {
          s.close();
        } catch (SQLException e) {
          throw new BlockStoreException("Failed to close PreparedStatement");
        }
    }
  }
コード例 #3
0
 public void setChainHead(StoredBlock chainHead) throws BlockStoreException {
   Sha256Hash hash = chainHead.getHeader().getHash();
   this.chainHeadHash = hash;
   this.chainHeadBlock = chainHead;
   maybeConnect();
   try {
     PreparedStatement s =
         conn.get().prepareStatement("UPDATE settings SET value = ? WHERE name = ?");
     s.setString(2, CHAIN_HEAD_SETTING);
     s.setBytes(1, hash.getBytes());
     s.executeUpdate();
     s.close();
   } catch (SQLException ex) {
     throw new BlockStoreException(ex);
   }
 }
コード例 #4
0
 public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException {
   Sha256Hash hash = chainHead.getHeader().getHash();
   this.verifiedChainHeadHash = hash;
   this.verifiedChainHeadBlock = chainHead;
   maybeConnect();
   try {
     PreparedStatement s =
         conn.get().prepareStatement("UPDATE settings SET value = ? WHERE name = ?");
     s.setString(2, VERIFIED_CHAIN_HEAD_SETTING);
     s.setBytes(1, hash.getBytes());
     s.executeUpdate();
     s.close();
   } catch (SQLException ex) {
     throw new BlockStoreException(ex);
   }
   if (this.chainHeadBlock.getHeight() < chainHead.getHeight()) setChainHead(chainHead);
   removeUndoableBlocksWhereHeightIsLessThan(chainHead.getHeight() - fullStoreDepth);
 }
コード例 #5
0
 public boolean hasUnspentOutputs(Sha256Hash hash, int numOutputs) throws BlockStoreException {
   maybeConnect();
   PreparedStatement s = null;
   try {
     s = conn.get().prepareStatement("SELECT COUNT(*) FROM openOutputs WHERE hash = ?");
     s.setBytes(1, hash.getBytes());
     ResultSet results = s.executeQuery();
     if (!results.next()) {
       throw new BlockStoreException("Got no results from a COUNT(*) query");
     }
     int count = results.getInt(1);
     return count != 0;
   } catch (SQLException ex) {
     throw new BlockStoreException(ex);
   } finally {
     if (s != null)
       try {
         s.close();
       } catch (SQLException e) {
         throw new BlockStoreException("Failed to close PreparedStatement");
       }
   }
 }
コード例 #6
0
  public StoredUndoableBlock getUndoBlock(Sha256Hash hash) throws BlockStoreException {
    maybeConnect();
    PreparedStatement s = null;
    try {
      s =
          conn.get()
              .prepareStatement(
                  "SELECT txOutChanges, transactions FROM undoableBlocks WHERE hash = ?");
      // We skip the first 4 bytes because (on prodnet) the minimum target has 4 0-bytes

      byte[] hashBytes = new byte[28];
      System.arraycopy(hash.getBytes(), 3, hashBytes, 0, 28);
      s.setBytes(1, hashBytes);
      ResultSet results = s.executeQuery();
      if (!results.next()) {
        return null;
      }
      // Parse it.
      byte[] txOutChanges = results.getBytes(1);
      byte[] transactions = results.getBytes(2);
      StoredUndoableBlock block;
      if (txOutChanges == null) {
        int offset = 0;
        int numTxn =
            ((transactions[offset++] & 0xFF) << 0)
                | ((transactions[offset++] & 0xFF) << 8)
                | ((transactions[offset++] & 0xFF) << 16)
                | ((transactions[offset++] & 0xFF) << 24);
        List<Transaction> transactionList = new LinkedList<Transaction>();
        for (int i = 0; i < numTxn; i++) {
          Transaction tx = new Transaction(params, transactions, offset);
          transactionList.add(tx);
          offset += tx.getMessageSize();
        }
        block = new StoredUndoableBlock(hash, transactionList);
      } else {
        TransactionOutputChanges outChangesObject =
            new TransactionOutputChanges(new ByteArrayInputStream(txOutChanges));
        block = new StoredUndoableBlock(hash, outChangesObject);
      }
      return block;
    } catch (SQLException ex) {
      throw new BlockStoreException(ex);
    } catch (NullPointerException e) {
      // Corrupted database.
      throw new BlockStoreException(e);
    } catch (ClassCastException e) {
      // Corrupted database.
      throw new BlockStoreException(e);
    } catch (ProtocolException e) {
      // Corrupted database.
      throw new BlockStoreException(e);
    } catch (IOException e) {
      // Corrupted database.
      throw new BlockStoreException(e);
    } finally {
      if (s != null)
        try {
          s.close();
        } catch (SQLException e) {
          throw new BlockStoreException("Failed to close PreparedStatement");
        }
    }
  }