Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readme update #7

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
eae879f
finish task() step
SomaKoii Mar 29, 2023
be7ad78
update fetchsubmisstion
SomaKoii Mar 29, 2023
6fdd693
add validate function
SomaKoii Mar 29, 2023
51a151e
update config
SomaKoii Mar 29, 2023
dbac6b6
update test
SomaKoii Mar 29, 2023
790a94f
update task id
SomaKoii Mar 29, 2023
bbb407a
update
SomaKoii Mar 30, 2023
94d25ae
update testing code
SomaKoii Mar 30, 2023
e5f3be2
update testing code
SomaKoii Mar 30, 2023
04038dc
update testing code
SomaKoii Mar 30, 2023
656d035
update cidValidation
SomaKoii Mar 30, 2023
5b674e7
update testing
SomaKoii Mar 30, 2023
5b0f5f5
fix a bug
SomaKoii Mar 31, 2023
d94d3d3
update test code
SomaKoii Mar 31, 2023
6fea835
validation pass
SomaKoii Mar 31, 2023
2d3dd70
update code params
SomaKoii Mar 31, 2023
ae179d1
update syed code
SomaKoii Mar 31, 2023
bc044b4
updates
SomaKoii Mar 31, 2023
dc2a7a5
update coreLogic
SomaKoii Mar 31, 2023
10ca612
update coreLogic
SomaKoii Mar 31, 2023
585912c
update testing code
SomaKoii Mar 31, 2023
c0df130
setting up log listening
SomaKoii Mar 31, 2023
15c6f6b
Added the get-logs endpoint
SyedGhazanferAnwar Apr 1, 2023
ae94893
Merge pull request #3 from somali0128/linktree/js
somali0128 Apr 3, 2023
acfacc6
remove soma's web3 token key
SomaKoii Apr 3, 2023
d450acc
debug task node issue
SomaKoii Apr 4, 2023
6d933b6
comment testing timer
SomaKoii Apr 4, 2023
2a7ace4
update path
SomaKoii Apr 4, 2023
ed33d4e
update localdb get
SomaKoii Apr 4, 2023
4bf6e4d
update docker code
SomaKoii Apr 4, 2023
e7d2d74
update testdata and apply merge data
SomaKoii Apr 10, 2023
b350b0d
remove unused test
SomaKoii Apr 10, 2023
154a6fe
seperate linktree and proofs
SomaKoii Apr 10, 2023
4132151
update endpoint
SomaKoii Apr 11, 2023
8bbf6ca
update gitignore
SomaKoii Apr 11, 2023
7442482
fix minor bugs
SomaKoii Apr 11, 2023
0b49f30
update submit list and return cid
SomaKoii Apr 11, 2023
2acde9d
update localdb format
SomaKoii Apr 11, 2023
86964dc
update db model and testin
SomaKoii Apr 11, 2023
04c4821
fix dbmodel bug
SomaKoii Apr 12, 2023
003abbe
replace index store to db
SomaKoii Apr 12, 2023
d00d7e5
update dbmodel and index.js, more endpoint
SomaKoii Apr 12, 2023
04b9d94
more endpoints
SomaKoii Apr 12, 2023
546cfe8
update CoreLogic using db-model
SomaKoii Apr 12, 2023
e05524b
remove unused test file
SomaKoii Apr 12, 2023
0bfe2e6
add db for authlist, update cid validation
SomaKoii Apr 12, 2023
ff06dd8
update endpoint for authlist
SomaKoii Apr 12, 2023
e04b4c9
update db
SomaKoii Apr 12, 2023
d017edc
code review
SomaKoii Apr 12, 2023
8ee8b27
minor fix
SomaKoii Apr 12, 2023
84cc6b9
update testing
SomaKoii Apr 13, 2023
d5d4c9f
update testing code
SomaKoii Apr 13, 2023
f7aae26
update for compose test
SomaKoii Apr 13, 2023
277b4d5
fix some code
SomaKoii Apr 14, 2023
d12b740
fix round submission
SomaKoii Apr 14, 2023
f4de512
update namespace Wrapper
SomaKoii Apr 14, 2023
5f57e59
update code
SomaKoii Apr 14, 2023
f8ca8bc
debug code
SomaKoii Apr 14, 2023
18f7716
debug code
SomaKoii Apr 14, 2023
0b45757
update distribution list
SomaKoii Apr 17, 2023
71c7b3a
Merge pull request #5 from somali0128/update/distribution-list
somali0128 Apr 18, 2023
420c08e
Merge pull request #6 from somali0128/@feature/code-review
somali0128 Apr 18, 2023
df9acce
uncommon function
SomaKoii Apr 18, 2023
ebb8d6b
updated the readme
saimkoii Apr 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .env-local
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ NODE_MODE="service"
# The nodes address
SERVICE_URL="http://localhost:8080"
# For CI/CD purpose to automate the staking wallet creation
INITIAL_STAKING_WALLET_BALANCE=1
INITIAL_STAKING_WALLET_BALANCE=15
# Intial balance for the distribution wallet which will be used to hold the distribution list.
INITIAL_DISTRIBUTION_WALLET_BALANCE=1
# Global timers which track the round time, submission window and audit window and call those functions
Expand All @@ -35,16 +35,13 @@ K2_NODE_URL="https://k2-testnet.koii.live"
# registering with the crete-task-cli. This variable supports a comma separated list:
# TASKS="id1,id2,id3"
# TASK_STAKES="1,1,1"
TASKS=""
TASK_STAKES=1
TASKS="7jP87G1LJzWmLrr6RqQcA8bH6spZven4RHxGCgbPFzSo"
TASK_STAKES=10

# User can enter as many environment variables as they like below. These can be task
# specific variables that are needed for the task to perform it's job. Some examples:
# Secrets must follow this convention for task to be able to use it (SECRET_<secret name>)
SECRET_WEB3_STORAGE_KEY=""
TWITTER_CONSUMER_KEY=""
TWITTER_CONSUMER_SECRET=""
TWITTER_BEARER_TOKEN=""
SECRET_WEB3_STORAGE_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweGY0ODYxMzAzOTdDNTY1QzlDYTRCOTUzZTA2RWQ4NUI4MGRBQzRkYTIiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjYzNjU1OTk5MDMsIm5hbWUiOiJTb21hIn0.TU-KUFS9vjI9blN5dx6VsLLuIjJnpjPrxDHBvjXQUxw"



6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ data/*
executables/*
namespace/*
config/*
taskStateInfoKeypair.json
localKOIIDB
.env
taskStateInfoKeypair.json
taskStateInfoKeypair.json
linktrees
test/testdb
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "avoid",
"endOfLine": "auto"
}
90 changes: 61 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,91 @@
# K2-Task-Template
# KOII LINKTREE TASK

LinkTree is a koii task that creates a LinkTree page. You can add links to your favorite websites or social media profiles on your page.

Tasks run following a periodic structure of 'rounds':

![Screenshot_20230307-091958](https://user-images.githubusercontent.com/66934242/223565192-3ecce9c6-0f9a-4a58-8b02-2db19c61141f.png)
executeTask => {

1. Do the work
2. Audit the work of other nodes
3. Pay the rewards or slash stake

}

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](https://docs.koii.network/microservices-and-tasks/what-are-tasks/gradual-consensus).
## The structure of the LinkTree task

If this is your first time writing a Koii Task, you might want to use the [task organizer](https://www.figma.com/community/file/1220194939977550205/Task-Outline).
## coreLogic.js

## Requirements
- [Node >=16.0.0](https://nodejs.org)
- [Docker compose](https://docs.docker.com/compose/install/docker)
The most important file in any koii task is the coreLogic.js file. It is provided in the task template and is where most of the functionality will be coded.

## What's in the template?
`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
In the Linktree task’s coreLogic, the first function is the task(). This function calls the linktree_task function from the linktree_task.js file.

## Linktree_task => this function fetches proofs from the local database, creates a submission object for those proofs, and uploads it to IPFS. IPFS gives back a reference CID of the submission, which the function returns.

`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
The task function gets the CID from the linktree_task and stores it to the levelDB.

`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.
The fetchSubmission function can be used to get the CID of the submission back.

## Runtime Options
There are two ways to run your task when doing development:
The generateDistributionList function generates a distribution list for rewards based on submissions made by the nodes. It takes all the votes that the nodes have made on the submission and if the false votes are higher than the true votes, this function slashes 70% of the stake of the task submitter as a penalty. If the true votes are higher than the submission is valid and it distributes the rewards.

1. 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.
The submitDistributionList function submits the distribution list generated by the "generateDistributionList" function.

2. With Timer OFF - This allows you to do manual calls to K2 and disables the triggers for round managemnt on K2. Transactions are only accepted during the correct period. Guide for manual calls is in index.js
The validateNode function is called when a node is selected to validate the submission value. It calls the linktree_validate function from the linktree_validate file.

## Linktree_validate.js => this file verifies the validity of a Linktree CID submission.


The validateDistribution function validates a distribution list submitted by a node for a specific round.

The submitTask function submits the address with a distribution list to K2.

# Modifying CoreLogic.js
Task nodes will trigger a set of predefined functions during operation.
The auditTask function checks a submission in a specific round and confirms that the task is valid

There are in total 9 functions in CoreLogic which the you can modify according to your needs:
The auditDistribution checks the distribution list is valid for a specific round

1. *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.
## Db_model.js

2. *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.
This file contains all the functions required for storing and retrieving data related to the linktree and node proofs used in the Linktree CID Validation task as well as the authorized users list.

3. *submitTask()* - It makes the call to namespace function of task-node using the wrapper.
getLinktree: retrieves the linktree by it’s public key
setLinktree: sets the linktree associated with the given public key
getAllLinktrees: retrieves all link trees stored in the database
getProofs: retrieves the proofs associated with a given public key
setProofs: sets the proofs associated with a given public key
getAllProofs: retrieves all proofs stored in the database
getNodeProofCid: retrieves the CID associated with a given round of node proofs
setNodeProofCid: sets the CID associated with a given round of node proofs
getAllNodeProofCids: retrieves all CIDs associated with node proofs stored in the database
getAuthList: retrieves the list of the authorized users
setAuthList: sets the authorized list
getAllAuthLists: retrieves all authorized lists stored in the database
namespaceWrapper.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.

4. *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()
## Index.js

5. *submitDistributionList()* - makes call to the namesapce function of task-node to upload the list and on succesful upload does the transaction to update the state.
This is the entry point of the task. It sets the round timers and ties together all the other parts of the task

6. *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.
## The test folder

7. *validateDistribution()* - The logic to validate the distribution list goes here and the function will receive the distribution list submitted form task-state.
This folder contains all the test files for the linktree task.

8. *auditTask()* - makes call to namespace of task-node to raise an audit against the submission value if the validation fails.
## Router.js

This file contains a set of API endpoints for the linktree.


## Runtime Options
There are two ways to run your task when doing development:

1. 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.

2. With Timer OFF - This allows you to do manual calls to K2 and disables the triggers for round managemnt on K2. Transactions are only accepted during the correct period. Guide for manual calls is in index.js

9. *auditDistribution()* - makes call to namespace of task-node to raise an audit against the distribution list if the validation fails.

# Testing and Deploying
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 you begin this process, be sure to check your code and write unit tests wherever possible to verify individual core logic functions. `unitTest.js` file helps you to mock task state parameters that are required in core logic function and test it. 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.

## Build
Before deploying a task, you'll need to build it into a single file executable by running
Expand Down
8 changes: 4 additions & 4 deletions config-task.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Name and desciption sof your task
task_name: "test-task"
task_description: "This task is to test out the namespace function"
task_name: "linktree-task"
task_description: "linktree-task"
# network value can be DEVELOPMENT , ARWEAVE or IPFS
task_executable_network: "DEVELOPMENT"
# Provide your web.storage key in case of IPFS otherwise leave blank
secret_web3_storage_key: ""
secret_web3_storage_key: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweGY0ODYxMzAzOTdDNTY1QzlDYTRCOTUzZTA2RWQ4NUI4MGRBQzRkYTIiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjYzNjU1OTk5MDMsIm5hbWUiOiJTb21hIn0.TU-KUFS9vjI9blN5dx6VsLLuIjJnpjPrxDHBvjXQUxw"

# Path to your executable webpack if the selected network is IPFS otherwise leave blank
task_audit_program: ""
task_audit_program: "/home/soma/code-ie/task-template-linktree/dist/main.js"

# Provide your transaction ID in case of ARWEAVE and in case of DEVELOPMENT give your executable name as main otherwise leave blank
task_audit_program_id: "main"
Expand Down
Loading