Skip to content

Instantly share code, notes, and snippets.

@c-geek
Last active December 12, 2016 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c-geek/626830bf7ea051d49552f7326d1535c0 to your computer and use it in GitHub Desktop.
Save c-geek/626830bf7ea051d49552f7326d1535c0 to your computer and use it in GitHub Desktop.
"use strict";
const durees = [1, 2, 3]; // 1 an, 2 ans, 3 ans
const stocks = [70, 100, 150];
const stepMalveillants = [5, 4, 3, 2, 1];
const STEPMAX = 6;
for (const stepMalveillant of stepMalveillants) {
for (const duree of durees) {
for (const SIGSTOCK of stocks) {
const sigQty = 5;
const PERIODS = 53 * duree;
const MAXSTEP_MALV = STEPMAX - stepMalveillant;
const ids = [];
const links = {};
for (let i = 0; i <= MAXSTEP_MALV; i++) {
ids[i] = 1;
}
const wot = [{
members: Array.from({ length: sigQty }, (value, index) => {
return {
id: ['S0', ids[0]++].join('#'),
received: 0,
stock: SIGSTOCK
}
})
}];
for (let p = 0; p < PERIODS; p++) {
// console.log('>> Period %s', p + 1);
const wotLen = wot.length; // We take the inital length
const newMembers = [];
for (let step = 0; step < wotLen && step < MAXSTEP_MALV; step++) {
const issuers = wot[step].members;
for (let i = 0; i < issuers.length; i++) {
const issuer = issuers[i];
// We need enough stock to proceed
if (issuer.stock > 0) {
const target = getPendingAccountOrAddOne(step + 1, issuer);
target.received++;
issuer.stock--;
createLink(issuer, target);
// console.log('%s -> %s', issuer.id, target.id);
if (target.received == sigQty) {
// Remove it from pendings
removeFromPendings(step + 1, target);
// Push it in the newcomers
newMembers.push({
step: step + 1,
target
});
}
}
}
}
// Accept new members
for (const newMember of newMembers) {
wot[newMember.step].members.push(newMember.target);
}
}
// Analysis from step initial +1
let sybils = 0;
for (let step = 1; step < wot.length; step++) {
// We count only the members
sybils += wot[step].members.length;
}
function getStep(step) {
if (!wot[step]) {
wot.push({
pending: [],
members: []
});
}
return wot[step];
}
function getPendingAccountOrAddOne(step, issuer) {
const accounts = getStep(step);
const pending = accounts.pending;
let target = null;
for (let i = 0; i < pending.length && target == null; i++) {
if (!existsLink(issuer, pending[i])) {
target = pending[i];
}
}
if (!target) {
target = {
id: ['S' + step, ids[step]++].join('#'),
received: 0,
stock: SIGSTOCK
};
accounts.pending.push(target);
}
return target;
}
function existsLink(issuer, target) {
return (links[target.id] && links[target.id][issuer.id]) || false;
}
function createLink(issuer, target) {
links[target.id] = links[target.id] || {};
links[target.id][issuer.id] = true;
}
function removeFromPendings(step, target) {
// Move from pending to members
const accounts = wot[step];
let index = null;
for (let j = 0; j < accounts.pending.length && index == null; j++) {
if (accounts.pending[j].id == target.id) {
index = j;
}
}
// console.log('%s has become a member', target.id);
// Remove from pending
accounts.pending.splice(index, 1);
}
console.log('stepMalveillants = %s, durée %s an(s), sigStock = %s, %s sybils crées', stepMalveillant, duree, SIGSTOCK, sybils);
}
}
}
process.exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment