Skip to content

Instantly share code, notes, and snippets.

@asn-d6
Last active April 6, 2022 14:16
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 asn-d6/1d972421667a23a0635f65cd5d12d0e7 to your computer and use it in GitHub Desktop.
Save asn-d6/1d972421667a23a0635f65cd5d12d0e7 to your computer and use it in GitHub Desktop.
"""
Given a number of validators, compute the expected number of P2P nodes that correspond to them given the current network configuration
"""
import numpy as np
import random
import sys
from matplotlib import pyplot as plt
def Zipf(a: np.float64, min: np.uint64, max: np.uint64, size=None):
"""
Generate Zipf-like random variables,
but in inclusive [min...max] interval
Taken from: https://stackoverflow.com/a/57420941
"""
if min == 0:
raise ZeroDivisionError("")
v = np.arange(min, max+1) # values to sample
p = 1.0 / np.power(v, a) # probabilities
p /= np.sum(p) # normalized
# print("Weight of the first 20%%: %s" % sum(p[:int(max*0.2)]))
return np.random.choice(v, size=size, replace=True, p=p)
N_RUNS = 1000 # simulation runs
# An alpha parameter for a power law distribution that provides a 80-20 shape
POWER_LAW_ALPHA = 0.97
# Number of P2P nodes on the network
NODES = 5000
# Number of validators on the network
VALIDATORS = 250000
# The anonymity set (in validators)
ANONYMITY_SET = 8192
n_total = 0
for _ in range(N_RUNS):
# For every validator, assign a unique node ID to it, using a discrete power-law 80-20 distribution
validators = Zipf(POWER_LAW_ALPHA, 1, NODES, VALIDATORS)
# Now choose 8192 random validators (i.e. proposers)
proposers = random.choices(validators, k=ANONYMITY_SET)
# Count their unique nodes
n_total += len(set(proposers))
print("Average number of nodes in the anonymity set: %s" % (n_total/N_RUNS))
# Since we are here let's also plot a graph showing nodes (x axis) to validators per node (y axis) for the zipf(alpha)-law used
# plt.hist(validators, bins=5000)
# plt.title("Zipf")
# plt.show()
@asn-d6
Copy link
Author

asn-d6 commented Jan 13, 2022

Graph showing nodes (x axis) to validators per node (y axis) for the zipf(alpha)-law used

valspernode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment