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
|
|
|
|
2023-01-01 21:09:02 -06:00
|
|
|
class PostVertex extends Vertex {
|
|
|
|
constructor(forum, authorId, postContent) {
|
2022-12-31 16:08:42 -06:00
|
|
|
super();
|
2022-11-30 09:13:52 -06:00
|
|
|
this.forum = forum;
|
2023-01-01 21:09:02 -06:00
|
|
|
this.id = postContent.id;
|
2022-11-30 09:13:52 -06:00
|
|
|
this.authorId = authorId;
|
2023-01-01 21:09:02 -06:00
|
|
|
this.citations = postContent.citations;
|
2022-11-30 09:13:52 -06:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-01-01 21:09:02 -06:00
|
|
|
async addPost(authorId, postContent) {
|
|
|
|
const post = new PostVertex(this, authorId, postContent);
|
|
|
|
this.posts.addVertex(post.id, post);
|
|
|
|
for (const { postId: citedPostId, weight } of postContent.citations) {
|
|
|
|
this.posts.addEdge('citation', post.id, citedPostId, { weight });
|
2022-11-30 09:13:52 -06:00
|
|
|
}
|
|
|
|
// this.applyReputationEffects(post);
|
|
|
|
// initiateValidationPool(authorId, {postId, fee, duration, tokenLossRatio, contentiousDebate, signingPublicKey}) {
|
2023-01-01 21:09:02 -06:00
|
|
|
|
|
|
|
// const pool = await this.bench.initiateValidationPool(authorId, {
|
|
|
|
// ...poolParams,
|
|
|
|
// postId,
|
|
|
|
// });
|
|
|
|
// return pool;
|
2022-11-30 09:13:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|