dao-governance-framework/forum-network/public/classes/forum.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-12-31 16:08:42 -06:00
import { Actor } from './actor.js';
import { Graph, Vertex } from './graph.js';
import params from './params.js';
2022-11-30 09:13:52 -06:00
class Post extends Vertex {
constructor(forum, id, authorId, citations) {
2022-12-31 16:08:42 -06:00
super();
2022-11-30 09:13:52 -06:00
this.forum = forum;
this.id = id;
this.authorId = authorId;
this.citations = citations;
this.value = 0;
}
onValidate({ tokensMinted }) {
this.value = params.initialPostValueFunction({ tokensMinted });
this.forum.distributeReputation(this, this.value);
}
}
2022-12-31 16:08:42 -06:00
/**
* Purpose: Maintain a directed, acyclic, weighted graph of posts referencing other posts
*/
2022-11-30 09:13:52 -06:00
export class Forum extends Actor {
constructor(bench, name, scene) {
super(name, scene);
this.bench = bench;
this.posts = new Graph();
}
2022-12-31 16:08:42 -06:00
async addPost({
authorId, postId, citations = [], poolParams,
}) {
2022-11-30 09:13:52 -06:00
const post = new Post(this, postId, authorId);
this.posts.addVertex(postId, post);
for (const { postId: citedPostId, weight } of citations) {
2022-12-31 16:08:42 -06:00
this.posts.addEdge('citation', postId, citedPostId, { weight });
2022-11-30 09:13:52 -06:00
}
// this.applyReputationEffects(post);
// initiateValidationPool(authorId, {postId, fee, duration, tokenLossRatio, contentiousDebate, signingPublicKey}) {
const pool = await this.bench.initiateValidationPool(authorId, {
...poolParams,
postId,
});
return pool;
}
getPost(postId) {
return this.posts.getVertexData(postId);
}
getPosts() {
return this.posts.getVertices();
}
distributeReputation(post, amount, depth = 0) {
2022-12-31 16:08:42 -06:00
console.log('distributeReputation', { post, amount, depth });
2022-11-30 09:13:52 -06:00
// Add the given value to the current post
post.value += amount;
// Distribute a fraction of the added value among cited posts
const distributeAmongCitations = amount * params.citationFraction;
// Here we allow an arbitrary scale for the amount of the citations.
// We normalize by dividing each by the total.
const totalWeight = post.citations
?.map(({ weight }) => weight)
.reduce((acc, cur) => (acc += cur), 0);
for (const {
to: citedPostId,
data: { weight },
2022-12-31 16:08:42 -06:00
} of post.getEdges('citation', true)) {
2022-11-30 09:13:52 -06:00
const citedPost = this.getPost(citedPostId);
if (!citedPost) {
throw new Error(
2022-12-31 16:08:42 -06:00
`Post ${post.postId} cites unknown post ${citedPostId}`,
2022-11-30 09:13:52 -06:00
);
}
this.distributeReputation(
citedPost,
(weight / totalWeight) * distributeAmongCitations,
2022-12-31 16:08:42 -06:00
depth + 1,
2022-11-30 09:13:52 -06:00
);
}
}
}