Skip to content

Instantly share code, notes, and snippets.

@stefanv
Last active March 5, 2021 23:58
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 stefanv/7c5ccef8c2b566e166f5f94972aa05ca to your computer and use it in GitHub Desktop.
Save stefanv/7c5ccef8c2b566e166f5f94972aa05ca to your computer and use it in GitHub Desktop.
import numpy as np
validators_online = 102
consensus_size = 28
replacement_factor = 4
validators = np.arange(validators_online)
replacement_N = len(validators) // replacement_factor
N = 10000
failed_elections = {}
for nr_elections in range(1, 10):
failed_elections[nr_elections] = 0
for i in range(N):
trials = []
elected_validators = np.random.choice(validators, replace=False, size=consensus_size)
# Choose any node that isn't already a validator
node_id = np.setdiff1d(validators, elected_validators)[0]
for j in range(nr_elections):
electable = np.setdiff1d(validators, elected_validators)
elected = np.random.choice(electable, replace=False, size=replacement_N)
elected_validators = np.concatenate((
np.random.permutation(elected_validators)[replacement_N:],
elected
))
assert len(elected_validators) == consensus_size
trials.append(node_id in elected_validators)
if np.sum(trials) == 0:
failed_elections[nr_elections] += 1
print('Validators online:', validators_online)
print('Consensus size:', consensus_size)
print('Replacement factor:', replacement_factor)
print()
print('Probability of not being selected to consensus group N elections in a row:')
for (elections, count) in failed_elections.items():
print(elections, '->', f'{count / N * 100:.2f}% [1 - p = {1 - (count / N):.2f}]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment