Created
April 23, 2022 08:19
-
-
Save dionyziz/823077b534d1ad66f72d8f16b3e520d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ADVERSARIAL_POWER = 0.49 | |
const BLOCK_LIMIT = 500 | |
const MONTE_CARLO = 10 | |
function simulate() { | |
let honestBlocks = 1 | |
let adversaryHeadStart = 0 | |
let chainLength = 1 | |
while (chainLength < BLOCK_LIMIT) { | |
if (Math.random() < ADVERSARIAL_POWER) { | |
// adversary got a block | |
++adversaryHeadStart | |
} | |
else { | |
// honest got a block | |
if (adversaryHeadStart > 0) { | |
--adversaryHeadStart | |
} | |
else { | |
++honestBlocks | |
} | |
++chainLength | |
} | |
} | |
return honestBlocks / BLOCK_LIMIT | |
} | |
let sumQuality = 0 | |
for (let i = 0; i < MONTE_CARLO; ++i) { | |
sumQuality += simulate() | |
} | |
console.log(sumQuality / MONTE_CARLO) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment