From e7120f87b1a7b9a177c3fe50d96c5007f9f99f79 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Thu, 17 Nov 2022 08:30:06 -0600 Subject: [PATCH] Fixes to validation pool --- forum-network/public/classes/bench.js | 4 +- forum-network/public/classes/member.js | 13 ++++-- forum-network/public/classes/params.js | 4 +- forum-network/public/classes/scene.js | 1 - .../public/classes/validation-pool.js | 36 +++++++++------- forum-network/public/mermaid-test.html | 2 - forum-network/public/validation-pool-test.js | 41 ++++++++++--------- 7 files changed, 55 insertions(+), 46 deletions(-) diff --git a/forum-network/public/classes/bench.js b/forum-network/public/classes/bench.js index 89883a3..b57f588 100644 --- a/forum-network/public/classes/bench.js +++ b/forum-network/public/classes/bench.js @@ -52,9 +52,9 @@ export class Bench extends Actor { .reduce((acc, cur) => acc += cur, 0); } - initiateValidationPool(authorId, {fee, duration, tokenLossRatio, contentiousDebate}) { + initiateValidationPool(authorId, {fee, duration, tokenLossRatio, contentiousDebate, signingPublicKey}) { const validationPoolNumber = this.validationPools.size + 1; - const validationPool = new ValidationPool(this, authorId, {fee, duration, tokenLossRatio, contentiousDebate}, + const validationPool = new ValidationPool(this, authorId, {fee, duration, tokenLossRatio, contentiousDebate, signingPublicKey}, `pool${validationPoolNumber}`, scene); this.validationPools.set(validationPool.id, validationPool); this.actions.createValidationPool.log(this, validationPool) diff --git a/forum-network/public/classes/member.js b/forum-network/public/classes/member.js index 4ec0e07..b657243 100644 --- a/forum-network/public/classes/member.js +++ b/forum-network/public/classes/member.js @@ -17,7 +17,8 @@ export class Member extends Actor { async initialize() { this.reputationKey = await CryptoUtil.generateAsymmetricKey(); - this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey); + // this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey); + this.reputationPublicKey = this.name; this.status.set('Initialized'); this.activate(); return this; @@ -32,13 +33,17 @@ export class Member extends Actor { await forumNode.receiveMessage(JSON.stringify(postMessage.toJSON())); } - initiateValidationPool(bench, options) { + async initiateValidationPool(bench, options) { // For now, directly call bench.initiateValidationPool(); + const signingKey = await CryptoUtil.generateAsymmetricKey(); + const signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey) this.actions.initiateValidationPool.log(this, bench); - return bench.initiateValidationPool(this.reputationPublicKey, options); + const pool = bench.initiateValidationPool(this.reputationPublicKey, {...options, signingPublicKey}); + this.validationPools.set(pool.id, {signingPublicKey}); + return pool; } - async castVote(validationPool, position, stake, lockingTime) { + async castVote(validationPool, {position, stake, lockingTime}) { const signingKey = await CryptoUtil.generateAsymmetricKey(); const signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey) this.validationPools.set(validationPool.id, {signingPublicKey}); diff --git a/forum-network/public/classes/params.js b/forum-network/public/classes/params.js index bf8264d..033ed14 100644 --- a/forum-network/public/classes/params.js +++ b/forum-network/public/classes/params.js @@ -1,9 +1,9 @@ const params = { mintingRatio: 1, // c1 stakeForWin: 0.5, // c2 - stakeForAuthor: 0.5, // c3 + // stakeForAuthor: 0.5, // c3 - For now we keep the default that stakeForAuthor = stakeForWin winningRatio: 0.5, // c4 - quorum: 1, // c5 + quorum: 0, // c5 activeVoterThreshold: null, // c6 voteDuration: { // c7 min: 0, diff --git a/forum-network/public/classes/scene.js b/forum-network/public/classes/scene.js index 7b2d33c..155f976 100644 --- a/forum-network/public/classes/scene.js +++ b/forum-network/public/classes/scene.js @@ -48,7 +48,6 @@ export class Scene { deactivateAll() { for (const actor of this.actors.values()) { - console.log(actor); while (actor.active) { actor.deactivate(); } diff --git a/forum-network/public/classes/validation-pool.js b/forum-network/public/classes/validation-pool.js index 462da6c..616d5ce 100644 --- a/forum-network/public/classes/validation-pool.js +++ b/forum-network/public/classes/validation-pool.js @@ -10,7 +10,7 @@ const ValidationPoolStates = Object.freeze({ }); export class ValidationPool extends Actor { - constructor(bench, authorId, {fee, duration, tokenLossRatio, contentiousDebate = false}, name, scene) { + constructor(bench, authorId, {signingPublicKey, fee, duration, tokenLossRatio, contentiousDebate = false}, name, scene) { super(name, scene); // If contentiousDebate = true, we will follow the progression defined by getTokenLossRatio() if (!contentiousDebate && (tokenLossRatio < 0 || tokenLossRatio > 1 || [null, undefined].includes(tokenLossRatio))) { @@ -33,9 +33,10 @@ export class ValidationPool extends Actor { this.tokens = { for: fee * params.mintingRatio * params.stakeForWin, against: fee * params.mintingRatio * (1 - params.stakeForWin), - author: fee * params.mintingRatio * params.stakeForAuthor, + // author: fee * params.mintingRatio * params.stakeForAuthor, } // TODO: Consider availability stakes + this.castVote(signingPublicKey, true, this.tokens.for, 0); } castVote(signingPublicKey, position, stake, lockingTime) { @@ -120,6 +121,7 @@ export class ValidationPool extends Actor { // TODO: If quorum is not met, what should happen? if (!quorumMet) { this.deactivate(); + // console.log("Quorum is not met", {upvoteValue, downvoteValue, activeAvailableReputation}); throw new Error("Quorum is not met"); } return votePasses && quorumMet; @@ -128,19 +130,23 @@ export class ValidationPool extends Actor { distributeTokens(result) { // Reward the author // TODO: If the vote fails, distribute tokens.author among winning voters - this.bench.reputations.addTokens(this.authorId, this.tokens.author); - // Reward the vote winners, in proportion to their stakes - const tokensForWinners = result ? this.tokens.for : this.tokens.against; - const winningVotes = this.listVotes(result); - const totalStakes = Array.from(winningVotes.values()) - .map(({stake}) => stake).reduce((acc, cur) => acc += cur, 0); - if (!totalStakes) { - return; - } - for (const [signingPublicKey, {stake}] of winningVotes.entries()) { - const {reputationPublicKey} = this.voters.get(signingPublicKey); - const reward = tokensForWinners * stake / totalStakes; - this.bench.reputations.addTokens(reputationPublicKey, reward); + if (result === true) { + // console.log("awarding to author", {id: this.authorId, tokens: this.tokens.for}); + this.bench.reputations.addTokens(this.authorId, this.tokens.for); + // Reward the vote winners, in proportion to their stakes + const tokensForWinners = this.tokens.against; + const winningVotes = this.listVotes(result); + const totalStakes = Array.from(winningVotes.values()) + .map(({stake}) => stake).reduce((acc, cur) => acc += cur, 0); + if (!totalStakes) { + return; + } + for (const [signingPublicKey, {stake}] of winningVotes.entries()) { + const {reputationPublicKey} = this.voters.get(signingPublicKey); + const reward = tokensForWinners * stake / totalStakes; + // console.log("awarding to winning voter", {id: reputationPublicKey, tokens: reward, stake, totalStakes, tokensForWinners}); + this.bench.reputations.addTokens(reputationPublicKey, reward); + } } } } diff --git a/forum-network/public/mermaid-test.html b/forum-network/public/mermaid-test.html index 427e92c..ad33055 100644 --- a/forum-network/public/mermaid-test.html +++ b/forum-network/public/mermaid-test.html @@ -14,8 +14,6 @@ }; const graphDefinition = 'graph TB\na-->b'; const graph = await mermaid.mermaidAPI.render('graphDiv', graphDefinition, insertSvg); - console.log("executed..."); - console.log(graph); const div = document.createElement('div'); div.innerHTML = graph; document.body.append(div); diff --git a/forum-network/public/validation-pool-test.js b/forum-network/public/validation-pool-test.js index f4410ea..7144550 100644 --- a/forum-network/public/validation-pool-test.js +++ b/forum-network/public/validation-pool-test.js @@ -27,34 +27,35 @@ await delay(1000); // First member can self-approve { - const pool = member1.initiateValidationPool(bench, {fee: 1, duration: 1000, tokenLossRatio: 1}); - await member1.castVote(pool, true, 0, 0); + const pool = await member1.initiateValidationPool(bench, {fee: 7, duration: 1000, tokenLossRatio: 1}); + // await member1.castVote(pool, true, 0, 0); await member1.revealIdentity(pool); // Vote passes await updateDisplayValues(); await delay(1000); } -// Failure example: second member can not self-approve -try { - const pool = member2.initiateValidationPool(bench, {fee: 1, duration: 1000, tokenLossRatio: 1}); - await member2.castVote(pool, true, 0, 0); - await member2.revealIdentity(pool); // Quorum not met! - await updateDisplayValues(); - await delay(1000); -} catch(e) { - if (e.message.match(/Quorum is not met/)) { - console.log("Caught expected error: Quorum not met") - } else { - console.error("Unexpected error") - throw e; - } -} +// // Failure example: second member can not self-approve +// try { +// const pool = member2.initiateValidationPool(bench, {fee: 1, duration: 1000, tokenLossRatio: 1}); +// await member2.castVote(pool, true, 0, 0); +// await member2.revealIdentity(pool); // Quorum not met! +// await updateDisplayValues(); +// await delay(1000); +// } catch(e) { +// if (e.message.match(/Quorum is not met/)) { +// console.log("Caught expected error: Quorum not met") +// } else { +// console.error("Unexpected error") +// throw e; +// } +// } // Second member must be approved by first member { - const pool = member2.initiateValidationPool(bench, {fee: 1, duration: 1000, tokenLossRatio: 1}); - await member1.castVote(pool, true, 0.5, 1); - await member1.revealIdentity(pool); // Vote passes + const pool = await member2.initiateValidationPool(bench, {fee: 1, duration: 1000, tokenLossRatio: 1}); + await member1.castVote(pool, {position: true, stake: 4, lockingTime: 0}); + await member1.revealIdentity(pool); + await member2.revealIdentity(pool); // Vote passes await updateDisplayValues(); await delay(1000); }