Skip to content

Instantly share code, notes, and snippets.

@Benhawkins18
Created May 10, 2024 17:57
Show Gist options
  • Save Benhawkins18/0c02bd14723abd35cf8893211d0ecf11 to your computer and use it in GitHub Desktop.
Save Benhawkins18/0c02bd14723abd35cf8893211d0ecf11 to your computer and use it in GitHub Desktop.
Solana Validator Decentralization
import requests
from collections import defaultdict
# weights should sum to 100
def HHI(weights):
return sum([x**2 for x in weights])
MAX_HHI_SCORE = 10000
#NE is the number of nodes with a perfectly even distribution with an equivalent HHI score.
def NE(HHI_score):
return 1 / (HHI_score / MAX_HHI_SCORE)
stake_wiz_data = requests.get('https://api.stakewiz.com/validators').json()
total_stake = sum([x['activated_stake'] for x in stake_wiz_data])
print(f"Total Stake: {total_stake}")
stake_by_geopgraphy = defaultdict(lambda: 0)
stake_by_physical_data_center = defaultdict(lambda: 0)
stake_by_data_center_provider = defaultdict(lambda: 0)
# loop through validator list and sum up stake by relevant categories
for validator in stake_wiz_data:
country = validator['ip_country']
stake_by_geopgraphy[country] += validator['activated_stake']
physical_data_center = str(validator['ip_asn'])+"-"+str(validator['ip_city'])
stake_by_physical_data_center[physical_data_center] += validator['activated_stake']
data_center_provider = validator['ip_org']
stake_by_data_center_provider[data_center_provider] += validator['activated_stake']
node_weights = [x["activated_stake"]/total_stake*100 for x in stake_wiz_data if x["activated_stake"] > 1 and not x["delinquent"]]
geographic_weights = [x/total_stake*100 for x in stake_by_geopgraphy.values()]
physical_datacenter_weights = [x/total_stake*100 for x in stake_by_physical_data_center.values()]
data_center_provider_weights = [x/total_stake*100 for x in stake_by_data_center_provider.values()]
node_NE_sqrt = NE(HHI(node_weights))**.5
geographic_NE_sqrt = NE(HHI(geographic_weights))**.5
physical_datacenter_NE_sqrt = NE(HHI(physical_datacenter_weights))**.5
data_center_provider_NE_sqrt = NE(HHI(data_center_provider_weights))**.5
print(f"Number of Validat ors: {len(node_weights)}")
print(f"Number of Countries: {len(geographic_weights)}")
print(f"Number of Data Centers: {len(physical_datacenter_weights)}")
print(f"Number of Data Center Providers: {len(data_center_provider_weights)}")
print()
#print HII Scores
print(f"Node HHI: {HHI(node_weights)}")
print(f"Geographic HHI: {HHI(geographic_weights)}")
print(f"Data Center HHI: {HHI(physical_datacenter_weights)}")
print(f"Data Center Provider HHI: {HHI(data_center_provider_weights)}")
print()
#print NE Scores
print(f"Node NE: {NE(HHI(node_weights))}")
print(f"Geographic NE: {NE(HHI(geographic_weights))}")
print(f"Data Center NE: {NE(HHI(physical_datacenter_weights))}")
print(f"Data Center Provider NE: {NE(HHI(data_center_provider_weights))}")
print()
print(f"Node Decentralization Score (NDS): {node_NE_sqrt}")
print(f"Geographic Decentralization Score (GDS): {geographic_NE_sqrt}")
print(f"Data Center Decentralization Score (DCDS): {physical_datacenter_NE_sqrt}")
print(f"Data Center Provider Score (DCPDS): {data_center_provider_NE_sqrt}")
print()
print(f"Validator Decentralization Score (VDS): {node_NE_sqrt+geographic_NE_sqrt+physical_datacenter_NE_sqrt+data_center_provider_NE_sqrt}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment