transfer from monorepo

This commit is contained in:
Laura Abro
2025-04-24 10:24:42 -03:00
parent a2175785d5
commit 1c6fc5540b
95 changed files with 7110 additions and 0 deletions

View File

@ -0,0 +1,4 @@
export async function setup(): Promise<void> {
// define any steps that must be executed before the task starts
console.log("CUSTOM SETUP");
}

215
worker/src/task/1-task.ts Normal file
View File

@ -0,0 +1,215 @@
import { getOrcaClient } from "@_koii/task-manager/extensions";
import { namespaceWrapper, TASK_ID } from "@_koii/namespace-wrapper";
import "dotenv/config";
import { getRandomNodes } from "../utils/leader";
import { getExistingIssues } from "../utils/existingIssues";
import { status, middleServerUrl } from "../utils/constant";
import dotenv from "dotenv";
import { checkAnthropicAPIKey, isValidAnthropicApiKey } from "../utils/anthropicCheck";
import { checkGitHub } from "../utils/githubCheck";
import { LogLevel } from "@_koii/namespace-wrapper/dist/types";
import { actionMessage } from "../utils/constant";
import { errorMessage } from "../utils/constant";
dotenv.config();
export async function task(roundNumber: number): Promise<void> {
/**
* Run your task and store the proofs to be submitted for auditing
* It is expected you will store the proofs in your container
* The submission of the proofs is done in the submission function
*/
// FORCE TO PAUSE 30 SECONDS
// No submission on Round 0 so no need to trigger fetch audit result before round 3
// Changed from 3 to 4 to have more time
if (roundNumber >= 4) {
const triggerFetchAuditResult = await fetch(`${middleServerUrl}/api/builder/summarizer/trigger-fetch-audit-result`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ taskId: TASK_ID, round: roundNumber - 4 })
});
console.log(`[TASK] Trigger fetch audit result for round ${roundNumber - 3}. Result is ${triggerFetchAuditResult.status}.`);
}
console.log(`[TASK] EXECUTE TASK FOR ROUND ${roundNumber}`);
try {
const orcaClient = await getOrcaClient();
// check if the env variable is valid
if (!process.env.ANTHROPIC_API_KEY) {
await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.ANTHROPIC_API_KEY_INVALID, actionMessage.ANTHROPIC_API_KEY_INVALID);
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ANTHROPIC_API_KEY_INVALID);
return;
}
if (!isValidAnthropicApiKey(process.env.ANTHROPIC_API_KEY!)) {
await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.ANTHROPIC_API_KEY_INVALID, actionMessage.ANTHROPIC_API_KEY_INVALID);
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ANTHROPIC_API_KEY_INVALID);
return;
}
const isAnthropicAPIKeyValid = await checkAnthropicAPIKey(process.env.ANTHROPIC_API_KEY!);
if (!isAnthropicAPIKeyValid) {
await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.ANTHROPIC_API_KEY_NO_CREDIT, actionMessage.ANTHROPIC_API_KEY_NO_CREDIT);
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ANTHROPIC_API_KEY_NO_CREDIT);
return;
}
if (!process.env.GITHUB_USERNAME || !process.env.GITHUB_TOKEN) {
await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.GITHUB_CHECK_FAILED, actionMessage.GITHUB_CHECK_FAILED);
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.GITHUB_CHECK_FAILED);
return;
}
const isGitHubValid = await checkGitHub(process.env.GITHUB_USERNAME!, process.env.GITHUB_TOKEN!);
if (!isGitHubValid) {
await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.GITHUB_CHECK_FAILED, actionMessage.GITHUB_CHECK_FAILED);
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.GITHUB_CHECK_FAILED);
return;
}
if (!orcaClient) {
await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.NO_ORCA_CLIENT, actionMessage.NO_ORCA_CLIENT);
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.NO_ORCA_CLIENT);
return;
}
const stakingKeypair = await namespaceWrapper.getSubmitterAccount();
if (!stakingKeypair) {
throw new Error("No staking keypair found");
}
const stakingKey = stakingKeypair.publicKey.toBase58();
const pubKey = await namespaceWrapper.getMainAccountPubkey();
if (!pubKey) {
throw new Error("No public key found");
}
/****************** All issues need to be starred ******************/
const existingIssues = await getExistingIssues();
const githubUrls = existingIssues.map((issue) => issue.githubUrl);
try {
await orcaClient.podCall(`star/${roundNumber}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ taskId: TASK_ID, round_number: String(roundNumber), github_urls: githubUrls }),
});
} catch (error) {
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.STAR_ISSUE_FAILED);
console.error("Error starring issues:", error);
}
/****************** All these issues need to be generate a markdown file ******************/
const signature = await namespaceWrapper.payloadSigning(
{
taskId: TASK_ID,
roundNumber: roundNumber,
action: "fetch",
githubUsername: stakingKey,
stakingKey: stakingKey
},
stakingKeypair.secretKey,
);
// const initializedDocumentSummarizeIssues = await getInitializedDocumentSummarizeIssues(existingIssues);
console.log(`[TASK] Making Request to Middle Server with taskId: ${TASK_ID} and round: ${roundNumber}`);
const requiredWorkResponse = await fetch(`${middleServerUrl}/api/builder/summarizer/fetch-summarizer-todo`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ signature: signature, stakingKey: stakingKey }),
});
// check if the response is 200
if (requiredWorkResponse.status !== 200) {
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.NO_ISSUES_PENDING_TO_BE_SUMMARIZED);
return;
}
const requiredWorkResponseData = await requiredWorkResponse.json();
console.log("[TASK] requiredWorkResponseData: ", requiredWorkResponseData);
const jsonBody = {
taskId: TASK_ID,
round_number: String(roundNumber),
repo_url: `https://github.com/${requiredWorkResponseData.data.repo_owner}/${requiredWorkResponseData.data.repo_name}`,
};
console.log("[TASK] jsonBody: ", jsonBody);
try {
const repoSummaryResponse = await orcaClient.podCall(`repo_summary/${roundNumber}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(jsonBody),
});
console.log("[TASK] repoSummaryResponse: ", repoSummaryResponse);
console.log("[TASK] repoSummaryResponse.data.result.data ", repoSummaryResponse.data.result.data);
const payload = {
taskId: TASK_ID,
action: "add",
roundNumber: roundNumber,
prUrl: repoSummaryResponse.data.result.data.pr_url,
stakingKey: stakingKey
}
console.log("[TASK] Signing payload: ", payload);
if (repoSummaryResponse.status === 200) {
try{
const signature = await namespaceWrapper.payloadSigning(
payload,
stakingKeypair.secretKey,
);
console.log("[TASK] signature: ", signature);
const addPrToSummarizerTodoResponse = await fetch(`${middleServerUrl}/api/builder/summarizer/add-pr-to-summarizer-todo`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ signature: signature, stakingKey: stakingKey }),
});
console.log("[TASK] addPrToSummarizerTodoResponse: ", addPrToSummarizerTodoResponse);
}catch(error){
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_FAILED_TO_ADD_PR_TO_SUMMARIZER_TODO);
console.error("[TASK] Error adding PR to summarizer todo:", error);
}
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_SUCCESSFULLY_SUMMARIZED);
} else {
// post this summary response to slack` to notify the team
// THE HOOK IS ALREADY DISABLED
// try{
// const slackResponse = await fetch('https://hooks.slack.com/services/', {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// text: `[TASK] Error summarizing issue:\nStatus: ${repoSummaryResponse.status}\nData: ${JSON.stringify(repoSummaryResponse.data, null, 2)}`
// }),
// });
// console.log("[TASK] slackResponse: ", slackResponse);
// }catch(error){
// console.error("[TASK] Error posting to slack:", error);
// }
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_FAILED_TO_BE_SUMMARIZED);
}
} catch (error) {
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_FAILED_TO_BE_SUMMARIZED);
// try{
// const slackResponse = await fetch('https://hooks.slack.com/services', {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// text: `[TASK] Error summarizing issue:\n ${JSON.stringify(error)}`
// }),
// });
// console.log("[TASK] slackResponse: ", slackResponse);
// }catch(error){
// console.error("[TASK] Error posting to slack:", error);
// }
console.error("[TASK] EXECUTE TASK ERROR:", error);
}
} catch (error) {
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.UNKNOWN_ERROR);
console.error("[TASK] EXECUTE TASK ERROR:", error);
}
}

