Add special case: Incineration
This commit is contained in:
parent
63b43a0f4d
commit
5988950857
|
@ -3,7 +3,7 @@ import { Action } from '../display/action.js';
|
||||||
import { Actor } from '../display/actor.js';
|
import { Actor } from '../display/actor.js';
|
||||||
import params from '../../params.js';
|
import params from '../../params.js';
|
||||||
import { ReputationHolder } from '../reputation/reputation-holder.js';
|
import { ReputationHolder } from '../reputation/reputation-holder.js';
|
||||||
import { displayNumber, EPSILON } from '../../util.js';
|
import { displayNumber, EPSILON, INCINERATOR_ADDRESS } from '../../util.js';
|
||||||
|
|
||||||
const CITATION = 'citation';
|
const CITATION = 'citation';
|
||||||
const BALANCE = 'balance';
|
const BALANCE = 'balance';
|
||||||
|
@ -27,11 +27,11 @@ class Post extends Actor {
|
||||||
.reduce((total, { weight }) => total += weight, 0);
|
.reduce((total, { weight }) => total += weight, 0);
|
||||||
if (leachingTotal > params.revaluationLimit) {
|
if (leachingTotal > params.revaluationLimit) {
|
||||||
throw new Error('Post leaching total exceeds revaluation limit '
|
throw new Error('Post leaching total exceeds revaluation limit '
|
||||||
+ `(${leachingTotal} > ${params.revaluationLimit})`);
|
+ `(${leachingTotal} > ${params.revaluationLimit})`);
|
||||||
}
|
}
|
||||||
if (donationTotal > params.revaluationLimit) {
|
if (donationTotal > params.revaluationLimit) {
|
||||||
throw new Error('Post donation total exceeds revaluation limit '
|
throw new Error('Post donation total exceeds revaluation limit '
|
||||||
+ `(${donationTotal} > ${params.revaluationLimit})`);
|
+ `(${donationTotal} > ${params.revaluationLimit})`);
|
||||||
}
|
}
|
||||||
if (this.citations.some(({ weight }) => Math.abs(weight) > params.revaluationLimit)) {
|
if (this.citations.some(({ weight }) => Math.abs(weight) > params.revaluationLimit)) {
|
||||||
throw new Error(`Each citation magnitude must not exceed revaluation limit ${params.revaluationLimit}`);
|
throw new Error(`Each citation magnitude must not exceed revaluation limit ${params.revaluationLimit}`);
|
||||||
|
@ -70,9 +70,14 @@ export class Forum extends ReputationHolder {
|
||||||
}
|
}
|
||||||
|
|
||||||
async addPost(authorId, postContent) {
|
async addPost(authorId, postContent) {
|
||||||
|
console.log('addPost', { authorId, postContent });
|
||||||
const post = new Post(this, authorId, postContent);
|
const post = new Post(this, authorId, postContent);
|
||||||
this.posts.addVertex(post.id, post, post.getLabel());
|
this.posts.addVertex(post.id, post, post.getLabel());
|
||||||
for (const { postId: citedPostId, weight } of post.citations) {
|
for (const { postId: citedPostId, weight } of post.citations) {
|
||||||
|
// Special case: Incinerator
|
||||||
|
if (citedPostId === INCINERATOR_ADDRESS && !this.posts.getVertex(INCINERATOR_ADDRESS)) {
|
||||||
|
this.posts.addVertex(INCINERATOR_ADDRESS, { name: 'Incinerator' }, 'Incinerator');
|
||||||
|
}
|
||||||
this.posts.addEdge(CITATION, post.id, citedPostId, weight);
|
this.posts.addEdge(CITATION, post.id, citedPostId, weight);
|
||||||
}
|
}
|
||||||
return post;
|
return post;
|
||||||
|
@ -171,22 +176,43 @@ export class Forum extends ReputationHolder {
|
||||||
for (const citationEdge of citationEdges) {
|
for (const citationEdge of citationEdges) {
|
||||||
const { weight } = citationEdge;
|
const { weight } = citationEdge;
|
||||||
let outboundAmount = weight * increment;
|
let outboundAmount = weight * increment;
|
||||||
const balanceToOutbound = this.posts.getEdgeWeight(BALANCE, citationEdge.from, citationEdge.to) ?? 0;
|
|
||||||
// We need to ensure that we at most undo the prior effects of this post
|
|
||||||
if (initialNegative) {
|
|
||||||
outboundAmount = outboundAmount < 0
|
|
||||||
? Math.max(outboundAmount, -balanceToOutbound)
|
|
||||||
: Math.min(outboundAmount, -balanceToOutbound);
|
|
||||||
}
|
|
||||||
if (Math.abs(outboundAmount) > EPSILON) {
|
if (Math.abs(outboundAmount) > EPSILON) {
|
||||||
const refundFromOutbound = await this.propagateValue(citationEdge, {
|
const balanceToOutbound = this.posts.getEdgeWeight(BALANCE, citationEdge.from, citationEdge.to) ?? 0;
|
||||||
rewardsAccumulator,
|
let refundFromOutbound = 0;
|
||||||
increment: outboundAmount,
|
|
||||||
depth: depth + 1,
|
|
||||||
initialNegative: initialNegative || (depth === 0 && outboundAmount < 0),
|
|
||||||
});
|
|
||||||
|
|
||||||
outboundAmount -= refundFromOutbound;
|
// Special case: Incineration.
|
||||||
|
if (citationEdge.to.id === INCINERATOR_ADDRESS) {
|
||||||
|
// Only a positive amount may be incinerated! Otherwise the sink could be used as a source.
|
||||||
|
if (outboundAmount < 0) {
|
||||||
|
throw new Error('Incinerator can only receive positive citations!');
|
||||||
|
}
|
||||||
|
// Reputation sent to the incinerator is burned! This means it is deducted from the sender,
|
||||||
|
// without increasing the value of any other token.
|
||||||
|
this.actions.propagate.log(citationEdge.from.data, { name: 'Incinerator' }, `(${increment})`);
|
||||||
|
} else {
|
||||||
|
// We need to ensure that we at most undo the prior effects of this post
|
||||||
|
if (initialNegative) {
|
||||||
|
outboundAmount = outboundAmount < 0
|
||||||
|
? Math.max(outboundAmount, -balanceToOutbound)
|
||||||
|
: Math.min(outboundAmount, -balanceToOutbound);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively propagate reputation effects
|
||||||
|
refundFromOutbound = await this.propagateValue(citationEdge, {
|
||||||
|
rewardsAccumulator,
|
||||||
|
increment: outboundAmount,
|
||||||
|
depth: depth + 1,
|
||||||
|
initialNegative: initialNegative || (depth === 0 && outboundAmount < 0),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Any excess (negative) amount that could not be propagated,
|
||||||
|
// i.e. because a cited post has been reduced to zero value,
|
||||||
|
// is retained by the citing post.
|
||||||
|
outboundAmount -= refundFromOutbound;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep a record of the effect of the reputation transferred along this edge in the graph,
|
||||||
|
// so that later, negative citations can be constrained to at most undo these effects.
|
||||||
this.posts.setEdgeWeight(BALANCE, citationEdge.from, citationEdge.to, balanceToOutbound + outboundAmount);
|
this.posts.setEdgeWeight(BALANCE, citationEdge.from, citationEdge.to, balanceToOutbound + outboundAmount);
|
||||||
totalOutboundAmount += outboundAmount;
|
totalOutboundAmount += outboundAmount;
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
<li><a href="./tests/forum5.test.html">Destroy a post after it has received positive citations</a></li>
|
<li><a href="./tests/forum5.test.html">Destroy a post after it has received positive citations</a></li>
|
||||||
<li><a href="./tests/forum6.test.html">Initially zero-valued posts later receive citations</a></li>
|
<li><a href="./tests/forum6.test.html">Initially zero-valued posts later receive citations</a></li>
|
||||||
<li><a href="./tests/forum7.test.html">Negatively cite a zero-valued post</a></li>
|
<li><a href="./tests/forum7.test.html">Negatively cite a zero-valued post</a></li>
|
||||||
|
<li><a href="./tests/forum8.test.html">Incinerate reputation</a></li>
|
||||||
</ol>
|
</ol>
|
||||||
</ul>
|
</ul>
|
||||||
<ul>
|
<ul>
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
<script type="module" src="./scripts/forum/forum5.test.js"></script>
|
<script type="module" src="./scripts/forum/forum5.test.js"></script>
|
||||||
<script type="module" src="./scripts/forum/forum6.test.js"></script>
|
<script type="module" src="./scripts/forum/forum6.test.js"></script>
|
||||||
<script type="module" src="./scripts/forum/forum7.test.js"></script>
|
<script type="module" src="./scripts/forum/forum7.test.js"></script>
|
||||||
|
<script type="module" src="./scripts/forum/forum8.test.js"></script>
|
||||||
<script defer class="mocha-init">
|
<script defer class="mocha-init">
|
||||||
mocha.setup({
|
mocha.setup({
|
||||||
ui: 'bdd',
|
ui: 'bdd',
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Forum test 8</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
|
||||||
|
<link type="text/css" rel="stylesheet" href="../index.css" />
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h2><a href="../">DGF Tests</a></h2>
|
||||||
|
<div id="mocha"></div>
|
||||||
|
<div id="scene"></div>
|
||||||
|
</body>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/radash/10.7.0/radash.js"
|
||||||
|
integrity="sha512-S207zKWG3iqXqe6msO7/Mr8X3DzzF4u8meFlokHjGtBPTGUhgzVo0lpcqEy0GoiMUdcoct+H+SqzoLsxXbynzg=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
<script src="https://unpkg.com/mocha/mocha.js"></script>
|
||||||
|
<script src="https://unpkg.com/chai/chai.js"></script>
|
||||||
|
<script type="module" src="./scripts/forum/forum8.test.js"></script>
|
||||||
|
<script defer class="mocha-init">
|
||||||
|
mocha.setup({
|
||||||
|
ui: 'bdd',
|
||||||
|
});
|
||||||
|
chai.should();
|
||||||
|
</script>
|
|
@ -0,0 +1,40 @@
|
||||||
|
import { INCINERATOR_ADDRESS, mochaRun } from '../../../util.js';
|
||||||
|
import { ForumTest } from './forum.test-util.js';
|
||||||
|
|
||||||
|
describe('Forum', function tests() {
|
||||||
|
this.timeout(0);
|
||||||
|
|
||||||
|
const forumTest = new ForumTest();
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
await forumTest.setup();
|
||||||
|
});
|
||||||
|
|
||||||
|
context('Incinerate reputation', async () => {
|
||||||
|
let forum;
|
||||||
|
let experts;
|
||||||
|
let posts;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
forum = forumTest.forum;
|
||||||
|
experts = forumTest.experts;
|
||||||
|
posts = forumTest.posts;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Post1', async () => {
|
||||||
|
await forumTest.addPost(experts[0], 10);
|
||||||
|
forum.getPost(posts[0]).value.should.equal(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Post2 burns reputation from Post1', async () => {
|
||||||
|
await forumTest.addPost(experts[0], 10, [
|
||||||
|
{ postId: posts[0], weight: -1 },
|
||||||
|
{ postId: INCINERATOR_ADDRESS, weight: 1 },
|
||||||
|
]);
|
||||||
|
forum.getPost(posts[0]).value.should.equal(0);
|
||||||
|
forum.getPost(posts[1]).value.should.equal(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
mochaRun();
|
|
@ -4,6 +4,8 @@ const timers = new Map();
|
||||||
|
|
||||||
export const EPSILON = 2.23e-16;
|
export const EPSILON = 2.23e-16;
|
||||||
|
|
||||||
|
export const INCINERATOR_ADDRESS = 0;
|
||||||
|
|
||||||
export const debounce = async (fn, delayMs) => {
|
export const debounce = async (fn, delayMs) => {
|
||||||
const timer = timers.get(fn);
|
const timer = timers.get(fn);
|
||||||
if (timer) {
|
if (timer) {
|
||||||
|
|
Loading…
Reference in New Issue