Skip to content

Commit 6a44bcf

Browse files
authored
Merge pull request #15 from algorandfoundation/ci/increase-cov
test: internal/accounts.go
2 parents a21f70a + 9617992 commit 6a44bcf

File tree

7 files changed

+308
-3
lines changed

7 files changed

+308
-3
lines changed

.github/workflows/test.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: Start Docker Compose
2626
run: docker compose up -d
2727

28+
- name: Wait for the server to start
29+
run: npx wait-on tcp:8080
30+
2831
- name: Install dependencies
2932
run: go get .
3033

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

58-
- name: check test coverage
61+
- name: Check test coverage
5962
uses: vladopajic/go-test-coverage@v2
6063
with:
6164
config: ./.testcoverage.yaml

api/lf.go

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generate.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ output-options:
1919
- AppendKeys
2020
- GetBlock
2121
- AccountInformation
22+
- GetGenesis

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ require (
1515
github.com/oapi-codegen/runtime v1.1.1
1616
github.com/spf13/cobra v1.8.1
1717
github.com/spf13/viper v1.19.0
18+
github.com/stretchr/testify v1.9.0
1819
)
1920

20-
require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
21+
require (
22+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
23+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
24+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
25+
)
2126

2227
require (
2328
github.com/algorand/go-algorand-sdk/v2 v2.6.0

internal/accounts.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package internal
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
8+
"io"
79
"time"
810

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

32+
// Gets the list of addresses created at genesis from the genesis file
33+
func getAddressesFromGenesis(client *api.ClientWithResponses) ([]string, string, string, error) {
34+
resp, err := client.GetGenesis(context.Background())
35+
if err != nil {
36+
return []string{}, "", "", err
37+
}
38+
39+
if resp.StatusCode != 200 {
40+
return []string{}, "", "", errors.New(fmt.Sprintf("Failed to get genesis file. Received error code: %d", resp.StatusCode))
41+
}
42+
43+
defer resp.Body.Close()
44+
45+
// Read the response body
46+
body, err := io.ReadAll(resp.Body)
47+
if err != nil {
48+
return []string{}, "", "", err
49+
}
50+
51+
// Unmarshal the JSON response into a map
52+
var jsonResponse map[string]interface{}
53+
err = json.Unmarshal(body, &jsonResponse)
54+
if err != nil {
55+
return []string{}, "", "", err
56+
}
57+
58+
// Two special addresses
59+
rewardsPool := "7777777777777777777777777777777777777777777777777774MSJUVU"
60+
feeSink := "A7NMWS3NT3IUDMLVO26ULGXGIIOUQ3ND2TXSER6EBGRZNOBOUIQXHIBGDE"
61+
rewardsPoolIncluded := false
62+
feeSinkIncluded := false
63+
64+
// Loop over each entry in the "alloc" list and collect the "addr" values
65+
var addresses []string
66+
if allocList, ok := jsonResponse["alloc"].([]interface{}); ok {
67+
for _, entry := range allocList {
68+
if entryMap, ok := entry.(map[string]interface{}); ok {
69+
if addr, ok := entryMap["addr"].(string); ok {
70+
if addr == rewardsPool {
71+
rewardsPoolIncluded = true
72+
} else if addr == feeSink {
73+
feeSinkIncluded = true
74+
} else {
75+
addresses = append(addresses, addr)
76+
}
77+
} else {
78+
return []string{}, "", "", fmt.Errorf("In genesis.json no addr string found in list element entry: %+v", entry)
79+
}
80+
} else {
81+
return []string{}, "", "", fmt.Errorf("In genesis.json list element of alloc-field is not a map: %+v", entry)
82+
}
83+
}
84+
} else {
85+
return []string{}, "", "", errors.New("alloc is not a list")
86+
}
87+
88+
if !rewardsPoolIncluded || !feeSinkIncluded {
89+
return []string{}, "", "", errors.New("Expected RewardsPool and/or FeeSink addresses NOT found in genesis file")
90+
}
91+
92+
return addresses, rewardsPool, feeSink, nil
93+
}
94+
95+
func isValidStatus(status string) bool {
96+
validStatuses := map[string]bool{
97+
"Online": true,
98+
"Offline": true,
99+
"Not Participating": true,
100+
}
101+
return validStatuses[status]
102+
}
103+
30104
// Get Online Status of Account
31105
func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (string, error) {
32106
var format api.AccountInformationParamsFormat = "json"
@@ -45,6 +119,10 @@ func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (st
45119
return "N/A", errors.New(fmt.Sprintf("Failed to get account information. Received error code: %d", r.StatusCode()))
46120
}
47121

122+
if r.JSON200 == nil {
123+
return "N/A", errors.New("Failed to get account information. JSON200 is nil")
124+
}
125+
48126
return r.JSON200.Status, nil
49127
}
50128

0 commit comments

Comments
 (0)