Example #1
0
 @Override
 public BlockImpl getBlock(long blockId) {
   BlockImpl block = lastBlock.get();
   if (block.getId() == blockId) {
     return block;
   }
   return BlockDb.findBlock(blockId);
 }
Example #2
0
 @Override
 public BlockImpl getLastBlock(int timestamp) {
   BlockImpl block = lastBlock.get();
   if (timestamp >= block.getTimestamp()) {
     return block;
   }
   return BlockDb.findLastBlock(timestamp);
 }
Example #3
0
 @Override
 public BlockImpl getBlockAtHeight(int height) {
   BlockImpl block = lastBlock.get();
   if (height > block.getHeight()) {
     throw new IllegalArgumentException(
         "Invalid height " + height + ", current blockchain is at " + block.getHeight());
   }
   if (height == block.getHeight()) {
     return block;
   }
   return BlockDb.findBlockAtHeight(height);
 }
Example #4
0
 @Override
 public List<BlockImpl> getBlocksAfter(long blockId, int limit) {
   try (Connection con = Db.db.getConnection();
       PreparedStatement pstmt =
           con.prepareStatement(
               "SELECT * FROM block WHERE db_id > (SELECT db_id FROM block WHERE id = ?) ORDER BY db_id ASC LIMIT ?")) {
     List<BlockImpl> result = new ArrayList<>();
     pstmt.setLong(1, blockId);
     pstmt.setInt(2, limit);
     pstmt.setFetchSize(100);
     try (ResultSet rs = pstmt.executeQuery()) {
       while (rs.next()) {
         result.add(BlockDb.loadBlock(con, rs, true));
       }
     }
     return result;
   } catch (SQLException e) {
     throw new RuntimeException(e.toString(), e);
   }
 }
Example #5
0
 @Override
 public List<BlockImpl> getBlocksAfter(long blockId, List<Long> blockList) {
   List<BlockImpl> result = new ArrayList<>();
   if (blockList.isEmpty()) return result;
   try (Connection con = Db.db.getConnection();
       PreparedStatement pstmt =
           con.prepareStatement(
               "SELECT * FROM block WHERE db_id > (SELECT db_id FROM block WHERE id = ?) ORDER BY db_id ASC LIMIT ?")) {
     pstmt.setLong(1, blockId);
     pstmt.setInt(2, blockList.size());
     pstmt.setFetchSize(100);
     try (ResultSet rs = pstmt.executeQuery()) {
       int index = 0;
       while (rs.next()) {
         BlockImpl block = BlockDb.loadBlock(con, rs, true);
         if (block.getId() != blockList.get(index++)) break;
         result.add(block);
       }
     }
     return result;
   } catch (SQLException e) {
     throw new RuntimeException(e.toString(), e);
   }
 }
Example #6
0
 @Override
 public boolean hasBlock(long blockId) {
   return lastBlock.get().getId() == blockId || BlockDb.hasBlock(blockId);
 }