File size: 2,973 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { db } from "../config/gun.js";
import { gunSafe } from "../utils/gunUtils.js";
import { broadcastHiveEvent } from "./hiveService.js";
import { economyService } from "./economyService.js";

/**
 * Swarm Compute Service
 * Implements decentralized task distribution and result aggregation.
 */
export const swarmComputeService = {
    /**
     * Publishes a new compute task to the swarm.
     */
    async publishTask(data) {
        const taskId = `swarm-task-${Date.now()}`;
        const taskData = gunSafe({
            id: taskId,
            type: data.type || "HEAVY_PROOF_SEARCH",
            creator: data.agentId,
            description: data.description,
            reward: data.reward || 50,
            status: "ACTIVE",
            totalUnits: data.totalUnits || 10,
            completedUnits: 0,
            timestamp: Date.now()
        });

        db.get("swarm-compute-tasks").get(taskId).put(taskData);
        broadcastHiveEvent('swarm_task_published', { id: taskId, type: taskData.type, reward: taskData.reward });
        return taskId;
    },

    /**
     * Submits a work unit or result for a swarm task.
     */
    async submitResult(taskId, agentId, resultData) {
        return new Promise((resolve) => {
            db.get("swarm-compute-tasks").get(taskId).once(async (task) => {
                if (!task || task.status === "COMPLETED") {
                    resolve({ success: false, error: "TASK_NOT_FOUND_OR_COMPLETED" });
                    return;
                }

                const resultId = `result-${Date.now()}-${agentId}`;
                const resultRecord = gunSafe({
                    agentId,
                    result: resultData,
                    timestamp: Date.now()
                });

                db.get("swarm-compute-tasks").get(taskId).get("results").get(agentId).put(resultRecord);

                const newCompleted = (task.completedUnits || 0) + 1;
                const status = newCompleted >= task.totalUnits ? "COMPLETED" : "ACTIVE";

                db.get("swarm-compute-tasks").get(taskId).put(gunSafe({
                    completedUnits: newCompleted,
                    status
                }));

                // Reward the agent
                await economyService.credit(agentId, task.reward, `Swarm Compute Contribution: ${taskId}`);

                broadcastHiveEvent('swarm_work_submitted', { taskId, agentId, status });
                resolve({ success: true, status, completedUnits: newCompleted });
            });
        });
    },

    /**
     * Gets all active swarm tasks.
     */
    async getActiveTasks() {
        const tasks = [];
        return new Promise((resolve) => {
            db.get("swarm-compute-tasks").map().once((data, id) => {
                if (data && data.status === "ACTIVE") {
                    tasks.push(data);
                }
            });
            setTimeout(() => resolve(tasks), 1000);
        });
    }
};