Skip to content

Commit

Permalink
refactor: make tx router and query router per module
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonyongjung committed Sep 6, 2023
1 parent baf6a8f commit 15b554e
Show file tree
Hide file tree
Showing 22 changed files with 701 additions and 206 deletions.
56 changes: 0 additions & 56 deletions client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,6 @@ func (xplac *XplaClient) Query() (string, error) {
queryClient := core.NewIXplaClient(xplac, setQueryType(xplac))

return controller.Controller().Get(xplac.GetModule()).NewQueryRouter(*queryClient)

// switch {
// case xplac.GetModule() == mauth.AuthModule:
// return mauth.QueryAuth(*queryClient)

// case xplac.GetModule() == mauthz.AuthzModule:
// return mauthz.QueryAuthz(*queryClient)

// case xplac.GetModule() == mbank.BankModule:
// return mbank.QueryBank(*queryClient)

// case xplac.GetModule() == mbase.Base:
// return mbase.QueryBase(*queryClient)

// case xplac.GetModule() == mdist.DistributionModule:
// return mdist.QueryDistribution(*queryClient)

// case xplac.GetModule() == mevidence.EvidenceModule:
// return mevidence.QueryEvidence(*queryClient)

// case xplac.GetModule() == mevm.EvmModule:
// return mevm.QueryEvm(*queryClient)

// case xplac.GetModule() == mfeegrant.FeegrantModule:
// return mfeegrant.QueryFeegrant(*queryClient)

// case xplac.GetModule() == mgov.GovModule:
// return mgov.QueryGov(*queryClient)

// case xplac.GetModule() == mibc.IbcModule:
// return mibc.QueryIbc(*queryClient)

// case xplac.GetModule() == mmint.MintModule:
// return mmint.QueryMint(*queryClient)

// case xplac.GetModule() == mparams.ParamsModule:
// return mparams.QueryParams(*queryClient)

// case xplac.GetModule() == mreward.RewardModule:
// return mreward.QueryReward(*queryClient)

// case xplac.GetModule() == mslashing.SlashingModule:
// return mslashing.QuerySlashing(*queryClient)

// case xplac.GetModule() == mstaking.StakingModule:
// return mstaking.QueryStaking(*queryClient)

// case xplac.GetModule() == mupgrade.UpgradeModule:
// return mupgrade.QueryUpgrade(*queryClient)

// case xplac.GetModule() == mwasm.WasmModule:
// return mwasm.QueryWasm(*queryClient)

// default:
// return "", util.LogErr(errors.ErrInvalidRequest, "invalid module")
// }
}

