Skip to content

Commit

Permalink
[Gateway] Implement StakeGateway Message and Add Tests (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: DK <[email protected]>
  • Loading branch information
h5law and okdas authored Oct 18, 2023
1 parent b5cb88d commit 6b5f300
Show file tree
Hide file tree
Showing 24 changed files with 725 additions and 80 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,5 @@ jobs:
- name: Build
run: ignite chain build --debug --skip-proto

- name: Mockgen
run: make go_mockgen

- name: Test
run: make go_test
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,30 @@ todo_count: ## Print a count of all the TODOs in the project
todo_this_commit: ## List all the TODOs needed to be done in this commit
grep --exclude-dir={.git,vendor,prototype,.vscode} --exclude=Makefile -r -e "TODO_IN_THIS_COMMIT" -e "DISCUSS_IN_THIS_COMMIT"

####################
### Gateways ###
####################

.PHONY: gateway_list
gateway_list: ## List all the staked gateways
pocketd --home=$(POCKETD_HOME) q gateway list-gateway --node $(POCKET_NODE)

.PHONY: gateway_stake
gateway_stake: ## Stake tokens for the gateway specified (must specify the gateway env var)
pocketd --home=$(POCKETD_HOME) tx gateway stake-gateway 1000upokt --keyring-backend test --from $(gateway) --node $(POCKET_NODE)

.PHONY: gateway1_stake
gateway1_stake: ## Stake gateway1
gateway=gateway1 make gateway_stake

.PHONY: gateway2_stake
gateway2_stake: ## Stake gateway2
gateway=gateway2 make gateway_stake

.PHONY: gateway3_stake
gateway3_stake: ## Stake gateway3
gateway=gateway3 make gateway_stake

####################
### Applications ###
####################
Expand Down Expand Up @@ -234,4 +258,4 @@ acc_balance_total_supply: ## Query the total supply of the network

.PHONY: ignite_acc_list
ignite_acc_list: ## List all the accounts in LocalNet
ignite account list --keyring-dir=$(POCKETD_HOME) --keyring-backend test --address-prefix $(POCKET_ADDR_PREFIX)
ignite account list --keyring-dir=$(POCKETD_HOME) --keyring-backend test --address-prefix $(POCKET_ADDR_PREFIX)
2 changes: 2 additions & 0 deletions cmd/pocketd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

// this line is used by starport scaffolding # root/moduleImport

"pocket/app"
appparams "pocket/app/params"
)
Expand Down
6 changes: 3 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ accounts:
mnemonic: "client city senior tenant source soda spread buffalo shaft amused bar carbon keen off feel coral easily announce metal orphan sustain maple expand loop"
coins:
- 330000000upokt
- name: portal1
- name: gateway1
mnemonic: "salt iron goat also absorb depend involve agent apology between lift shy door left bulb arrange industry father jelly olive rifle return predict into"
coins:
- 100000000upokt
- name: portal2
- name: gateway2
mnemonic: "suffer wet jelly furnace cousin flip layer render finish frequent pledge feature economy wink like water disease final erase goat include apple state furnace"
coins:
- 200000000upokt
- name: portal3
- name: gateway3
mnemonic: "elder spatial erosion soap athlete tide subject recipe also awkward head pattern cart version beach usual oxygen confirm erupt diamond maze smooth census garment"
coins:
- 300000000upokt
Expand Down
82 changes: 82 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46690,6 +46690,23 @@ paths:
properties:
address:
type: string
title: The Bech32 address of the gateway
stake:
title: The total amount of uPOKT the gateway has staked
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the
custom method

signatures required by gogoproto.
pagination:
type: object
properties:
Expand Down Expand Up @@ -46810,6 +46827,23 @@ paths:
properties:
address:
type: string
title: The Bech32 address of the gateway
stake:
title: The total amount of uPOKT the gateway has staked
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the
custom method

signatures required by gogoproto.
default:
description: An unexpected error response.
schema:
Expand Down Expand Up @@ -76040,6 +76074,20 @@ definitions:
properties:
address:
type: string
title: The Bech32 address of the gateway
stake:
title: The total amount of uPOKT the gateway has staked
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
pocket.gateway.MsgStakeGatewayResponse:
type: object
pocket.gateway.MsgUnstakeGatewayResponse:
Expand All @@ -76057,6 +76105,23 @@ definitions:
properties:
address:
type: string
title: The Bech32 address of the gateway
stake:
title: The total amount of uPOKT the gateway has staked
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
pagination:
type: object
properties:
Expand Down Expand Up @@ -76091,6 +76156,23 @@ definitions:
properties:
address:
type: string
title: The Bech32 address of the gateway
stake:
title: The total amount of uPOKT the gateway has staked
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
pocket.gateway.QueryParamsResponse:
type: object
properties:
Expand Down
6 changes: 5 additions & 1 deletion proto/pocket/gateway/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package pocket.gateway;

option go_package = "pocket/x/gateway/types";

import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

message Gateway {
string address = 1;
string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the gateway
cosmos.base.v1beta1.Coin stake = 2; // The total amount of uPOKT the gateway has staked
}

9 changes: 8 additions & 1 deletion proto/pocket/gateway/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ package pocket.gateway;

option go_package = "pocket/x/gateway/types";

import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

