From 5b0b87d2d3aa324c599e4d0c4ee9e1933f95d468 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Sun, 16 Apr 2023 08:21:09 -0500 Subject: [PATCH] Show author reputations on sequence diagram --- forum-network/src/classes/actors/expert.js | 4 +-- forum-network/src/classes/dao/dao.js | 6 ++--- forum-network/src/classes/dao/forum.js | 26 +++++++++++-------- forum-network/src/classes/display/actor.js | 4 +-- forum-network/src/classes/display/scene.js | 6 +++-- .../classes/reputation/reputation-holder.js | 4 +-- .../tests/scripts/forum/forum.test-util.js | 10 +++---- .../src/tests/scripts/forum/forum10.test.js | 4 +-- 8 files changed, 34 insertions(+), 30 deletions(-) diff --git a/forum-network/src/classes/actors/expert.js b/forum-network/src/classes/actors/expert.js index 69a1d7c..99f98bb 100644 --- a/forum-network/src/classes/actors/expert.js +++ b/forum-network/src/classes/actors/expert.js @@ -4,8 +4,8 @@ import { CryptoUtil } from '../util/crypto.js'; import { ReputationHolder } from '../reputation/reputation-holder.js'; export class Expert extends ReputationHolder { - constructor(dao, name, scene) { - super(name, scene); + constructor(dao, name, scene, options) { + super(name, scene, options); this.dao = dao; this.actions = { submitPostViaNetwork: new Action('submit post via network', scene), diff --git a/forum-network/src/classes/dao/dao.js b/forum-network/src/classes/dao/dao.js index 938d019..bdf4ec0 100644 --- a/forum-network/src/classes/dao/dao.js +++ b/forum-network/src/classes/dao/dao.js @@ -13,8 +13,8 @@ import { Actor } from '../display/actor.js'; * - Reputation: Keep track of reputation accrued to each expert */ export class DAO extends Actor { - constructor(name, scene) { - super(name, scene); + constructor(name, scene, options) { + super(name, scene, options); /* Contracts */ this.forum = new Forum(this, 'Forum', scene); @@ -38,7 +38,7 @@ export class DAO extends Actor { return Array.from(this.experts.values()).filter((voter) => { const hasVoted = !!voter.dateLastVote; const withinThreshold = !params.activeVoterThreshold - || new Date() - voter.dateLastVote >= params.activeVoterThreshold; + || new Date() - voter.dateLastVote >= params.activeVoterThreshold; return hasVoted && withinThreshold; }); } diff --git a/forum-network/src/classes/dao/forum.js b/forum-network/src/classes/dao/forum.js index 5d2a758..01175ca 100644 --- a/forum-network/src/classes/dao/forum.js +++ b/forum-network/src/classes/dao/forum.js @@ -69,8 +69,9 @@ class Post extends Actor { } } -class Author { +class Author extends Actor { constructor(forum, publicKey) { + super(publicKey, forum.scene); this.forum = forum; this.publicKey = publicKey; this.name = publicKey; @@ -158,6 +159,7 @@ export class Forum extends ReputationHolder { const addAuthorToGraph = (publicKey, weight, authorTokenId) => { const author = new Author(this, publicKey); + author.setDisplayValue('reputation', () => author.getReputation()); const authorVertex = this.graph.getVertex(publicKey) ?? this.graph.addVertex(VertexTypes.AUTHOR, publicKey, author, author.getLabel()); const authorEdge = this.graph.addEdge( @@ -217,6 +219,7 @@ export class Forum extends ReputationHolder { const { tokenId: authorTokenId } = authorEdge.data; this.dao.reputation.transfer(this.id, author.publicKey, authorTokenId); authorVertex.setDisplayLabel(author.getLabel()); + await author.computeDisplayValues(); } } @@ -338,6 +341,17 @@ export class Forum extends ReputationHolder { const appliedIncrement = newValue - post.value; const refundToInbound = increment - appliedIncrement; + // Apply reputation effects to post authors, not to the post directly + for (const authorEdge of postVertex.getEdges(EdgeTypes.AUTHOR, true)) { + const { weight, data: { tokenId }, to: { data: author } } = authorEdge; + const authorIncrement = weight * appliedIncrement; + rewardsAccumulator.set(tokenId, authorIncrement); + this.actions.propagate.log(post, author, `(${authorIncrement})`); + } + + // Increment the value of the post + await post.setValue(newValue); + console.log('propagateValue end', { depth, increment, @@ -347,16 +361,6 @@ export class Forum extends ReputationHolder { refundToInbound, }); - // Apply reputation effects to post authors, not to the post directly - for (const authorEdge of postVertex.getEdges(EdgeTypes.AUTHOR, true)) { - const { weight, data: { tokenId } } = authorEdge; - const authorIncrement = weight * appliedIncrement; - rewardsAccumulator.set(tokenId, authorIncrement); - } - - // Increment the value of the post - await post.setValue(newValue); - return refundToInbound; } } diff --git a/forum-network/src/classes/display/actor.js b/forum-network/src/classes/display/actor.js index 046f923..f489d4d 100644 --- a/forum-network/src/classes/display/actor.js +++ b/forum-network/src/classes/display/actor.js @@ -1,7 +1,7 @@ import { displayNumber } from '../../util.js'; export class Actor { - constructor(name, scene) { + constructor(name, scene, { announce = false } = {}) { if (!scene) throw new Error('An actor without a scene!'); this.name = name; this.scene = scene; @@ -11,7 +11,7 @@ export class Actor { this.values = new Map(); this.valueFunctions = new Map(); this.active = 0; - scene?.registerActor(this); + scene?.registerActor(this, announce); } activate() { diff --git a/forum-network/src/classes/display/scene.js b/forum-network/src/classes/display/scene.js index f0e76b3..344109c 100644 --- a/forum-network/src/classes/display/scene.js +++ b/forum-network/src/classes/display/scene.js @@ -86,9 +86,11 @@ export class Scene { return this; } - registerActor(actor) { + registerActor(actor, announce) { this.actors.add(actor); - // this.sequence?.log(`participant ${actor.name}`); + if (announce) { + this.sequence?.log(`participant ${actor.name}`); + } } findActor(fn) { diff --git a/forum-network/src/classes/reputation/reputation-holder.js b/forum-network/src/classes/reputation/reputation-holder.js index b2aaf0e..29696ae 100644 --- a/forum-network/src/classes/reputation/reputation-holder.js +++ b/forum-network/src/classes/reputation/reputation-holder.js @@ -2,8 +2,8 @@ import { randomID } from '../../util.js'; import { Actor } from '../display/actor.js'; export class ReputationHolder extends Actor { - constructor(name, scene) { - super(name, scene); + constructor(name, scene, options) { + super(name, scene, options); this.reputationPublicKey = `${name}_${randomID()}`; } } 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 c90f9ab..aabcd46 100644 --- a/forum-network/src/tests/scripts/forum/forum.test-util.js +++ b/forum-network/src/tests/scripts/forum/forum.test-util.js @@ -55,10 +55,10 @@ export class ForumTest { return postId; } - async newExpert() { + async newExpert(options) { const index = this.experts.length; const name = `Expert${index + 1}`; - const expert = await new Expert(this.dao, name, this.scene).initialize(); + const expert = await new Expert(this.dao, name, this.scene, options).initialize(); this.experts.push(expert); // await expert.addComputedValue('rep', () => this.dao.reputation.valueOwnedBy(expert.reputationPublicKey)); return expert; @@ -81,15 +81,13 @@ export class ForumTest { scene.addDisplayValue('q4. leachingValue').set(params.leachingValue); scene.addDisplayValue(' '); - this.dao = new DAO('DAO', scene); + this.dao = new DAO('DAO', scene, { announce: true }); this.forum = this.dao.forum; this.experts = []; this.posts = []; - await this.newExpert(); + await this.newExpert({ announce: true }); await this.dao.addComputedValue('total value', () => this.dao.reputation.getTotal()); - // await this.dao.addComputedValue('total reputation', () => this.dao.forum.getTotalValue()); - this.dao.computeDisplayValues(); } } diff --git a/forum-network/src/tests/scripts/forum/forum10.test.js b/forum-network/src/tests/scripts/forum/forum10.test.js index 693ed8b..fa3fb02 100644 --- a/forum-network/src/tests/scripts/forum/forum10.test.js +++ b/forum-network/src/tests/scripts/forum/forum10.test.js @@ -20,8 +20,8 @@ describe('Forum', function tests() { experts = forumTest.experts; posts = forumTest.posts; - await forumTest.newExpert(); - await forumTest.newExpert(); + await forumTest.newExpert({ announce: true }); + await forumTest.newExpert({ announce: true }); }); it('Post1', async () => {