function vrf() {
return Math.random();
}
const epochs = 1;
const slots_per_epoch = 432000;
const pools = [10, 20, 30, 40, 10, 15, 20];
const total_stake = pools.reduce((a, n) => a + n, 0);
const chain_density = 0.05;
console.log("VRF w/variable cadence: ")
let block_count_per_pool = [ 0, 0, 0, 0, 0, 0, 0];
let block_allocation_array = [ 0, 0, 0, 0, 0, 0, 0];
let cadence_histogram = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0]
let last_block_slot = 0;
let slots_since_last_block = 0;
for (let slot = 0; slot < slots_per_epoch * epochs; slot++ ){
slots_since_last_block = slot - last_block_slot;
// clear the array
for (let pool = 0; pool < pools.length; pool++ ){
block_allocation_array[pool] = 0;
}
// Allocate blocks for the slot
for (let pool = 0; pool < pools.length; pool++ ){
if ( vrf() < chain_density * pools[pool] / total_stake ){
block_allocation_array[pool]++;
last_block_slot = slot;
//console.log("Slot:", slot, " Array : ", block_allocation_array );
}
}
//
// Is there a battle?
if ( block_allocation_array.reduce((a, n) => a + n, 0) > 1 ) {
cadence_histogram[0]++;
}
// Else only one pool creates a block
else {
if ( block_allocation_array.reduce((a, n) => a + n, 0) == 1 ) {
// record the newly allocated block
for (let pool = 0; pool < pools.length; pool++ ){
block_count_per_pool[pool] += block_allocation_array[pool];
}
// update the cadence histogram
if ( slots_since_last_block > 90 ) {
cadence_histogram[90]++;
}
else {
cadence_histogram[slots_since_last_block]++;
}
}
}
}
console.log("Total Nobattle Blocks : ", block_count_per_pool.reduce((a, n) => a + n, 0) );
console.log("Total Blocks : ", cadence_histogram.reduce((a, n) => a + n, 0) );
console.log("Cadence Histogram : ", cadence_histogram);
console.log(" ");
//
console.log("VRF w/o variable cadence:")
block_count_per_pool = [ 0, 0, 0, 0, 0, 0, 0];
block_allocation_array = [ 0, 0, 0, 0, 0, 0, 0];
cadence_histogram = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0]
last_block_slot = 0;
slots_since_last_block = 0;
for (let slot = 0; slot < slots_per_epoch * epochs; slot += 13.0 ){
slots_since_last_block = slot - last_block_slot;
// clear the array
for (let pool = 0; pool < pools.length; pool++ ){
block_allocation_array[pool] = 0;
}
// Allocate blocks for the slot
for (let pool = 0; pool < pools.length; pool++ ){
if ( vrf() < 1.00 * pools[pool] / total_stake ){
block_allocation_array[pool]++;
last_block_slot = slot;
//console.log("Slot:", slot, " Array : ", block_allocation_array );
}
}
//
// Is there a battle?
if ( block_allocation_array.reduce((a, n) => a + n, 0) > 1 ) {
cadence_histogram[0]++;
}
// Else only one pool creates a block
else {
if ( block_allocation_array.reduce((a, n) => a + n, 0) == 1 ) {
// record the newly allocated block
for (let pool = 0; pool < pools.length; pool++ ){
block_count_per_pool[pool] += block_allocation_array[pool];
}
// update the cadence histogram
if ( slots_since_last_block > 90 ) {
cadence_histogram[90]++;
}
else {
cadence_histogram[slots_since_last_block]++;
}
}
}
}
console.log("Total Nobattle Blocks : ", block_count_per_pool.reduce((a, n) => a + n, 0) );
console.log("Total Blocks : ", cadence_histogram.reduce((a, n) => a + n, 0) );
console.log("Cadence Histogram : ", cadence_histogram);