Skip to content

Commit

Permalink
refactor(internal): move GetAddressFromGenesis to test package
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 26, 2024
1 parent d6b8708 commit f9352b9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 66 deletions.
65 changes: 0 additions & 65 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package internal

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

"github.com/algorandfoundation/hack-tui/api"
Expand All @@ -28,69 +26,6 @@ type Account struct {
Expires time.Time
}

// Gets the list of addresses created at genesis from the genesis file
func getAddressesFromGenesis(client api.ClientInterface) ([]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
}

// Get Online Status of Account
func GetAccount(client api.ClientWithResponsesInterface, address string) (api.Account, error) {
var format api.AccountInformationParamsFormat = "json"
Expand Down
4 changes: 3 additions & 1 deletion internal/accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package internal

import (
"context"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal/test"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +20,7 @@ func Test_AccountsFromState(t *testing.T) {
}
client, err := api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))

addresses, rewardsPool, feeSink, err := getAddressesFromGenesis(client)
addresses, rewardsPool, feeSink, err := test.GetAddressesFromGenesis(context.Background(), client)

if err != nil {
t.Fatal(err)
Expand Down
73 changes: 73 additions & 0 deletions internal/test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package test

import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/algorandfoundation/hack-tui/api"
"io"
)

// GetAddressesFromGenesis gets the list of addresses created at genesis from the genesis file
func GetAddressesFromGenesis(ctx context.Context, client api.ClientInterface) ([]string, string, string, error) {
resp, err := client.GetGenesis(ctx)
if err != nil {
return []string{}, "", "", err
}

Check warning on line 17 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L16-L17

Added lines #L16 - L17 were not covered by tests

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

Check warning on line 21 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L20-L21

Added lines #L20 - L21 were not covered by tests

defer resp.Body.Close()

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

Check warning on line 29 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L28-L29

Added lines #L28 - L29 were not covered by tests

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

Check warning on line 36 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L35-L36

Added lines #L35 - L36 were not covered by tests

// 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)
}

Check warning on line 62 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L57-L62

Added lines #L57 - L62 were not covered by tests
}
} else {
return []string{}, "", "", errors.New("alloc is not a list")
}

Check warning on line 66 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L64-L66

Added lines #L64 - L66 were not covered by tests

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

Check warning on line 70 in internal/test/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/test/utils.go#L69-L70

Added lines #L69 - L70 were not covered by tests

return addresses, rewardsPool, feeSink, nil
}

0 comments on commit f9352b9

Please sign in to comment.