Skip to content

Commit

Permalink
test: Update staking tests to use a json config
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Nov 14, 2023
1 parent 52077c1 commit 3f82f1b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
34 changes: 17 additions & 17 deletions x/supplier/client/cli/stake_options_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,47 @@ import (
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// parsedStakeConfig is a parsed version of the stake config file
type parsedStakeConfig struct {
// ParsedStakeConfig is a parsed version of the stake config file
type ParsedStakeConfig struct {
stake sdk.Coin
services []*sharedtypes.SupplierServiceConfig
}

// stakeConfig is the structure of the stake config file
type stakeConfig struct {
// StakeConfig is the structure of the stake config file
type StakeConfig struct {
Stake string `json:"stake"`
Services []stakeService `json:"services"`
Services []StakeService `json:"services"`
}

// stakeService is the structure describing a single service stake entry in the stake config file
type stakeService struct {
// StakeService is the structure describing a single service stake entry in the stake config file
type StakeService struct {
ServiceId string `json:"service_id"`
Endpoints []serviceEndpoint `json:"endpoints"`
Endpoints []ServiceEndpoint `json:"endpoints"`
}

// serviceEndpoint is the structure describing a single service endpoint in the stake config file
type serviceEndpoint struct {
// ServiceEndpoint is the structure describing a single service endpoint in the stake config file
type ServiceEndpoint struct {
Url string `json:"url"`
RPCType string `json:"rpc_type"`
Config map[string]string `json:"config"`
}

// parseStakeConfig parses the stake config file into a parsedStakeConfig
func parseStakeConfig(configFile string) (*parsedStakeConfig, error) {
func parseStakeConfig(configFile string) (*ParsedStakeConfig, error) {
// Read the stake config file into memory
configContent, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}

// Unmarshal the stake config file into a stakeConfig
var stakeConfig *stakeConfig
var stakeConfig *StakeConfig
if err := json.Unmarshal(configContent, &stakeConfig); err != nil {
return nil, err
}

// Prepare the parsedStakeConfig
parsedStakeConfig := &parsedStakeConfig{}
parsedStakeConfig := &ParsedStakeConfig{}

// Parse the stake amount and assign it to the parsedStakeConfig
parsedStakeConfig.stake, err = sdk.ParseCoinNormalized(stakeConfig.Stake)
Expand Down Expand Up @@ -93,7 +93,7 @@ func parseStakeConfig(configFile string) (*parsedStakeConfig, error) {
return parsedStakeConfig, nil
}

func parseEndpointEntry(endpoint serviceEndpoint) (*sharedtypes.SupplierEndpoint, error) {
func parseEndpointEntry(endpoint ServiceEndpoint) (*sharedtypes.SupplierEndpoint, error) {
endpointEntry := &sharedtypes.SupplierEndpoint{}

// Endpoint URL
Expand All @@ -112,7 +112,7 @@ func parseEndpointEntry(endpoint serviceEndpoint) (*sharedtypes.SupplierEndpoint
return endpointEntry, nil
}

func validateEndpointURL(endpoint serviceEndpoint) (string, error) {
func validateEndpointURL(endpoint ServiceEndpoint) (string, error) {
// Validate the endpoint URL
if _, err := url.Parse(endpoint.Url); err != nil {
return "", err
Expand All @@ -121,7 +121,7 @@ func validateEndpointURL(endpoint serviceEndpoint) (string, error) {
return endpoint.Url, nil
}

func parseEndpointConfigs(endpoint serviceEndpoint) []*sharedtypes.ConfigOption {
func parseEndpointConfigs(endpoint ServiceEndpoint) []*sharedtypes.ConfigOption {
// Prepare the endpoint configs slice
endpointConfigs := []*sharedtypes.ConfigOption{}

Expand Down Expand Up @@ -150,7 +150,7 @@ func parseEndpointConfigs(endpoint serviceEndpoint) []*sharedtypes.ConfigOption
return endpointConfigs
}

func parseEndpointRPCType(endpoint serviceEndpoint) sharedtypes.RPCType {
func parseEndpointRPCType(endpoint ServiceEndpoint) sharedtypes.RPCType {
switch endpoint.RPCType {
case "json_rpc":
return sharedtypes.RPCType_JSON_RPC
Expand Down
36 changes: 34 additions & 2 deletions x/supplier/client/cli/tx_stake_supplier_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli_test

import (
"encoding/json"
"fmt"
"strings"
"testing"

sdkerrors "cosmossdk.io/errors"
Expand Down Expand Up @@ -179,10 +181,12 @@ func TestCLI_StakeSupplier(t *testing.T) {
// Wait for a new block to be committed
require.NoError(t, net.WaitForNextBlock())

// write the stake config to a file
configPath := writeTestConfigToFile(tt.stakeString, tt.servicesString, t)

// Prepare the arguments for the CLI command
args := []string{
tt.stakeString,
tt.servicesString,
fmt.Sprintf("--config=%s", configPath),
fmt.Sprintf("--%s=%s", flags.FlagFrom, tt.address),
}
args = append(args, commonArgs...)
Expand All @@ -208,3 +212,31 @@ func TestCLI_StakeSupplier(t *testing.T) {
})
}
}

func writeTestConfigToFile(stakeString, servicesString string, t *testing.T) string {
stakeConfig := &cli.StakeConfig{
Stake: stakeString,
Services: []cli.StakeService{},
}
services := strings.Split(servicesString, ",")

for _, service := range services {
serviceParts := strings.Split(service, ";")

stakeConfig.Services = append(stakeConfig.Services, cli.StakeService{
ServiceId: serviceParts[0],
Endpoints: []cli.ServiceEndpoint{
{
Url: serviceParts[1],
RPCType: "json_rpc",
},
},
})
}

bz, err := json.Marshal(stakeConfig)
require.NoError(t, err)

file := testutil.WriteToNewTempFile(t, string(bz))
return file.Name()
}

0 comments on commit 3f82f1b

Please sign in to comment.