Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limiting for API calls and update the votes list #2

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ The script uses a set of predefined criteria to filter validators. These include

You can modify these criteria in the `filter_out_validators` function to suit your requirements.

### API Rate Limiting

The script enforces a rate limit on API requests to prevent exceeding the CSPR.cloud API restrictions.

By default, the script allows **100 requests per minute**, but this can be adjusted by setting the `API_RATE_LIMIT` environment variable.

To change the rate limit, use:

```bash
export API_RATE_LIMIT=200 # Adjusts the limit to 200 requests per minute
```

This ensures compliance with different API tiers (e.g., Free vs. Pro plan).

## Casper Association Delegation Policy

This tool aligns with the [Casper Association Delegation Policy](https://forum.casper.network/t/cvv002-casper-association-delegation-policy/1220), which was accepted by Mainnet validators through an on-chain governance vote. The policy ensures a systematic and transparent delegation process to enhance network decentralization and incentivize optimal validator performance.
Expand Down
38 changes: 22 additions & 16 deletions fetch_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
import csv
import time

# Adjustable rate limit via environment variable (default: 100 requests per minute)
RATE_LIMIT = int(os.getenv("API_RATE_LIMIT", 100)) # Default: 100
RATE_PERIOD = 60 # Seconds
REQUEST_INTERVAL = RATE_PERIOD / RATE_LIMIT # Time delay between requests

def rate_limited_request(url, headers):
"""Wrapper function to enforce rate limiting on API requests."""
time.sleep(REQUEST_INTERVAL) # Enforce delay
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise error if request fails
return response

def fetch_auction_metrics():
# Get the authorization key from the environment variable
auth_key = os.getenv("CSPR_CLOUD_KEY")
Expand All @@ -22,10 +34,7 @@ def fetch_auction_metrics():
try:
print("Fetching auction metrics...")
# Make the GET request
response = requests.get(url, headers=headers)

# Raise an HTTPError if the response was unsuccessful
response.raise_for_status()
response = rate_limited_request(url, headers)

# Parse the JSON response
data = response.json().get("data", {})
Expand Down Expand Up @@ -55,10 +64,7 @@ def fetch_validator_performance(public_key, eras, auth_key):
url = f"{base_url}?era_id={era_param}&page=1"

# Make the GET request
response = requests.get(url, headers=headers)

# Raise an HTTPError if the response was unsuccessful
response.raise_for_status()
response = rate_limited_request(url, headers)

# Parse the JSON response
data = response.json().get("data", [])
Expand Down Expand Up @@ -88,10 +94,7 @@ def check_voting_participation(public_key, auth_key, contract_package_hash, star
)

# Make the GET request
response = requests.get(url, headers=headers)

# Raise an HTTPError if the response was unsuccessful
response.raise_for_status()
response = rate_limited_request(url, headers)

# Parse the JSON response
data = response.json().get("data", [])
Expand Down Expand Up @@ -140,6 +143,12 @@ def fetch_validators(last_era_id):
"start_block": 3991820,
"end_block": 4012820,
"column_name": "voting_participation_002"
},
{
"contract_package_hash": "6c8d2ae7de3367a600a9fb33c1e3cdbe00d8bda7e17360b81f99e32c5b2ff39f",
"start_block": 4222300,
"end_block": 4239400,
"column_name": "voting_participation_003"
}
]

Expand All @@ -162,10 +171,7 @@ def fetch_validators(last_era_id):
url = f"{base_url}?era_id={last_era_id}&page={page}&includes={includes_param}"

# Make the GET request
response = requests.get(url, headers=headers)

# Raise an HTTPError if the response was unsuccessful
response.raise_for_status()
response = rate_limited_request(url, headers)

# Parse the JSON response
data = response.json()
Expand Down
Loading