-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: in-memory test network utils
- Loading branch information
1 parent
af531b0
commit 278365b
Showing
30 changed files
with
1,155 additions
and
773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.