60 lines
2.0 KiB
JavaScript
60 lines
2.0 KiB
JavaScript
|
import { Actor } from "../actor.js";
|
||
|
import { Action } from "../action.js";
|
||
|
import { Reputations } from "../reputation.js";
|
||
|
import { VoteInstance } from "./vote-instance.js";
|
||
|
import { Vote } from "./vote.js";
|
||
|
import { Voter } from "./voter.js";
|
||
|
import params from "./params.js";
|
||
|
|
||
|
export class ValidationPool extends Actor {
|
||
|
constructor(name, scene) {
|
||
|
super(name, scene);
|
||
|
this.voteInstances = new Map();
|
||
|
this.voters = new Map();
|
||
|
this.reputations = new Reputations();
|
||
|
|
||
|
this.actions = {
|
||
|
};
|
||
|
}
|
||
|
|
||
|
listVoteInstances() {
|
||
|
Array.from(this.voteInstances.values());
|
||
|
}
|
||
|
|
||
|
listActiveVoters() {
|
||
|
const now = new Date();
|
||
|
const thresholdSet = !!params.activeVoterThreshold;
|
||
|
return Array.from(this.voters.values()).filter(voter => {
|
||
|
const hasVoted = !!voter.dateLastVote;
|
||
|
const withinThreshold = now - voter.dateLastVote >= params.activeVoterThreshold;
|
||
|
return hasVoted && (!thresholdSet || withinThreshold);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
activeAvailableReputation() {
|
||
|
return this.listActiveVoters()
|
||
|
.map(({reputationPublicKey}) => this.reputations.getAvailableTokens(reputationPublicKey))
|
||
|
.reduce((acc, cur) => acc += cur, 0);
|
||
|
}
|
||
|
|
||
|
initiateVote(authorId, {fee, duration, tokenLossRatio, contentiousDebate}) {
|
||
|
const vote = new VoteInstance(this, authorId, {fee, duration, tokenLossRatio, contentiousDebate});
|
||
|
this.voteInstances.set(vote.id, vote);
|
||
|
return vote.id;
|
||
|
}
|
||
|
|
||
|
castVote(voteId, signingPublicKey, position, stake, lockingTime) {
|
||
|
const vote = new Vote(position, stake, lockingTime);
|
||
|
const voteInstance = this.voteInstances.get(voteId);
|
||
|
voteInstance.castVote(signingPublicKey, vote);
|
||
|
}
|
||
|
|
||
|
revealIdentity(voteId, signingPublicKey, reputationPublicKey) {
|
||
|
const voteInstance = this.voteInstances.get(voteId);
|
||
|
const voter = this.voters.get(reputationPublicKey) ?? new Voter(reputationPublicKey);
|
||
|
voter.addVoteRecord(voteInstance);
|
||
|
this.voters.set(reputationPublicKey, voter);
|
||
|
voteInstance.revealIdentity(signingPublicKey, voter);
|
||
|
}
|
||
|
}
|