dao-governance-framework/forum-network/public/classes/blockchain.js

73 lines
1.4 KiB
JavaScript
Raw Normal View History

class Blockchain {
constructor() {
this.posts = new CollectionWithReputation();
this.authors = new CollectionWithReputation();
this.nodes = new CollectionWithReputation();
}
vote(voter, batch) {
if (!this.activeVote) {
this.activeVote = new VoteInstance(batch);
this.activeVote.vote(voter, true);
return;
}
if (this.activeVote.matches(batch)) {
this.activeVote.vote(voter, true);
} else {
this.activeVote.vote(voter, false);
}
}
applyBatch(batch) {}
}
class CollectionWithReputation {
constructor() {
this.collection = new Map();
this.reputations = new Map();
}
set(id, value) {
this.collection.set(id, value);
}
get(id) {
this.collection.get(id);
}
setReputation(id, value) {
this.reputations.set(id, value);
}
get(id) {
this.reputations.get(id);
}
}
class VoteInstance {
constructor(content) {
this.content = content;
this.votes = new Map();
}
matches(content) {
return JSON.stringify(content) === JSON.stringify(this.content);
}
vote(voter, opinion) {
this.votes.set(voter.id, { voter, opinion });
}
finalize() {
const count = { for: 0, against: 0 };
for (const vote of this.votes) {
if (vote.opinion === true) {
count.for++;
} else {
count.against++;
}
}
return count.for > count.against;
}
}