-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add minimum required version metric.
Add new metric solana_min_required_version to track the minimum required Solana version for foundation delegation program across different clusters.
- Loading branch information
Showing
7 changed files
with
153 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Client struct { | ||
HttpClient http.Client | ||
cache struct { | ||
version string | ||
lastCheck time.Time | ||
} | ||
mu sync.RWMutex | ||
// How often to refresh the cache | ||
cacheTimeout time.Duration | ||
} | ||
|
||
func NewClient() *Client { | ||
return &Client{ | ||
HttpClient: http.Client{}, | ||
cacheTimeout: 6 * time.Hour, // Cache for 6 hours by default | ||
} | ||
} | ||
|
||
func (c *Client) GetMinRequiredVersion(ctx context.Context, cluster string) (string, error) { | ||
// Check cache first | ||
c.mu.RLock() | ||
if !c.cache.lastCheck.IsZero() && time.Since(c.cache.lastCheck) < c.cacheTimeout { | ||
version := c.cache.version | ||
c.mu.RUnlock() | ||
return version, nil | ||
} | ||
c.mu.RUnlock() | ||
|
||
// Cache miss or expired, fetch new data | ||
url := fmt.Sprintf("https://api.solana.org/api/validators/epoch-stats?cluster=%s&epoch=latest", cluster) | ||
|
||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
resp, err := c.HttpClient.Do(req) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to fetch min required version: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var stats ValidatorEpochStats | ||
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil { | ||
return "", fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
|
||
// Validate the response structure | ||
if stats.Stats.Config.MinVersion == "" { | ||
return "", fmt.Errorf("min_version not found in response") | ||
} | ||
|
||
// Update cache | ||
c.mu.Lock() | ||
c.cache.version = stats.Stats.Config.MinVersion | ||
c.cache.lastCheck = time.Now() | ||
c.mu.Unlock() | ||
|
||
return stats.Stats.Config.MinVersion, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package api | ||
|
||
type ValidatorEpochStats struct { | ||
Stats struct { | ||
Config struct { | ||
MinVersion string `json:"min_version"` | ||
} `json:"config"` | ||
} `json:"stats"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package rpc |