public static LazyList getBlocks(Boolean orderAscending, INxtService nxt) { Connection con = null; /* Count number of records */ // int available = nxt.getSmartBlockCount(); int available = Nxt.getBlockchain().getBlockCount(); /* Create the record iterator */ try { con = Db.getConnection(); StringBuilder buf = new StringBuilder(); buf.append("SELECT * FROM block "); if (orderAscending != null) { if (Boolean.TRUE.equals(orderAscending)) { buf.append("ORDER BY db_id ASC"); } else if (Boolean.FALSE.equals(orderAscending)) { buf.append("ORDER BY db_id DESC"); } } PreparedStatement pstmt = con.prepareStatement(buf.toString()); DbIterator<? extends Block> iterator = Nxt.getBlockchain().getBlocks(con, pstmt); LazyList lazyList = new LazyList(iterator, available); return lazyList; } catch (SQLException e) { logger.error("SQL Error", e); if (con != null) { DbUtils.close(con); } } return null; }
@Override JSONStreamAware processRequest(HttpServletRequest req) { JSONObject response = new JSONObject(); int numBlocks = 0; try { numBlocks = Integer.parseInt(req.getParameter("numBlocks")); } catch (NumberFormatException e) { } int height = 0; try { height = Integer.parseInt(req.getParameter("height")); } catch (NumberFormatException e) { } List<? extends Block> blocks; JSONArray blocksJSON = new JSONArray(); if (numBlocks > 0) { blocks = Nxt.getBlockchainProcessor().popOffTo(Nxt.getBlockchain().getHeight() - numBlocks); } else if (height > 0) { blocks = Nxt.getBlockchainProcessor().popOffTo(height); } else { response.put("error", "invalid numBlocks or height"); return response; } for (Block block : blocks) { blocksJSON.add(JSONData.block(block, true)); } response.put("blocks", blocksJSON); return response; }
public static List<Block> getGeneratedBlocks(Long accountId) { Connection con = null; try { con = Db.getConnection(); PreparedStatement pstmt = con.prepareStatement("SELECT * FROM block WHERE generator_id = ? ORDER BY db_id ASC"); pstmt.setLong(1, accountId); List<Block> result = new ArrayList<Block>(); DbIterator<? extends Block> iterator = Nxt.getBlockchain().getBlocks(con, pstmt); while (iterator.hasNext()) { result.add(iterator.next()); } return result; } catch (SQLException e) { DbUtils.close(con); throw new RuntimeException(e.toString(), e); } }