From 7cda474d207a0c2a2ae39c58af4c24b09b7fbc3a Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Sun, 29 Jan 2023 18:28:27 -0600 Subject: [PATCH] Fix forum logic for negative citations --- forum-network/src/classes/forum.js | 37 +++-- forum-network/src/classes/wdag.js | 8 +- forum-network/src/index.html | 8 +- .../{forum.test.html => forum1.test.html} | 6 +- forum-network/src/tests/forum2.test.html | 27 ++++ .../src/tests/scripts/forum.test-util.js | 88 ++++++++++++ forum-network/src/tests/scripts/forum.test.js | 135 ------------------ .../src/tests/scripts/forum1.test.js | 39 +++++ .../src/tests/scripts/forum2.test.js | 39 +++++ 9 files changed, 228 insertions(+), 159 deletions(-) rename forum-network/src/tests/{forum.test.html => forum1.test.html} (91%) create mode 100644 forum-network/src/tests/forum2.test.html create mode 100644 forum-network/src/tests/scripts/forum.test-util.js delete mode 100644 forum-network/src/tests/scripts/forum.test.js create mode 100644 forum-network/src/tests/scripts/forum1.test.js create mode 100644 forum-network/src/tests/scripts/forum2.test.js diff --git a/forum-network/src/classes/forum.js b/forum-network/src/classes/forum.js index 3880421..cbd0629 100644 --- a/forum-network/src/classes/forum.js +++ b/forum-network/src/classes/forum.js @@ -1,5 +1,5 @@ import { Actor } from './actor.js'; -import { WDAG } from './wdag.js'; +import { WDAG, Vertex } from './wdag.js'; import { Action } from './action.js'; import { CryptoUtil } from './crypto.js'; import params from '../params.js'; @@ -14,7 +14,7 @@ class Post extends Actor { const index = forum.posts.countVertices(); const name = `Post${index + 1}`; super(name, forum.scene); - this.id = postContent.id ?? `post_${CryptoUtil.randomUUID().slice(0, 4)}`; + this.id = postContent.id ?? name; this.authorPublicKey = authorPublicKey; this.value = 0; this.initialValue = 0; @@ -147,41 +147,48 @@ export class Forum extends ReputationHolder { if (params.referenceChainLimit === null || depth <= params.referenceChainLimit) { for (const citationEdge of postVertex.getEdges(CITATION, true)) { const { to: citedPostVertex, weight } = citationEdge; - const citedPost = citedPostVertex.data; let outboundAmount = weight * increment; - const balance = this.posts.getEdge(BALANCE, postVertex, citedPostVertex)?.data || 0; + const balance = this.posts.getEdge(BALANCE, postVertex, citedPostVertex)?.weight || 0; console.log('Citation', { - citationEdge, outboundAmount, balance, citedPostValue: citedPost.value, + citationEdge, outboundAmount, balance, }); // We need to ensure that we propagate no more reputation than we leached - if (outboundAmount < 0) { - outboundAmount = Math.max(outboundAmount, -citedPost.value); - if (depth > 0) { - outboundAmount = Math.max(outboundAmount, -balance); - } + if (depth > 0 && weight < 0) { + outboundAmount = outboundAmount < 0 + ? Math.max(outboundAmount, -balance) + : Math.min(outboundAmount, -balance); } - increment -= outboundAmount * params.leachingValue; - this.posts.setEdge(BALANCE, postVertex, citedPostVertex, balance + outboundAmount); - await this.propagateValue(citationEdge, { + const refundFromOutbound = await this.propagateValue(citationEdge, { rewardsAccumulator, increment: outboundAmount, depth: depth + 1, }); + outboundAmount -= refundFromOutbound; + this.posts.setEdge(BALANCE, postVertex, citedPostVertex, balance + outboundAmount); + increment -= outboundAmount * params.leachingValue; } } - const newValue = post.value + increment; + const rawNewValue = post.value + increment; + const newValue = Math.max(0, rawNewValue); + const appliedIncrement = newValue - post.value; + const refundToInbound = increment - appliedIncrement; console.log('propagateValue end', { depth, increment, + rawNewValue, newValue, + appliedIncrement, + refundToInbound, }); // Award reputation to post author - rewardsAccumulator.set(post.tokenId, increment); + rewardsAccumulator.set(post.tokenId, appliedIncrement); // Increment the value of the post await this.setPostValue(post, newValue); + + return refundToInbound; } } diff --git a/forum-network/src/classes/wdag.js b/forum-network/src/classes/wdag.js index 58000aa..2aca682 100644 --- a/forum-network/src/classes/wdag.js +++ b/forum-network/src/classes/wdag.js @@ -1,4 +1,4 @@ -class Vertex { +export class Vertex { constructor(id, data) { this.id = id; this.data = data; @@ -15,7 +15,7 @@ class Vertex { } } -class Edge { +export class Edge { constructor(label, from, to, weight) { this.from = from; this.to = to; @@ -80,9 +80,7 @@ export class WDAG { setEdge(label, from, to, edge) { from = from instanceof Vertex ? from : this.getVertex(from); to = to instanceof Vertex ? to : this.getVertex(to); - if (!(edge instanceof Edge)) { - edge = new Edge(edge); - } + edge = typeof edge === 'number' ? new Edge(label, from, to, edge) : edge; let edges = this.edgeLabels.get(label); if (!edges) { edges = new Map(); diff --git a/forum-network/src/index.html b/forum-network/src/index.html index 6bdf9f7..9d68a12 100644 --- a/forum-network/src/index.html +++ b/forum-network/src/index.html @@ -9,7 +9,13 @@

Secondary