func setQueryType(xplac *XplaClient) uint8 {
Expand Down
150 changes: 2 additions & 148 deletions client/tx_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,154 +34,8 @@ func setTxBuilderMsg(xplac *XplaClient) (cmclient.TxBuilder, error) {

builder := xplac.GetEncoding().TxConfig.NewTxBuilder()

return controller.Controller().Get(xplac.GetModule()).NewTxRouter(builder, xplac.GetMsgType())

// switch {
// // Authz module
// case xplac.GetMsgType() == mauthz.AuthzGrantMsgType:
// convertMsg := xplac.GetMsg().(authz.MsgGrant)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mauthz.AuthzRevokeMsgType:
// convertMsg := xplac.GetMsg().(authz.MsgRevoke)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mauthz.AuthzExecMsgType:
// convertMsg := xplac.GetMsg().(authz.MsgExec)
// builder.SetMsgs(&convertMsg)

// // Bank module
// case xplac.GetMsgType() == mbank.BankSendMsgType:
// convertMsg := xplac.GetMsg().(banktypes.MsgSend)
// builder.SetMsgs(&convertMsg)

// // Crisis module
// case xplac.GetMsgType() == mcrisis.CrisisInvariantBrokenMsgType:
// convertMsg := xplac.GetMsg().(crisistypes.MsgVerifyInvariant)
// builder.SetMsgs(&convertMsg)

// // Distribution module
// case xplac.GetMsgType() == mdist.DistributionFundCommunityPoolMsgType:
// convertMsg := xplac.GetMsg().(disttypes.MsgFundCommunityPool)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mdist.DistributionProposalCommunityPoolSpendMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgSubmitProposal)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mdist.DistributionWithdrawRewardsMsgType:
// convertMsg := xplac.GetMsg().([]sdk.Msg)
// builder.SetMsgs(convertMsg...)

// case xplac.GetMsgType() == mdist.DistributionWithdrawAllRewardsMsgType:
// convertMsg := xplac.GetMsg().([]sdk.Msg)
// builder.SetMsgs(convertMsg...)

// case xplac.GetMsgType() == mdist.DistributionSetWithdrawAddrMsgType:
// convertMsg := xplac.GetMsg().(disttypes.MsgSetWithdrawAddress)
// builder.SetMsgs(&convertMsg)

// // Feegrant module
// case xplac.GetMsgType() == mfeegrant.FeegrantGrantMsgType:
// convertMsg := xplac.GetMsg().(feegrant.MsgGrantAllowance)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mfeegrant.FeegrantRevokeGrantMsgType:
// convertMsg := xplac.GetMsg().(feegrant.MsgRevokeAllowance)
// builder.SetMsgs(&convertMsg)

// // Gov module
// case xplac.GetMsgType() == mgov.GovSubmitProposalMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgSubmitProposal)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mgov.GovDepositMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgDeposit)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mgov.GovVoteMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgVote)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mgov.GovWeightedVoteMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgVoteWeighted)
// builder.SetMsgs(&convertMsg)

// // Params module
// case xplac.GetMsgType() == mparams.ParamsProposalParamChangeMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgSubmitProposal)
// builder.SetMsgs(&convertMsg)

// // Reward module
// case xplac.GetMsgType() == mreward.RewardFundFeeCollectorMsgType:
// convertMsg := xplac.GetMsg().(rewardtypes.MsgFundFeeCollector)
// builder.SetMsgs(&convertMsg)

// // slashing module
// case xplac.GetMsgType() == mslashing.SlahsingUnjailMsgType:
// convertMsg := xplac.GetMsg().(slashingtypes.MsgUnjail)
// builder.SetMsgs(&convertMsg)

// // Staking module
// case xplac.GetMsgType() == mstaking.StakingCreateValidatorMsgType:
// convertMsg := xplac.GetMsg().(sdk.Msg)
// builder.SetMsgs(convertMsg)

// case xplac.GetMsgType() == mstaking.StakingEditValidatorMsgType:
// convertMsg := xplac.GetMsg().(stakingtypes.MsgEditValidator)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mstaking.StakingDelegateMsgType:
// convertMsg := xplac.GetMsg().(stakingtypes.MsgDelegate)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mstaking.StakingUnbondMsgType:
// convertMsg := xplac.GetMsg().(stakingtypes.MsgUndelegate)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mstaking.StakingRedelegateMsgType:
// convertMsg := xplac.GetMsg().(stakingtypes.MsgBeginRedelegate)
// builder.SetMsgs(&convertMsg)

// // Upgrade module
// case xplac.GetMsgType() == mupgrade.UpgradeProposalSoftwareUpgradeMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgSubmitProposal)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mupgrade.UpgradeCancelSoftwareUpgradeMsgType:
// convertMsg := xplac.GetMsg().(govtypes.MsgSubmitProposal)
// builder.SetMsgs(&convertMsg)

// // Wasm module
// case xplac.GetMsgType() == mwasm.WasmStoreMsgType:
// convertMsg := xplac.GetMsg().(wasm.MsgStoreCode)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mwasm.WasmInstantiateMsgType:
// convertMsg := xplac.GetMsg().(wasm.MsgInstantiateContract)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mwasm.WasmExecuteMsgType:
// convertMsg := xplac.GetMsg().(wasm.MsgExecuteContract)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mwasm.WasmClearContractAdminMsgType:
// convertMsg := xplac.GetMsg().(wasm.MsgClearAdmin)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mwasm.WasmSetContractAdminMsgType:
// convertMsg := xplac.GetMsg().(wasm.MsgUpdateAdmin)
// builder.SetMsgs(&convertMsg)

// case xplac.GetMsgType() == mwasm.WasmMigrateMsgType:
// convertMsg := xplac.GetMsg().(wasm.MsgMigrateContract)
// builder.SetMsgs(&convertMsg)

// default:
// return nil, util.LogErr(errors.ErrInvalidMsgType, xplac.GetMsgType())
// }

// return builder, nil
return controller.Controller().Get(xplac.GetModule()).
NewTxRouter(builder, xplac.GetMsgType(), xplac.GetMsg())
}

// Set information for transaction builder.
Expand Down
34 changes: 34 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import (

"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/core/auth"
"github.com/xpladev/xpla.go/core/authz"
"github.com/xpladev/xpla.go/core/bank"
"github.com/xpladev/xpla.go/core/base"
"github.com/xpladev/xpla.go/core/crisis"
"github.com/xpladev/xpla.go/core/distribution"
"github.com/xpladev/xpla.go/core/evidence"
"github.com/xpladev/xpla.go/core/evm"
"github.com/xpladev/xpla.go/core/feegrant"
"github.com/xpladev/xpla.go/core/gov"
"github.com/xpladev/xpla.go/core/ibc"
"github.com/xpladev/xpla.go/core/mint"
"github.com/xpladev/xpla.go/core/params"
"github.com/xpladev/xpla.go/core/reward"
"github.com/xpladev/xpla.go/core/slashing"
"github.com/xpladev/xpla.go/core/staking"
"github.com/xpladev/xpla.go/core/upgrade"
"github.com/xpladev/xpla.go/core/wasm"
)

var once sync.Once
Expand All @@ -27,6 +44,23 @@ func Controller() *coreController {
func() {
cc = NewCoreController(
auth.NewCoreModule(),
authz.NewCoreModule(),
bank.NewCoreModule(),
base.NewCoreModule(),
crisis.NewCoreModule(),
distribution.NewCoreModule(),
evidence.NewCoreModule(),
evm.NewCoreModule(),
feegrant.NewCoreModule(),
gov.NewCoreModule(),
ibc.NewCoreModule(),
mint.NewCoreModule(),
params.NewCoreModule(),
reward.NewCoreModule(),
slashing.NewCoreModule(),
staking.NewCoreModule(),
upgrade.NewCoreModule(),
wasm.NewCoreModule(),
)
})
return cc
Expand Down
2 changes: 1 addition & 1 deletion core/auth/auth.go → core/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *coreModule) Name() string {
return AuthModule
}

