Refactor to simplify

- Add DAO class to contain the various contracts and data
- Use window?.scene? instead of passing as arg to every constructor
This commit is contained in:
Ladd Hoffman 2023-02-01 21:43:47 -06:00
parent c8ce74cf13
commit 3541802d93
36 changed files with 493 additions and 448 deletions

View File

@ -39,6 +39,7 @@ module.exports = {
context: 'readonly',
it: 'readonly',
specify: 'readonly',
should: 'readonly',
before: 'readonly',
after: 'readonly',
beforeEach: 'readonly',

View File

@ -0,0 +1,5 @@
expert Expert1
expert Expert2
forum Forum
source -- action --> destination

View File

@ -59,14 +59,8 @@ In a messaging system, the work is
- Receiving messages
- Processing messages
- Sending messages
and may include
- Maintaining context related to incoming messages for the duration of some operation
- Performing computations
- Storing data
- Retrieving data
- Performing other actions
The work of verifying peers in a messaging system is
@ -76,6 +70,8 @@ The work of verifying peers in a messaging system is
- Voting in validation pools?
The work of providing a storage service extends that of participating in a messaging system.
- Storing data
- Retrieving data
The work of verifying peers work products in a storage network is

View File

@ -1,16 +1,13 @@
export class Action {
constructor(name, scene) {
constructor(name) {
this.name = name;
this.scene = scene;
}
async log(src, dest, msg, obj, symbol = '->>') {
if (this.scene.sequence) {
await this.scene.sequence.log(
`${src.name} ${symbol} ${dest.name} : ${this.name} ${msg ?? ''} ${
JSON.stringify(obj) ?? ''
}`,
);
}
await window?.scene?.sequence?.log(
`${src.name} ${symbol} ${dest.name} : ${this.name} ${msg ?? ''} ${
JSON.stringify(obj) ?? ''
}`,
);
}
}

View File

@ -1,21 +1,20 @@
import { displayNumber } from '../util.js';
export class Actor {
constructor(name, scene) {
constructor(name) {
this.name = name;
this.scene = scene;
this.callbacks = new Map();
this.status = this.scene.addDisplayValue(`${this.name} status`);
this.status = window?.scene?.addDisplayValue(`${this.name} status`);
this.status.set('Created');
this.values = new Map();
this.valueFunctions = new Map();
this.active = 0;
this.scene.registerActor(this);
window?.scene?.registerActor(this);
}
activate() {
this.active += 1;
this.scene.sequence.log(`activate ${this.name}`, false);
window?.scene?.sequence.log(`activate ${this.name}`, false);
}
async deactivate() {
@ -23,7 +22,7 @@ export class Actor {
throw new Error(`${this.name} is not active, can not deactivate`);
}
this.active -= 1;
await this.scene.sequence.log(`deactivate ${this.name}`);
await window?.scene?.sequence.log(`deactivate ${this.name}`);
}
async send(dest, action, detail) {
@ -36,7 +35,7 @@ export class Actor {
const cb = this.callbacks.get(action.name);
if (!cb) {
throw new Error(
`[${this.scene.name} actor ${this.name} does not have a callback registered for ${action.name}`,
`[${window?.scene?.name} actor ${this.name} does not have a callback registered for ${action.name}`,
);
}
await cb(src, detail);
@ -54,7 +53,7 @@ export class Actor {
}
addValue(label, fn) {
this.values.set(label, this.scene.addDisplayValue(`${this.name} ${label}`));
this.values.set(label, window?.scene?.addDisplayValue(`${this.name} ${label}`));
if (fn) {
this.valueFunctions.set(label, fn);
}
@ -67,11 +66,11 @@ export class Actor {
}
let displayValue = this.values.get(label);
if (!displayValue) {
displayValue = this.scene.addDisplayValue(`${this.name} ${label}`);
displayValue = window?.scene?.addDisplayValue(`${this.name} ${label}`);
this.values.set(label, displayValue);
}
if (value !== displayValue.get()) {
await this.scene.sequence.log(`note over ${this.name} : ${label} = ${value}`);
await window?.scene?.sequence.log(`note over ${this.name} : ${label} = ${value}`);
}
displayValue.set(value);
return this;
@ -85,7 +84,7 @@ export class Actor {
value = displayNumber(value);
}
if (value !== displayValue.get()) {
await this.scene.sequence.log(`note over ${this.name} : ${label} = ${value}`);
await window?.scene?.sequence.log(`note over ${this.name} : ${label} = ${value}`);
}
displayValue.set(value);
}

View File

@ -17,12 +17,12 @@ class Worker {
* Purpose: Enable staking reputation to enter the pool of workers
*/
export class Availability extends Actor {
constructor(bench, name, scene) {
super(name, scene);
this.bench = bench;
constructor(dao, name) {
super(name);
this.dao = dao;
this.actions = {
assignWork: new Action('assign work', scene),
assignWork: new Action('assign work'),
};
this.workers = new Map();
@ -30,7 +30,7 @@ export class Availability extends Actor {
register(reputationPublicKey, { stakeAmount, tokenId, duration }) {
// TODO: Should be signed by token owner
this.bench.reputation.lock(tokenId, stakeAmount, duration);
this.dao.reputation.lock(tokenId, stakeAmount, duration);
const workerId = CryptoUtil.randomUUID();
this.workers.set(workerId, new Worker(reputationPublicKey, tokenId, stakeAmount, duration));
return workerId;

View File

@ -1,11 +1,11 @@
import { DisplayValue } from './display-value.js';
import { CryptoUtil } from './crypto.js';
import { randomID } from '../util.js';
export class Box {
constructor(name, parentEl, elementType = 'div') {
this.name = name;
this.el = document.createElement(elementType);
this.el.id = `box_${CryptoUtil.randomUUID().replaceAll('-', '').slice(0, 8)}`;
this.el.id = `box_${randomID()}`;
this.el.classList.add('box');
if (name) {
this.el.setAttribute('box-name', name);

View File

@ -1,11 +1,15 @@
import { randomID } from '../util.js';
import { Action } from './action.js';
import { Actor } from './actor.js';
import { CryptoUtil } from './crypto.js';
import { PostContent } from './post-content.js';
class Request {
static nextSeq = 0;
constructor(fee, content) {
this.id = CryptoUtil.randomUUID();
this.seq = this.nextSeq;
this.nextSeq += 1;
this.id = `req_${randomID()}`;
this.fee = fee;
this.content = content;
this.worker = null;
@ -16,16 +20,14 @@ class Request {
* Purpose: Enable fee-driven work requests, to be completed by workers from the availability pool
*/
export class Business extends Actor {
constructor(bench, forum, availability, name, scene) {
super(name, scene);
this.bench = bench;
this.forum = forum;
this.availability = availability;
constructor(dao, name) {
super(name);
this.dao = dao;
this.actions = {
assignWork: new Action('assign work', scene),
submitPost: new Action('submit post', scene),
initiateValidationPool: new Action('initiate validation pool', scene),
assignWork: new Action('assign work'),
submitPost: new Action('submit post'),
initiateValidationPool: new Action('initiate validation pool'),
};
this.requests = new Map();
@ -34,8 +36,8 @@ export class Business extends Actor {
async submitRequest(fee, content) {
const request = new Request(fee, content);
this.requests.set(request.id, request);
await this.actions.assignWork.log(this, this.availability);
const worker = await this.availability.assignWork(request.id);
await this.actions.assignWork.log(this, this.dao.availability);
const worker = await this.dao.availability.assignWork(request.id);
request.worker = worker;
return request.id;
}
@ -45,6 +47,10 @@ export class Business extends Actor {
return request;
}
async getRequests() {
return Array.from(this.requests.values());
}
async submitWork(reputationPublicKey, requestId, workEvidence, { tokenLossRatio, duration }) {
const request = this.requests.get(requestId);
if (!request) {
@ -60,16 +66,18 @@ export class Business extends Actor {
requestId,
workEvidence,
});
const requestIndex = Array.from(this.requests.values())
.findIndex(({ id }) => id === request.id);
post.setTitle(`Work Evidence ${requestIndex + 1}`);
await this.actions.submitPost.log(this, this.forum);
const postId = await this.forum.addPost(reputationPublicKey, post);
await this.actions.submitPost.log(this, this.dao);
const postId = await this.dao.forum.addPost(reputationPublicKey, post);
// Initiate a validation pool for this work evidence.
await this.actions.initiateValidationPool.log(this, this.bench);
const pool = await this.bench.initiateValidationPool({
await this.actions.initiateValidationPool.log(this, this.dao);
const pool = await this.dao.initiateValidationPool({
postId,
fee: request.fee,
duration,

View File

@ -1,22 +1,35 @@
import { Actor } from './actor.js';
import { ValidationPool } from './validation-pool.js';
import params from '../params.js';
import { Action } from './action.js';
import params from '../params.js';
import { Forum } from './forum.js';
import { ReputationTokenContract } from './reputation-token.js';
import { ValidationPool } from './validation-pool.js';
import { Availability } from './availability.js';
import { Business } from './business.js';
import { Actor } from './actor.js';
/**
* Purpose: Keep track of reputation holders
* Purpose:
* - Forum: Maintain a directed, acyclic, graph of positively and negatively weighted citations.
* and the value accrued via each post and citation.
* - Reputation: Keep track of reputation accrued to each expert
*/
export class Bench extends Actor {
constructor(forum, name, scene) {
super(name, scene);
this.forum = forum;
this.validationPools = new Map();
this.voters = new Map();
export class DAO extends Actor {
constructor(name) {
super(name);
/* Contracts */
this.forum = new Forum(this, `${name} Forum`);
this.availability = new Availability(this, `${name} Availability`);
this.business = new Business(this, `${name} Business`);
this.reputation = new ReputationTokenContract();
/* Data */
this.validationPools = new Map();
this.experts = new Map();
this.actions = {
createValidationPool: new Action('create validation pool', scene),
addPost: new Action('add post'),
createValidationPool: new Action('create validation pool'),
};
}
@ -25,7 +38,7 @@ export class Bench extends Actor {
}
listActiveVoters() {
return Array.from(this.voters.values()).filter((voter) => {
return Array.from(this.experts.values()).filter((voter) => {
const hasVoted = !!voter.dateLastVote;
const withinThreshold = !params.activeVoterThreshold
|| new Date() - voter.dateLastVote >= params.activeVoterThreshold;
@ -48,7 +61,7 @@ export class Bench extends Actor {
async initiateValidationPool(poolOptions, stakeOptions) {
const validationPoolNumber = this.validationPools.size + 1;
const name = `Pool${validationPoolNumber}`;
const pool = new ValidationPool(this, this.forum, poolOptions, name, this.scene);
const pool = new ValidationPool(this, poolOptions, name);
this.validationPools.set(pool.id, pool);
await this.actions.createValidationPool.log(this, pool);
pool.activate();

View File

@ -4,16 +4,17 @@ import { CryptoUtil } from './crypto.js';
import { ReputationHolder } from './reputation-holder.js';
export class Expert extends ReputationHolder {
constructor(name, scene) {
super(undefined, name, scene);
constructor(dao, name) {
super(name);
this.dao = dao;
this.actions = {
submitPostViaNetwork: new Action('submit post via network', scene),
submitPost: new Action('submit post', scene),
initiateValidationPool: new Action('initiate validation pool', scene),
stake: new Action('stake on post', scene),
registerAvailability: new Action('register availability', scene),
getAssignedWork: new Action('get assigned work', scene),
submitWork: new Action('submit work evidence', scene),
submitPostViaNetwork: new Action('submit post via network'),
submitPost: new Action('submit post'),
initiateValidationPool: new Action('initiate validation pool'),
stake: new Action('stake on post'),
registerAvailability: new Action('register availability'),
getAssignedWork: new Action('get assigned work'),
submitWork: new Action('submit work evidence'),
};
this.validationPools = new Map();
this.tokens = [];
@ -36,29 +37,29 @@ export class Expert extends ReputationHolder {
await forumNode.receiveMessage(JSON.stringify(postMessage.toJSON()));
}
async submitPost(forum, postContent) {
async submitPost(postContent) {
// TODO: Include fee
await this.actions.submitPost.log(this, forum);
return forum.addPost(this.reputationPublicKey, postContent);
await this.actions.submitPost.log(this, this.dao.forum);
return this.dao.forum.addPost(this.reputationPublicKey, postContent);
}
async submitPostWithFee(bench, forum, postContent, poolOptions) {
await this.actions.submitPost.log(this, forum);
const postId = await forum.addPost(this.reputationPublicKey, postContent);
const pool = await this.initiateValidationPool(bench, { ...poolOptions, postId });
async submitPostWithFee(postContent, poolOptions) {
await this.actions.submitPost.log(this, this.dao.forum);
const postId = await this.dao.forum.addPost(this.reputationPublicKey, postContent);
const pool = await this.initiateValidationPool({ ...poolOptions, postId });
this.tokens.push(pool.tokenId);
return { postId, pool };
}
async initiateValidationPool(bench, poolOptions) {
async initiateValidationPool(poolOptions) {
// For now, directly call bench.initiateValidationPool();
poolOptions.reputationPublicKey = this.reputationPublicKey;
await this.actions.initiateValidationPool.log(
this,
bench,
this.dao,
`(fee: ${poolOptions.fee}, stake: ${poolOptions.authorStakeAmount ?? 0})`,
);
const pool = await bench.initiateValidationPool(poolOptions);
const pool = await this.dao.initiateValidationPool(poolOptions);
this.tokens.push(pool.tokenId);
this.validationPools.set(pool.id, poolOptions);
return pool;
@ -79,23 +80,27 @@ export class Expert extends ReputationHolder {
});
}
async registerAvailability(availability, stakeAmount, duration) {
await this.actions.registerAvailability.log(this, availability, `(stake: ${stakeAmount}, duration: ${duration})`);
this.workerId = await availability.register(this.reputationPublicKey, {
async registerAvailability(stakeAmount, duration) {
await this.actions.registerAvailability.log(
this,
this.dao.availability,
`(stake: ${stakeAmount}, duration: ${duration})`,
);
this.workerId = await this.dao.availability.register(this.reputationPublicKey, {
stakeAmount,
tokenId: this.tokens[0],
duration,
});
}
async getAssignedWork(availability, business) {
const requestId = await availability.getAssignedWork(this.workerId);
const request = await business.getRequest(requestId);
async getAssignedWork() {
const requestId = await this.dao.availability.getAssignedWork(this.workerId);
const request = await this.dao.business.getRequest(requestId);
return request;
}
async submitWork(business, requestId, evidence, { tokenLossRatio, duration }) {
await this.actions.submitWork.log(this, business);
return business.submitWork(this.reputationPublicKey, requestId, evidence, { tokenLossRatio, duration });
async submitWork(requestId, evidence, { tokenLossRatio, duration }) {
await this.actions.submitWork.log(this, this.dao.business);
return this.dao.business.submitWork(this.reputationPublicKey, requestId, evidence, { tokenLossRatio, duration });
}
}

View File

@ -2,17 +2,17 @@ import { Action } from './action.js';
import {
Message, PostMessage, PeerMessage, messageFromJSON,
} from './message.js';
import { CryptoUtil } from './crypto.js';
import { ForumView } from './forum-view.js';
import { NetworkNode } from './network-node.js';
import { randomID } from '../util.js';
export class ForumNode extends NetworkNode {
constructor(name, scene) {
super(name, scene);
constructor(name) {
super(name);
this.forumView = new ForumView();
this.actions = {
...this.actions,
storePost: new Action('store post', scene),
storePost: new Action('store post'),
};
}
@ -42,7 +42,7 @@ export class ForumNode extends NetworkNode {
// Process an incoming post, received by whatever means
async processPost(authorId, post) {
if (!post.id) {
post.id = CryptoUtil.randomUUID();
post.id = randomID();
}
await this.actions.storePost.log(this, this);
// this.forumView.addPost(authorId, post.id, post, stake);

View File

@ -1,69 +1,28 @@
import { Actor } from './actor.js';
import { WDAG } from './wdag.js';
import { Action } from './action.js';
import { CryptoUtil } from './crypto.js';
import params from '../params.js';
import { ReputationHolder } from './reputation-holder.js';
import { displayNumber, EPSILON } from '../util.js';
import { EPSILON } from '../util.js';
import { Post } from './post.js';
const CITATION = 'citation';
const BALANCE = 'balance';
class Post extends Actor {
constructor(forum, authorPublicKey, postContent) {
const index = forum.posts.countVertices();
const name = `Post${index + 1}`;
super(name, forum.scene);
this.id = postContent.id ?? name;
this.authorPublicKey = authorPublicKey;
this.value = 0;
this.initialValue = 0;
this.citations = postContent.citations;
this.title = postContent.title;
const leachingTotal = this.citations
.filter(({ weight }) => weight < 0)
.reduce((total, { weight }) => total += -weight, 0);
const donationTotal = this.citations
.filter(({ weight }) => weight > 0)
.reduce((total, { weight }) => total += weight, 0);
if (leachingTotal > params.revaluationLimit) {
throw new Error('Post leaching total exceeds revaluation limit '
+ `(${leachingTotal} > ${params.revaluationLimit})`);
}
if (donationTotal > params.revaluationLimit) {
throw new Error('Post donation total exceeds revaluation limit '
+ `(${donationTotal} > ${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}`);
}
}
getLabel() {
return `${this.name}
<table><tr>
<td>initial</td>
<td>${displayNumber(this.initialValue)}</td>
</tr><tr>
<td>value</td>
<td>${displayNumber(this.value)}</td>
</tr></table>`
.replaceAll(/\n\s*/g, '');
}
}
/**
* Purpose: Maintain a directed, acyclic, weighted graph of posts referencing other posts
* Purpose:
* - Forum: Maintain a directed, acyclic, graph of positively and negatively weighted citations.
* and the value accrued via each post and citation.
*/
export class Forum extends ReputationHolder {
constructor(name, scene) {
super(`forum_${CryptoUtil.randomUUID()}`, name, scene);
constructor(dao, name) {
super(name);
this.dao = dao;
this.id = this.reputationPublicKey;
this.posts = new WDAG(scene);
this.posts = new WDAG();
this.actions = {
addPost: new Action('add post', scene),
propagateValue: new Action('propagate', this.scene),
transfer: new Action('transfer', this.scene),
addPost: new Action('add post'),
propagateValue: new Action('propagate'),
transfer: new Action('transfer'),
};
}
@ -96,10 +55,10 @@ export class Forum extends ReputationHolder {
}
async onValidate({
bench, postId, tokenId,
postId, tokenId,
}) {
this.activate();
const initialValue = bench.reputation.valueOf(tokenId);
const initialValue = this.dao.reputation.valueOf(tokenId);
const postVertex = this.posts.getVertex(postId);
const post = postVertex.data;
post.setStatus('Validated');
@ -121,16 +80,16 @@ export class Forum extends ReputationHolder {
// Apply computed rewards to update values of tokens
for (const [id, value] of rewardsAccumulator) {
if (value < 0) {
bench.reputation.transferValueFrom(id, post.tokenId, -value);
this.dao.reputation.transferValueFrom(id, post.tokenId, -value);
} else {
bench.reputation.transferValueFrom(post.tokenId, id, value);
this.dao.reputation.transferValueFrom(post.tokenId, id, value);
}
}
// Transfer ownership of the minted/staked token, from the forum to the post author
bench.reputation.transferFrom(this.id, post.authorPublicKey, post.tokenId);
const toActor = this.scene.findActor((actor) => actor.reputationPublicKey === post.authorPublicKey);
const value = bench.reputation.valueOf(post.tokenId);
// Transfer ownership of the minted/staked token, from the posts to the post author
this.dao.reputation.transferFrom(this.id, post.authorPublicKey, post.tokenId);
const toActor = window?.scene?.findActor((actor) => actor.reputationPublicKey === post.authorPublicKey);
const value = this.dao.reputation.valueOf(post.tokenId);
this.actions.transfer.log(this, toActor, `(${value})`);
this.deactivate();
}

View File

@ -4,11 +4,11 @@ import { CryptoUtil } from './crypto.js';
import { PrioritizedQueue } from './prioritized-queue.js';
export class NetworkNode extends Actor {
constructor(name, scene) {
super(name, scene);
constructor(name) {
super(name);
this.queue = new PrioritizedQueue();
this.actions = {
peerMessage: new Action('peer message', scene),
peerMessage: new Action('peer message'),
};
}

View File

@ -0,0 +1,46 @@
import { Actor } from './actor.js';
import { displayNumber } from '../util.js';
import params from '../params.js';
export class Post extends Actor {
constructor(forum, authorPublicKey, postContent) {
const index = forum.posts.countVertices();
const name = `Post${index + 1}`;
super(name, forum.scene);
this.id = postContent.id ?? name;
this.authorPublicKey = authorPublicKey;
this.value = 0;
this.initialValue = 0;
this.citations = postContent.citations;
this.title = postContent.title;
const leachingTotal = this.citations
.filter(({ weight }) => weight < 0)
.reduce((total, { weight }) => total += -weight, 0);
const donationTotal = this.citations
.filter(({ weight }) => weight > 0)
.reduce((total, { weight }) => total += weight, 0);
if (leachingTotal > params.revaluationLimit) {
throw new Error('Post leaching total exceeds revaluation limit '
+ `(${leachingTotal} > ${params.revaluationLimit})`);
}
if (donationTotal > params.revaluationLimit) {
throw new Error('Post donation total exceeds revaluation limit '
+ `(${donationTotal} > ${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}`);
}
}
getLabel() {
return `${this.name}
<table><tr>
<td>initial</td>
<td>${displayNumber(this.initialValue)}</td>
</tr><tr>
<td>value</td>
<td>${displayNumber(this.value)}</td>
</tr></table>`
.replaceAll(/\n\s*/g, '');
}
}

View File

@ -2,10 +2,10 @@ import { Action } from './action.js';
import { Actor } from './actor.js';
export class Public extends Actor {
constructor(name, scene) {
super(name, scene);
constructor(name) {
super(name);
this.actions = {
submitRequest: new Action('submit work request', scene),
submitRequest: new Action('submit work request'),
};
}

View File

@ -1,8 +1,9 @@
import { randomID } from '../util.js';
import { Actor } from './actor.js';
export class ReputationHolder extends Actor {
constructor(reputationPublicKey, name, scene) {
super(name, scene);
this.reputationPublicKey = reputationPublicKey;
constructor(name) {
super(name);
this.reputationPublicKey = `${name}_${randomID()}`;
}
}

View File

@ -1,7 +1,6 @@
import { ERC721 } from './erc721.js';
import { CryptoUtil } from './crypto.js';
import { EPSILON } from '../util.js';
import { EPSILON, randomID } from '../util.js';
class Lock {
constructor(tokenId, amount, duration) {
@ -21,7 +20,7 @@ export class ReputationTokenContract extends ERC721 {
}
mint(to, value, context) {
const tokenId = `token_${CryptoUtil.randomUUID()}`;
const tokenId = `token_${randomID()}`;
super.mint(to, tokenId);
this.values.set(tokenId, value);
this.histories.set(tokenId, [{ increment: value, context }]);

View File

@ -173,7 +173,7 @@ export class Scene {
}
async addActor(name) {
const actor = new Actor(name, this);
const actor = new Actor(name);
if (this.sequence) {
await this.sequence.log(`participant ${name}`);
}

View File

@ -1,4 +1,3 @@
import { CryptoUtil } from './crypto.js';
import { ReputationHolder } from './reputation-holder.js';
import { Stake } from './stake.js';
import { Voter } from './voter.js';
@ -16,8 +15,7 @@ const ValidationPoolStates = Object.freeze({
*/
export class ValidationPool extends ReputationHolder {
constructor(
bench,
forum,
dao,
{
postId,
reputationPublicKey,
@ -27,14 +25,13 @@ export class ValidationPool extends ReputationHolder {
contentiousDebate = false,
},
name,
scene,
) {
super(`pool_${CryptoUtil.randomUUID()}`, name, scene);
super(name);
this.id = this.reputationPublicKey;
this.actions = {
reward: new Action('reward', scene),
transfer: new Action('transfer', scene),
reward: new Action('reward'),
transfer: new Action('transfer'),
};
// If contentiousDebate = true, we will follow the progression defined by getTokenLossRatio()
@ -59,8 +56,7 @@ export class ValidationPool extends ReputationHolder {
}]; got ${duration}`,
);
}
this.bench = bench;
this.forum = forum;
this.dao = dao;
this.postId = postId;
this.state = ValidationPoolStates.OPEN;
this.setStatus('Open');
@ -72,7 +68,7 @@ export class ValidationPool extends ReputationHolder {
this.tokenLossRatio = tokenLossRatio;
this.contentiousDebate = contentiousDebate;
this.mintedValue = fee * params.mintingRatio();
this.tokenId = this.bench.reputation.mint(this.id, this.mintedValue);
this.tokenId = this.dao.reputation.mint(this.id, this.mintedValue);
// Tokens minted "for" the post go toward stake of author voting for their own post.
// Also, author can provide additional stakes, e.g. availability stakes for work evidence post.
this.stake(this.id, {
@ -87,9 +83,9 @@ export class ValidationPool extends ReputationHolder {
});
// Keep a record of voters and their votes
const voter = this.bench.voters.get(reputationPublicKey) ?? new Voter(reputationPublicKey);
const voter = this.dao.experts.get(reputationPublicKey) ?? new Voter(reputationPublicKey);
voter.addVoteRecord(this);
this.bench.voters.set(reputationPublicKey, voter);
this.dao.experts.set(reputationPublicKey, voter);
}
getTokenLossRatio() {
@ -158,7 +154,7 @@ export class ValidationPool extends ReputationHolder {
);
}
if (reputationPublicKey !== this.bench.reputation.ownerOf(tokenId)) {
if (reputationPublicKey !== this.dao.reputation.ownerOf(tokenId)) {
throw new Error('Reputation may only be staked by its owner!');
}
@ -168,18 +164,18 @@ export class ValidationPool extends ReputationHolder {
this.stakes.add(stake);
// Transfer staked amount from the sender to the validation pool
this.bench.reputation.transferValueFrom(tokenId, this.tokenId, amount);
this.dao.reputation.transferValueFrom(tokenId, this.tokenId, amount);
// Keep a record of voters and their votes
if (tokenId !== this.tokenId) {
const voter = this.bench.voters.get(reputationPublicKey) ?? new Voter(reputationPublicKey);
const voter = this.dao.experts.get(reputationPublicKey) ?? new Voter(reputationPublicKey);
voter.addVoteRecord(this);
this.bench.voters.set(reputationPublicKey, voter);
this.dao.experts.set(reputationPublicKey, voter);
}
// Update computed display values
for (const voter of this.bench.voters.values()) {
const actor = this.scene.findActor((a) => a.reputationPublicKey === voter.reputationPublicKey);
for (const voter of this.dao.experts.values()) {
const actor = window?.scene?.findActor((a) => a.reputationPublicKey === voter.reputationPublicKey);
await actor.computeValues();
}
}
@ -189,7 +185,7 @@ export class ValidationPool extends ReputationHolder {
// we need to make sure any staked tokens are locked for the
// specified amounts of time.
for (const { tokenId, amount, lockingTime } of this.stakes.values()) {
this.bench.reputation.lock(tokenId, amount, lockingTime);
this.dao.reputation.lock(tokenId, amount, lockingTime);
// TODO: If there is an exception here, the voter may have voted incorrectly. Consider penalties.
}
}
@ -208,7 +204,7 @@ export class ValidationPool extends ReputationHolder {
const upvoteValue = this.getTotalValueOfStakesForOutcome(true);
const downvoteValue = this.getTotalValueOfStakesForOutcome(false);
const activeAvailableReputation = this.bench.getActiveAvailableReputation();
const activeAvailableReputation = this.dao.getActiveAvailableReputation();
const votePasses = upvoteValue >= params.winningRatio * downvoteValue;
const quorumMet = upvoteValue + downvoteValue >= params.quorum * activeAvailableReputation;
@ -220,13 +216,13 @@ export class ValidationPool extends ReputationHolder {
if (quorumMet) {
this.setStatus(`Resolved - ${votePasses ? 'Won' : 'Lost'}`);
this.scene.sequence.log(`note over ${this.name} : ${votePasses ? 'Win' : 'Lose'}`);
window?.scene?.sequence.log(`note over ${this.name} : ${votePasses ? 'Win' : 'Lose'}`);
this.applyTokenLocking();
await this.distributeReputation({ votePasses });
// TODO: distribute fees
} else {
this.setStatus('Resolved - Quorum not met');
this.scene.sequence.log(`note over ${this.name} : Quorum not met`);
window?.scene?.sequence.log(`note over ${this.name} : Quorum not met`);
}
this.deactivate();
@ -250,26 +246,25 @@ export class ValidationPool extends ReputationHolder {
const value = stake.getStakeValue();
const reward = tokensForWinners * (value / totalValueOfStakesForWin);
// Also return each winning voter their staked amount
const reputationPublicKey = this.bench.reputation.ownerOf(tokenId);
const reputationPublicKey = this.dao.reputation.ownerOf(tokenId);
console.log(`reward for winning stake by ${reputationPublicKey}: ${reward}`);
this.bench.reputation.transferValueFrom(this.tokenId, tokenId, reward + amount);
const toActor = this.scene.findActor((actor) => actor.reputationPublicKey === reputationPublicKey);
this.dao.reputation.transferValueFrom(this.tokenId, tokenId, reward + amount);
const toActor = window?.scene?.findActor((actor) => actor.reputationPublicKey === reputationPublicKey);
this.actions.reward.log(this, toActor, `(${reward})`);
}
if (votePasses && !!this.forum) {
if (votePasses) {
// Distribute awards to author via the forum
// const tokensForAuthor = this.mintedValue * params.stakeForAuthor + rewards.get(this.tokenId);
console.log(`sending reward for author stake to forum: ${this.bench.reputation.valueOf(this.tokenId)}`);
console.log(`sending reward for author stake to forum: ${this.dao.reputation.valueOf(this.tokenId)}`);
// Transfer ownership of the minted token, from the pool to the forum
this.bench.reputation.transferFrom(this.id, this.forum.id, this.tokenId);
const value = this.bench.reputation.valueOf(this.tokenId);
this.actions.transfer.log(this, this.forum, `(${value})`);
this.dao.reputation.transferFrom(this.id, this.dao.forum.id, this.tokenId);
const value = this.dao.reputation.valueOf(this.tokenId);
this.actions.transfer.log(this, this.dao.forum, `(${value})`);
// Recurse through forum to determine reputation effects
await this.forum.onValidate({
bench: this.bench,
await this.dao.forum.onValidate({
pool: this,
postId: this.postId,
tokenId: this.tokenId,
@ -279,15 +274,12 @@ export class ValidationPool extends ReputationHolder {
console.log('pool complete');
// Update computed display values
for (const voter of this.bench.voters.values()) {
const actor = this.scene.findActor((a) => a.reputationPublicKey === voter.reputationPublicKey);
for (const voter of this.dao.experts.values()) {
const actor = window?.scene?.findActor((a) => a.reputationPublicKey === voter.reputationPublicKey);
await actor.computeValues();
}
await this.bench.computeValues();
if (this.forum) {
await this.forum.computeValues();
}
await this.dao.forum.computeValues();
this.scene.stateToTable(`validation pool ${this.name} complete`);
window?.scene?.stateToTable(`validation pool ${this.name} complete`);
}
}

View File

@ -25,17 +25,16 @@ export class Edge {
}
export class WDAG {
constructor(scene) {
this.scene = scene;
constructor() {
this.vertices = new Map();
this.edgeLabels = new Map();
this.nextVertexId = 0;
this.flowchart = scene?.flowchart ?? null;
this.flowchart = window?.scene?.flowchart ?? null;
}
withFlowchart() {
this.scene.withAdditionalFlowchart();
this.flowchart = this.scene.lastFlowchart();
window?.scene?.withAdditionalFlowchart();
this.flowchart = window?.scene?.lastFlowchart();
return this;
}
@ -71,7 +70,7 @@ export class WDAG {
}
static getEdgeKey({ from, to }) {
return btoa([from.id, to.id]).replaceAll(/[^A-Z]+/g, '');
return btoa([from.id, to.id]).replaceAll(/[^A-Za-z0-9]+/g, '');
}
getEdge(label, from, to) {
@ -122,7 +121,7 @@ export class WDAG {
from = from instanceof Vertex ? from : this.getVertex(from);
to = to instanceof Vertex ? to : this.getVertex(to);
if (this.getEdge(label, from, to)) {
throw new Error(`Edge ${label} from ${from} to ${to} already exists`);
throw new Error(`Edge ${label} from ${from.id} to ${to.id} already exists`);
}
const edge = this.setEdgeWeight(label, from, to, weight);
from.edges.from.push(edge);

View File

@ -4,187 +4,7 @@
<link type="text/css" rel="stylesheet" href="../index.css" />
</head>
<body>
<div id="availability-test"></div>
<div id="scene"></div>
</body>
<script type="module">
import { Box } from '../classes/box.js';
import { Scene } from '../classes/scene.js';
import { Expert } from '../classes/expert.js';
import { Bench } from '../classes/bench.js';
import { Business } from '../classes/business.js';
import { Availability } from '../classes/availability.js';
import { delay } from '../util.js';
import { Forum } from '../classes/forum.js';
import { Public } from '../classes/public.js';
import { PostContent } from '../classes/post-content.js';
const DELAY_INTERVAL = 500;
const rootElement = document.getElementById('availability-test');
const rootBox = new Box('rootBox', rootElement).flex();
const scene = (window.scene = new Scene('Availability test', rootBox));
scene.withSequenceDiagram();
scene.withFlowchart();
scene.withTable();
const experts = (window.experts = []);
const newExpert = async () => {
const index = experts.length;
const name = `Expert${index + 1}`;
const expert = await new Expert(name, scene).initialize();
experts.push(expert);
return expert;
};
const expert1 = await newExpert();
const expert2 = await newExpert();
const forum = (window.forum = new Forum('Forum', scene));
const bench = (window.bench = new Bench(forum, 'Bench', scene));
const availability = (window.availability = new Availability(
bench,
'Availability',
scene,
));
const business = (window.business = new Business(
bench,
forum,
availability,
'Business',
scene,
));
const requestor = new Public('Public', scene);
const updateDisplayValues = async () => {
for (const expert of experts) {
await expert.setValue(
'rep',
bench.reputation.valueOwnedBy(expert.reputationPublicKey),
);
}
await bench.setValue('total rep', bench.reputation.getTotal());
await scene.sequence.render();
};
const updateDisplayValuesAndDelay = async (delayMs = DELAY_INTERVAL) => {
await updateDisplayValues();
await delay(delayMs);
};
const getActiveWorker = async () => {
let worker;
let request;
for (const expert of experts) {
request = await expert.getAssignedWork(availability, business);
if (request) {
worker = expert;
await worker.actions.getAssignedWork.log(worker, availability);
worker.activate();
break;
}
}
return { worker, request };
};
const voteForWorkEvidence = async (worker, pool) => {
for (const expert of experts) {
if (expert !== worker) {
await expert.stake(pool, {
position: true,
amount: 1,
});
}
}
};
await updateDisplayValuesAndDelay();
// Experts gain initial reputation by submitting a post with fee
const { postId: postId1, pool: pool1 } = await expert1.submitPostWithFee(
bench,
forum,
new PostContent({ hello: 'there' }).setTitle('Post 1'),
{
fee: 10,
duration: 1000,
tokenLossRatio: 1,
},
);
await updateDisplayValuesAndDelay(1000);
await pool1.evaluateWinningConditions();
await updateDisplayValuesAndDelay();
const { pool: pool2 } = await expert2.submitPostWithFee(
bench,
forum,
new PostContent({ hello: 'to you as well' })
.setTitle('Post 2')
.addCitation(postId1, 0.5),
{
fee: 10,
duration: 1000,
tokenLossRatio: 1,
},
);
await updateDisplayValuesAndDelay(1000);
await pool2.evaluateWinningConditions();
await updateDisplayValuesAndDelay();
// Populate availability pool
await expert1.registerAvailability(availability, 1, 10000);
await expert2.registerAvailability(availability, 1, 10000);
await updateDisplayValuesAndDelay();
// Submit work request
await requestor.submitRequest(
business,
{ fee: 100 },
{ please: 'do some work' },
);
await updateDisplayValuesAndDelay();
// Receive work request
const { worker, request } = await getActiveWorker();
// Submit work evidence
const pool = await worker.submitWork(
business,
request.id,
{
here: 'is some evidence of work product',
},
{
tokenLossRatio: 1,
duration: 1000,
},
);
worker.deactivate();
await updateDisplayValuesAndDelay();
// Stake on work evidence
await voteForWorkEvidence(worker, pool);
await updateDisplayValuesAndDelay();
// Wait for validation pool duration to elapse
await delay(1000);
// Distribute reputation awards and fees
await pool.evaluateWinningConditions();
await updateDisplayValuesAndDelay();
// This should throw an exception since the pool is already resolved
try {
await pool.evaluateWinningConditions();
} catch (e) {
if (e.message.match(/Validation pool has already been resolved/)) {
console.log(
'Caught expected error: Validation pool has already been resolved',
);
} else {
console.error('Unexpected error');
throw e;
}
}
<script type="module" src="./scripts/availability.test.js">
</script>

View File

@ -26,7 +26,7 @@
}
if (true) {
const scene = new Scene('Scene 1', rootBox).withSequenceDiagram();
const scene = (window.scene = new Scene('Scene 1', rootBox).withSequenceDiagram());
const webClientStatus = scene.addDisplayValue('WebClient Status');
const node1Status = scene.addDisplayValue('Node 1 Status');
const blockchainStatus = scene.addDisplayValue('Blockchain Status');

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<head>
<title>Business</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>
<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/business.test.js"></script>
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'dao', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
window.should = chai.should();
</script>
<script defer class="mocha-exec">
// TODO: Weird race condition -- resolve this in a better way
setTimeout(() => mocha.run(), 1000);
</script>

View File

@ -16,7 +16,7 @@
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
globals: ['scene', 'dao', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();

View File

@ -16,7 +16,7 @@
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
globals: ['scene', 'dao', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();

View File

@ -16,7 +16,7 @@
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
globals: ['scene', 'dao', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();

View File

@ -16,7 +16,7 @@
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
globals: ['scene', 'dao', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();

View File

@ -0,0 +1,165 @@
import { Box } from '../../classes/box.js';
import { Scene } from '../../classes/scene.js';
import { Expert } from '../../classes/expert.js';
import { delay } from '../../util.js';
import { DAO } from '../../classes/dao.js';
import { Public } from '../../classes/public.js';
import { PostContent } from '../../classes/post-content.js';
const DELAY_INTERVAL = 500;
let dao;
let experts;
let requestor;
const newExpert = async () => {
const index = experts.length;
const name = `Expert${index + 1}`;
const expert = await new Expert(dao, name).initialize();
experts.push(expert);
return expert;
};
async function setup() {
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
const scene = (window.scene = new Scene('Availability test', rootBox));
scene.withSequenceDiagram();
scene.withFlowchart();
scene.withTable();
experts = (window.experts = []);
dao = (window.dao = new DAO('DGF'));
await newExpert();
await newExpert();
requestor = new Public('Public');
}
const updateDisplayValues = async () => {
for (const expert of experts) {
await expert.setValue(
'rep',
dao.reputation.valueOwnedBy(expert.reputationPublicKey),
);
}
await dao.setValue('total rep', dao.reputation.getTotal());
await window.scene.sequence.render();
};
const updateDisplayValuesAndDelay = async (delayMs = DELAY_INTERVAL) => {
await updateDisplayValues();
await delay(delayMs);
};
const getActiveWorker = async () => {
let worker;
let request;
for (const expert of experts) {
request = await expert.getAssignedWork();
if (request) {
worker = expert;
await worker.actions.getAssignedWork.log(worker, dao.availability);
worker.activate();
break;
}
}
return { worker, request };
};
const voteForWorkEvidence = async (worker, pool) => {
for (const expert of experts) {
if (expert !== worker) {
await expert.stake(pool, {
position: true,
amount: 1,
});
}
}
};
await setup();
await updateDisplayValuesAndDelay();
// Experts gain initial reputation by submitting a post with fee
const { postId: postId1, pool: pool1 } = await experts[0].submitPostWithFee(
new PostContent({ hello: 'there' }).setTitle('Post 1'),
{
fee: 10,
duration: 1000,
tokenLossRatio: 1,
},
);
await updateDisplayValuesAndDelay(1000);
await pool1.evaluateWinningConditions();
await updateDisplayValuesAndDelay();
const { pool: pool2 } = await experts[1].submitPostWithFee(
new PostContent({ hello: 'to you as well' })
.setTitle('Post 2')
.addCitation(postId1, 0.5),
{
fee: 10,
duration: 1000,
tokenLossRatio: 1,
},
);
await updateDisplayValuesAndDelay(1000);
await pool2.evaluateWinningConditions();
await updateDisplayValuesAndDelay();
// Populate availability pool
await experts[0].registerAvailability(1, 10000);
await experts[1].registerAvailability(1, 10000);
await updateDisplayValuesAndDelay();
// Submit work request
await requestor.submitRequest(
dao.business,
{ fee: 100 },
{ please: 'do some work' },
);
await updateDisplayValuesAndDelay();
// Receive work request
const { worker, request } = await getActiveWorker();
// Submit work evidence
const pool = await worker.submitWork(
request.id,
{
here: 'is some evidence of work product',
},
{
tokenLossRatio: 1,
duration: 1000,
},
);
worker.deactivate();
await updateDisplayValuesAndDelay();
// Stake on work evidence
await voteForWorkEvidence(worker, pool);
await updateDisplayValuesAndDelay();
// Wait for validation pool duration to elapse
await delay(1000);
// Distribute reputation awards and fees
await pool.evaluateWinningConditions();
await updateDisplayValuesAndDelay();
// This should throw an exception since the pool is already resolved
try {
await pool.evaluateWinningConditions();
} catch (e) {
if (e.message.match(/Validation pool has already been resolved/)) {
console.log(
'Caught expected error: Validation pool has already been resolved',
);
} else {
console.error('Unexpected error');
throw e;
}
}

View File

@ -0,0 +1,16 @@
import { Business } from '../../classes/business.js';
import { Scene } from '../../classes/scene.js';
import { Box } from '../../classes/box.js';
describe('Business', () => {
let scene;
before(async () => {
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
scene = new Scene('Business', rootBox);
});
it('Should exist', () => {
const business = new Business(null, 'Business', scene);
should.exist(business);
});
});

View File

@ -4,8 +4,7 @@ import { PostContent } from '../../classes/post-content.js';
import { Expert } from '../../classes/expert.js';
import { ForumNode } from '../../classes/forum-node.js';
import { Network } from '../../classes/network.js';
import { CryptoUtil } from '../../classes/crypto.js';
import { delay } from '../../util.js';
import { delay, randomID } from '../../util.js';
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
@ -36,7 +35,7 @@ const processInterval = setInterval(async () => {
// const blockchain = new Blockchain();
window.post1 = new PostContent({ message: 'hi' });
window.post1.id = CryptoUtil.randomUUID();
window.post1.id = randomID();
window.post2 = new PostContent({ message: 'hello' }).addCitation(
window.post1.id,
1.0,

View File

@ -1,17 +1,14 @@
import { Box } from '../../../classes/box.js';
import { Scene } from '../../../classes/scene.js';
import { Expert } from '../../../classes/expert.js';
import { Bench } from '../../../classes/bench.js';
import { delay } from '../../../util.js';
import { Forum } from '../../../classes/forum.js';
import { PostContent } from '../../../classes/post-content.js';
import { delay } from '../../../util.js';
import params from '../../../params.js';
import { DAO } from '../../../classes/dao.js';
export class ForumTest {
constructor(options) {
this.scene = null;
this.forum = null;
this.bench = null;
this.dao = null;
this.experts = null;
this.posts = null;
this.options = {
@ -24,16 +21,16 @@ export class ForumTest {
async newExpert() {
const index = this.experts.length;
const name = `Expert${index + 1}`;
const expert = await new Expert(name, this.scene).initialize();
const expert = await new Expert(this.dao, name).initialize();
this.experts.push(expert);
// expert.addValue('rep', () => this.bench.reputation.valueOwnedBy(expert.reputationPublicKey));
// expert.addValue('rep', () => this.dao.reputation.valueOwnedBy(expert.reputationPublicKey));
return expert;
}
async addPost(author, fee, citations = []) {
const postIndex = this.posts.length;
const title = `posts[${postIndex}]`;
await this.scene.startSection();
await window.scene.startSection();
const postContent = new PostContent({}).setTitle(title);
for (const { postId, weight } of citations) {
@ -41,8 +38,6 @@ export class ForumTest {
}
const { pool, postId } = await author.submitPostWithFee(
this.bench,
this.forum,
postContent,
{
fee,
@ -53,7 +48,7 @@ export class ForumTest {
this.posts.push(postId);
await delay(this.options.poolDurationMs);
await pool.evaluateWinningConditions();
await this.scene.endSection();
await window.scene.endSection();
await delay(this.options.defaultDelayMs);
return postId;
}
@ -62,21 +57,21 @@ export class ForumTest {
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
this.scene = (window.scene = new Scene('Forum test', rootBox));
this.scene.withSequenceDiagram();
this.scene.withFlowchart();
this.scene.withTable();
const scene = (window.scene = new Scene('Forum test', rootBox));
scene.withSequenceDiagram();
scene.withFlowchart();
scene.withTable();
this.scene.addDisplayValue('c3. stakeForAuthor').set(params.stakeForAuthor);
this.scene.addDisplayValue('q2. revaluationLimit').set(params.revaluationLimit);
this.scene
scene.addDisplayValue('c3. stakeForAuthor').set(params.stakeForAuthor);
scene.addDisplayValue('q2. revaluationLimit').set(params.revaluationLimit);
scene
.addDisplayValue('q3. referenceChainLimit')
.set(params.referenceChainLimit);
this.scene.addDisplayValue('q4. leachingValue').set(params.leachingValue);
this.scene.addDisplayValue('&nbsp;');
scene.addDisplayValue('q4. leachingValue').set(params.leachingValue);
scene.addDisplayValue('&nbsp;');
this.forum = (window.forum = new Forum('Forum', this.scene));
this.bench = (window.bench = new Bench(this.forum, 'Bench', this.scene));
this.dao = (window.dao = new DAO('DGF'));
this.forum = this.dao.forum;
this.experts = (window.experts = []);
this.posts = (window.posts = []);
@ -84,7 +79,7 @@ export class ForumTest {
// await newExpert();
// await newExpert();
// bench.addValue('total rep', () => bench.reputation.getTotal());
this.forum.addValue('total value', () => this.forum.getTotalValue());
// this.dao.addValue('total rep', () => this.dao.reputation.getTotal());
this.dao.forum.addValue('total value', () => this.dao.forum.getTotalValue());
}
}

View File

@ -1,24 +1,22 @@
import { Box } from '../../classes/box.js';
import { Scene } from '../../classes/scene.js';
import { Expert } from '../../classes/expert.js';
import { Bench } from '../../classes/bench.js';
import { Forum } from '../../classes/forum.js';
import { PostContent } from '../../classes/post-content.js';
import { delay } from '../../util.js';
import { DAO } from '../../classes/dao.js';
const POOL_DURATION_MS = 100;
const DEFAULT_DELAY_MS = 100;
let scene;
let experts;
let forum;
let bench;
let dao;
async function newExpert() {
const index = experts.length;
const name = `Expert${index + 1}`;
const expert = await new Expert(name, scene).initialize();
expert.addValue('rep', () => bench.reputation.valueOwnedBy(expert.reputationPublicKey));
const expert = await new Expert(dao, name).initialize();
expert.addValue('rep', () => dao.reputation.valueOwnedBy(expert.reputationPublicKey));
experts.push(expert);
return expert;
}
@ -31,11 +29,11 @@ async function setup() {
scene.withSequenceDiagram();
scene.withTable();
dao = new DAO('DGF');
experts = (window.experts = []);
await newExpert();
await newExpert();
forum = (window.forum = new Forum('Forum', scene));
bench = (window.bench = new Bench(forum, 'Bench', scene));
await delay(DEFAULT_DELAY_MS);
}
@ -45,13 +43,9 @@ describe('Validation Pool', () => {
await setup();
});
after(async () => {
await scene.deactivateAll();
});
it('First expert can self-approve', async () => {
scene.startSection();
const { pool } = await experts[0].submitPostWithFee(bench, forum, new PostContent(), {
const { pool } = await experts[0].submitPostWithFee(new PostContent(), {
fee: 7,
duration: POOL_DURATION_MS,
tokenLossRatio: 1,
@ -79,7 +73,7 @@ describe('Validation Pool', () => {
it('Failure example: second expert can not self-approve', async () => {
scene.startSection();
try {
const { pool } = await experts[1].submitPostWithFee(bench, forum, new PostContent(), {
const { pool } = await experts[1].submitPostWithFee(new PostContent(), {
fee: 1,
duration: POOL_DURATION_MS,
tokenLossRatio: 1,
@ -95,7 +89,7 @@ describe('Validation Pool', () => {
it('Second expert must be approved by first expert', async () => {
scene.startSection();
const { pool } = await experts[1].submitPostWithFee(bench, forum, new PostContent(), {
const { pool } = await experts[1].submitPostWithFee(new PostContent(), {
fee: 1,
duration: POOL_DURATION_MS,
tokenLossRatio: 1,

View File

@ -5,10 +5,12 @@ import { WDAG } from '../../classes/wdag.js';
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
window.scene = new Scene('WDAG test', rootBox);
describe('Query the graph', () => {
let graph;
before(() => {
graph = (window.graph = new WDAG(window.scene)).withFlowchart();
graph = (window.graph = new WDAG()).withFlowchart();
graph.addVertex({});
graph.addVertex({});
@ -21,14 +23,17 @@ describe('Query the graph', () => {
graph.addEdge('e1', 3, 1, 0.25);
graph.addEdge('e1', 1, 4, 0.125);
});
it('can query for all e1 edges', () => {
const edges = graph.getEdges('e1');
edges.should.have.length(4);
});
it('can query for all e1 edges from a particular vertex', () => {
const edges = graph.getEdges('e1', 2);
edges.map(({ from, to, weight }) => [from.id, to.id, weight]).should.have.deep.members([[2, 1, 0.5]]);
});
it('can query for all e1 edges to a particular vertex', () => {
const edges = graph.getEdges('e1', null, 1);
edges.map(({ from, to, weight }) => [from.id, to.id, weight]).should.have.deep.members([

View File

@ -16,7 +16,7 @@
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
globals: ['scene', 'dao', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();

View File

@ -16,7 +16,7 @@
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', 'graph', '__REACT_DEVTOOLS_*'],
globals: ['graph', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();

View File

@ -1,3 +1,5 @@
import { CryptoUtil } from './classes/crypto.js';
const timers = new Map();
export const EPSILON = 2.23e-16;
@ -32,3 +34,5 @@ export const hexToRGB = (input) => {
};
export const displayNumber = (value) => (value.toString().length > 6 ? value.toFixed(2) : value);
export const randomID = () => CryptoUtil.randomUUID().replaceAll('-', '').slice(0, 8);