-
-
Save asn-d6/1d972421667a23a0635f65cd5d12d0e7 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
""" | |
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Graph showing
nodes
(x axis) tovalidators per node
(y axis) for the zipf(alpha)-law used