Skip to content

Commit

Permalink
refactor: in-memory test network utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 19, 2023
1 parent af531b0 commit 278365b
Show file tree
Hide file tree
Showing 30 changed files with 1,155 additions and 773 deletions.
3 changes: 2 additions & 1 deletion pkg/client/delegation/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"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/sessionnet"
apptypes "github.com/pokt-network/poktroll/x/application/types"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
)
Expand Down Expand Up @@ -147,7 +148,7 @@ func createNetworkWithApplicationsAndGateways(
// Initialize all the accounts
for i, account := range accounts {
signatureSequenceNumber := i + 1
network.InitAccountWithSequence(t, net, account.Address, signatureSequenceNumber)
sessionnet.InitAccountWithSequence(t, net, account.Address, signatureSequenceNumber)
}
// need to wait for the account to be initialized in the next block
require.NoError(t, net.WaitForNextBlock())
Expand Down
19 changes: 19 additions & 0 deletions testutil/network/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package network

import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil/network"
)

type Config = network.Config

type InMemoryNetworkConfig struct {
NumSessions int
NumRelaysPerSession int
NumBlocksPerSession int
NumSuppliers int
NumApplications int
NumDelegates int
CosmosCfg *Config
Keyring keyring.Keyring
}
68 changes: 68 additions & 0 deletions testutil/network/delegations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package network

import (
"fmt"
"testing"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
testcli "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/x/application/client/cli"
)

// DelegateAppToGateway delegates the provided application to the provided gateway
func DelegateAppToGateway(
t *testing.T,
net *Network,
appAddr string,
gatewayAddr string,
) {
t.Helper()

val := net.Validators[0]
ctx := val.ClientCtx
args := []string{
gatewayAddr,
fmt.Sprintf("--%s=%s", flags.FlagFrom, appAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, types.NewCoins(types.NewCoin(net.Config.BondDenom, math.NewInt(10))).String()),
}
responseRaw, err := testcli.ExecTestCLICmd(ctx, cli.CmdDelegateToGateway(), args)
require.NoError(t, err)
var resp types.TxResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(responseRaw.Bytes(), &resp))
require.NotNil(t, resp)
require.NotNil(t, resp.TxHash)
require.Equal(t, uint32(0), resp.Code)
}

// UndelegateAppFromGateway undelegates the provided application from the provided gateway
func UndelegateAppFromGateway(
t *testing.T,
net *Network,
appAddr string,
gatewayAddr string,
) {
t.Helper()

val := net.Validators[0]
ctx := val.ClientCtx
args := []string{
gatewayAddr,
fmt.Sprintf("--%s=%s", flags.FlagFrom, appAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, types.NewCoins(types.NewCoin(net.Config.BondDenom, math.NewInt(10))).String()),
}
responseRaw, err := testcli.ExecTestCLICmd(ctx, cli.CmdUndelegateFromGateway(), args)
require.NoError(t, err)
var resp types.TxResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(responseRaw.Bytes(), &resp))
require.NotNil(t, resp)
require.NotNil(t, resp.TxHash)
require.Equal(t, uint32(0), resp.Code)
}
80 changes: 80 additions & 0 deletions testutil/network/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package network

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/pokt-network/poktroll/testutil/sample"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
"github.com/pokt-network/poktroll/x/supplier/types"
)

// GatewayModuleGenesisStateWithAddresses generates a GenesisState object with
// a gateway list full of gateways with the given addresses.
func GatewayModuleGenesisStateWithAddresses(t *testing.T, addresses []string) *gatewaytypes.GenesisState {
t.Helper()

state := gatewaytypes.DefaultGenesis()
for _, addr := range addresses {
gateway := gatewaytypes.Gateway{
Address: addr,
Stake: &sdk.Coin{Denom: "upokt", Amount: sdk.NewInt(10000)},
}
state.GatewayList = append(state.GatewayList, gateway)
}
return state
}

// DefaultGatewayModuleGenesisState generates a GenesisState object with a given number of gateways.
// It returns the populated GenesisState object.
func DefaultGatewayModuleGenesisState(t *testing.T, n int) *gatewaytypes.GenesisState {
t.Helper()

state := gatewaytypes.DefaultGenesis()
for i := 0; i < n; i++ {
stake := sdk.NewCoin("upokt", sdk.NewInt(int64(i)))
gateway := gatewaytypes.Gateway{
Address: sample.AccAddress(),
Stake: &stake,
}
// TODO_CONSIDERATION: Evaluate whether we need `nullify.Fill` or if we should enforce `(gogoproto.nullable) = false` everywhere
// nullify.Fill(&gateway)
state.GatewayList = append(state.GatewayList, gateway)
}
return state
}

// TODO_IN_THIS_COMMIT: still need this?
//
// DefaultSupplierModuleGenesisState generates a GenesisState object with a given number of suppliers.
// It returns the populated GenesisState object.
func DefaultSupplierModuleGenesisState(t *testing.T, n int) *types.GenesisState {
t.Helper()

state := types.DefaultGenesis()
for i := 0; i < n; i++ {
stake := sdk.NewCoin("upokt", sdk.NewInt(int64(i)))
supplier := sharedtypes.Supplier{
Address: sample.AccAddress(),
Stake: &stake,
Services: []*sharedtypes.SupplierServiceConfig{
{
Service: &sharedtypes.Service{Id: fmt.Sprintf("svc%d", i)},
Endpoints: []*sharedtypes.SupplierEndpoint{
{
Url: fmt.Sprintf("http://localhost:%d", i),
RpcType: sharedtypes.RPCType_JSON_RPC,
},
},
},
},
}
// TODO_CONSIDERATION: Evaluate whether we need `nullify.Fill` or if we should enforce `(gogoproto.nullable) = false` everywhere
// nullify.Fill(&supplier)
state.SupplierList = append(state.SupplierList, supplier)
}
return state
}
14 changes: 14 additions & 0 deletions testutil/network/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package network

import (
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil/network"
)

// TODO_IN_THIS_COMMIT: move implementation to own pkg.
type InMemoryCosmosNetwork interface {
GetClientCtx(*testing.T) client.Context
GetNetwork(*testing.T) *network.Network
}
Loading

0 comments on commit 278365b

Please sign in to comment.