explorer.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #!/usr/bin/env python3
  2. # This file is part of DarkFi (https://dark.fi)
  3. #
  4. # Copyright (C) 2020-2026 Dyne.org foundation
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. from datetime import datetime, timezone
  19. from quart import Quart, render_template, abort, request, redirect, url_for, Response
  20. from rpc_client import JsonRpcPool, JsonRpcError, RpcUnavailableError
  21. app = Quart(__name__)
  22. app.config.update(
  23. RPC_HOST="127.0.0.1",
  24. RPC_PORT="22222",
  25. RPC_MIN_CONNECTIONS=5,
  26. RPC_MAX_CONNECTIONS=50,
  27. RPC_RECONNECT_INTERVAL=5.0,
  28. NETWORK="Testnet",
  29. )
  30. # Global pool
  31. rpc: JsonRpcPool = None
  32. @app.before_serving
  33. async def startup():
  34. global rpc
  35. rpc = JsonRpcPool(
  36. host=app.config["RPC_HOST"],
  37. port=app.config["RPC_PORT"],
  38. min_connections=app.config["RPC_MIN_CONNECTIONS"],
  39. max_connections=app.config["RPC_MAX_CONNECTIONS"],
  40. reconnect_interval=app.config["RPC_RECONNECT_INTERVAL"],
  41. )
  42. await rpc.start()
  43. app.logger.info(f"RPC pool initialized for {app.config['RPC_HOST']}:{app.config['RPC_PORT']}")
  44. @app.after_serving
  45. async def shutdown():
  46. await rpc.close()
  47. app.logger.info("RPC pool closed")
  48. @app.errorhandler(JsonRpcError)
  49. async def handle_rpc_error(error: JsonRpcError):
  50. app.logger.error(f"RPC Error: {error.code} - {error.message}")
  51. if error.code == -32600:
  52. return await render_template(
  53. "error.html",
  54. network=app.config["NETWORK"],
  55. error_code="404",
  56. error="The requested resource was not found"
  57. ), 404
  58. return await render_template(
  59. "error.html",
  60. network=app.config["NETWORK"],
  61. error_code="500",
  62. error=error.message
  63. ), 500
  64. @app.errorhandler(RpcUnavailableError)
  65. async def handle_rpc_unavailable(error: RpcUnavailableError):
  66. app.logger.error(f"RPC Unavailable: {error}")
  67. return await render_template(
  68. "error.html",
  69. network=app.config["NETWORK"],
  70. error_code="503",
  71. error="Blockchain node is currently unavailable. Please try again later."
  72. ), 503
  73. @app.errorhandler(ConnectionError)
  74. async def handle_connection_error(error):
  75. app.logger.error(f"Connection Error: {error}")
  76. return await render_template(
  77. "error.html",
  78. network=app.config["NETWORK"],
  79. error_code="503",
  80. error="Service temporarily unavailable"
  81. ), 503
  82. @app.errorhandler(404)
  83. async def handle_not_found(error):
  84. return await render_template(
  85. "error.html",
  86. network=app.config["NETWORK"],
  87. error_code="404",
  88. error="Page not found"
  89. ), 404
  90. def format_hashrate(hashrate: float) -> str:
  91. """Format hashrate with appropriate unit."""
  92. if hashrate >= 1e12:
  93. return f"{hashrate / 1e12:.2f} TH/s"
  94. elif hashrate >= 1e9:
  95. return f"{hashrate / 1e9:.2f} GH/s"
  96. elif hashrate >= 1e6:
  97. return f"{hashrate / 1e6:.2f} MH/s"
  98. elif hashrate >= 1e3:
  99. return f"{hashrate / 1e3:.2f} KH/s"
  100. else:
  101. return f"{hashrate:.2f} H/s"
  102. def format_bytes(size: int) -> str:
  103. """Format byte size with appropriate unit."""
  104. if size >= 1024 * 1024:
  105. return f"{size / (1024 * 1024):.2f} MB"
  106. elif size >= 1024:
  107. return f"{size / 1024:.2f} KB"
  108. else:
  109. return f"{size} bytes"
  110. @app.route("/")
  111. async def index():
  112. current_difficulty = await rpc.call("current_difficulty", params=[])
  113. current_height = await rpc.call("current_height", params=[])
  114. latest_blocks = await rpc.call("latest_blocks", params=[20])
  115. hashrate = await rpc.call("get_hashrate", params=[])
  116. for block in latest_blocks:
  117. dt = datetime.fromtimestamp(block["timestamp"], tz=timezone.utc)
  118. block["timestamp"] = dt.strftime("%H:%M UTC %d %b %Y")
  119. return await render_template(
  120. "index.html",
  121. network=app.config["NETWORK"],
  122. current_difficulty=current_difficulty[0],
  123. current_height=current_height,
  124. hashrate=format_hashrate(hashrate),
  125. latest_blocks=latest_blocks,
  126. )
  127. @app.route("/block/<int:block_height>")
  128. async def get_block_by_height(block_height: int):
  129. if block_height < 0:
  130. abort(404)
  131. current_difficulty = await rpc.call("current_difficulty", params=[])
  132. current_height = await rpc.call("current_height", params=[])
  133. hashrate = await rpc.call("get_hashrate", params=[])
  134. block = await rpc.call("get_block", params=[block_height])
  135. dt = datetime.fromtimestamp(block["timestamp"], tz=timezone.utc)
  136. block["timestamp"] = dt.strftime("%H:%M UTC %d %b %Y")
  137. block["n_txs"] = len(block["txs"])
  138. return await render_template(
  139. "block.html",
  140. network=app.config["NETWORK"],
  141. current_difficulty=current_difficulty[0],
  142. current_height=current_height,
  143. hashrate=format_hashrate(hashrate),
  144. block=block,
  145. )
  146. @app.route("/tx/<tx_hash>")
  147. async def get_tx_by_hash(tx_hash: str):
  148. # Validate hex string
  149. if not all(c in '0123456789abcdefABCDEF' for c in tx_hash):
  150. abort(404)
  151. current_difficulty = await rpc.call("current_difficulty", params=[])
  152. current_height = await rpc.call("current_height", params=[])
  153. hashrate = await rpc.call("get_hashrate", params=[])
  154. tx = await rpc.call("get_tx", params=[tx_hash])
  155. return await render_template(
  156. "tx.html",
  157. network=app.config["NETWORK"],
  158. current_difficulty=current_difficulty[0],
  159. current_height=current_height,
  160. hashrate=format_hashrate(hashrate),
  161. tx=tx,
  162. )
  163. @app.route("/search")
  164. async def search():
  165. """Search for blocks by height/hash or transactions by hash."""
  166. query = request.args.get("q", "").strip()
  167. if not query:
  168. return redirect(url_for("index"))
  169. # Try to interpret as block height (integer)
  170. if query.isdigit():
  171. return redirect(url_for("get_block_by_height", block_height=int(query)))
  172. # Check if it looks like a hex hash
  173. if all(c in '0123456789abcdefABCDEF' for c in query):
  174. # Use the search RPC to determine if it's a block or tx hash
  175. try:
  176. result = await rpc.call("search", params=[query])
  177. if result["type"] == "block":
  178. return redirect(f"/block/{result['height']}")
  179. elif result["type"] == "tx":
  180. return redirect(url_for("get_tx_by_hash", tx_hash=query))
  181. except JsonRpcError:
  182. pass
  183. # Try as contract ID (base58)
  184. try:
  185. contract = await rpc.call("get_contract", params=[query])
  186. return redirect(url_for("get_contract", contract_id=query))
  187. except JsonRpcError:
  188. pass
  189. # Nothing found
  190. return await render_template(
  191. "error.html",
  192. network=app.config["NETWORK"],
  193. error_code="Not Found",
  194. error=f"No block, transaction, or contract found for: {query}"
  195. ), 404
  196. @app.route("/contract/<contract_id>")
  197. async def get_contract(contract_id: str):
  198. current_difficulty = await rpc.call("current_difficulty", params=[])
  199. current_height = await rpc.call("current_height", params=[])
  200. hashrate = await rpc.call("get_hashrate", params=[])
  201. contract = await rpc.call("get_contract", params=[contract_id])
  202. contract["wasm_size_formatted"] = format_bytes(int(contract["wasm_size"]))
  203. return await render_template(
  204. "contract.html",
  205. network=app.config["NETWORK"],
  206. current_difficulty=current_difficulty[0],
  207. current_height=current_height,
  208. hashrate=format_hashrate(hashrate),
  209. contract=contract,
  210. )
  211. @app.route("/contracts")
  212. async def list_contracts():
  213. current_difficulty = await rpc.call("current_difficulty", params=[])
  214. current_height = await rpc.call("current_height", params=[])
  215. hashrate = await rpc.call("get_hashrate", params=[])
  216. contracts = await rpc.call("list_contracts", params=[])
  217. contract_count = await rpc.call("contract_count", params=[])
  218. for contract in contracts:
  219. contract["wasm_size_formatted"] = format_bytes(int(contract["wasm_size"]))
  220. return await render_template(
  221. "contracts.html",
  222. network=app.config["NETWORK"],
  223. current_difficulty=current_difficulty[0],
  224. current_height=current_height,
  225. hashrate=format_hashrate(hashrate),
  226. contracts=contracts,
  227. contract_count=contract_count,
  228. )
  229. @app.route("/stats")
  230. async def stats():
  231. current_difficulty = await rpc.call("current_difficulty", params=[])
  232. current_height = await rpc.call("current_height", params=[])
  233. hashrate = await rpc.call("get_hashrate", params=[])
  234. stats_data = await rpc.call("get_stats", params=[])
  235. return await render_template(
  236. "stats.html",
  237. network=app.config["NETWORK"],
  238. current_difficulty=current_difficulty[0],
  239. current_height=current_height,
  240. hashrate=format_hashrate(hashrate),
  241. stats=stats_data,
  242. )
  243. @app.route("/stats/daily_tx_chart.png")
  244. async def daily_tx_chart():
  245. """Generate daily average transactions chart as PNG using matplotlib."""
  246. import io
  247. import matplotlib
  248. matplotlib.use('Agg') # Non-interactive backend
  249. import matplotlib.pyplot as plt
  250. import matplotlib.dates as mdates
  251. stats_data = await rpc.call("get_stats", params=[])
  252. daily_stats = stats_data.get("daily_stats", [])
  253. # Filter to last 90 days
  254. if daily_stats:
  255. max_day = max(d["day"] for d in daily_stats)
  256. daily_stats = [d for d in daily_stats if d["day"] >= max_day - 90]
  257. # Create figure with dark theme
  258. plt.style.use('dark_background')
  259. fig, ax = plt.subplots(figsize=(12, 4), dpi=100)
  260. fig.patch.set_facecolor('#0d1117')
  261. ax.set_facecolor('#0d1117')
  262. if daily_stats:
  263. # Convert day numbers to dates
  264. dates = [datetime.fromtimestamp(d["day"] * 86400, tz=timezone.utc) for d in daily_stats]
  265. values = [d["avg_tx"] for d in daily_stats]
  266. ax.fill_between(dates, values, alpha=0.3, color='#6366f1')
  267. ax.plot(dates, values, color='#6366f1', linewidth=2)
  268. # Format x-axis
  269. ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
  270. ax.xaxis.set_major_locator(mdates.DayLocator(interval=7))
  271. plt.xticks(rotation=45, ha='right')
  272. ax.set_ylabel('Avg TX per Block', color='#9ca3af')
  273. ax.tick_params(colors='#9ca3af')
  274. ax.spines['bottom'].set_color('#30363d')
  275. ax.spines['left'].set_color('#30363d')
  276. ax.spines['top'].set_visible(False)
  277. ax.spines['right'].set_visible(False)
  278. ax.grid(True, alpha=0.2, color='#30363d')
  279. else:
  280. ax.text(0.5, 0.5, 'No data available', ha='center', va='center',
  281. transform=ax.transAxes, color='#9ca3af', fontsize=14)
  282. ax.set_xlim(0, 1)
  283. ax.set_ylim(0, 1)
  284. plt.tight_layout()
  285. # Save to bytes buffer
  286. buf = io.BytesIO()
  287. plt.savefig(buf, format='png', facecolor='#0d1117', edgecolor='none')
  288. plt.close(fig)
  289. buf.seek(0)
  290. return Response(buf.getvalue(), mimetype='image/png')
  291. if __name__ == "__main__":
  292. app.run(host="127.0.0.1", port=5000, debug=True)