-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: status and http interfaces
- Loading branch information
Showing
38 changed files
with
803 additions
and
497 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"strings" | ||
) | ||
|
||
type CatchPointUrl string | ||
|
||
const ( | ||
FNet CatchPointUrl = "https://fnet-catchpoints.algorand.green/latest" | ||
BetaNet CatchPointUrl = "https://algorand-catchpoints.s3.us-east-2.amazonaws.com/channel/betanet/latest.catchpoint" | ||
TestNet CatchPointUrl = "https://algorand-catchpoints.s3.us-east-2.amazonaws.com/channel/testnet/latest.catchpoint" | ||
MainNet CatchPointUrl = "https://algorand-catchpoints.s3.us-east-2.amazonaws.com/channel/mainnet/latest.catchpoint" | ||
) | ||
|
||
type LatestCatchpointResponse struct { | ||
ResponseCode int | ||
ResponseStatus string | ||
JSON200 string | ||
} | ||
|
||
func (r LatestCatchpointResponse) StatusCode() int { | ||
return r.ResponseCode | ||
} | ||
func (r LatestCatchpointResponse) Status() string { | ||
return r.ResponseStatus | ||
} | ||
|
||
func GetLatestCatchpointWithResponse(http HttpPkgInterface, network string) (LatestCatchpointResponse, error) { | ||
var response LatestCatchpointResponse | ||
|
||
var url CatchPointUrl | ||
switch network { | ||
case "fnet-v1", "fnet": | ||
url = FNet | ||
case "betanet-v1.0", "betanet": | ||
url = BetaNet | ||
case "testnet-v1.0", "testnet": | ||
url = TestNet | ||
case "mainnet-v1.0", "mainnet": | ||
url = MainNet | ||
} | ||
|
||
res, err := http.Get(string(url)) | ||
if err != nil { | ||
return response, err | ||
} | ||
defer res.Body.Close() | ||
|
||
// Handle invalid codes as errors | ||
if res.StatusCode >= 300 { | ||
return response, errors.New(res.Status) | ||
} | ||
|
||
// Read from the response | ||
var body []byte | ||
body, err = io.ReadAll(res.Body) | ||
if err != nil { | ||
return response, err | ||
} | ||
// Set the body and return | ||
response.JSON200 = strings.Replace(string(body), "\n", "", -1) | ||
return response, 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
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,65 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
) | ||
|
||
const ChannelNotFoundMsg = "channel not found" | ||
|
||
type GithubVersionResponse struct { | ||
ResponseCode int | ||
ResponseStatus string | ||
JSON200 string | ||
} | ||
|
||
func (r GithubVersionResponse) StatusCode() int { | ||
return r.ResponseCode | ||
} | ||
func (r GithubVersionResponse) Status() string { | ||
return r.ResponseStatus | ||
} | ||
|
||
func GetGoAlgorandReleaseWithResponse(http HttpPkgInterface, channel string) (*GithubVersionResponse, error) { | ||
var versions GithubVersionResponse | ||
resp, err := http.Get("https://api.github.com/repos/algorand/go-algorand/releases") | ||
if resp == nil || err != nil { | ||
return nil, err | ||
} | ||
// Update Model | ||
versions.ResponseCode = resp.StatusCode | ||
versions.ResponseStatus = resp.Status | ||
|
||
// Exit if not 200 | ||
if resp.StatusCode != 200 { | ||
return &versions, nil | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
// Parse the versions to a map | ||
var versionsMap []map[string]interface{} | ||
if err = json.NewDecoder(resp.Body).Decode(&versionsMap); err != nil { | ||
return &versions, err | ||
} | ||
// Look for versions in the response | ||
var versionResponse *string | ||
for i := range versionsMap { | ||
tn := versionsMap[i]["tag_name"].(string) | ||
if strings.Contains(tn, channel) { | ||
versionResponse = &tn | ||
break | ||
} | ||
|
||
} | ||
|
||
// If the tag was not found, return an error | ||
if versionResponse == nil { | ||
return &versions, errors.New(ChannelNotFoundMsg) | ||
} | ||
|
||
// Update the JSON200 data and return | ||
versions.JSON200 = *versionResponse | ||
return &versions, 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,54 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
type testResponse struct { | ||
HttpPkgInterface | ||
} | ||
|
||
var jsonStr = `[{ | ||
"tag_name": "v3.26.0-beta" | ||
}]` | ||
|
||
func (testResponse) Get(url string) (resp *http.Response, err error) { | ||
|
||
responseBody := io.NopCloser(bytes.NewReader([]byte(jsonStr))) | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: responseBody, | ||
}, nil | ||
} | ||
|
||
type testError struct { | ||
HttpPkgInterface | ||
} | ||
|
||
func (testError) Get(url string) (resp *http.Response, err error) { | ||
return &http.Response{ | ||
StatusCode: 404, | ||
}, errors.New("not found") | ||
} | ||
|
||
func Test_Github(t *testing.T) { | ||
r, err := GetGoAlgorandReleaseWithResponse(new(testResponse), "beta") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if r.StatusCode() != 200 { | ||
t.Error("should be 200 response") | ||
} | ||
if r.JSON200 != "v3.26.0-beta" { | ||
t.Error("should return v3.26.0-beta") | ||
} | ||
|
||
_, err = GetGoAlgorandReleaseWithResponse(new(testError), "beta") | ||
if err == nil { | ||
t.Error("should fail to get") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package api | ||
|
||
import "net/http" | ||
|
||
|
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,6 @@ | ||
package api | ||
|
||
type ResponseInterface interface { | ||
StatusCode() int | ||
Status() string | ||
} |
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,30 @@ | ||
package api | ||
|
||
type StatusLike struct { | ||
Catchpoint *string `json:"catchpoint,omitempty"` | ||
CatchpointAcquiredBlocks *int `json:"catchpoint-acquired-blocks,omitempty"` | ||
CatchpointProcessedAccounts *int `json:"catchpoint-processed-accounts,omitempty"` | ||
CatchpointProcessedKvs *int `json:"catchpoint-processed-kvs,omitempty"` | ||
CatchpointTotalAccounts *int `json:"catchpoint-total-accounts,omitempty"` | ||
CatchpointTotalBlocks *int `json:"catchpoint-total-blocks,omitempty"` | ||
CatchpointTotalKvs *int `json:"catchpoint-total-kvs,omitempty"` | ||
CatchpointVerifiedAccounts *int `json:"catchpoint-verified-accounts,omitempty"` | ||
CatchpointVerifiedKvs *int `json:"catchpoint-verified-kvs,omitempty"` | ||
CatchupTime int `json:"catchup-time"` | ||
LastCatchpoint *string `json:"last-catchpoint,omitempty"` | ||
LastRound int `json:"last-round"` | ||
LastVersion string `json:"last-version"` | ||
NextVersion string `json:"next-version"` | ||
NextVersionRound int `json:"next-version-round"` | ||
NextVersionSupported bool `json:"next-version-supported"` | ||
StoppedAtUnsupportedRound bool `json:"stopped-at-unsupported-round"` | ||
TimeSinceLastRound int `json:"time-since-last-round"` | ||
UpgradeDelay *int `json:"upgrade-delay,omitempty"` | ||
UpgradeNextProtocolVoteBefore *int `json:"upgrade-next-protocol-vote-before,omitempty"` | ||
UpgradeNoVotes *int `json:"upgrade-no-votes,omitempty"` | ||
UpgradeNodeVote *bool `json:"upgrade-node-vote,omitempty"` | ||
UpgradeVoteRounds *int `json:"upgrade-vote-rounds,omitempty"` | ||
UpgradeVotes *int `json:"upgrade-votes,omitempty"` | ||
UpgradeVotesRequired *int `json:"upgrade-votes-required,omitempty"` | ||
UpgradeYesVotes *int `json:"upgrade-yes-votes,omitempty"` | ||
} |
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,65 @@ | ||
package node | ||
|
||
import ( | ||
"context" | ||
"github.com/algorandfoundation/algorun-tui/api" | ||
"github.com/algorandfoundation/algorun-tui/cmd/utils" | ||
"github.com/algorandfoundation/algorun-tui/internal/algod" | ||
"github.com/algorandfoundation/algorun-tui/ui/style" | ||
"github.com/charmbracelet/log" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var syncEndpoint string | ||
var syncToken string | ||
var defaultLag int = 30_000 | ||
var syncCmd = utils.WithAlgodFlags(&cobra.Command{ | ||
Use: "sync", | ||
Short: "Fast Catchup", | ||
Long: "Checks if the node is caught up and if not, starts catching up.", | ||
SilenceUsage: true, | ||
PersistentPreRun: NeedsToBeRunning, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := utils.InitConfig() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ep := viper.GetString("algod-endpoint") | ||
t := viper.GetString("algod-token") | ||
if ep == "" { | ||
log.Fatal("algod-endpoint is required") | ||
} | ||
if t == "" { | ||
log.Fatal("algod-token is required") | ||
} | ||
// TODO: Perf testing as a dedicated cmd (node perf catchup with exit 0 or 1) | ||
// NOTE: we do not want to pollute this command with perf testing | ||
// just allow it to post it's minimum requirements and preform a fast catchup if necessary. | ||
ctx := context.Background() | ||
httpPkg := new(api.HttpPkg) | ||
client, err := algod.GetClient(viper.GetString("algod-endpoint"), viper.GetString("algod-token")) | ||
cobra.CheckErr(err) | ||
|
||
status, _, err := algod.NewStatus(ctx, client, httpPkg) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if status.State == algod.FastCatchupState { | ||
log.Fatal(style.Red.Render("Node is currently catching up. Use --abort to cancel.")) | ||
} | ||
|
||
catchpoint, _, err := algod.GetLatestCatchpoint(httpPkg, status.Network) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Info(style.Green.Render("Latest Catchpoint: " + catchpoint)) | ||
|
||
res, _, err := algod.PostCatchpoint(ctx, client, catchpoint, &api.StartCatchupParams{Min: &defaultLag}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Info(style.Green.Render(res)) | ||
}, | ||
}, &syncEndpoint, &syncToken) |
Oops, something went wrong.