Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing, Tooling] refactor: in-memory network usage #285

74 changes: 24 additions & 50 deletions pkg/client/delegation/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,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 @@ -37,7 +37,25 @@ const (
func TestDelegationClient_RedelegationsObservables(t *testing.T) {
t.Skip("TODO(@h5law): Figure out how to subscribe to events on the simulated localnet")
// 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 @@ -94,22 +112,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 @@ -125,47 +143,3 @@ func TestDelegationClient_RedelegationsObservables(t *testing.T) {
)
}
}

// createNetworkWithApplicationsAndGateways creates a network with 2 applications
// and 1 gateway. It returns the network with all accounts 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]
}
4 changes: 1 addition & 3 deletions testutil/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func (cfg *InMemoryNetworkConfig) GetNumKeyringAccounts(t *testing.T) int {

// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig
//
// TODO_UPNEXT(@bryanchriswhite #285): Remove _ prefix after DebugConfig is removed from network.go.
func _DefaultConfig() network.Config {
func DefaultConfig() network.Config {
var (
encoding = app.MakeEncodingConfig()
chainID = "chain-" + rand.NewRand().Str(6)
Expand Down
Loading
Loading