2022-12-31 16:08:42 -06:00
|
|
|
import { Actor } from './actor.js';
|
|
|
|
import { Reputations } from './reputation.js';
|
|
|
|
import { ValidationPool } from './validation-pool.js';
|
|
|
|
import params from './params.js';
|
|
|
|
import { Action } from './action.js';
|
2022-11-13 10:54:07 -06:00
|
|
|
|
2022-12-31 16:08:42 -06:00
|
|
|
/**
|
|
|
|
* Purpose: Keep track of reputation holders
|
|
|
|
*/
|
2022-11-13 10:54:07 -06:00
|
|
|
export class Bench extends Actor {
|
|
|
|
constructor(name, scene) {
|
|
|
|
super(name, scene);
|
|
|
|
this.validationPools = new Map();
|
|
|
|
this.voters = new Map();
|
|
|
|
this.reputations = new Reputations();
|
|
|
|
|
|
|
|
this.actions = {
|
2022-12-31 16:08:42 -06:00
|
|
|
createValidationPool: new Action('create validation pool', scene),
|
2022-11-13 10:54:07 -06:00
|
|
|
};
|
2022-11-13 12:23:30 -06:00
|
|
|
|
|
|
|
this.activate();
|
2022-11-13 10:54:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
listValidationPools() {
|
|
|
|
Array.from(this.validationPools.values());
|
|
|
|
}
|
|
|
|
|
|
|
|
listActiveVoters() {
|
|
|
|
const now = new Date();
|
|
|
|
const thresholdSet = !!params.activeVoterThreshold;
|
2022-11-30 09:13:52 -06:00
|
|
|
return Array.from(this.voters.values()).filter((voter) => {
|
2022-11-13 10:54:07 -06:00
|
|
|
const hasVoted = !!voter.dateLastVote;
|
2022-12-31 16:08:42 -06:00
|
|
|
const withinThreshold = now - voter.dateLastVote >= params.activeVoterThreshold;
|
2022-11-13 10:54:07 -06:00
|
|
|
return hasVoted && (!thresholdSet || withinThreshold);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getTotalReputation() {
|
|
|
|
return this.reputations.getTotal();
|
|
|
|
}
|
|
|
|
|
|
|
|
getTotalAvailableReputation() {
|
|
|
|
return this.reputations.getTotalAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
getTotalActiveReputation() {
|
|
|
|
return this.listActiveVoters()
|
2022-12-31 16:08:42 -06:00
|
|
|
.map(({ reputationPublicKey }) => this.reputations.getTokens(reputationPublicKey))
|
2022-11-30 09:13:52 -06:00
|
|
|
.reduce((acc, cur) => (acc += cur), 0);
|
2022-11-13 10:54:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getTotalActiveAvailableReputation() {
|
|
|
|
return this.listActiveVoters()
|
2022-12-31 16:08:42 -06:00
|
|
|
.map(({ reputationPublicKey }) => this.reputations.getAvailableTokens(reputationPublicKey))
|
2022-11-30 09:13:52 -06:00
|
|
|
.reduce((acc, cur) => (acc += cur), 0);
|
2022-11-13 10:54:07 -06:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:13:52 -06:00
|
|
|
initiateValidationPool(
|
|
|
|
authorId,
|
|
|
|
{
|
|
|
|
postId,
|
|
|
|
fee,
|
|
|
|
duration,
|
|
|
|
tokenLossRatio,
|
|
|
|
contentiousDebate,
|
|
|
|
signingPublicKey,
|
2023-01-01 21:09:02 -06:00
|
|
|
authorStake,
|
2023-01-02 13:14:32 -06:00
|
|
|
anonymous,
|
2022-12-31 16:08:42 -06:00
|
|
|
},
|
2022-11-30 09:13:52 -06:00
|
|
|
) {
|
2022-11-13 12:23:30 -06:00
|
|
|
const validationPoolNumber = this.validationPools.size + 1;
|
2022-11-30 09:13:52 -06:00
|
|
|
const validationPool = new ValidationPool(
|
|
|
|
this,
|
|
|
|
authorId,
|
|
|
|
{
|
|
|
|
postId,
|
|
|
|
fee,
|
|
|
|
duration,
|
|
|
|
tokenLossRatio,
|
|
|
|
contentiousDebate,
|
|
|
|
signingPublicKey,
|
2023-01-01 21:09:02 -06:00
|
|
|
authorStake,
|
2023-01-02 13:14:32 -06:00
|
|
|
anonymous,
|
2022-11-30 09:13:52 -06:00
|
|
|
},
|
|
|
|
`pool${validationPoolNumber}`,
|
2022-12-31 16:08:42 -06:00
|
|
|
this.scene,
|
2022-11-30 09:13:52 -06:00
|
|
|
);
|
2022-11-13 12:23:30 -06:00
|
|
|
this.validationPools.set(validationPool.id, validationPool);
|
2022-11-30 09:13:52 -06:00
|
|
|
this.actions.createValidationPool.log(this, validationPool);
|
2022-11-13 12:23:30 -06:00
|
|
|
validationPool.activate();
|
|
|
|
return validationPool;
|
2022-11-13 10:54:07 -06:00
|
|
|
}
|
|
|
|
}
|