Skip to content

Commit

Permalink
Implement the logic of Challenge query
Browse files Browse the repository at this point in the history
  • Loading branch information
ulbqb committed May 19, 2023
1 parent e424237 commit d764401
Show file tree
Hide file tree
Showing 16 changed files with 459 additions and 20 deletions.
13 changes: 13 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ import (
"github.com/Finschia/finschia-sdk/x/mint"
mintkeeper "github.com/Finschia/finschia-sdk/x/mint/keeper"
minttypes "github.com/Finschia/finschia-sdk/x/mint/types"
"github.com/Finschia/finschia-sdk/x/or/settlement"
settlementkeeper "github.com/Finschia/finschia-sdk/x/or/settlement/keeper"
settlementtypes "github.com/Finschia/finschia-sdk/x/or/settlement/types"
"github.com/Finschia/finschia-sdk/x/params"
paramsclient "github.com/Finschia/finschia-sdk/x/params/client"
paramskeeper "github.com/Finschia/finschia-sdk/x/params/keeper"
Expand Down Expand Up @@ -142,6 +145,7 @@ var (
vesting.AppModuleBasic{},
tokenmodule.AppModuleBasic{},
collectionmodule.AppModuleBasic{},
settlement.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -201,6 +205,7 @@ type SimApp struct {
ClassKeeper classkeeper.Keeper
TokenKeeper tokenkeeper.Keeper
CollectionKeeper collectionkeeper.Keeper
SettlementKeeper settlementkeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -254,6 +259,7 @@ func NewSimApp(
token.StoreKey,
collection.StoreKey,
authzkeeper.StoreKey,
settlementtypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
Expand Down Expand Up @@ -322,6 +328,8 @@ func NewSimApp(
app.TokenKeeper = tokenkeeper.NewKeeper(appCodec, keys[token.StoreKey], app.ClassKeeper)
app.CollectionKeeper = collectionkeeper.NewKeeper(appCodec, keys[collection.StoreKey], app.ClassKeeper)

app.SettlementKeeper = settlementkeeper.NewKeeper(appCodec, keys[settlementtypes.StoreKey], keys[settlementtypes.MemStoreKey])

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
Expand Down Expand Up @@ -387,6 +395,7 @@ func NewSimApp(
tokenmodule.NewAppModule(appCodec, app.TokenKeeper),
collectionmodule.NewAppModule(appCodec, app.CollectionKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
settlement.NewAppModule(appCodec, app.SettlementKeeper, app.AccountKeeper, app.BankKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -414,6 +423,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
settlementtypes.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand All @@ -435,6 +445,7 @@ func NewSimApp(
foundation.ModuleName,
token.ModuleName,
collection.ModuleName,
settlementtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -462,6 +473,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
settlementtypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down Expand Up @@ -712,6 +724,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(slashingtypes.ModuleName)
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable())
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(settlementtypes.ModuleName)

return paramsKeeper
}
20 changes: 15 additions & 5 deletions x/or/settlement/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cli
import (
"context"
"fmt"
"strconv"

// "strings"

"github.com/spf13/cobra"

"github.com/Finschia/finschia-sdk/client"
"github.com/Finschia/finschia-sdk/client/flags"
"github.com/Finschia/finschia-sdk/version"

// "github.com/Finschia/finschia-sdk/client/flags"
// sdk "github.com/Finschia/finschia-sdk/types"
Expand All @@ -35,15 +37,23 @@ func GetQueryCmd(queryRoute string) *cobra.Command {

func NewQueryCmdChallenge() *cobra.Command {
cmd := &cobra.Command{
Use: "challenge",
Short: "shows the challenge of the module",
Args: cobra.NoArgs,
Use: "challenge",
Short: "shows the challenge of the module",
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(`$ %s query %s challenge <challenge-id>`, version.AppName, types.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Challenge(context.Background(), &types.QueryChallengeRequest{})
ci, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}

req := &types.QueryChallengeRequest{
ChallengeId: ci,
}
res, err := queryClient.Challenge(context.Background(), req)
if err != nil {
return err
}
Expand Down
36 changes: 30 additions & 6 deletions x/or/settlement/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,49 @@ func GetTxCmd() *cobra.Command {
}

func NewTxCmdInitiateChallenge() *cobra.Command {
panic("implement me")
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
panic("implement me")
},
}
}

func NewTxCmdProposeState() *cobra.Command {
panic("implement me")
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
panic("implement me")
},
}
}

func NewTxCmdRespondState() *cobra.Command {
panic("implement me")
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
panic("implement me")
},
}
}

func NewTxCmdConfirmStateTransition() *cobra.Command {
panic("implement me")
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
panic("implement me")
},
}
}

func NewTxCmdDenyStateTransition() *cobra.Command {
panic("implement me")
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
panic("implement me")
},
}
}

func NewTxCmdAddTrieNode() *cobra.Command {
panic("implement me")
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
panic("implement me")
},
}
}
31 changes: 31 additions & 0 deletions x/or/settlement/keeper/challenge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keeper

