-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add minimum required version metric. #85
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
Merged
johnstonematt
merged 1 commit into
asymmetric-research:master
from
XLabs:metrics/sfdp-min-version-required
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
// CacheTimeout defines how often to refresh the minimum required version (6 hours) | ||
CacheTimeout = 6 * time.Hour | ||
|
||
// SolanaEpochStatsAPI is the base URL for the Solana validators epoch stats API | ||
SolanaEpochStatsAPI = "https://api.solana.org/api/validators/epoch-stats" | ||
) | ||
|
||
type Client struct { | ||
HttpClient http.Client | ||
baseURL string | ||
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: CacheTimeout, | ||
baseURL: SolanaEpochStatsAPI, | ||
} | ||
} | ||
|
||
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() | ||
|
||
// Make API request | ||
url := fmt.Sprintf("%s?cluster=%s&epoch=latest", c.baseURL, 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 | ||
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 hidden or 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,101 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClient_GetMinRequiredVersion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cluster string | ||
mockJSON string | ||
wantErr bool | ||
wantErrMsg string | ||
want string | ||
}{ | ||
{ | ||
name: "valid mainnet response", | ||
cluster: "mainnet-beta", | ||
mockJSON: `{ | ||
"stats": { | ||
"config": { | ||
"min_version": "2.0.20" | ||
} | ||
} | ||
}`, | ||
want: "2.0.20", | ||
}, | ||
{ | ||
name: "valid testnet response", | ||
cluster: "testnet", | ||
mockJSON: `{ | ||
"stats": { | ||
"config": { | ||
"min_version": "2.1.6" | ||
} | ||
} | ||
}`, | ||
want: "2.1.6", | ||
}, | ||
{ | ||
name: "invalid json response", | ||
cluster: "mainnet-beta", | ||
mockJSON: `{"invalid": "json"`, | ||
wantErr: true, | ||
wantErrMsg: "failed to decode response", | ||
}, | ||
{ | ||
name: "missing version in response", | ||
cluster: "mainnet-beta", | ||
mockJSON: `{"stats": {"config": {}}}`, | ||
wantErr: true, | ||
wantErrMsg: "min_version not found in response", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Create test server | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Verify request | ||
assert.Equal(t, "/api/validators/epoch-stats", r.URL.Path) | ||
assert.Equal(t, tt.cluster, r.URL.Query().Get("cluster")) | ||
assert.Equal(t, "latest", r.URL.Query().Get("epoch")) | ||
|
||
// Send response | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(tt.mockJSON)) | ||
})) | ||
defer server.Close() | ||
|
||
// Create client with test server URL | ||
client := &Client{ | ||
HttpClient: http.Client{}, | ||
baseURL: server.URL + "/api/validators/epoch-stats", | ||
cacheTimeout: time.Hour, | ||
} | ||
|
||
// Test GetMinRequiredVersion | ||
got, err := client.GetMinRequiredVersion(context.Background(), tt.cluster) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.wantErrMsg) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
|
||
// Test caching | ||
cachedVersion, err := client.GetMinRequiredVersion(context.Background(), tt.cluster) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, cachedVersion) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.