-
Notifications
You must be signed in to change notification settings - Fork 18
feat(tools): Slack monitor on SR votes #105
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
base: develop
Are you sure you want to change the base?
Changes from all commits
2a40e61
8bea3b4
e1adb27
27203f8
f4d8bb1
0bb37bf
9ef9e76
2cc3203
730864d
1083b5d
e5bba78
7bf1968
f330c1a
837d86d
8a85b2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Slack SR Monitor Configuration | ||
|
|
||
| # The Slack Webhook URL for sending notifications | ||
| SLACK_WEBHOOK=your_slack_webhook_url_here | ||
|
|
||
| # The Tron node API endpoint | ||
| # Default: https://api.trongrid.io | ||
| TRON_NODE=https://api.trongrid.io |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| slack_sr_monitor | ||
| logs/ | ||
| .env |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Build stage | ||
| FROM golang:1.25-alpine@sha256:8d22e29d960bc50cd025d93d5b7c7d220b1ee9aa7a239b3c8f55a57e987e8d45 AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Install dependencies | ||
| COPY go.mod go.sum ./ | ||
| RUN go mod download | ||
|
|
||
| # Copy source code | ||
| COPY . . | ||
|
|
||
| # Build the application | ||
| RUN CGO_ENABLED=0 GOOS=linux go build -o slack_sr_monitor main.go | ||
|
|
||
| # Final stage | ||
| FROM alpine:3.20@sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc | ||
|
|
||
| RUN apk --no-cache add ca-certificates tzdata \ | ||
| && addgroup -S monitor && adduser -S -G monitor monitor | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MUST] Runtime base image using :latest tag (no digest) Problem:
Why this is P0 (Critical):
Fix: docker pull alpine:3.20
docker inspect alpine:3.20 --format='{{index .RepoDigests 0}}'
# Replace: FROM alpine:3.20@sha256:xyz789...
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in f330c1a. Switched to |
||
| WORKDIR /home/monitor | ||
|
|
||
| # Create logs directory owned by the non-root user | ||
| RUN mkdir -p logs && chown -R monitor:monitor /home/monitor | ||
|
|
||
| # Copy the binary from builder | ||
| COPY --from=builder --chown=monitor:monitor /app/slack_sr_monitor . | ||
|
|
||
| USER monitor | ||
|
|
||
| # Command to run | ||
| CMD ["./slack_sr_monitor"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| ## Slack SR Monitor Tool | ||
| The Slack SR Monitor tool is designed to monitor TRON Super Representatives (SRs) and notify a Slack channel after every maintenance period. | ||
| It automatically tracks vote changes and detects replacements in the top 27 SR positions, providing a clear and formatted report. | ||
|
|
||
| ### Build and Run the monitor | ||
| To run the monitor tool, you can choose between native Go execution or Docker deployment. | ||
|
|
||
| #### Native Go Execution | ||
| Make sure you have Go 1.25+ installed. | ||
| ```shell | ||
| # enter the directory | ||
| cd tools/slack_sr_monitor | ||
| # install dependencies | ||
| go mod tidy | ||
| # run the tool | ||
| go run main.go | ||
| ``` | ||
|
|
||
| #### Docker Deployment | ||
| We provide a Docker-based deployment for easier management in production environments. | ||
| ```shell | ||
| # build and start the container | ||
| docker-compose up -d --build | ||
| # check logs | ||
| docker logs -f slack-sr-monitor | ||
| ``` | ||
|
|
||
| ### Configuration | ||
| All configurations are managed via environment variables or a `.env` file in the project root. Please refer to [.env.example](./.env.example) as an example. | ||
|
|
||
| - `SLACK_WEBHOOK`: The Slack Incoming Webhook URL used to send notifications. | ||
| - `TRON_NODE`: The TRON node HTTP API endpoint (e.g., `http://https://api.trongrid.io`). Default is Trongrid. | ||
|
|
||
| ### Key Features | ||
|
|
||
| #### SR vote monitor | ||
| Use `/wallet/getpaginatednowwitnesslist` to get the top **28** real-time votes, also the SR address and URL. | ||
|
|
||
| #### Dynamic Scheduling | ||
| Instead of a fixed interval, the tool queries `/wallet/getnextmaintenancetime` to calculate the exact wait time. It triggers the report **1 minute** after each maintenance period begins to ensure data consistency. | ||
|
|
||
| #### Parallel Data Acquisition | ||
| The tool uses Go routines to fetch `account_name` for all 28 witnesses in parallel from the `/wallet/getaccount` interface, significantly reducing the collection time. | ||
|
|
||
| #### Vote Change Tracking | ||
| The tool maintains an in-memory snapshot of the previous period's votes. It calculates the `Change` for each SR: | ||
| ```text | ||
| *1. Poloniex* | ||
| Current: `3,228,089,488` Change: `+89,488` | ||
| ``` | ||
|
|
||
| #### Top 27 Replacement Detection | ||
| After each report, it compares the current Top 27 list with the previous one and highlights any changes: | ||
| ```text | ||
| SR Replacement Detected: | ||
| >:inbox_tray: *Entered:* New_SR_Name | ||
| >:outbox_tray: *Left:* Old_SR_Name | ||
| ``` | ||
| If no changes occur, it displays `Top 27 SRs remain unchanged.` | ||
|
|
||
| ### Notifications | ||
|
|
||
| This monitor only support java-tron node v4.8.1+, because of the API it used. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,10 @@ | ||||||||||||||
| services: | ||||||||||||||
| slack-sr-monitor: | ||||||||||||||
| build: . | ||||||||||||||
| container_name: slack-sr-monitor | ||||||||||||||
| restart: unless-stopped | ||||||||||||||
| environment: | ||||||||||||||
| - SLACK_WEBHOOK=${SLACK_WEBHOOK} | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [SHOULD] Restart policy 'always' risks infinite restart loop Problem: restart: alwaysContainer will be restarted infinitely, even on persistent failures. Why this is P1 (Should Fix): Scenario: Invalid Slack Webhook Consequences:
Comparison of restart policies:
Fix: services:
slack-sr-monitor:
# Option 1 (Recommended): Stop on failure, allow manual restart
restart: unless-stopped
# Option 2: Auto-restart but stop after 5 failed attempts
# restart: on-failure:5Benefit of 'unless-stopped': When the service crashes, it stays down until manual intervention. This forces developers to investigate instead of ignoring the logs.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, switched to |
||||||||||||||
| - TRON_NODE=${TRON_NODE:-https://api.trongrid.io} | ||||||||||||||
| volumes: | ||||||||||||||
| - ./logs:/home/monitor/logs | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module github.com/tronprotocol/tron-docker/tools/slack_sr_monitor | ||
|
|
||
| go 1.25.5 | ||
|
|
||
| require github.com/joho/godotenv v1.5.1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check the security of this package
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package is the de-facto standard for environment variable management in the Go community (10k+ Stars), repo: https://github.com/joho/godotenv. Its logic is minimal and transparent (reading local files only). I've scanned the project using the official Go vulnerability tool |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
| github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MUST] Builder base image missing digest pinning
Problem:
FROM golang:1.25-alpine AS builderlacks @sha256 digest.Why this is P0 (Critical):
Fix:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f330c1a. Pinned to
golang:1.25-alpine@sha256:8d22e29d960bc50cd025d93d5b7c7d220b1ee9aa7a239b3c8f55a57e987e8d45. Thanks for the catch.