#!/usr/bin/nodejs // "Stupid" gossip algorithm simulation tool function test_simple(options) { options.total ||= 100; options.gossip ||= 4; options.msgcap ||= 5; options.update ||= 0; options.initial ||= 5; let messages_sent = 0; let tick = 1; const known = {}; const lists = {}; const listsv2 = {}; for (let i = 1; i <= options.total; i++) { known[i] = {}; lists[i] = []; for (let j = 1; j <= (options.update ? options.total : options.initial); j++) { known[i][j] = 1; // meta version 1 lists[i].push(j); } listsv2[i] = []; } let cmp_lists; let cmp_n; if (options.update) { // We want to update nodes metadata to version 2 for (let i = 1; i <= options.update; i++) { known[i][i] = 2; listsv2[i].push(i); } cmp_lists = listsv2; cmp_n = options.update; } else { // We want to join for (let i = 1; i <= options.initial; i++) { if (!known[i][i]) { known[i][i] = 1; lists[i].push(i); } for (let alive = options.initial+1; alive <= options.total; alive++) { if (!known[i][alive]) { known[i][alive] = true; lists[i].push(alive); } } } cmp_lists = lists; cmp_n = options.total; } let in_sync = 0; for (let i = 1; i <= options.total; i++) { if (cmp_lists[i].length == cmp_n) { in_sync++; } } let avg_known = 0; while (in_sync < options.total) { console.log('tick '+tick+': '+in_sync+' in sync, avg '+avg_known); for (let i = 1; i <= options.total; i++) { const known_i = lists[i]; const send_to = []; for (let j = 0; j < options.gossip; j++) { send_to.push(known_i[0|(Math.random()*known_i.length)]); } const send_what = []; for (let j = 0; j < options.msgcap; j++) { // FIXME: Exclude duplicates, exclude send_what.push(known_i[0|(Math.random()*known_i.length)]); } for (const alive of send_what) { for (const to of send_to) { if (!known[to][alive] || known[i][alive] > known[to][alive]) { known[to][alive] = known[i][alive]; cmp_lists[to].push(alive); if (cmp_lists[to].length == cmp_n) { console.log('node '+to+': tick '+tick); in_sync++; } } } } messages_sent += send_what.length*send_to.length; } avg_known = 0; for (let i = 1; i <= options.total; i++) { avg_known += cmp_lists[i].length; } avg_known /= options.total; tick++; } console.log('tick '+tick+': '+in_sync+' in sync, avg '+avg_known); console.log(messages_sent+' messages sent'); } const options = {}; for (let i = 2; i < process.argv.length; i++) { if (process.argv[i] === '-h' || process.argv[i] === '--help') { console.error('USAGE: '+process.argv[0]+' '+process.argv[1]+` [OPTIONS] --gossip 4 how many nodes to gossip with every tick --msgcap 5 how many nodes to gossip about every tick --total 1000 total nodes --update 0 total nodes to update if testing update. if 0 then test joining --initial 5 initial nodes in sync to test joining (when --update is 0)`); process.exit(); } else if (process.argv[i].substr(0, 2) == '--') { options[process.argv[i].substr(2)] = 0|process.argv[i+1]; i++; } } test_simple(options);