Search…
⌃K
Links

Submit Distribution List

After the nodes have done their off-chain duty and validated each others submissions, the next stage is the distribution stage; where a node is selected to generate and submit a distribution list on-chain.

generateDistributionList()

Following the submission of the list, other participating nodes audit it to ensure its validity; if the distribution list is valid, rewards are distributed among nodes.
async function generateDistributionList(round) {
console.log("GenerateDistributionList called");
console.log("I am selected node");
// Write the logic to generate the distribution list here by introducing the rules of your choice
/* **** SAMPLE LOGIC FOR GENERATING DISTRIBUTION LIST ******/
let distributionList = {};
const taskAccountDataJSON = await namespaceWrapper.getTaskState();
const submissions = taskAccountDataJSON.submissions[round];
const submissions_audit_trigger =
taskAccountDataJSON.submissions_audit_trigger[round];
if (submissions == null) {
console.log("No submisssions found in N-2 round");
return distributionList;
} else {
const keys = Object.keys(submissions);
const values = Object.values(submissions);
const size = values.length;
console.log("Submissions from last round: ", keys, values, size);
for (let i = 0; i < size; i++) {
const candidatePublicKey = keys[i];
if (
submissions_audit_trigger &&
submissions_audit_trigger[candidatePublicKey]
) {
console.log(
submissions_audit_trigger[candidatePublicKey].votes,
"distributions_audit_trigger votes "
);
const votes = submissions_audit_trigger[candidatePublicKey].votes;
let numOfVotes = 0;
for (let index = 0; index < votes.length; index++) {
if (votes[i].is_valid) numOfVotes++;
else numOfVotes--;
}
if (numOfVotes < 0) continue;
}
distributionList[candidatePublicKey] = 1;
}
}
console.log("Distribution List", distributionList);
return distributionList;
}
In the code block above:
  • An empty distribution list is initialized
  • The task's data is retrieved using the getTaskState helper function
  • All valid submissions are fetched from the task's data
  • The submissions_audit_trigger is also retrieved from the task's data; this object contains information about the audited submissions
  • If submissions is null, an empty distribution list is returned. Else, the keys and values of the submissions are grouped separately and in a loop, it calculates the audits on each submission and how many votes on the audit, and based on that it makes a decision.

submitDistributionList()

This function submits the distribution list generated by generateDistributionList() to K2.
async function submitDistributionList(round) {
console.log("SubmitDistributionList called");
const distributionList = await generateDistributionList(round);
const decider = await namespaceWrapper.uploadDistributionList(
distributionList,
round
);
console.log("DECIDER", decider);
if (decider) {
const response = await namespaceWrapper.distributionListSubmissionOnChain(
round
);
console.log("RESPONSE FROM DISTRIBUTION LIST", response);
}
}