Skip to content

Commit

Permalink
feat: moves getAddressesFromGenesis into accounts.go from test
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 28, 2024
1 parent 5d16adf commit 03a7861
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 77 deletions.
77 changes: 77 additions & 0 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package internal

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

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

func getAddressesFromGenesis() ([]string, string, string, error) {

// TODO: replace with calls to GetGenesis

resp, err := http.Get("http://localhost:8080/genesis")
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 Down
82 changes: 5 additions & 77 deletions internal/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package internal

import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
"time"

Expand All @@ -13,79 +10,6 @@ import (
"github.com/stretchr/testify/assert"
)

func getAddressesFromGenesis(t *testing.T) ([]string, string, string) {

// TODO: replace with calls to GetGenesis

resp, err := http.Get("http://localhost:8080/genesis")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

// Check the response status code
assert.Equal(t, 200, resp.StatusCode)

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

// Unmarshal the JSON response into a map
var jsonResponse map[string]interface{}
err = json.Unmarshal(body, &jsonResponse)
if err != nil {
t.Fatal(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 {
t.Logf("Address not found in entry: %+v", entry)
}
} else {
t.Logf("Entry is not a map: %+v", entry)
}
}
} else {
t.Fatal("alloc is not a list")
}

if !rewardsPoolIncluded || !feeSinkIncluded {

t.Fatalf("Expected RewardsPool and/or FeeSink addresses NOT found in genesis file")
}

return addresses, rewardsPool, feeSink
}

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

func Test_AccountsFromState(t *testing.T) {

// Setup elevated client
Expand All @@ -95,7 +19,11 @@ func Test_AccountsFromState(t *testing.T) {
}
client, err := api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))

addresses, rewardsPool, feeSink := getAddressesFromGenesis(t)
addresses, rewardsPool, feeSink, err := getAddressesFromGenesis()

if err != nil {
t.Fatal(err)
}

// Test getAccountOnlineStatus

Expand Down

0 comments on commit 03a7861

Please sign in to comment.