import (
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/or/settlement/types"
)

func (k Keeper) SetChallenge(ctx sdk.Context, challengeID int64, challenge types.Challenge) {
store := ctx.KVStore(k.storeKey)
key := challengeKey(challengeID)

bz, err := challenge.Marshal()
if err != nil {
panic(err)
}
store.Set(key, bz)
}

func (k Keeper) GetChallenge(ctx sdk.Context, challengeID int64) (*types.Challenge, error) {
store := ctx.KVStore(k.storeKey)
key := challengeKey(challengeID)
bz := store.Get(key)
if bz == nil {
return nil, types.ErrChallengeNotExist.Wrapf("no challenge for %d", challengeID)
}

var challenge types.Challenge
k.cdc.MustUnmarshal(bz, &challenge)

return &challenge, nil
}
36 changes: 36 additions & 0 deletions x/or/settlement/keeper/challenge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package keeper_test

import (
"github.com/Finschia/finschia-sdk/x/or/settlement/types"
)

func (s *KeeperTestSuite) TestGetChallenge() {
testCases := map[string]struct {
challengeID int64
expected *types.Challenge
err error
}{
"valid request": {
challengeID: 1,
expected: &types.Challenge{},
},
"not found (not existing challenge id)": {
challengeID: 2,
err: types.ErrChallengeNotExist,
},
}

for name, tc := range testCases {
s.Run(name, func() {
if tc.expected != nil {
s.app.SettlementKeeper.SetChallenge(s.ctx, tc.challengeID, *tc.expected)
}
challenge, err := s.app.SettlementKeeper.GetChallenge(s.ctx, tc.challengeID)
s.Require().ErrorIs(err, tc.err)
if tc.err != nil {
return
}
s.Require().Equal(tc.expected, challenge)
})
}
}
19 changes: 17 additions & 2 deletions x/or/settlement/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ package keeper
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/or/settlement/types"
)

var _ types.QueryServer = Keeper{}

func (k Keeper) Challenge(ctx context.Context, req *types.QueryChallengeRequest) (*types.QueryChallengeResponse, error) {
panic("implement me")
func (k Keeper) Challenge(c context.Context, req *types.QueryChallengeRequest) (*types.QueryChallengeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)
challenge, err := k.GetChallenge(ctx, req.ChallengeId)

if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}

return &types.QueryChallengeResponse{Challenge: challenge}, err
}
46 changes: 46 additions & 0 deletions x/or/settlement/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper_test

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/or/settlement/types"
)

func (s *KeeperTestSuite) TestChallenge() {
ctx := sdk.WrapSDKContext(s.ctx)

testCases := map[string]struct {
req *types.QueryChallengeRequest
expectedRes *types.QueryChallengeResponse
err error
}{
"valid request": {
req: &types.QueryChallengeRequest{ChallengeId: 1},
expectedRes: &types.QueryChallengeResponse{Challenge: &types.Challenge{}},
},
"nil request": {
err: status.Error(codes.InvalidArgument, "empty request"),
},
"non-existent challenge id": {
req: &types.QueryChallengeRequest{ChallengeId: 2},
err: status.Error(codes.NotFound, types.ErrChallengeNotExist.Wrapf("no challenge for %d", 2).Error()),
},
}

for name, tc := range testCases {
s.Run(name, func() {
if tc.expectedRes != nil {
s.app.SettlementKeeper.SetChallenge(s.ctx, tc.req.ChallengeId, *tc.expectedRes.Challenge)
}
res, err := s.app.SettlementKeeper.Challenge(ctx, tc.req)
if tc.err != nil {
s.Require().EqualError(tc.err, err.Error())
return
}
s.Require().NoError(err)
s.Require().Equal(tc.expectedRes, res)
})
}
}
4 changes: 2 additions & 2 deletions x/or/settlement/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey sdk.StoreKey,
) *Keeper {
return &Keeper{
) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
Expand Down
41 changes: 41 additions & 0 deletions x/or/settlement/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/suite"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/Finschia/finschia-sdk/baseapp"
"github.com/Finschia/finschia-sdk/simapp"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/or/settlement/types"
)

type KeeperTestSuite struct {
suite.Suite

app *simapp.SimApp
ctx sdk.Context

queryClient types.QueryClient
}

func (s *KeeperTestSuite) SetupTest() {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
app.SettlementKeeper.SetParams(ctx, types.DefaultParams())

queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.SettlementKeeper)
queryClient := types.NewQueryClient(queryHelper)

s.app = app
s.ctx = ctx
s.queryClient = queryClient
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
Loading

0 comments on commit d764401

Please sign in to comment.