View File

@ -0,0 +1,102 @@
import { storeFile } from "../utils/ipfs";
import { getOrcaClient } from "@_koii/task-manager/extensions";
import { namespaceWrapper, TASK_ID } from "@_koii/namespace-wrapper";
import { status } from "../utils/constant";
export async function submission(roundNumber: number) : Promise<string | void> {
/**
* Retrieve the task proofs from your container and submit for auditing
* Must return a string of max 512 bytes to be submitted on chain
* The default implementation handles uploading the proofs to IPFS
* and returning the CID
*/
console.log(`[SUBMISSION] Starting submission process for round ${roundNumber}`);
try {
console.log("[SUBMISSION] Initializing Orca client...");
const orcaClient = await getOrcaClient();
if (!orcaClient) {
console.error("[SUBMISSION] Failed to initialize Orca client");
return;
}
console.log("[SUBMISSION] Orca client initialized successfully");
console.log(`[SUBMISSION] Fetching task result for round ${roundNumber}...`);
const taskResult = await namespaceWrapper.storeGet(`result-${roundNumber}`);
if (!taskResult) {
console.log("[SUBMISSION] No task result found for this round");
return status.NO_DATA_FOR_THIS_ROUND;
}
console.log(`[SUBMISSION] Task result status: ${taskResult}`);
if (taskResult !== status.ISSUE_SUCCESSFULLY_SUMMARIZED) {
console.log(`[SUBMISSION] Task not successfully summarized. Status: ${taskResult}`);
return taskResult;
}
console.log(`[SUBMISSION] Fetching submission data for round ${roundNumber}...`);
const result = await orcaClient.podCall(`submission/${roundNumber}`);
let submission;
console.log("[SUBMISSION] Submission result:", result.data);
if (result.data === "No submission") {
console.log("[SUBMISSION] No existing submission found, creating new submission object");
submission = {
githubUsername: process.env.GITHUB_USERNAME,
prUrl: "none",
roundNumber,
};
} else {
submission = result.data;
}
console.log("[SUBMISSION] Validating submission data...");
if (submission.roundNumber !== roundNumber) {
console.error(`[SUBMISSION] Round number mismatch. Expected: ${roundNumber}, Got: ${submission.roundNumber}`);
throw new Error("Submission is not for the current round");
}
if (!submission.prUrl) {
console.error("[SUBMISSION] Missing PR URL in submission");
throw new Error("Submission is missing PR URL");
}
console.log("[SUBMISSION] Submission data validated successfully:", submission);
console.log("[SUBMISSION] Getting submitter account...");
const stakingKeypair = await namespaceWrapper.getSubmitterAccount();
if (!stakingKeypair) {
console.error("[SUBMISSION] No staking keypair found");
throw new Error("No staking keypair found");
}
console.log("[SUBMISSION] Submitter account retrieved successfully");
const stakingKey = stakingKeypair.publicKey.toBase58();
const pubKey = await namespaceWrapper.getMainAccountPubkey();
console.log("[SUBMISSION] Staking key:", stakingKey);
console.log("[SUBMISSION] Public key:", pubKey);
console.log("[SUBMISSION] Signing submission payload...");
const signature = await namespaceWrapper.payloadSigning(
{
taskId: TASK_ID,
roundNumber,
stakingKey,
pubKey,
action: "audit",
...submission,
},
stakingKeypair.secretKey,
);
console.log("[SUBMISSION] Payload signed successfully");
console.log("[SUBMISSION] Storing submission on IPFS...");
const cid = await storeFile({ signature }, "submission.json");
console.log("[SUBMISSION] Submission stored successfully. CID:", cid);
return cid || void 0;
} catch (error) {
console.error("[SUBMISSION] Error during submission process:", error);
throw error;
}
}

