Skip to content

Commit

Permalink
refactor: in-memory network usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jan 2, 2024
1 parent 191e427 commit aebfd77
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 385 deletions.
76 changes: 25 additions & 51 deletions pkg/client/delegation/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()

Expand Down Expand Up @@ -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())
Expand All @@ -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())

Expand All @@ -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]
}
73 changes: 49 additions & 24 deletions x/application/client/cli/query_application_test.go
Original file line number Diff line number Diff line change
@@ -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),
}
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -93,46 +118,46 @@ 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),
)
}
})
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),
)
})
Expand Down
Loading

0 comments on commit aebfd77

Please sign in to comment.