Skip to content

Commit

Permalink
Merge pull request #15 from algorandfoundation/ci/increase-cov
Browse files Browse the repository at this point in the history
test: internal/accounts.go
  • Loading branch information
PhearZero authored Oct 29, 2024
2 parents 4cf589d + 6671144 commit d7ec2cc
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 3 deletions.
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

0 comments on commit d7ec2cc

Please sign in to comment.