func (c *coreModule) NewTxRouter(txBuiler cmclient.TxBuilder, msgType string) (cmclient.TxBuilder, error) {
func (c *coreModule) NewTxRouter(_ cmclient.TxBuilder, _ string, _ interface{}) (cmclient.TxBuilder, error) {
return nil, util.LogErr(errors.ErrInvalidRequest, c.Name(), "module has not tx")
}

Expand Down
45 changes: 45 additions & 0 deletions core/authz/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package authz

import (
"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"

cmclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/x/authz"
)

type coreModule struct{}

func NewCoreModule() core.CoreModule {
return &coreModule{}
}

func (c *coreModule) Name() string {
return AuthzModule
}

func (c *coreModule) NewTxRouter(builder cmclient.TxBuilder, msgType string, msg interface{}) (cmclient.TxBuilder, error) {
switch {
case msgType == AuthzGrantMsgType:
convertMsg := msg.(authz.MsgGrant)
builder.SetMsgs(&convertMsg)

case msgType == AuthzRevokeMsgType:
convertMsg := msg.(authz.MsgRevoke)
builder.SetMsgs(&convertMsg)

case msgType == AuthzExecMsgType:
convertMsg := msg.(authz.MsgExec)
builder.SetMsgs(&convertMsg)

default:
return nil, util.LogErr(errors.ErrInvalidMsgType, msgType)
}

return builder, nil
}

func (c *coreModule) NewQueryRouter(q core.QueryClient) (string, error) {
return QueryAuthz(q)
}
37 changes: 37 additions & 0 deletions core/bank/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package bank

import (
"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"

cmclient "github.com/cosmos/cosmos-sdk/client"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type coreModule struct{}

func NewCoreModule() core.CoreModule {
return &coreModule{}
}

func (c *coreModule) Name() string {
return BankModule
}

func (c *coreModule) NewTxRouter(builder cmclient.TxBuilder, msgType string, msg interface{}) (cmclient.TxBuilder, error) {
switch {
case msgType == BankSendMsgType:
convertMsg := msg.(banktypes.MsgSend)
builder.SetMsgs(&convertMsg)

default:
return nil, util.LogErr(errors.ErrInvalidMsgType, msgType)
}

return builder, nil
}

func (c *coreModule) NewQueryRouter(q core.QueryClient) (string, error) {
return QueryBank(q)
}
27 changes: 27 additions & 0 deletions core/base/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package base

import (
"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"

cmclient "github.com/cosmos/cosmos-sdk/client"
)

type coreModule struct{}

func NewCoreModule() core.CoreModule {
return &coreModule{}
}

func (c *coreModule) Name() string {
return Base
}

func (c *coreModule) NewTxRouter(_ cmclient.TxBuilder, _ string, _ interface{}) (cmclient.TxBuilder, error) {
return nil, util.LogErr(errors.ErrInvalidRequest, c.Name(), "module has not tx")
}

func (c *coreModule) NewQueryRouter(q core.QueryClient) (string, error) {
return QueryBase(q)
}
3 changes: 2 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package core

import cmclient "github.com/cosmos/cosmos-sdk/client"

// The standard form for a module in the core package.
type CoreModule interface {
Name() string
NewTxRouter(cmclient.TxBuilder, string) (cmclient.TxBuilder, error)
NewTxRouter(cmclient.TxBuilder, string, interface{}) (cmclient.TxBuilder, error)
NewQueryRouter(QueryClient) (string, error)
}
Loading

0 comments on commit 15b554e

Please sign in to comment.