From 7e74773242dd890e4587c45646733315576118d3 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Sun, 23 Apr 2023 11:55:03 -0500 Subject: [PATCH] Refactor to clarify input parameters for validation pool --- forum-network/src/classes/actors/expert.js | 16 +++++++------- forum-network/src/classes/dao/business.js | 10 ++++++--- forum-network/src/classes/dao/dao.js | 22 +++++++++---------- .../src/classes/dao/validation-pool.js | 13 +++++------ .../src/tests/scripts/availability.test.js | 4 ++++ .../tests/scripts/forum/forum.test-util.js | 2 ++ .../src/tests/scripts/validation-pool.test.js | 3 +++ 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/forum-network/src/classes/actors/expert.js b/forum-network/src/classes/actors/expert.js index 6928e45..112f867 100644 --- a/forum-network/src/classes/actors/expert.js +++ b/forum-network/src/classes/actors/expert.js @@ -1,5 +1,4 @@ import { Action } from '../display/action.js'; -import { PostMessage } from '../forum-network/message.js'; import { CryptoUtil } from '../supporting/crypto.js'; import { ReputationHolder } from '../reputation/reputation-holder.js'; import { EdgeTypes } from '../../util/constants.js'; @@ -18,7 +17,6 @@ export class Expert extends ReputationHolder { getAssignedWork: new Action('get assigned work', scene), submitWork: new Action('submit work evidence', scene), }; - this.validationPools = new Map(); this.tokens = []; } @@ -49,21 +47,23 @@ export class Expert extends ReputationHolder { return this; } - async submitPostWithFee(postContent, poolOptions) { + async submitPostWithFee(postContent, { fee }, params) { const post = await this.dao.forum.addPost(this.reputationPublicKey, postContent); await this.actions.submitPost.log(this, post); const postId = post.id; - const pool = await this.initiateValidationPool({ ...poolOptions, postId }); + const pool = await this.initiateValidationPool({ fee, postId }, params); this.tokens.push(pool.tokenId); return { postId, pool }; } - async initiateValidationPool(poolOptions) { + async initiateValidationPool({ postId, fee }, params) { // For now, make direct call rather than network - poolOptions.reputationPublicKey = this.reputationPublicKey; - const pool = await this.dao.initiateValidationPool(this, poolOptions); + const pool = await this.dao.initiateValidationPool(this, { + reputationPublicKey: this.reputationPublicKey, + postId, + fee, + }, params); this.tokens.push(pool.tokenId); - this.validationPools.set(pool.id, poolOptions); return pool; } diff --git a/forum-network/src/classes/dao/business.js b/forum-network/src/classes/dao/business.js index da603ed..7dcf53d 100644 --- a/forum-network/src/classes/dao/business.js +++ b/forum-network/src/classes/dao/business.js @@ -80,12 +80,16 @@ export class Business extends Actor { const pool = await this.dao.initiateValidationPool(this, { postId, fee: request.fee, + reputationPublicKey, + }, { duration, tokenLossRatio, - }, { - reputationPublicKey, - authorStakeAmount: request.worker.stakeAmount, + }); + + await pool.stake(reputationPublicKey, { tokenId: request.worker.tokenId, + amount: request.worker.stakeAmount, + position: true, }); // When the validation pool concludes, diff --git a/forum-network/src/classes/dao/dao.js b/forum-network/src/classes/dao/dao.js index 867423c..4e31fa8 100644 --- a/forum-network/src/classes/dao/dao.js +++ b/forum-network/src/classes/dao/dao.js @@ -3,6 +3,7 @@ import { ReputationTokenContract } from '../reputation/reputation-token.js'; import { ValidationPool } from './validation-pool.js'; import { Availability } from './availability.js'; import { Business } from './business.js'; +import { Voter } from '../supporting/voter.js'; import { Actor } from '../display/actor.js'; /** @@ -33,6 +34,12 @@ export class DAO extends Actor { Array.from(this.validationPools.values()); } + addVoteRecord(reputationPublicKey, validationPool) { + const voter = this.experts.get(reputationPublicKey) ?? new Voter(reputationPublicKey); + voter.addVoteRecord(validationPool); + this.experts.set(reputationPublicKey, voter); + } + listActiveVoters({ activeVoterThreshold } = {}) { return Array.from(this.experts.values()).filter((voter) => { const hasVoted = !!voter.dateLastVote; @@ -54,21 +61,14 @@ export class DAO extends Actor { .reduce((acc, cur) => (acc += cur), 0); } - async initiateValidationPool(fromActor, poolOptions, stakeOptions) { + async initiateValidationPool(fromActor, { postId, reputationPublicKey, fee }, params) { const validationPoolNumber = this.validationPools.size + 1; const name = `Pool${validationPoolNumber}`; - const pool = new ValidationPool(this, poolOptions, name, this.scene, fromActor); + const pool = new ValidationPool(this, { + postId, reputationPublicKey, fee, + }, params, name, this.scene, fromActor); this.validationPools.set(pool.id, pool); - if (stakeOptions) { - const { reputationPublicKey, tokenId, authorStakeAmount } = stakeOptions; - await pool.stake(reputationPublicKey, { - tokenId, - position: true, - amount: authorStakeAmount, - }); - } - return pool; } } diff --git a/forum-network/src/classes/dao/validation-pool.js b/forum-network/src/classes/dao/validation-pool.js index f3cbfa5..520fc83 100644 --- a/forum-network/src/classes/dao/validation-pool.js +++ b/forum-network/src/classes/dao/validation-pool.js @@ -1,6 +1,5 @@ import { ReputationHolder } from '../reputation/reputation-holder.js'; import { Stake } from '../supporting/stake.js'; -import { Voter } from '../supporting/voter.js'; import { Action } from '../display/action.js'; import { displayNumber } from '../../util/helpers.js'; @@ -47,6 +46,8 @@ export class ValidationPool extends ReputationHolder { postId, reputationPublicKey, fee, + }, + { duration, tokenLossRatio, contentiousDebate = false, @@ -147,9 +148,7 @@ export class ValidationPool extends ReputationHolder { this.actions.mint.log(this, this, `(${this.mintedValue})`); // Keep a record of voters and their votes - const voter = this.dao.experts.get(reputationPublicKey) ?? new Voter(reputationPublicKey); - voter.addVoteRecord(this); - this.dao.experts.set(reputationPublicKey, voter); + this.dao.addVoteRecord(reputationPublicKey, this); } getTokenLossRatio() { @@ -232,12 +231,10 @@ export class ValidationPool extends ReputationHolder { // Keep a record of voters and their votes if (reputationPublicKey !== this.id) { - const voter = this.dao.experts.get(reputationPublicKey) ?? new Voter(reputationPublicKey); - voter.addVoteRecord(this); - this.dao.experts.set(reputationPublicKey, voter); + this.dao.addVoteRecord(reputationPublicKey, this); // Update computed display values - const actor = this.scene?.findActor((a) => a.reputationPublicKey === voter.reputationPublicKey); + const actor = this.scene?.findActor((a) => a.reputationPublicKey === reputationPublicKey); await actor.computeDisplayValues(); } } diff --git a/forum-network/src/tests/scripts/availability.test.js b/forum-network/src/tests/scripts/availability.test.js index 418335c..64c45a7 100644 --- a/forum-network/src/tests/scripts/availability.test.js +++ b/forum-network/src/tests/scripts/availability.test.js @@ -51,6 +51,8 @@ const setup = async () => { new PostContent({ hello: 'there' }).setTitle('Post 1'), { fee: 10, + }, + { duration: POOL_DURATION, tokenLossRatio: 1, }, @@ -68,6 +70,8 @@ const setup = async () => { .addCitation(postId1, 0.5), { fee: 10, + }, + { duration: POOL_DURATION, tokenLossRatio: 1, }, diff --git a/forum-network/src/tests/scripts/forum/forum.test-util.js b/forum-network/src/tests/scripts/forum/forum.test-util.js index cd8deac..9aa34d5 100644 --- a/forum-network/src/tests/scripts/forum/forum.test-util.js +++ b/forum-network/src/tests/scripts/forum/forum.test-util.js @@ -42,6 +42,8 @@ export class ForumTest { postContent, { fee, + }, + { duration: this.options.poolDurationMs, tokenLossRatio: 1, }, diff --git a/forum-network/src/tests/scripts/validation-pool.test.js b/forum-network/src/tests/scripts/validation-pool.test.js index 337ff74..b7a5b05 100644 --- a/forum-network/src/tests/scripts/validation-pool.test.js +++ b/forum-network/src/tests/scripts/validation-pool.test.js @@ -57,6 +57,7 @@ describe('Validation Pool', function tests() { await scene.sequence.startSection(); const { pool } = await experts[0].submitPostWithFee(new PostContent(), { fee: 7, + }, { duration: POOL_DURATION_MS, tokenLossRatio: 1, }); @@ -84,6 +85,7 @@ describe('Validation Pool', function tests() { try { const { pool } = await experts[1].submitPostWithFee(new PostContent(), { fee: 1, + }, { duration: POOL_DURATION_MS, tokenLossRatio: 1, }); @@ -98,6 +100,7 @@ describe('Validation Pool', function tests() { it('Second expert must be approved by first expert', async () => { const { pool } = await experts[1].submitPostWithFee(new PostContent(), { fee: 1, + }, { duration: POOL_DURATION_MS, tokenLossRatio: 1, });