Skip to content

dysnix/base-rpc-tests

Repository files navigation

Base Blockchain Latency Test

This Python script measures the average latency between block confirmation and event reception from a Base blockchain RPC endpoint using WebSocket methods.

Features

  • Real-time Monitoring: Uses WebSocket connections for real-time block and event monitoring
  • Latency Measurement: Calculates precise latency between block confirmation and event reception
  • Statistical Analysis: Provides comprehensive statistics including mean, median, min, max, and standard deviation
  • Detailed Logging: Comprehensive logging for debugging and monitoring
  • Configurable: Customizable endpoint, duration, and block limits

Installation

  1. Clone or download this repository
  2. Install the required dependencies:
pip install -r requirements.txt

Usage

Basic Usage

Run the script with default settings (5 minutes test on mainnet):

python base_latency_test.py

Advanced Usage

# Test for 10 minutes on mainnet
python base_latency_test.py --duration 600

# Test on a custom endpoint
python base_latency_test.py --endpoint wss://your-base-endpoint.com

# Test for 2 minutes on testnet
python base_latency_test.py --endpoint wss://goerli.base.org --duration 120

# Test with maximum block limit
python base_latency_test.py --duration 300 --blocks 50

Command Line Arguments

  • --endpoint: Base RPC WebSocket endpoint (default: wss://mainnet.base.org)
  • --duration: Test duration in seconds (default: 300)
  • --blocks: Maximum number of blocks to test (optional)

How It Works

  1. Connection: Establishes WebSocket connection to the Base RPC endpoint
  2. Subscription: Subscribes to new block headers (newHeads) and logs/events
  3. Monitoring: Listens for incoming messages and tracks:
    • Block confirmation timestamps
    • Event reception timestamps
    • Calculates latency between them
  4. Statistics: Computes comprehensive latency statistics
  5. Reporting: Displays detailed results with measurements

Output Example

============================================================
BASE BLOCKCHAIN LATENCY TEST RESULTS
============================================================
Total Measurements: 15
Average Latency: 245.67ms
Median Latency: 238.45ms
Min Latency: 189.23ms
Max Latency: 312.89ms
Standard Deviation: 34.56ms

Detailed Measurements:
------------------------------------------------------------
Block #12345678 | Hash: 0x1a2b3c4d5e... | Latency: 234.56ms | Time: 2024-01-15 14:30:25
Block #12345679 | Hash: 0x6f7g8h9i0j... | Latency: 267.89ms | Time: 2024-01-15 14:30:30
...
============================================================

Supported Endpoints

  • Mainnet: wss://mainnet.base.org
  • Testnet: wss://goerli.base.org
  • Custom: Any WebSocket-enabled Base RPC endpoint

Technical Details

WebSocket Methods Used

  • eth_subscribe with newHeads - Subscribe to new block headers
  • eth_subscribe with logs - Subscribe to contract events/logs
  • eth_getBlockByHash - Get detailed block information

Latency Calculation

The script measures latency as:

Latency = Event Reception Time - Block Confirmation Time

Error Handling

  • Automatic reconnection on connection failures
  • Graceful handling of malformed messages
  • Timeout protection for message reception
  • Comprehensive error logging

Troubleshooting

Common Issues

  1. Connection Failed: Check if the endpoint is correct and accessible
  2. No Events Received: Some blocks may not contain events; the script will continue monitoring
  3. High Latency: Network conditions or endpoint performance may affect latency

Debug Mode

Enable debug logging by modifying the logging level in the script:

logging.basicConfig(level=logging.DEBUG)

Requirements

  • Python 3.7+
  • websockets library
  • Internet connection to Base RPC endpoint

License

This project is open source and available under the MIT License.

About

Test scripts for Base RPC

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages