K2 Task Template
Last updated
Last updated
Tasks run following a periodic structure of "rounds":
Each round is set by a specific time period, and nodes participate by uploading data to IPFS, posting CIDs to the K2 settlement layer, and sending messages across REST APIs and WebSockets.
For more information on how the Task Flow works, check out the runtime environment docs.
index.js
— is the hub of your app, and ties together the other pieces. This will be the entrypoint when your task runs on Task Nodes.
NamespaceWrappers.js
— contains the interfaces to make API calls to the core of the task-node. It contains all the necessary functions required to submit and audit the work, as well as the distribution lists. Check here to learn more Namespace functions.
coreLogic.js
— is where you'll define your task, audit, and distribution logic, and controls the majority of task functionality. You can of course break out separate features into sub-files and import them into the core logic before web-packing.
When a task is executed, it goes through different phases that occur consecutively, and during each phase, a function is required to be run. Timers are IPC calls on the task-node that call events on the task during a particular time/slot during a task execution period.
For example, after off-chain work has been done and a node submits its result on-chain, the timers trigger the next phase, which is the audit phase for verification of the submitted result.
The index.js
file has a function setup()
that is responsible for switching between phases based on the current events and also calling the necessary function for each phase.
There are two ways to run your task when doing development:
With Timer ON (see .env-local
)- When the timer is ON, IPC calls are made by calculating the average time slots of all the task running your node.
With Timer OFF - This allows you to do manual calls to K2 and disables the triggers for round management on K2. Transactions are only accepted during the correct period. Guide for manual calls is in index.js
Task nodes will trigger a set of predefined functions during operation.
There are in total 9 functions in CoreLogic which the you can modify according to your needs:
task()
- The logic for what your task should do goes here. There is a window in round that is dedicated to do work. The code in task is executed in that window.
fetchSubmission()
- After completing the task , the results/work will be stored somewhere like on IPFS or local levelDB. This function is the place where you can write the logic to fetch that work. It is called in submitTask()
function which does the actual submission on K2.
submitTask()
- It makes the call to namespace function of task-node using the wrapper.
generateDistributionList()
- You have full freedom to prepare your reward distributions as you like and the logic for that goes here. We have provided a sample logic that rewards 1 KOII to all the needs who did the correct submission for that round. This function is called in submitDistributionList()
submitDistributionList()
- makes call to the namespace function of task-node to upload the list and on successful upload does the transaction to update the state.
validateNode()
- this function is called to verify the submission value, so based on the value received from the task-state we can vote on the submission.
validateDistribution()
- The logic to validate the distribution list goes here and the function will receive the distribution list submitted form task-state.
auditTask()
- makes call to namespace of task-node to raise an audit against the submission value if the validation fails.
auditDistribution()
- makes call to namespace of task-node to raise an audit against the distribution list if the validation fails.
Before you begin this process, be sure to check your code and write unit tests wherever possible to verify individual core logic functions. Testing using the docker container should be mostly used for consensus flows, as it will take longer to rebuild and re-deploy the docker container.
Before deploying a task, you'll need to build it into a single file executable by running yarn webpack
Complete the following to deploy your task on the k2 testnet and test it locally with docker compose.
If you have already created an account on web3.storage you'll just need to enter the API key after the prompts in the deployment process.
If you have already generated a Koii wallet on your filesystem you can obtain the path to it by running koii config get
which should return something similar to the following:
The Keypair Path
will be used to pay gas fees and fund your bounty wallet by inputting it into the task CLI.
If you need to create a Koii wallet you can follow the instructions here. Make sure to either copy your keypair path from the output, or use the method above to supply the task CLI with the proper wallet path.
To test the task with the K2 Settlement Layer you'll need to deploy it.
We have included our CLI for creating and publish tasks to the K2 network in this repo. Tips on this flow can be found in the docs. One important thing to note is when you're presented with the choice of ARWEAVE, IPFS, or DEVELOPMENT you can select DEVELOPMENT and enter main
in the following prompt. This will tell the task node to look for a main.js
file in the dist
folder. You can create this locally by running yarn webpack
.
If you want to get a closer look at the console and test environment variables, you'll want to use the included docker-compose stack to run a task node locally.
Link or copy your wallet into the config
folder as id.json
Open .env-local
and add your TaskID you obtained after deploying to K2 into the TASKS
environment variable.
Run docker compose up
and watch the output of the task_node
. You can exit this process when your task has finished, or any other time if you have a long running persistent task.
You do not need to publish your task every time you make modifications. You do however need to restart the task_node
in order for the latest code to be used. To prepare your code you can run yarn webpack
to create the bundle. If you have a task_node
ruinning already, you can exit it and then run docker compose up
to restart (or start) the node.
Open the .env-local
file and make any modifications you need. You can include environment variables that your task expects to be present here, in case you're using custom secrets.