Skip to content

Instantly share code, notes, and snippets.

@dionyziz
Created April 23, 2022 08:19
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 dionyziz/823077b534d1ad66f72d8f16b3e520d1 to your computer and use it in GitHub Desktop.
Save dionyziz/823077b534d1ad66f72d8f16b3e520d1 to your computer and use it in GitHub Desktop.
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