75 lines
2.8 KiB
Python
75 lines
2.8 KiB
Python
from flask import jsonify, request
|
|
from flask_socketio import SocketIO
|
|
import threading
|
|
from multiprocessing import Process, Manager, Event
|
|
import uuid
|
|
import copy
|
|
import time
|
|
from libs.api.apiBlueprint import ApiBlueprint
|
|
|
|
|
|
class Blueprint(ApiBlueprint):
|
|
def __init__(self, nosys_module):
|
|
super().__init__(nosys_module)
|
|
from .miner import Miner
|
|
self.module:Miner = nosys_module
|
|
|
|
def routes(self):
|
|
@self.blueprint.route("/start", methods=["POST"])
|
|
def start_mining():
|
|
data = request.json
|
|
public_key = data.get("public_key")
|
|
force = int(data.get("force", 5))
|
|
nonce_length = data.get("nonce_length", 6)
|
|
|
|
task_id = self.module.start_mining(force, public_key, nonce_length)
|
|
return jsonify({"task_id": task_id})
|
|
|
|
@self.blueprint.route("/status/<task_id>")
|
|
def get_status(task_id):
|
|
task = self.module.tasks.get(task_id)
|
|
if not task:
|
|
return jsonify({"error": "Task not found"}), 404
|
|
|
|
return jsonify(dict(task["result"]))
|
|
|
|
@self.blueprint.route("/pause/<task_id>", methods=["POST"])
|
|
def pause_mining(task_id):
|
|
result = self.module.pause_task(task_id)
|
|
if not result:
|
|
return jsonify({"error": "Task not found"}), 404
|
|
return jsonify({"message": "Mining paused"})
|
|
|
|
@self.blueprint.route("/resume/<task_id>", methods=["POST"])
|
|
def resume_mining(task_id):
|
|
result = self.module.resume_task(task_id)
|
|
if not result:
|
|
return jsonify({"error": "Task not found"}), 404
|
|
return jsonify({"message": "Mining resumed"})
|
|
|
|
@self.blueprint.route("/cancel/<task_id>", methods=["POST"])
|
|
def cancel_mining(task_id):
|
|
result = self.module.cancel_task(task_id)
|
|
if not result:
|
|
return jsonify({"error": "Task not found"}), 404
|
|
return jsonify({"message": "Mining cancelled"})
|
|
|
|
@self.blueprint.route('/list', methods=['GET'])
|
|
def list_all_tasks():
|
|
tasks_info = {}
|
|
|
|
for task_id, task_data in self.module.tasks.items():
|
|
result = task_data["result"]
|
|
tasks_info[task_id] = {
|
|
"status": result.get("status"),
|
|
"attempts": result.get("attempts"),
|
|
"data": result.get("data"),
|
|
"target_force": result.get("target_force"),
|
|
"best_force": result.get("best_force"),
|
|
"duration": result.get("duration"),
|
|
"nonce": result.get("nonce"),
|
|
"best_nonce": result.get("best_nonce"),
|
|
"hash": result.get("hash"),
|
|
"best_hash": result.get("best_hash")
|
|
}
|
|
return jsonify(tasks_info) |