Skip to content

Commit

Permalink
refactor: status and http interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 17, 2024
1 parent 05be82f commit 91e23f8
Show file tree
Hide file tree
Showing 38 changed files with 803 additions and 497 deletions.
66 changes: 66 additions & 0 deletions api/catchpoint.go
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
}
5 changes: 2 additions & 3 deletions internal/catchpoint_test.go → api/catchpoint_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package internal
package api

import (
"context"
"testing"
)

func Test_GetLatestCatchpoint(t *testing.T) {
catchpoint, err := GetLatestCatchpoint(context.Background(), new(HttpPkg), "mainnet")
catchpoint, err := GetLatestCatchpointWithResponse(new(HttpPkg), "mainnet")
if err != nil {
t.Error(err)
}
Expand Down
65 changes: 65 additions & 0 deletions api/github.go
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
}
54 changes: 54 additions & 0 deletions api/github_test.go
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")
}
}
2 changes: 1 addition & 1 deletion internal/http.go → api/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package api

import "net/http"

Expand Down
6 changes: 6 additions & 0 deletions api/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package api

type ResponseInterface interface {
StatusCode() int
Status() string
}
30 changes: 30 additions & 0 deletions api/status.go
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"`
}
11 changes: 5 additions & 6 deletions cmd/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package configure
import (
"bytes"
"fmt"
"github.com/algorandfoundation/algorun-tui/cmd/node"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/utils"
"os"
Expand All @@ -16,11 +15,11 @@ import (
)

var Cmd = &cobra.Command{
Use: "configure",
Short: "Configure Algod",
Long: "Configure Algod settings",
SilenceUsage: true,
PersistentPreRun: node.NeedsToBeStopped,
Use: "configure",
Short: "Configure Algod",
Long: "Configure Algod settings",
SilenceUsage: true,
//PersistentPreRun:
//RunE: func(cmd *cobra.Command, args []string) error {
// return configureNode()
//},
Expand Down
1 change: 1 addition & 0 deletions cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ func init() {
Cmd.AddCommand(stopCmd)
Cmd.AddCommand(uninstallCmd)
Cmd.AddCommand(upgradeCmd)
Cmd.AddCommand(syncCmd)
Cmd.AddCommand(debugCmd)
}
65 changes: 65 additions & 0 deletions cmd/node/sync.go
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)
Loading

0 comments on commit 91e23f8

Please sign in to comment.