From aebfd77ae41a580f3cbf5a0d3512a2fa55f8404f Mon Sep 17 00:00:00 2001 From: Bryan White Date: Thu, 21 Dec 2023 19:37:43 +0100 Subject: [PATCH] refactor: in-memory network usage --- .../delegation/client_integration_test.go | 76 +++++-------- .../client/cli/query_application_test.go | 73 ++++++++----- .../client/cli/tx_delegate_to_gateway_test.go | 61 ++++++----- .../client/cli/tx_stake_application_test.go | 72 +++++++------ .../cli/tx_undelegate_from_gateway_test.go | 59 +++++------ .../client/cli/tx_unstake_application_test.go | 41 ++++--- x/gateway/client/cli/query_gateway_test.go | 77 +++++++++----- x/gateway/client/cli/tx_stake_gateway_test.go | 62 +++++------ .../client/cli/tx_unstake_gateway_test.go | 40 ++++--- .../client/cli/query_get_session_test.go | 39 +++++-- x/supplier/client/cli/query_claim_test.go | 87 +++++++++------ x/supplier/client/cli/query_supplier_test.go | 39 +++++-- .../client/cli/tx_stake_supplier_test.go | 100 ++++++++++-------- .../client/cli/tx_unstake_supplier_test.go | 49 +++++---- 14 files changed, 490 insertions(+), 385 deletions(-) diff --git a/pkg/client/delegation/client_integration_test.go b/pkg/client/delegation/client_integration_test.go index e52922442..40bacfc9f 100644 --- a/pkg/client/delegation/client_integration_test.go +++ b/pkg/client/delegation/client_integration_test.go @@ -18,13 +18,13 @@ import ( "time" "cosmossdk.io/depinject" - "github.com/cosmos/cosmos-sdk/testutil" "github.com/stretchr/testify/require" "github.com/pokt-network/poktroll/pkg/client" "github.com/pokt-network/poktroll/pkg/client/delegation" "github.com/pokt-network/poktroll/pkg/client/events" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/gatewaynet" apptypes "github.com/pokt-network/poktroll/x/application/types" gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types" ) @@ -39,7 +39,25 @@ const ( func TestDelegationClient_RedelegationsObservables(t *testing.T) { t.SkipNow() // Create the network with 2 applications and 1 gateway - net, appAddresses, gatewayAddr := createNetworkWithApplicationsAndGateways(t) + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumApplications: 2, + NumGateways: 1, + }, + ) + memnet.Start(ctx, t) + + gatewayGenesisState := network.GetGenesisState[*gatewaytypes.GenesisState](t, gatewaytypes.ModuleName, memnet) + gatewayAddr := gatewayGenesisState.GatewayList[0].GetAddress() + + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + var appAddresses []string + for _, application := range appGenesisState.ApplicationList { + appAddresses = append(appAddresses, application.GetAddress()) + } + + net := memnet.GetNetwork(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -74,7 +92,7 @@ func TestDelegationClient_RedelegationsObservables(t *testing.T) { // of the Redelegation event alternates between app1 and app2 if previousRedelegation != nil { require.NotEqual(t, previousRedelegation.GetAppAddress(), change.GetAppAddress()) - if previousRedelegation.AppAddress() == appAddresses[0] { + if previousRedelegation.GetAppAddress() == appAddresses[0] { require.Equal(t, appAddresses[1], change.GetAppAddress()) } else { require.Equal(t, appAddresses[0], change.GetAppAddress()) @@ -96,22 +114,22 @@ func TestDelegationClient_RedelegationsObservables(t *testing.T) { // Delegate from app1 to gateway t.Log(time.Now().String()) t.Logf("delegating from app %s to gateway %s", appAddresses[0], gatewayAddr) - network.DelegateAppToGateway(t, net, appAddresses[0], gatewayAddr) + memnet.DelegateAppToGateway(t, appAddresses[0], gatewayAddr) // need to wait for the account to be initialized in the next block require.NoError(t, net.WaitForNextBlock()) // Delegate from app2 to gateway t.Logf("delegating from app %s to gateway %s", appAddresses[1], gatewayAddr) - network.DelegateAppToGateway(t, net, appAddresses[1], gatewayAddr) + memnet.DelegateAppToGateway(t, appAddresses[1], gatewayAddr) // need to wait for the account to be initialized in the next block require.NoError(t, net.WaitForNextBlock()) // Undelegate from app1 to gateway t.Logf("undelegating from app %s to gateway %s", appAddresses[0], gatewayAddr) - network.UndelegateAppFromGateway(t, net, appAddresses[0], gatewayAddr) + memnet.UndelegateAppFromGateway(t, appAddresses[0], gatewayAddr) // need to wait for the account to be initialized in the next block require.NoError(t, net.WaitForNextBlock()) // Undelegate from app2 to gateway t.Logf("undelegating from app %s to gateway %s", appAddresses[1], gatewayAddr) - network.UndelegateAppFromGateway(t, net, appAddresses[1], gatewayAddr) + memnet.UndelegateAppFromGateway(t, appAddresses[1], gatewayAddr) // need to wait for the account to be initialized in the next block require.NoError(t, net.WaitForNextBlock()) @@ -127,47 +145,3 @@ func TestDelegationClient_RedelegationsObservables(t *testing.T) { ) } } - -// createNetworkWithApplicationsAndGateways creates a network with 2 applications -// and 1 gateway. It returns the network with all accoutns initialized via a -// transaction from the first validator. -func createNetworkWithApplicationsAndGateways( - t *testing.T, -) (net *network.Network, appAddresses []string, gatewayAddress string) { - // Prepare the network - cfg := network.DefaultConfig() - net = network.New(t, cfg) - ctx := net.Validators[0].ClientCtx - - // Prepare the keyring for the 2 applications and 1 gateway account - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 3) - ctx = ctx.WithKeyring(kr) - - // Initialize all the accounts - for i, account := range accounts { - signatureSequenceNumber := i + 1 - network.InitAccountWithSequence(t, net, account.Address, signatureSequenceNumber) - } - // need to wait for the account to be initialized in the next block - require.NoError(t, net.WaitForNextBlock()) - - addresses := make([]string, len(accounts)) - for i, account := range accounts { - addresses[i] = account.Address.String() - } - - // Create two applications - appGenesisState := network.ApplicationModuleGenesisStateWithAddresses(t, addresses[0:2]) - buf, err := cfg.Codec.MarshalJSON(appGenesisState) - require.NoError(t, err) - cfg.GenesisState[apptypes.ModuleName] = buf - - // Create a single gateway - gatewayGenesisState := network.GatewayModuleGenesisStateWithAddresses(t, addresses[2:3]) - buf, err = cfg.Codec.MarshalJSON(gatewayGenesisState) - require.NoError(t, err) - cfg.GenesisState[gatewaytypes.ModuleName] = buf - - return net, addresses[0:2], addresses[2] -} diff --git a/x/application/client/cli/query_application_test.go b/x/application/client/cli/query_application_test.go index 10a8b77c6..419137777 100644 --- a/x/application/client/cli/query_application_test.go +++ b/x/application/client/cli/query_application_test.go @@ -1,26 +1,39 @@ package cli_test import ( + "context" "fmt" "strconv" "testing" tmcli "github.com/cometbft/cometbft/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/testutil/nullify" "github.com/pokt-network/poktroll/x/application/client/cli" - "github.com/pokt-network/poktroll/x/application/types" + apptypes "github.com/pokt-network/poktroll/x/application/types" ) func TestShowApplication(t *testing.T) { - net, objs := networkWithApplicationObjects(t, 2) + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + AppSupplierPairingRatio: 1, + }, + ) + memnet.Start(ctx, t) + + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + applications := appGenesisState.ApplicationList - ctx := net.Validators[0].ClientCtx + net := memnet.GetNetwork(t) common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -30,14 +43,14 @@ func TestShowApplication(t *testing.T) { args []string err error - obj types.Application + obj apptypes.Application }{ { desc: "found", - idAddress: objs[0].Address, + idAddress: applications[0].Address, args: common, - obj: objs[0], + obj: applications[0], }, { desc: "not found", @@ -53,14 +66,14 @@ func TestShowApplication(t *testing.T) { tc.idAddress, } args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowApplication(), args) + out, err := testcli.ExecTestCLICmd(memnet.GetClientCtx(t), cli.CmdShowApplication(), args) if tc.err != nil { stat, ok := status.FromError(tc.err) require.True(t, ok) require.ErrorIs(t, stat.Err(), tc.err) } else { require.NoError(t, err) - var resp types.QueryGetApplicationResponse + var resp apptypes.QueryGetApplicationResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Application) require.Equal(t, @@ -73,9 +86,21 @@ func TestShowApplication(t *testing.T) { } func TestListApplication(t *testing.T) { - net, objs := networkWithApplicationObjects(t, 5) + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 5, + AppSupplierPairingRatio: 1, + }, + ) + memnet.Start(ctx, t) + + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + applications := appGenesisState.ApplicationList + + net := memnet.GetNetwork(t) + clientCtx := memnet.GetClientCtx(t) - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -93,15 +118,15 @@ func TestListApplication(t *testing.T) { } t.Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(applications); i += step { args := request(nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListApplication(), args) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdListApplication(), args) require.NoError(t, err) - var resp types.QueryAllApplicationResponse + var resp apptypes.QueryAllApplicationResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Application), step) require.Subset(t, - nullify.Fill(objs), + nullify.Fill(applications), nullify.Fill(resp.Application), ) } @@ -109,30 +134,30 @@ func TestListApplication(t *testing.T) { t.Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(applications); i += step { args := request(next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListApplication(), args) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdListApplication(), args) require.NoError(t, err) - var resp types.QueryAllApplicationResponse + var resp apptypes.QueryAllApplicationResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Application), step) require.Subset(t, - nullify.Fill(objs), + nullify.Fill(applications), nullify.Fill(resp.Application), ) next = resp.Pagination.NextKey } }) t.Run("Total", func(t *testing.T) { - args := request(nil, 0, uint64(len(objs)), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListApplication(), args) + args := request(nil, 0, uint64(len(applications)), true) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdListApplication(), args) require.NoError(t, err) - var resp types.QueryAllApplicationResponse + var resp apptypes.QueryAllApplicationResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) + require.Equal(t, len(applications), int(resp.Pagination.Total)) require.ElementsMatch(t, - nullify.Fill(objs), + nullify.Fill(applications), nullify.Fill(resp.Application), ) }) diff --git a/x/application/client/cli/tx_delegate_to_gateway_test.go b/x/application/client/cli/tx_delegate_to_gateway_test.go index 22d2630b2..bd9526acb 100644 --- a/x/application/client/cli/tx_delegate_to_gateway_test.go +++ b/x/application/client/cli/tx_delegate_to_gateway_test.go @@ -1,37 +1,40 @@ package cli_test import ( + "context" "fmt" "testing" sdkerrors "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/x/application/client/cli" - "github.com/pokt-network/poktroll/x/application/types" + apptypes "github.com/pokt-network/poktroll/x/application/types" ) func TestCLI_DelegateToGateway(t *testing.T) { - net, _ := networkWithApplicationObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + AppSupplierPairingRatio: 1, + }, + ) + memnet.Start(ctx, t) - // Create a keyring and add an account for the application to be delegated - // and the gateway to be delegated to - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 2) - appAccount := accounts[0] - gatewayAccount := accounts[1] + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + applications := appGenesisState.ApplicationList - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + net := memnet.GetNetwork(t) + appBech32 := applications[0].GetAddress() + gatewayBech32 := applications[1].GetAddress() // Common args used for all requests commonArgs := []string{ @@ -48,39 +51,35 @@ func TestCLI_DelegateToGateway(t *testing.T) { }{ { desc: "delegate to gateway: valid", - appAddress: appAccount.Address.String(), - gatewayAddress: gatewayAccount.Address.String(), + appAddress: appBech32, + gatewayAddress: gatewayBech32, }, { desc: "invalid - missing app address", - // appAddress: appAccount.Address.String(), - gatewayAddress: gatewayAccount.Address.String(), - err: types.ErrAppInvalidAddress, + // appAddress: intentionally omitted, + gatewayAddress: gatewayBech32, + err: apptypes.ErrAppInvalidAddress, }, { desc: "invalid - invalid app address", appAddress: "invalid address", - gatewayAddress: gatewayAccount.Address.String(), - err: types.ErrAppInvalidAddress, + gatewayAddress: gatewayBech32, + err: apptypes.ErrAppInvalidAddress, }, { desc: "invalid - missing gateway address", - appAddress: appAccount.Address.String(), - // gatewayAddress: gatewayAccount.Address.String(), - err: types.ErrAppInvalidGatewayAddress, + appAddress: appBech32, + // gatewayAddress: intentionally omitted, + err: apptypes.ErrAppInvalidGatewayAddress, }, { desc: "invalid - invalid gateway address", - appAddress: appAccount.Address.String(), + appAddress: appBech32, gatewayAddress: "invalid address", - err: types.ErrAppInvalidGatewayAddress, + err: apptypes.ErrAppInvalidGatewayAddress, }, } - // Initialize the App and Gateway Accounts by sending it some funds from the validator account that is part of genesis - network.InitAccountWithSequence(t, net, appAccount.Address, 1) - network.InitAccountWithSequence(t, net, gatewayAccount.Address, 2) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -95,7 +94,7 @@ func TestCLI_DelegateToGateway(t *testing.T) { args = append(args, commonArgs...) // Execute the command - delegateOutput, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDelegateToGateway(), args) + delegateOutput, err := testcli.ExecTestCLICmd(memnet.GetClientCtx(t), cli.CmdDelegateToGateway(), args) // Validate the error if one is expected if tt.err != nil { diff --git a/x/application/client/cli/tx_stake_application_test.go b/x/application/client/cli/tx_stake_application_test.go index d382dee01..73e338845 100644 --- a/x/application/client/cli/tx_stake_application_test.go +++ b/x/application/client/cli/tx_stake_application_test.go @@ -1,6 +1,7 @@ package cli_test import ( + "context" "fmt" "testing" @@ -8,29 +9,35 @@ import ( sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/testutil" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/testutil/yaml" "github.com/pokt-network/poktroll/x/application/client/cli" - "github.com/pokt-network/poktroll/x/application/types" + apptypes "github.com/pokt-network/poktroll/x/application/types" ) func TestCLI_StakeApplication(t *testing.T) { - net, _ := networkWithApplicationObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + AppSupplierPairingRatio: 1, + }, + ) + memnet.Start(ctx, t) - // Create a keyring and add an account for the application to be staked - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 1) - appAccount := accounts[0] + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + applications := appGenesisState.ApplicationList + appBech32 := applications[0].GetAddress() + //appAddr, err := sdk.AccAddressFromBech32(appBech32) + //require.NoError(t, err) - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + net := memnet.GetNetwork(t) // Common args used for all requests commonArgs := []string{ @@ -59,7 +66,7 @@ func TestCLI_StakeApplication(t *testing.T) { { desc: "valid", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "1000upokt", inputConfig: defaultConfig, @@ -73,7 +80,7 @@ func TestCLI_StakeApplication(t *testing.T) { inputStakeString: "1000upokt", inputConfig: defaultConfig, - expectedError: types.ErrAppInvalidAddress, + expectedError: apptypes.ErrAppInvalidAddress, }, { desc: "invalid: invalid address", @@ -82,73 +89,73 @@ func TestCLI_StakeApplication(t *testing.T) { inputStakeString: "1000upokt", inputConfig: defaultConfig, - expectedError: types.ErrAppInvalidAddress, + expectedError: apptypes.ErrAppInvalidAddress, }, // Error Paths - Stake Related { desc: "invalid: missing stake", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, // inputStakeString: "explicitly missing", inputConfig: defaultConfig, - expectedError: types.ErrAppInvalidStake, + expectedError: apptypes.ErrAppInvalidStake, }, { desc: "invalid: invalid stake denom", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "1000invalid", inputConfig: defaultConfig, - expectedError: types.ErrAppInvalidStake, + expectedError: apptypes.ErrAppInvalidStake, }, { desc: "invalid: stake amount (zero)", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "0upokt", inputConfig: defaultConfig, - expectedError: types.ErrAppInvalidStake, + expectedError: apptypes.ErrAppInvalidStake, }, { desc: "invalid: stake amount (negative)", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "-1000upokt", inputConfig: defaultConfig, - expectedError: types.ErrAppInvalidStake, + expectedError: apptypes.ErrAppInvalidStake, }, // Error Paths - Service Related { desc: "invalid: services (empty string)", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "1000upokt", inputConfig: "", - expectedError: types.ErrAppInvalidServiceConfigs, + expectedError: apptypes.ErrAppInvalidServiceConfigs, }, { desc: "invalid: single invalid service contains spaces", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "1000upokt", inputConfig: ` service_ids: - svc1 svc1_part2 svc1_part3 `, - expectedError: types.ErrAppInvalidServiceConfigs, + expectedError: apptypes.ErrAppInvalidServiceConfigs, }, { desc: "invalid: one of two services is invalid because it contains spaces", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "1000upokt", inputConfig: ` service_ids: @@ -156,12 +163,12 @@ func TestCLI_StakeApplication(t *testing.T) { - svc2 `, - expectedError: types.ErrAppInvalidServiceConfigs, + expectedError: apptypes.ErrAppInvalidServiceConfigs, }, { desc: "invalid: service ID is too long (8 chars is the max)", - inputAddress: appAccount.Address.String(), + inputAddress: appBech32, inputStakeString: "1000upokt", inputConfig: ` service_ids: @@ -169,13 +176,10 @@ func TestCLI_StakeApplication(t *testing.T) { - abcdefghi `, - expectedError: types.ErrAppInvalidServiceConfigs, + expectedError: apptypes.ErrAppInvalidServiceConfigs, }, } - // Initialize the App Account by sending it some funds from the validator account that is part of genesis - network.InitAccount(t, net, appAccount.Address) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -194,7 +198,7 @@ func TestCLI_StakeApplication(t *testing.T) { args = append(args, commonArgs...) // Execute the command - outStake, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdStakeApplication(), args) + outStake, err := testcli.ExecTestCLICmd(memnet.GetClientCtx(t), cli.CmdStakeApplication(), args) // Validate the error if one is expected if tt.expectedError != nil { diff --git a/x/application/client/cli/tx_undelegate_from_gateway_test.go b/x/application/client/cli/tx_undelegate_from_gateway_test.go index a5d942268..4ddf7066d 100644 --- a/x/application/client/cli/tx_undelegate_from_gateway_test.go +++ b/x/application/client/cli/tx_undelegate_from_gateway_test.go @@ -1,37 +1,40 @@ package cli_test import ( + "context" "fmt" "testing" sdkerrors "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/x/application/client/cli" - "github.com/pokt-network/poktroll/x/application/types" + apptypes "github.com/pokt-network/poktroll/x/application/types" ) func TestCLI_UndelegateFromGateway(t *testing.T) { - net, _ := networkWithApplicationObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + AppSupplierPairingRatio: 1, + }, + ) + memnet.Start(ctx, t) - // Create a keyring and add an account for the application to be delegated - // and the gateway to be delegated to - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 2) - appAccount := accounts[0] - gatewayAccount := accounts[1] + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + applications := appGenesisState.ApplicationList - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + net := memnet.GetNetwork(t) + appBech32 := applications[0].GetAddress() + gatewayBech32 := applications[1].GetAddress() // Common args used for all requests commonArgs := []string{ @@ -48,39 +51,35 @@ func TestCLI_UndelegateFromGateway(t *testing.T) { }{ { desc: "undelegate from gateway: valid", - appAddress: appAccount.Address.String(), - gatewayAddress: gatewayAccount.Address.String(), + appAddress: appBech32, + gatewayAddress: gatewayBech32, }, { desc: "invalid - missing app address", - // appAddress: appAccount.Address.String(), - gatewayAddress: gatewayAccount.Address.String(), - err: types.ErrAppInvalidAddress, + // appAddress: intentionally omitted + gatewayAddress: gatewayBech32, + err: apptypes.ErrAppInvalidAddress, }, { desc: "invalid - invalid app address", appAddress: "invalid address", - gatewayAddress: gatewayAccount.Address.String(), - err: types.ErrAppInvalidAddress, + gatewayAddress: gatewayBech32, + err: apptypes.ErrAppInvalidAddress, }, { desc: "invalid - missing gateway address", - appAddress: appAccount.Address.String(), - // gatewayAddress: gatewayAccount.Address.String(), - err: types.ErrAppInvalidGatewayAddress, + appAddress: appBech32, + // gatewayAddress: intentionally omitted + err: apptypes.ErrAppInvalidGatewayAddress, }, { desc: "invalid - invalid gateway address", - appAddress: appAccount.Address.String(), + appAddress: appBech32, gatewayAddress: "invalid address", - err: types.ErrAppInvalidGatewayAddress, + err: apptypes.ErrAppInvalidGatewayAddress, }, } - // Initialize the App and Gateway Accounts by sending it some funds from the validator account that is part of genesis - network.InitAccountWithSequence(t, net, appAccount.Address, 1) - network.InitAccountWithSequence(t, net, gatewayAccount.Address, 2) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -95,7 +94,7 @@ func TestCLI_UndelegateFromGateway(t *testing.T) { args = append(args, commonArgs...) // Execute the command - undelegateOutput, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUndelegateFromGateway(), args) + undelegateOutput, err := clitestutil.ExecTestCLICmd(memnet.GetClientCtx(t), cli.CmdUndelegateFromGateway(), args) // Validate the error if one is expected if tt.err != nil { diff --git a/x/application/client/cli/tx_unstake_application_test.go b/x/application/client/cli/tx_unstake_application_test.go index 2681a7d77..7c955a179 100644 --- a/x/application/client/cli/tx_unstake_application_test.go +++ b/x/application/client/cli/tx_unstake_application_test.go @@ -1,35 +1,37 @@ package cli_test import ( + "context" "fmt" "testing" sdkerrors "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/x/application/client/cli" - "github.com/pokt-network/poktroll/x/application/types" + apptypes "github.com/pokt-network/poktroll/x/application/types" ) func TestCLI_UnstakeApplication(t *testing.T) { - net, _ := networkWithApplicationObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx - - // Create a keyring and add an account for the application to be unstaked - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 1) - appAccount := accounts[0] + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + AppSupplierPairingRatio: 1, + }, + ) + memnet.Start(ctx, t) - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + application := appGenesisState.ApplicationList[0] + net := memnet.GetNetwork(t) // Common args used for all requests commonArgs := []string{ @@ -45,23 +47,20 @@ func TestCLI_UnstakeApplication(t *testing.T) { }{ { desc: "unstake application: valid", - address: appAccount.Address.String(), + address: application.GetAddress(), }, { desc: "unstake application: missing address", - // address: "explicitly missing", - err: types.ErrAppInvalidAddress, + // address: intentionally omitted, + err: apptypes.ErrAppInvalidAddress, }, { desc: "unstake application: invalid address", address: "invalid", - err: types.ErrAppInvalidAddress, + err: apptypes.ErrAppInvalidAddress, }, } - // Initialize the App Account by sending it some funds from the validator account that is part of genesis - network.InitAccount(t, net, appAccount.Address) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -75,7 +74,7 @@ func TestCLI_UnstakeApplication(t *testing.T) { args = append(args, commonArgs...) // Execute the command - outUnstake, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUnstakeApplication(), args) + outUnstake, err := testcli.ExecTestCLICmd(memnet.GetClientCtx(t), cli.CmdUnstakeApplication(), args) // Validate the error if one is expected if tt.err != nil { diff --git a/x/gateway/client/cli/query_gateway_test.go b/x/gateway/client/cli/query_gateway_test.go index fba32713c..95d9c412e 100644 --- a/x/gateway/client/cli/query_gateway_test.go +++ b/x/gateway/client/cli/query_gateway_test.go @@ -1,29 +1,41 @@ package cli_test import ( + "context" "fmt" "strconv" "testing" tmcli "github.com/cometbft/cometbft/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/gatewaynet" "github.com/pokt-network/poktroll/testutil/nullify" "github.com/pokt-network/poktroll/x/gateway/client/cli" - "github.com/pokt-network/poktroll/x/gateway/types" + gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types" ) // Prevent strconv unused error var _ = strconv.IntSize func TestShowGateway(t *testing.T) { - net, objs := networkWithGatewayObjects(t, 2) + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumGateways: 2, + }, + ) + memnet.Start(ctx, t) + + clientCtx := memnet.GetClientCtx(t) + codec := memnet.GetNetworkConfig(t).Codec + gateways := network.GetGenesisState[*gatewaytypes.GenesisState](t, gatewaytypes.ModuleName, memnet).GatewayList - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -33,14 +45,14 @@ func TestShowGateway(t *testing.T) { args []string err error - obj types.Gateway + obj gatewaytypes.Gateway }{ { desc: "found", - idAddress: objs[0].Address, + idAddress: gateways[0].Address, args: common, - obj: objs[0], + obj: gateways[0], }, { desc: "not found", @@ -56,15 +68,15 @@ func TestShowGateway(t *testing.T) { tc.idAddress, } args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowGateway(), args) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdShowGateway(), args) if tc.err != nil { stat, ok := status.FromError(tc.err) require.True(t, ok) require.ErrorIs(t, stat.Err(), tc.err) } else { require.NoError(t, err) - var resp types.QueryGetGatewayResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + var resp gatewaytypes.QueryGetGatewayResponse + require.NoError(t, codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Gateway) require.Equal(t, nullify.Fill(&tc.obj), @@ -76,9 +88,18 @@ func TestShowGateway(t *testing.T) { } func TestListGateway(t *testing.T) { - net, objs := networkWithGatewayObjects(t, 5) + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumGateways: 5, + }, + ) + memnet.Start(ctx, t) + + clientCtx := memnet.GetClientCtx(t) + codec := memnet.GetNetworkConfig(t).Codec + gateways := network.GetGenesisState[*gatewaytypes.GenesisState](t, gatewaytypes.ModuleName, memnet).GatewayList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -96,15 +117,15 @@ func TestListGateway(t *testing.T) { } t.Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(gateways); i += step { args := request(nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGateway(), args) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdListGateway(), args) require.NoError(t, err) - var resp types.QueryAllGatewayResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + var resp gatewaytypes.QueryAllGatewayResponse + require.NoError(t, codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Gateway), step) require.Subset(t, - nullify.Fill(objs), + nullify.Fill(gateways), nullify.Fill(resp.Gateway), ) } @@ -112,30 +133,30 @@ func TestListGateway(t *testing.T) { t.Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(gateways); i += step { args := request(next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGateway(), args) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdListGateway(), args) require.NoError(t, err) - var resp types.QueryAllGatewayResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + var resp gatewaytypes.QueryAllGatewayResponse + require.NoError(t, codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Gateway), step) require.Subset(t, - nullify.Fill(objs), + nullify.Fill(gateways), nullify.Fill(resp.Gateway), ) next = resp.Pagination.NextKey } }) t.Run("Total", func(t *testing.T) { - args := request(nil, 0, uint64(len(objs)), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGateway(), args) + args := request(nil, 0, uint64(len(gateways)), true) + out, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdListGateway(), args) require.NoError(t, err) - var resp types.QueryAllGatewayResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + var resp gatewaytypes.QueryAllGatewayResponse + require.NoError(t, codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) + require.Equal(t, len(gateways), int(resp.Pagination.Total)) require.ElementsMatch(t, - nullify.Fill(objs), + nullify.Fill(gateways), nullify.Fill(resp.Gateway), ) }) diff --git a/x/gateway/client/cli/tx_stake_gateway_test.go b/x/gateway/client/cli/tx_stake_gateway_test.go index ce1fb39d0..c3aa766d4 100644 --- a/x/gateway/client/cli/tx_stake_gateway_test.go +++ b/x/gateway/client/cli/tx_stake_gateway_test.go @@ -1,35 +1,40 @@ package cli_test import ( + "context" "fmt" "testing" sdkerrors "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/gatewaynet" "github.com/pokt-network/poktroll/x/gateway/client/cli" - "github.com/pokt-network/poktroll/x/gateway/types" + gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types" ) func TestCLI_StakeGateway(t *testing.T) { - net, _ := networkWithGatewayObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumGateways: 5, + }, + ) + memnet.Start(ctx, t) + + clientCtx := memnet.GetClientCtx(t) + net := memnet.GetNetwork(t) - // Create a keyring and add an account for the gateway to be staked - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 1) - gatewayAccount := accounts[0] + gatewayAccount := network.GetGenesisState[*gatewaytypes.GenesisState](t, gatewaytypes.ModuleName, memnet).GatewayList[0] - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + t.Logf("val addr: %s", net.Validators[0].Address.String()) + t.Logf("gateway addr: %s", gatewayAccount.GetAddress()) // Common args used for all requests commonArgs := []string{ @@ -48,54 +53,51 @@ func TestCLI_StakeGateway(t *testing.T) { desc: "stake gateway: invalid address", address: "invalid", stake: "1000upokt", - err: types.ErrGatewayInvalidAddress, + err: gatewaytypes.ErrGatewayInvalidAddress, }, { desc: "stake gateway: missing address", - // address: gatewayAccount.Address.String(), + // address: intentionally omitted, stake: "1000upokt", - err: types.ErrGatewayInvalidAddress, + err: gatewaytypes.ErrGatewayInvalidAddress, }, { desc: "stake gateway: invalid stake amount (zero)", - address: gatewayAccount.Address.String(), + address: gatewayAccount.GetAddress(), stake: "0upokt", - err: types.ErrGatewayInvalidStake, + err: gatewaytypes.ErrGatewayInvalidStake, }, { desc: "stake gateway: invalid stake amount (negative)", - address: gatewayAccount.Address.String(), + address: gatewayAccount.GetAddress(), stake: "-1000upokt", - err: types.ErrGatewayInvalidStake, + err: gatewaytypes.ErrGatewayInvalidStake, }, { desc: "stake gateway: invalid stake denom", - address: gatewayAccount.Address.String(), + address: gatewayAccount.GetAddress(), stake: "1000invalid", - err: types.ErrGatewayInvalidStake, + err: gatewaytypes.ErrGatewayInvalidStake, }, { desc: "stake gateway: invalid stake missing denom", - address: gatewayAccount.Address.String(), + address: gatewayAccount.GetAddress(), stake: "1000", - err: types.ErrGatewayInvalidStake, + err: gatewaytypes.ErrGatewayInvalidStake, }, { desc: "stake gateway: invalid stake missing stake", - address: gatewayAccount.Address.String(), - // stake: "1000upokt", - err: types.ErrGatewayInvalidStake, + address: gatewayAccount.GetAddress(), + // stake: intentionally omitted, + err: gatewaytypes.ErrGatewayInvalidStake, }, { desc: "stake gateway: valid", - address: gatewayAccount.Address.String(), + address: gatewayAccount.GetAddress(), stake: "1000upokt", }, } - // Initialize the Gateway Account by sending it some funds from the validator account that is part of genesis - network.InitAccount(t, net, gatewayAccount.Address) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -110,7 +112,7 @@ func TestCLI_StakeGateway(t *testing.T) { args = append(args, commonArgs...) // Execute the command - outStake, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdStakeGateway(), args) + outStake, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdStakeGateway(), args) if tt.err != nil { stat, ok := status.FromError(tt.err) require.True(t, ok) diff --git a/x/gateway/client/cli/tx_unstake_gateway_test.go b/x/gateway/client/cli/tx_unstake_gateway_test.go index b0aa9fc16..f5689799f 100644 --- a/x/gateway/client/cli/tx_unstake_gateway_test.go +++ b/x/gateway/client/cli/tx_unstake_gateway_test.go @@ -1,35 +1,36 @@ package cli_test import ( + "context" "fmt" "testing" sdkerrors "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/gatewaynet" "github.com/pokt-network/poktroll/x/gateway/client/cli" - "github.com/pokt-network/poktroll/x/gateway/types" + gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types" ) func TestCLI_UnstakeGateway(t *testing.T) { - net, _ := networkWithGatewayObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx - - // Create a keyring and add an account for the gateway to be unstaked - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 1) - gatewayAccount := accounts[0] + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumGateways: 5, + }, + ) + memnet.Start(ctx, t) - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + clientCtx := memnet.GetClientCtx(t) + net := memnet.GetNetwork(t) + gatewayAccount := network.GetGenesisState[*gatewaytypes.GenesisState](t, gatewaytypes.ModuleName, memnet).GatewayList[0] // Common args used for all requests commonArgs := []string{ @@ -45,23 +46,20 @@ func TestCLI_UnstakeGateway(t *testing.T) { }{ { desc: "unstake gateway: valid", - address: gatewayAccount.Address.String(), + address: gatewayAccount.GetAddress(), }, { desc: "unstake gateway: missing address", - // address: gatewayAccount.Address.String(), - err: types.ErrGatewayInvalidAddress, + // address: intentionally omitted, + err: gatewaytypes.ErrGatewayInvalidAddress, }, { desc: "unstake gateway: invalid address", address: "invalid", - err: types.ErrGatewayInvalidAddress, + err: gatewaytypes.ErrGatewayInvalidAddress, }, } - // Initialize the Gateway Account by sending it some funds from the validator account that is part of genesis - network.InitAccount(t, net, gatewayAccount.Address) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -75,7 +73,7 @@ func TestCLI_UnstakeGateway(t *testing.T) { args = append(args, commonArgs...) // Execute the command - outUnstake, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUnstakeGateway(), args) + outUnstake, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdUnstakeGateway(), args) // Validate the error if one is expected if tt.err != nil { diff --git a/x/session/client/cli/query_get_session_test.go b/x/session/client/cli/query_get_session_test.go index 480bafa48..f36d5539e 100644 --- a/x/session/client/cli/query_get_session_test.go +++ b/x/session/client/cli/query_get_session_test.go @@ -1,6 +1,7 @@ package cli_test import ( + "context" "fmt" "testing" @@ -10,29 +11,49 @@ import ( "github.com/gogo/status" "github.com/stretchr/testify/require" + "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" + apptypes "github.com/pokt-network/poktroll/x/application/types" "github.com/pokt-network/poktroll/x/session/client/cli" sessiontypes "github.com/pokt-network/poktroll/x/session/types" + suppliertypes "github.com/pokt-network/poktroll/x/supplier/types" ) func TestCLI_GetSession(t *testing.T) { + ctx := context.Background() // Prepare the network - net, suppliers, applications := networkWithApplicationsAndSupplier(t, 2) + appSupplierPairingRatio := 2 + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + AppSupplierPairingRatio: appSupplierPairingRatio, + }, + ) + memnet.Start(ctx, t) + + net := memnet.GetNetwork(t) + + // TODO_DISCUSS_IN_THIS_COMMIT: do we still need this? _, err := net.WaitForHeight(10) // Wait for a sufficiently high block height to ensure the staking transactions have been processed require.NoError(t, err) - val := net.Validators[0] - ctx := val.ClientCtx + + appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet) + applications := appGenesisState.ApplicationList + + supplierGenesisState := network.GetGenesisState[*suppliertypes.GenesisState](t, suppliertypes.ModuleName, memnet) + suppliers := supplierGenesisState.SupplierList // Sanity check the application configs are what we expect them to be appSvc0 := applications[0] - appSvc1 := applications[1] + appSvc1 := applications[2] require.Len(t, appSvc0.ServiceConfigs, 2) require.Len(t, appSvc1.ServiceConfigs, 2) - require.Equal(t, appSvc0.ServiceConfigs[0].Service.Id, "svc0") // svc0 has a supplier - require.Equal(t, appSvc0.ServiceConfigs[1].Service.Id, "svc00") // svc00 doesn't have a supplier - require.Equal(t, appSvc1.ServiceConfigs[0].Service.Id, "svc1") // svc1 has a supplier - require.Equal(t, appSvc1.ServiceConfigs[1].Service.Id, "svc11") // svc11 doesn't have a supplier + require.Equal(t, appSvc0.ServiceConfigs[0].Service.Id, "svc0") // svc0 has a supplier + require.Equal(t, appSvc0.ServiceConfigs[1].Service.Id, "nosvc0") // nosvc0 doesn't have a supplier + require.Equal(t, appSvc1.ServiceConfigs[0].Service.Id, "svc1") // svc1 has a supplier + require.Equal(t, appSvc1.ServiceConfigs[1].Service.Id, "nosvc1") // nosvc1 doesn't have a supplier // Sanity check the supplier configs are what we expect them to be supplierSvc0 := suppliers[0] // supplier for svc0 @@ -171,7 +192,7 @@ func TestCLI_GetSession(t *testing.T) { args = append(args, common...) // Execute the command - getSessionOut, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetSession(), args) + getSessionOut, err := clitestutil.ExecTestCLICmd(memnet.GetClientCtx(t), cli.CmdGetSession(), args) if tt.expectedErr != nil { stat, ok := status.FromError(tt.expectedErr) require.True(t, ok) diff --git a/x/supplier/client/cli/query_claim_test.go b/x/supplier/client/cli/query_claim_test.go index d7bd4aabd..98771ef34 100644 --- a/x/supplier/client/cli/query_claim_test.go +++ b/x/supplier/client/cli/query_claim_test.go @@ -1,6 +1,7 @@ package cli_test import ( + "context" "fmt" "testing" @@ -11,6 +12,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/testutil/nullify" "github.com/pokt-network/poktroll/testutil/sample" "github.com/pokt-network/poktroll/x/supplier/client/cli" @@ -18,17 +21,21 @@ import ( ) func TestClaim_Show(t *testing.T) { - sessionCount := 1 - supplierCount := 3 - appCount := 3 - - net, claims := networkWithClaimObjects( - t, sessionCount, - appCount, - supplierCount, + ctx := context.Background() + + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSessions: 1, + NumSuppliers: 3, + AppSupplierPairingRatio: 2, + }, ) + memnet.Start(ctx, t) + + claims, _ := memnet.CreateClaims(t) + net := memnet.GetNetwork(t) - ctx := net.Validators[0].ClientCtx + clientCtx := memnet.GetClientCtx(t) common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -105,7 +112,7 @@ func TestClaim_Show(t *testing.T) { tc.supplierAddr, } args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowClaim(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdShowClaim(), args) if tc.expectedErr != nil { require.ErrorContains(t, err, tc.expectedErr.Error()) } else { @@ -123,21 +130,24 @@ func TestClaim_Show(t *testing.T) { } func TestClaim_List(t *testing.T) { - sessionCount := 2 - supplierCount := 4 - appCount := 3 - serviceCount := 1 - // Each supplier will submit a claim for each app x service combination (per session). - numClaimsPerSession := supplierCount * appCount * serviceCount - totalClaims := sessionCount * numClaimsPerSession - - net, claims := networkWithClaimObjects( - t, sessionCount, - supplierCount, - appCount, - ) + ctx := context.Background() + cfg := &network.InMemoryNetworkConfig{ + NumSessions: 2, + NumRelaysPerSession: 5, + NumSuppliers: 4, + AppSupplierPairingRatio: 2, + } + + numClaimsPerSession := cfg.GetNumApplications(t) + totalClaims := cfg.NumSessions * numClaimsPerSession - ctx := net.Validators[0].ClientCtx + memnet := sessionnet.NewInMemoryNetworkWithSessions(t, cfg) + memnet.Start(ctx, t) + + claims, _ := memnet.CreateClaims(t) + net := memnet.GetNetwork(t) + + clientCtx := memnet.GetClientCtx(t) prepareArgs := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -158,7 +168,7 @@ func TestClaim_List(t *testing.T) { step := 2 for i := 0; i < totalClaims; i += step { args := prepareArgs(nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaims(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListClaims(), args) require.NoError(t, err) var resp types.QueryAllClaimsResponse @@ -177,7 +187,7 @@ func TestClaim_List(t *testing.T) { var next []byte for i := 0; i < totalClaims; i += step { args := prepareArgs(next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaims(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListClaims(), args) require.NoError(t, err) var resp types.QueryAllClaimsResponse @@ -192,7 +202,7 @@ func TestClaim_List(t *testing.T) { } }) - t.Run("ByAddress", func(t *testing.T) { + t.Run("BySupplierAddress", func(t *testing.T) { supplierAddr := claims[0].SupplierAddress args := prepareArgs(nil, 0, uint64(totalClaims), true) args = append(args, fmt.Sprintf("--%s=%s", cli.FlagSupplierAddress, supplierAddr)) @@ -204,7 +214,7 @@ func TestClaim_List(t *testing.T) { } } - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaims(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListClaims(), args) require.NoError(t, err) var resp types.QueryAllClaimsResponse @@ -214,7 +224,7 @@ func TestClaim_List(t *testing.T) { nullify.Fill(expectedClaims), nullify.Fill(resp.Claim), ) - require.Equal(t, sessionCount*appCount, int(resp.Pagination.Total)) + require.Equal(t, cfg.NumSessions*cfg.AppSupplierPairingRatio, int(resp.Pagination.Total)) }) t.Run("BySession", func(t *testing.T) { @@ -229,7 +239,7 @@ func TestClaim_List(t *testing.T) { } } - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaims(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListClaims(), args) require.NoError(t, err) var resp types.QueryAllClaimsResponse @@ -239,7 +249,8 @@ func TestClaim_List(t *testing.T) { nullify.Fill(expectedClaims), nullify.Fill(resp.Claim), ) - require.Equal(t, supplierCount, int(resp.Pagination.Total)) + // TODO_IN_THIS_COMMIT: comment and/or update expectation/actual... ea. app only has 1 supplier w/ matching serviceId. + require.Equal(t, 1, int(resp.Pagination.Total)) }) t.Run("ByHeight", func(t *testing.T) { @@ -254,31 +265,37 @@ func TestClaim_List(t *testing.T) { } } - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaims(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListClaims(), args) require.NoError(t, err) var resp types.QueryAllClaimsResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.Equal(t, numClaimsPerSession, int(resp.Pagination.Total)) require.ElementsMatch(t, nullify.Fill(expectedClaims), nullify.Fill(resp.Claim), ) + + // TODO_TECHDEBT(#!96): This expectation SHOULD NOT be derived from expectedClaims + // (as it currently is). Additionally, it SHOULD be a larger value except that each + // claim currently takes a block (we apparently MUST call `net.WaitForNextBlock()`) + // for each create claim message. This limits the number of fixture claims we can + // store on-chain that share the same session number/start height. + require.Equal(t, len(expectedClaims), int(resp.Pagination.Total)) }) t.Run("Total", func(t *testing.T) { args := prepareArgs(nil, 0, uint64(totalClaims), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaims(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListClaims(), args) require.NoError(t, err) var resp types.QueryAllClaimsResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.Equal(t, totalClaims, int(resp.Pagination.Total)) require.ElementsMatch(t, nullify.Fill(claims), nullify.Fill(resp.Claim), ) + require.Equal(t, totalClaims, int(resp.Pagination.Total)) }) } diff --git a/x/supplier/client/cli/query_supplier_test.go b/x/supplier/client/cli/query_supplier_test.go index b8a4781a9..d7b6ca95b 100644 --- a/x/supplier/client/cli/query_supplier_test.go +++ b/x/supplier/client/cli/query_supplier_test.go @@ -1,6 +1,7 @@ package cli_test import ( + "context" "fmt" "strconv" "testing" @@ -12,6 +13,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/sessionnet" "github.com/pokt-network/poktroll/testutil/nullify" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" "github.com/pokt-network/poktroll/x/supplier/client/cli" @@ -19,9 +22,22 @@ import ( ) func TestShowSupplier(t *testing.T) { - net, suppliers := networkWithSupplierObjects(t, 2) + ctx := context.Background() + memnet := sessionnet.NewInMemoryNetworkWithSessions( + t, &network.InMemoryNetworkConfig{ + NumSuppliers: 2, + }, + ) + memnet.Start(ctx, t) + + supplierGenesisState := network.GetGenesisState[*types.GenesisState](t, types.ModuleName, memnet) + require.NotEmpty(t, supplierGenesisState) + suppliers := supplierGenesisState.GetSupplierList() + require.NotEmpty(t, suppliers) + + clientCtx := memnet.GetClientCtx(t) + net := memnet.GetNetwork(t) - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -54,7 +70,7 @@ func TestShowSupplier(t *testing.T) { tc.idAddress, } args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowSupplier(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdShowSupplier(), args) if tc.err != nil { stat, ok := status.FromError(tc.err) require.True(t, ok) @@ -74,9 +90,16 @@ func TestShowSupplier(t *testing.T) { } func TestListSupplier(t *testing.T) { - net, suppliers := networkWithSupplierObjects(t, 5) + ctx := context.Background() + memnet := sessionnet.DefaultNetworkWithSessions(t) + memnet.Start(ctx, t) + + supplierGenesisState := network.GetGenesisState[*types.GenesisState](t, types.ModuleName, memnet) + suppliers := supplierGenesisState.GetSupplierList() + + net := memnet.GetNetwork(t) - ctx := net.Validators[0].ClientCtx + clientCtx := memnet.GetClientCtx(t) request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -96,7 +119,7 @@ func TestListSupplier(t *testing.T) { step := 2 for i := 0; i < len(suppliers); i += step { args := request(nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListSupplier(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListSupplier(), args) require.NoError(t, err) var resp types.QueryAllSupplierResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) @@ -112,7 +135,7 @@ func TestListSupplier(t *testing.T) { var next []byte for i := 0; i < len(suppliers); i += step { args := request(next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListSupplier(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListSupplier(), args) require.NoError(t, err) var resp types.QueryAllSupplierResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) @@ -126,7 +149,7 @@ func TestListSupplier(t *testing.T) { }) t.Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(suppliers)), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListSupplier(), args) + out, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdListSupplier(), args) require.NoError(t, err) var resp types.QueryAllSupplierResponse require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) diff --git a/x/supplier/client/cli/tx_stake_supplier_test.go b/x/supplier/client/cli/tx_stake_supplier_test.go index 1549c44ec..edce35126 100644 --- a/x/supplier/client/cli/tx_stake_supplier_test.go +++ b/x/supplier/client/cli/tx_stake_supplier_test.go @@ -1,6 +1,7 @@ package cli_test import ( + "context" "fmt" "testing" @@ -8,29 +9,43 @@ import ( sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/testutil" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/gatewaynet" "github.com/pokt-network/poktroll/testutil/yaml" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" "github.com/pokt-network/poktroll/x/supplier/client/cli" - "github.com/pokt-network/poktroll/x/supplier/types" + suppliertypes "github.com/pokt-network/poktroll/x/supplier/types" ) func TestCLI_StakeSupplier(t *testing.T) { - net, _ := networkWithSupplierObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumGateways: 5, + // NB: Only used to ensure keyring & on-chain accounts exist. + NumSuppliers: 1, + }, + ) + memnet.Start(ctx, t) + + clientCtx := memnet.GetClientCtx(t) + net := memnet.GetNetwork(t) - // Create a keyring and add an account for the supplier to be staked - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 1) - supplierAccount := accounts[0] + preGeneratedAcct, ok := memnet.PreGeneratedAccounts.Next() + require.Truef(t, ok, "no pre-generated accounts available") - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + supplier := sharedtypes.Supplier{ + Address: preGeneratedAcct.Address.String(), + } + + // Create a supplier on-chain account. + // TODO_CONSIDERATION: alternative approach would be to send tokens from the validator via CLI cmd. + memnet.InitSupplierAccountsWithSequence(t, supplier) // Common args used for all requests commonArgs := []string{ @@ -56,7 +71,7 @@ func TestCLI_StakeSupplier(t *testing.T) { // Happy Paths { desc: "stake supplier: valid", - address: supplierAccount.Address.String(), + address: supplier.Address, stakeString: "1000upokt", config: defaultConfig, }, @@ -64,8 +79,8 @@ func TestCLI_StakeSupplier(t *testing.T) { // Error Paths - Address Related { desc: "stake supplier: missing address", - // address: "explicitly missing", - err: types.ErrSupplierInvalidAddress, + // address: "explicitly omitted", + err: suppliertypes.ErrSupplierInvalidAddress, stakeString: "1000upokt", config: defaultConfig, }, @@ -73,16 +88,16 @@ func TestCLI_StakeSupplier(t *testing.T) { desc: "stake supplier: invalid address", address: "invalid", stakeString: "1000upokt", - err: types.ErrSupplierInvalidAddress, + err: suppliertypes.ErrSupplierInvalidAddress, config: defaultConfig, }, // Error Paths - Stake Related { desc: "stake supplier: missing stake", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidStake, - // stakeString: "explicitly missing", + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidStake, + // stakeString: "explicitly omitted", config: ` - service_id: svc1 endpoints: @@ -92,8 +107,8 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "stake supplier: invalid stake denom", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidStake, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidStake, stakeString: "1000invalid", config: ` - service_id: svc1 @@ -104,8 +119,8 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "stake supplier: invalid stake amount (zero)", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidStake, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidStake, stakeString: "0upokt", config: ` - service_id: svc1 @@ -116,8 +131,8 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "stake supplier: invalid stake amount (negative)", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidStake, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidStake, stakeString: "-1000upokt", config: ` - service_id: svc1 @@ -130,7 +145,7 @@ func TestCLI_StakeSupplier(t *testing.T) { // Happy Paths - Service Related { desc: "services_test: valid multiple services", - address: supplierAccount.Address.String(), + address: supplier.GetAddress(), stakeString: "1000upokt", config: ` - service_id: svc1 @@ -145,7 +160,7 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "services_test: valid localhost", - address: supplierAccount.Address.String(), + address: supplier.GetAddress(), stakeString: "1000upokt", config: ` - service_id: svc1 @@ -156,7 +171,7 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "services_test: valid loopback", - address: supplierAccount.Address.String(), + address: supplier.GetAddress(), stakeString: "1000upokt", config: ` - service_id: svc1 @@ -167,7 +182,7 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "services_test: valid without a pork", - address: supplierAccount.Address.String(), + address: supplier.GetAddress(), stakeString: "1000upokt", config: ` - service_id: svc1 @@ -180,22 +195,22 @@ func TestCLI_StakeSupplier(t *testing.T) { // Error Paths - Service Related { desc: "services_test: invalid services (missing argument)", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidServiceConfig, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidServiceConfig, // servicesString: "explicitly omitted", stakeString: "1000upokt", }, { desc: "services_test: invalid services (empty string)", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidServiceConfig, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidServiceConfig, stakeString: "1000upokt", config: ``, }, { desc: "services_test: invalid URL", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidServiceConfig, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidServiceConfig, stakeString: "1000upokt", config: ` - service_id: svc1 @@ -206,8 +221,8 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "services_test: missing URLs", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidServiceConfig, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidServiceConfig, stakeString: "1000upokt", config: ` - service_id: svc1 @@ -216,8 +231,8 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "services_test: missing service IDs", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidServiceConfig, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidServiceConfig, stakeString: "1000upokt", config: ` - endpoints: @@ -230,8 +245,8 @@ func TestCLI_StakeSupplier(t *testing.T) { }, { desc: "services_test: missing rpc type", - address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidServiceConfig, + address: supplier.GetAddress(), + err: suppliertypes.ErrSupplierInvalidServiceConfig, stakeString: "1000upokt", config: ` - service_id: svc1 @@ -241,9 +256,6 @@ func TestCLI_StakeSupplier(t *testing.T) { }, } - // Initialize the Supplier Account by sending it some funds from the validator account that is part of genesis - network.InitAccount(t, net, supplierAccount.Address) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -262,7 +274,7 @@ func TestCLI_StakeSupplier(t *testing.T) { args = append(args, commonArgs...) // Execute the command - outStake, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdStakeSupplier(), args) + outStake, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdStakeSupplier(), args) // Validate the error if one is expected if tt.err != nil { diff --git a/x/supplier/client/cli/tx_unstake_supplier_test.go b/x/supplier/client/cli/tx_unstake_supplier_test.go index 179e5dce5..6f4389fa7 100644 --- a/x/supplier/client/cli/tx_unstake_supplier_test.go +++ b/x/supplier/client/cli/tx_unstake_supplier_test.go @@ -1,35 +1,49 @@ package cli_test import ( + "context" "fmt" "testing" sdkerrors "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + testcli "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/testutil/network/gatewaynet" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" "github.com/pokt-network/poktroll/x/supplier/client/cli" - "github.com/pokt-network/poktroll/x/supplier/types" + suppliertypes "github.com/pokt-network/poktroll/x/supplier/types" ) func TestCLI_UnstakeSupplier(t *testing.T) { - net, _ := networkWithSupplierObjects(t, 2) - val := net.Validators[0] - ctx := val.ClientCtx + ctx := context.Background() + memnet := gatewaynet.NewInMemoryNetworkWithGateways( + t, &network.InMemoryNetworkConfig{ + NumGateways: 5, + // NB: Only used to ensure keyring & on-chain accounts exist. + NumSuppliers: 1, + }, + ) + memnet.Start(ctx, t) + + clientCtx := memnet.GetClientCtx(t) + net := memnet.GetNetwork(t) - // Create a keyring and add an account for the supplier to be unstaked - kr := ctx.Keyring - accounts := testutil.CreateKeyringAccounts(t, kr, 1) - supplierAccount := accounts[0] + preGeneratedAcct, ok := memnet.PreGeneratedAccounts.Next() + require.Truef(t, ok, "no pre-generated accounts available") - // Update the context with the new keyring - ctx = ctx.WithKeyring(kr) + supplier := sharedtypes.Supplier{ + Address: preGeneratedAcct.Address.String(), + } + + // Create a supplier on-chain account. + // TODO_CONSIDERATION: alternative approach would be to send tokens from the validator via CLI cmd. + memnet.InitSupplierAccountsWithSequence(t, supplier) // Common args used for all requests commonArgs := []string{ @@ -45,23 +59,20 @@ func TestCLI_UnstakeSupplier(t *testing.T) { }{ { desc: "unstake supplier: valid", - address: supplierAccount.Address.String(), + address: supplier.GetAddress(), }, { desc: "unstake supplier: missing address", // address: supplierAccount.Address.String(), - err: types.ErrSupplierInvalidAddress, + err: suppliertypes.ErrSupplierInvalidAddress, }, { desc: "unstake supplier: invalid address", address: "invalid", - err: types.ErrSupplierInvalidAddress, + err: suppliertypes.ErrSupplierInvalidAddress, }, } - // Initialize the Supplier Account by sending it some funds from the validator account that is part of genesis - network.InitAccount(t, net, supplierAccount.Address) - // Run the tests for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { @@ -75,7 +86,7 @@ func TestCLI_UnstakeSupplier(t *testing.T) { args = append(args, commonArgs...) // Execute the command - outUnstake, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUnstakeSupplier(), args) + outUnstake, err := testcli.ExecTestCLICmd(clientCtx, cli.CmdUnstakeSupplier(), args) // Validate the error if one is expected if tt.err != nil {