Skip to content

completed block producer fairness example#210

Open
buggythanos wants to merge 1 commit intobnb-chain:mainfrom
buggythanos:block-producer-fairness-monitor
Open

completed block producer fairness example#210
buggythanos wants to merge 1 commit intobnb-chain:mainfrom
buggythanos:block-producer-fairness-monitor

Conversation

@buggythanos
Copy link

Description

This PR adds a Block Producer Fairness Monitor for BNB Smart Chain (BSC) that analyzes recent blocks to detect potential imbalances in validator distribution and helps ensure network decentralization.

block-producer-fairness-monitor

The monitor fetches recent blocks from BSC, identifies the producer (validator) for each block, and calculates distribution statistics to measure fairness. It uses the Gini coefficient to quantify inequality and highlights any significant deviations from expected block distribution.

Key Features:

  • Real-time block analysis: Fetches and analyzes recent blocks from BSC (10-1000 blocks)
  • Fairness metrics: Calculates Gini coefficient and deviation statistics
  • Modern dark mode UI: Beautiful, responsive interface with split-pane design
  • Visual distribution: Charts and tables showing block producer statistics
  • Fairness detection: Automatically determines if distribution is fair or imbalanced
  • RESTful API: /api/analyze endpoint for programmatic access
  • Health check endpoint: /api/health for monitoring

Fairness Assessment Logic:

  • Gini Coefficient: Measures distribution inequality (0 = perfect equality, 1 = maximum inequality)
  • Max Deviation: Largest difference between actual and expected block count per producer
  • Fairness Threshold: Distribution is considered fair when Gini < 0.9 AND max deviation < 50% of expected blocks
  • Producers are sorted by block count (descending) for easy identification of dominant validators

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

The implementation includes a comprehensive test suite with 18 unit tests covering:

  1. Gini Coefficient Calculation:

    • Empty array handling
    • Single element handling
    • Equal distribution (should return 0)
    • Unequal distribution detection
    • Maximum inequality scenarios
  2. Address Formatting:

    • Valid address formatting (truncation)
    • Zero address handling (returns "Unknown")
    • Empty string handling
  3. Block Fetching:

    • Successful block fetching
    • Error handling for failed block fetches
    • Missing miner field handling
  4. Block Producer Analysis:

    • Correct analysis of multiple producers
    • Unfair distribution detection
    • Percentage calculations
    • Deviation calculations
    • Producer sorting by block count
  5. Fairness Determination:

    • Fair distribution detection (Gini < 0.9, deviation < 50%)
    • Unfair distribution detection (Gini >= 0.9)

Test execution:

npm test

All 18 tests pass successfully. The test suite uses Jest with mocked ethers.js providers to ensure reliable, fast test execution without requiring actual blockchain connections.

Manual testing:

  • Web UI tested by accessing http://localhost:3000 and analyzing various block ranges
  • API endpoint tested via direct HTTP requests to /api/analyze?blockCount=100
  • Health check endpoint verified at /api/health
  • Server mode tested by running npm start and accessing the web interface

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional notes:

  • Includes comprehensive README.md with setup instructions, usage examples, API documentation, and configuration details
  • Includes setup.sh script for one-command installation and setup
  • Includes env.example for environment variable configuration
  • TypeScript implementation with full type safety
  • Uses ethers.js v6 for blockchain interactions
  • Express server with RESTful API endpoints
  • Modern, responsive web UI with dark mode styling and visual charts
  • Graceful error handling for network issues and missing data

@vivixu-cmd
Copy link

Congratulations! You have received a Cookbook reward. Please reply with your BSC wallet address.Thanks

@buggythanos
Copy link
Author

Congratulations! You have received a Cookbook reward. Please reply with your BSC wallet address.Thanks

Hi there, thank you!
0x4e5d4a29F7b02c8cf1310D112A42231Bde049180

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants