-
Notifications
You must be signed in to change notification settings - Fork 43
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.
- Loading branch information
Showing
10 changed files
with
328 additions
and
20 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
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,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 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.