View File

@ -0,0 +1,84 @@
import { getOrcaClient } from "@_koii/task-manager/extensions";
import { middleServerUrl, status } from "../utils/constant";
import { submissionJSONSignatureDecode } from "../utils/submissionJSONSignatureDecode";
// import { status } from '../utils/constant'
export async function audit(cid: string, roundNumber: number, submitterKey: string): Promise<boolean | void> {
/**
* Audit a submission
* This function should return true if the submission is correct, false otherwise
* The default implementation retrieves the proofs from IPFS
* and sends them to your container for auditing
*/
try {
const orcaClient = await getOrcaClient();
if (!orcaClient) {
// await namespaceWrapper.storeSet(`result-${roundNumber}`, status.NO_ORCA_CLIENT);
return;
}
// Check if the cid is one of the status
if (Object.values(status).includes(cid)) {
// This returns a dummy true
return true;
}
const decodeResult = await submissionJSONSignatureDecode({submission_value: cid, submitterPublicKey: submitterKey, roundNumber: roundNumber});
if (!decodeResult) {
console.log("[AUDIT] DECODE RESULT FAILED.")
return false;
}
console.log(`[AUDIT] ✅ Signature decoded successfully`);
console.log(`[AUDIT] Checking summarizer status for submitter ${submitterKey}`);
const checkSummarizerResponse = await fetch(`${middleServerUrl}/api/builder/summarizer/check-summarizer`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
stakingKey: submitterKey,
roundNumber,
githubUsername: decodeResult.githubUsername,
prUrl: decodeResult.prUrl
}),
});
const checkSummarizerJSON = await checkSummarizerResponse.json();
console.log(`[AUDIT] Summarizer check response:`, checkSummarizerJSON);
if (!checkSummarizerJSON.success) {
console.log(`[AUDIT] ❌ Audit failed for ${submitterKey}`);
return false;
}
console.log(`[AUDIT] ✅ Summarizer check passed`);
console.log(`[AUDIT] Sending audit request for submitter: ${submitterKey}`);
console.log(`[AUDIT] Submission data being sent to audit:`, decodeResult);
const result = await orcaClient.podCall(`audit/${roundNumber}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
submission: decodeResult,
}),
});
console.log(`[AUDIT] Raw audit result:`, result);
console.log(`[AUDIT] Audit result data type:`, typeof result.data);
console.log(`[AUDIT] Audit result data value:`, result.data);
if (result.data === true) {
console.log(`[AUDIT] ✅ Audit passed for ${submitterKey}`);
return true;
} else {
console.log(`[AUDIT] ❌ Audit failed for ${submitterKey}`);
console.log(`[AUDIT] Failed audit result data:`, result.data);
return false;
}
} catch (error) {
console.error("[AUDIT] Error auditing submission:", error);
// When Error---NO RETURN;
// return true;
}
}

View File

@ -0,0 +1,64 @@
import { Submitter, DistributionList } from "@_koii/task-manager";
import { namespaceWrapper, TASK_ID } from "@_koii/namespace-wrapper";
import { customReward, status } from "../utils/constant";
import { Submission } from "@_koii/namespace-wrapper/dist/types";
import { middleServerUrl } from "../utils/constant";
import { getOrcaClient } from "@_koii/task-manager/extensions";
import { submissionJSONSignatureDecode } from "../utils/submissionJSONSignatureDecode";
import { getRandomNodes } from "../utils/leader";
const getSubmissionList = async (roundNumber: number): Promise<Record<string, Submission>> => {
const submissionInfo = await namespaceWrapper.getTaskSubmissionInfo(roundNumber);
return submissionInfo?.submissions[roundNumber] || {};
}
export const getEmptyDistributionList = async (
submitters: Submitter[],
): Promise<DistributionList> => {
const distributionList: DistributionList = {};
for (const submitter of submitters) {
distributionList[submitter.publicKey] = 0;
}
return distributionList;
}
export const distribution = async (
submitters: Submitter[],
bounty: number,
roundNumber: number,
): Promise<DistributionList> => {
try {
const distributionList: DistributionList = {};
for (const submitter of submitters) {
console.log(`\n[DISTRIBUTION] Processing submitter: ${submitter.publicKey}`);
console.log(`[DISTRIBUTION] Getting submission list for round ${roundNumber}`);
const submitterSubmissions = await getSubmissionList(roundNumber);
console.log(`[DISTRIBUTION] Total submissions found: ${Object.keys(submitterSubmissions).length}`);
const submitterSubmission = submitterSubmissions[submitter.publicKey];
if (!submitterSubmission || submitterSubmission.submission_value === "") {
console.log(`[DISTRIBUTION] ❌ No valid submission found for submitter ${submitter.publicKey}`);
distributionList[submitter.publicKey] = 0;
continue;
}
if (Object.values(status).includes(submitterSubmission.submission_value)) {
distributionList[submitter.publicKey] = 0;
continue;
}else{
// TODO: Check if I should include = 0 here
if (submitter.votes >= 0) {
distributionList[submitter.publicKey] = customReward;
}else{
distributionList[submitter.publicKey] = 0;
}
}
}
console.log(`[DISTRIBUTION] ✅ Distribution completed successfully`);
console.log(`[DISTRIBUTION] Final distribution list:`, distributionList);
return distributionList;
} catch (error: any) {
console.error(`[DISTRIBUTION] ❌ ERROR IN DISTRIBUTION:`, error);
console.error(`[DISTRIBUTION] Error stack:`, error.stack);
return {};
}
};

View File

@ -0,0 +1,57 @@
import { namespaceWrapper, app } from "@_koii/task-manager/namespace-wrapper";
import { getLeaderNode, getRandomNodes } from "../utils/leader";
import { task } from "./1-task";
import { submission } from "./2-submission";
import { audit } from "./3-audit";
import { distribution } from "./4-distribution";
import { submissionJSONSignatureDecode } from "../utils/submissionJSONSignatureDecode";
import { Submission } from "@_koii/namespace-wrapper/dist/types";
import { taskRunner } from "@_koii/task-manager"
/**
*
* Define all your custom routes here
*
*/
//Example route
export async function routes() {
app.get("/value", async (_req, res) => {
const value = await namespaceWrapper.storeGet("value");
console.log("value", value);
res.status(200).json({ value: value });
});
app.get("/leader/:roundNumber/:submitterPublicKey", async (req, res) => {
const roundNumber = req.params.roundNumber;
const submitterPublicKey = req.params.submitterPublicKey;
const {isLeader, leaderNode} = await getLeaderNode({roundNumber: Number(roundNumber), submitterPublicKey: submitterPublicKey});
res.status(200).json({ isLeader: isLeader, leaderNode: leaderNode });
});
app.get("/task/:roundNumber", async (req, res) => {
console.log("task endpoint called with round number: ", req.params.roundNumber);
const roundNumber = req.params.roundNumber;
const taskResult = await task(Number(roundNumber));
res.status(200).json({ result: taskResult });
});
app.get("/audit/:roundNumber/:cid/:submitterPublicKey", async (req, res) => {
const cid = req.params.cid;
const roundNumber = req.params.roundNumber;
const submitterPublicKey = req.params.submitterPublicKey;
const auditResult = await audit(cid, Number(roundNumber), submitterPublicKey);
res.status(200).json({ result: auditResult });
});
app.get("/submission/:roundNumber", async (req, res) => {
const roundNumber = req.params.roundNumber;
const submissionResult = await submission(Number(roundNumber));
res.status(200).json({ result: submissionResult });
});
app.get("/submitDistribution/:roundNumber", async (req, res) => {
const roundNumber = req.params.roundNumber;
const submitDistributionResult = await taskRunner.submitDistributionList(Number(roundNumber));
res.status(200).json({ result: submitDistributionResult });
});
}