// Msg defines the Msg service.
service Msg {
rpc StakeGateway (MsgStakeGateway ) returns (MsgStakeGatewayResponse );
rpc UnstakeGateway (MsgUnstakeGateway) returns (MsgUnstakeGatewayResponse);
}
message MsgStakeGateway {
string address = 1;
option (cosmos.msg.v1.signer) = "address"; // https://docs.cosmos.network/main/build/building-modules/messages-and-queries

string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the gateway
cosmos.base.v1beta1.Coin stake = 2; // The total amount of uPOKT the gateway is staking. Must be ≥ to the current amount that the gateway has staked (if any)
}

message MsgStakeGatewayResponse {}
Expand Down
3 changes: 3 additions & 0 deletions testutil/gateway/mocks/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package mocks

// This file is in place to declare the package for dynamically generated mocks
14 changes: 11 additions & 3 deletions testutil/keeper/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package keeper
import (
"testing"

"pocket/x/gateway/keeper"
"pocket/x/gateway/types"

mocks "pocket/testutil/gateway/mocks"

tmdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand All @@ -12,9 +17,8 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"pocket/x/gateway/keeper"
"pocket/x/gateway/types"
)

func GatewayKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
Expand All @@ -30,6 +34,10 @@ func GatewayKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

ctrl := gomock.NewController(t)
mockBankKeeper := mocks.NewMockBankKeeper(ctrl)
mockBankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, gomock.Any()).AnyTimes()

paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
Expand All @@ -41,7 +49,7 @@ func GatewayKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey,
memStoreKey,
paramsSubspace,
nil,
mockBankKeeper,
nil,
)

Expand Down
34 changes: 22 additions & 12 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network

import (
"fmt"
"strconv"
"testing"
"time"

Expand All @@ -17,15 +18,15 @@ import (
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"

"pocket/app"
"pocket/testutil/nullify"
"pocket/testutil/sample"
"pocket/x/application/types"
app_types "pocket/x/application/types"
gateway_types "pocket/x/gateway/types"
)

type (
Expand Down Expand Up @@ -98,14 +99,14 @@ func DefaultConfig() network.Config {
}
}

// applicationModuleGenesis generates a GenesisState object with a given number of applications.
// DefaultApplicationModuleGenesisState generates a GenesisState object with a given number of applications.
// It returns the populated GenesisState object.
func DefaultApplicationModuleGenesisState(t *testing.T, n int) *types.GenesisState {
func DefaultApplicationModuleGenesisState(t *testing.T, n int) *app_types.GenesisState {
t.Helper()
state := types.DefaultGenesis()
state := app_types.DefaultGenesis()
for i := 0; i < n; i++ {
stake := sdk.NewCoin("upokt", sdk.NewInt(int64(i+1)))
application := types.Application{
application := app_types.Application{
Address: sample.AccAddress(),
Stake: &stake,
}
Expand All @@ -115,16 +116,25 @@ func DefaultApplicationModuleGenesisState(t *testing.T, n int) *types.GenesisSta
return state
}

// HydrateGenesisState adds a given module's GenesisState to the network's genesis state.
func HydrateGenesisState(t *testing.T, cfg *network.Config, moduleGenesisState *types.GenesisState, moduleName string) {
// DefaultGatewayModuleGenesisState generates a GenesisState object with a given number of gateways.
// It returns the populated GenesisState object.
func DefaultGatewayModuleGenesisState(t *testing.T, n int) *gateway_types.GenesisState {
t.Helper()
buf, err := cfg.Codec.MarshalJSON(moduleGenesisState)
require.NoError(t, err)
cfg.GenesisState[moduleName] = buf
state := gateway_types.DefaultGenesis()
for i := 0; i < n; i++ {
stake := sdk.NewCoin("upokt", sdk.NewInt(int64(i)))
gateway := gateway_types.Gateway{
Address: strconv.Itoa(i),
Stake: &stake,
}
nullify.Fill(&gateway)
state.GatewayList = append(state.GatewayList, gateway)
}
return state
}

// Initialize an Account by sending it some funds from the validator in the network to the address provided
func InitAccount(t *testing.T, net *Network, addr cosmostypes.AccAddress) {
func InitAccount(t *testing.T, net *Network, addr sdk.AccAddress) {
t.Helper()
val := net.Validators[0]
ctx := val.ClientCtx
Expand Down
6 changes: 5 additions & 1 deletion x/application/client/cli/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"pocket/cmd/pocketd/cmd"
"pocket/testutil/network"
"pocket/x/application/types"

"github.com/stretchr/testify/require"
)

// Dummy variable to avoid unused import error.
Expand All @@ -24,6 +26,8 @@ func networkWithApplicationObjects(t *testing.T, n int) (*network.Network, []typ
t.Helper()
cfg := network.DefaultConfig()
appGenesisState := network.DefaultApplicationModuleGenesisState(t, n)
network.HydrateGenesisState(t, &cfg, appGenesisState, types.ModuleName)
buf, err := cfg.Codec.MarshalJSON(appGenesisState)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), appGenesisState.ApplicationList
}
21 changes: 21 additions & 0 deletions x/gateway/client/cli/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cli_test

import (
"testing"

"pocket/testutil/network"
"pocket/x/gateway/types"

"github.com/stretchr/testify/require"
)

// networkWithGatewayObjects creates a network with a populated gateway state of n gateway objects
func networkWithGatewayObjects(t *testing.T, n int) (*network.Network, []types.Gateway) {
t.Helper()
cfg := network.DefaultConfig()
gatewayGenesisState := network.DefaultGatewayModuleGenesisState(t, n)
buf, err := cfg.Codec.MarshalJSON(gatewayGenesisState)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), gatewayGenesisState.GatewayList
}
Loading

0 comments on commit 6b5f300

Please sign in to comment.