explore.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2026 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. """
  18. Blueprint: explorer_bp
  19. This module defines a Flask blueprint (`explore_bp`) for managing the general functionality of the explorer application.
  20. It serves as the primary location for Flask routes related to the home page, search functionality, and other general features.
  21. """
  22. from flask import Blueprint, render_template, request
  23. import rpc
  24. # Create explore blueprint
  25. explore_bp = Blueprint("explore", __name__)
  26. @explore_bp.route('/', methods=["GET"])
  27. async def index():
  28. """
  29. Fetches and displays the explorer home page content using multiple RPC calls to the
  30. explorer daemon, retrieving the last 10 blocks, basic statistics, metric statistics,
  31. and DarkFi native contracts.
  32. Upon success, it returns a rendered template with recent blocks, basic statistics,
  33. latest metric statistics (if available), and native contracts.
  34. """
  35. # Fetch the latest 10 blocks
  36. blocks = await rpc.get_last_n_blocks(10)
  37. # Retrieve basic statistics summarizing the overall chain data
  38. basic_stats = await rpc.get_basic_statistics()
  39. # Fetch the metric statistics
  40. metric_stats = await rpc.get_metric_statistics()
  41. # Determine if metrics exist
  42. has_metrics = metric_stats and isinstance(metric_stats, list)
  43. # Get the latest metric statistics or return empty metrics
  44. latest_metric_stats = metric_stats[-1] if has_metrics else [0] * 15
  45. # Retrieve the native contracts
  46. native_contracts = await rpc.get_native_contracts()
  47. # Render the explorer home page
  48. return render_template(
  49. 'index.html',
  50. blocks=blocks,
  51. basic_stats=basic_stats,
  52. metric_stats=latest_metric_stats,
  53. native_contracts=native_contracts,
  54. )
  55. @explore_bp.route('/search', methods=['GET', 'POST'])
  56. async def search():
  57. """
  58. Searches for a block or transaction based on the provided hash using RPC calls to the
  59. explorer daemon. It retrieves relevant data using the search hash from provided query parameter,
  60. returning a rendered template that displays either block details with associated transactions
  61. or transaction details, depending on the search result.
  62. Query Params:
  63. search_hash (str): The hash of the block or transaction to search for.
  64. """
  65. # Get the search hash
  66. search_hash = request.args.get('search_hash', '')
  67. # Fetch the block corresponding to the search hash
  68. block = await rpc.get_block(search_hash)
  69. # Fetch transactions associated with the block
  70. transactions = await rpc.get_block_transactions(search_hash)
  71. if transactions:
  72. # Render block details with associated transactions if found
  73. return render_template('block.html', block=block, transactions=transactions)
  74. else:
  75. # Fetch transaction details if no transactions are found for the block
  76. transaction = await rpc.get_transaction(search_hash)
  77. return render_template('transaction.html', transaction=transaction)