From beaf093d94eb5f164374fbd97666c45623e796ae Mon Sep 17 00:00:00 2001 From: Redouane Lakrache Date: Tue, 14 Nov 2023 20:28:39 +0100 Subject: [PATCH] feat: Use yaml format instead of json --- x/supplier/client/cli/stake_options_reader.go | 4 +- .../client/cli/tx_stake_supplier_test.go | 310 +++++++----------- 2 files changed, 115 insertions(+), 199 deletions(-) diff --git a/x/supplier/client/cli/stake_options_reader.go b/x/supplier/client/cli/stake_options_reader.go index cf7613747..553434fc9 100644 --- a/x/supplier/client/cli/stake_options_reader.go +++ b/x/supplier/client/cli/stake_options_reader.go @@ -1,12 +1,12 @@ package cli import ( - "encoding/json" "fmt" "net/url" "os" sdk "github.com/cosmos/cosmos-sdk/types" + "gopkg.in/yaml.v2" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) @@ -46,7 +46,7 @@ func parseStakeConfig(configFile string) (*ParsedStakeConfig, error) { // Unmarshal the stake config file into a stakeConfig var stakeConfig *StakeConfig - if err := json.Unmarshal(configContent, &stakeConfig); err != nil { + if err := yaml.Unmarshal(configContent, &stakeConfig); err != nil { return nil, err } diff --git a/x/supplier/client/cli/tx_stake_supplier_test.go b/x/supplier/client/cli/tx_stake_supplier_test.go index ca1181e7e..3e8cd577f 100644 --- a/x/supplier/client/cli/tx_stake_supplier_test.go +++ b/x/supplier/client/cli/tx_stake_supplier_test.go @@ -38,21 +38,14 @@ func TestCLI_StakeSupplier(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), } - defaultConfig := - `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network:8081", - "rpc_type": "json_rpc" - } - ] - } - ] -}` + defaultConfig := `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network:8081 + rpc_type: json_rpc +` tests := []struct { desc string @@ -87,159 +80,106 @@ func TestCLI_StakeSupplier(t *testing.T) { address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidStake, // stakeString: "explicitly missing", - config: `{ - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network:8081", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network:8081 + rpc_type: json_rpc +`, }, { desc: "stake supplier: invalid stake denom", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidStake, - config: `{ - "stake": "1000invalid", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network:8081", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000invalid +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network:8081 + rpc_type: json_rpc +`, }, { desc: "stake supplier: invalid stake amount (zero)", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidStake, - config: `{ - "stake": "0upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network:8081", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 0upokt +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network:8081 + rpc_type: json_rpc +`, }, { desc: "stake supplier: invalid stake amount (negative)", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidStake, - config: `{ - "stake": "-1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network:8081", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: "-1000upokt" +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network:8081 + rpc_type: json_rpc +`, }, // Happy Paths - Service Related { desc: "services_test: valid multiple services", address: supplierAccount.Address.String(), - config: `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network:8081", - "rpc_type": "json_rpc" - } - ] - }, - { - "service_id": "svc2", - "endpoints": [ - { - "url": "http://pokt.network:8082", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network:8081 + rpc_type: json_rpc +- service_id: svc2 + endpoints: + - url: http://pokt.network:8082 + rpc_type: json_rpc +`, }, { desc: "services_test: valid localhost", address: supplierAccount.Address.String(), - config: `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://127.0.0.1:8082", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: http://127.0.0.1:8082 + rpc_type: json_rpc +`, }, { desc: "services_test: valid loopback", address: supplierAccount.Address.String(), - config: `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://localhost:8082", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: http://localhost:8082 + rpc_type: json_rpc +`, }, { desc: "services_test: valid without a pork", address: supplierAccount.Address.String(), - config: `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "http://pokt.network", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: http://pokt.network + rpc_type: json_rpc +`, }, // Error Paths - Service Related @@ -248,93 +188,69 @@ func TestCLI_StakeSupplier(t *testing.T) { address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidServiceConfig, // servicesString: "explicitly omitted", - config: `{ - "stake": "1000upokt", -}`, + config: `--- +stake: 1000upokt +`, }, { desc: "services_test: invalid services (empty string)", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidServiceConfig, - config: `{ - "stake": "1000upokt", - "services": [] -}`, + config: `--- +stake: 1000upokt +services: [] +`, }, { desc: "services_test: invalid URL", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidServiceConfig, - config: `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "bad_url", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: bad_url + rpc_type: json_rpc +`, }, { desc: "services_test: missing URLs", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidServiceConfig, - config: `{ - "stake": "1000upokt", - "services": [ - { "service_id": "svc1" }, - { "service_id": "svc2" } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 +- service_id: svc2 +`, }, { desc: "services_test: missing service IDs", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidServiceConfig, - config: `{ - "stake": "1000upokt", - "services": [ - { - "endpoints": [ - { - "url": "localhost:8081", - "rpc_type": "json_rpc" - } - ] - }, - { - "endpoints": [ - { - "url": "localhost:8082", - "rpc_type": "json_rpc" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- endpoints: + - url: localhost:8081 + rpc_type: json_rpc +- endpoints: + - url: localhost:8082 + rpc_type: json_rpc +`, }, { desc: "services_test: missing rpc type", address: supplierAccount.Address.String(), err: types.ErrSupplierInvalidServiceConfig, - config: `{ - "stake": "1000upokt", - "services": [ - { - "service_id": "svc1", - "endpoints": [ - { - "url": "localhost:8082" - } - ] - } - ] -}`, + config: `--- +stake: 1000upokt +services: +- service_id: svc1 + endpoints: + - url: localhost:8082 +`, }, }