Skip to content
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

test: internal/accounts.go #15

Merged
merged 10 commits into from
Oct 29, 2024
5 changes: 4 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- name: Start Docker Compose
run: docker compose up -d

- name: Wait for the server to start
run: npx wait-on tcp:8080

- name: Install dependencies
run: go get .

Expand Down Expand Up @@ -55,7 +58,7 @@ jobs:
- name: Test with the Go CLI
run: go test ./... -coverprofile=./cover.out -covermode=atomic -coverpkg=./...

- name: check test coverage
- name: Check test coverage
uses: vladopajic/go-test-coverage@v2
with:
config: ./.testcoverage.yaml
102 changes: 102 additions & 0 deletions api/lf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ output-options:
- AppendKeys
- GetBlock
- AccountInformation
- GetGenesis
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ require (
github.com/oapi-codegen/runtime v1.1.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
)

require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

require (
github.com/algorand/go-algorand-sdk/v2 v2.6.0
Expand Down
78 changes: 78 additions & 0 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package internal

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"time"

"github.com/algorandfoundation/hack-tui/api"
Expand All @@ -27,6 +29,78 @@ type Account struct {
LastModified int
}

// Gets the list of addresses created at genesis from the genesis file
func getAddressesFromGenesis(client *api.ClientWithResponses) ([]string, string, string, error) {
resp, err := client.GetGenesis(context.Background())
if err != nil {
return []string{}, "", "", err
}

if resp.StatusCode != 200 {
return []string{}, "", "", errors.New(fmt.Sprintf("Failed to get genesis file. Received error code: %d", resp.StatusCode))
}

defer resp.Body.Close()

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return []string{}, "", "", err
}

// Unmarshal the JSON response into a map
var jsonResponse map[string]interface{}
err = json.Unmarshal(body, &jsonResponse)
if err != nil {
return []string{}, "", "", err
}

// Two special addresses
rewardsPool := "7777777777777777777777777777777777777777777777777774MSJUVU"
feeSink := "A7NMWS3NT3IUDMLVO26ULGXGIIOUQ3ND2TXSER6EBGRZNOBOUIQXHIBGDE"
rewardsPoolIncluded := false
feeSinkIncluded := false

// Loop over each entry in the "alloc" list and collect the "addr" values
var addresses []string
if allocList, ok := jsonResponse["alloc"].([]interface{}); ok {
for _, entry := range allocList {
if entryMap, ok := entry.(map[string]interface{}); ok {
if addr, ok := entryMap["addr"].(string); ok {
if addr == rewardsPool {
rewardsPoolIncluded = true
} else if addr == feeSink {
feeSinkIncluded = true
} else {
addresses = append(addresses, addr)
}
} else {
return []string{}, "", "", fmt.Errorf("In genesis.json no addr string found in list element entry: %+v", entry)
}
} else {
return []string{}, "", "", fmt.Errorf("In genesis.json list element of alloc-field is not a map: %+v", entry)
}
}
} else {
return []string{}, "", "", errors.New("alloc is not a list")
}

if !rewardsPoolIncluded || !feeSinkIncluded {
return []string{}, "", "", errors.New("Expected RewardsPool and/or FeeSink addresses NOT found in genesis file")
}

return addresses, rewardsPool, feeSink, nil
}

func isValidStatus(status string) bool {
validStatuses := map[string]bool{
"Online": true,
"Offline": true,
"Not Participating": true,
}
return validStatuses[status]
}

// Get Online Status of Account
func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (string, error) {
var format api.AccountInformationParamsFormat = "json"
Expand All @@ -45,6 +119,10 @@ func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (st
return "N/A", errors.New(fmt.Sprintf("Failed to get account information. Received error code: %d", r.StatusCode()))
}

if r.JSON200 == nil {
return "N/A", errors.New("Failed to get account information. JSON200 is nil")
}

return r.JSON200.Status, nil
}

Expand Down
Loading
Loading