Skip to content

Commit

Permalink
Merge pull request #5 from xpladev/my/refactor-x
Browse files Browse the repository at this point in the history
refactor: change to modular design for tx/query routing
  • Loading branch information
Moonyongjung committed Sep 7, 2023
2 parents 194f606 + 5e7e0f0 commit b9402bd
Show file tree
Hide file tree
Showing 38 changed files with 1,751 additions and 242 deletions.
77 changes: 3 additions & 74 deletions client/query.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
package client

import (
"github.com/xpladev/xpla.go/controller"
"github.com/xpladev/xpla.go/core"

mauth "github.com/xpladev/xpla.go/core/auth"
mauthz "github.com/xpladev/xpla.go/core/authz"
mbank "github.com/xpladev/xpla.go/core/bank"
mbase "github.com/xpladev/xpla.go/core/base"
mdist "github.com/xpladev/xpla.go/core/distribution"
mevidence "github.com/xpladev/xpla.go/core/evidence"
mevm "github.com/xpladev/xpla.go/core/evm"
mfeegrant "github.com/xpladev/xpla.go/core/feegrant"
mgov "github.com/xpladev/xpla.go/core/gov"
mibc "github.com/xpladev/xpla.go/core/ibc"
mmint "github.com/xpladev/xpla.go/core/mint"
mparams "github.com/xpladev/xpla.go/core/params"
mreward "github.com/xpladev/xpla.go/core/reward"
mslashing "github.com/xpladev/xpla.go/core/slashing"
mstaking "github.com/xpladev/xpla.go/core/staking"
mupgrade "github.com/xpladev/xpla.go/core/upgrade"
mwasm "github.com/xpladev/xpla.go/core/wasm"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"
Expand All @@ -43,65 +28,9 @@ func (xplac *XplaClient) Query() (string, error) {
return "", util.LogErr(errors.ErrNotSatisfiedOptions, "at least one of the gRPC URL or LCD URL must exist for query")
}
}
queryClient := core.NewIXplaClient(xplac, setQueryType(xplac))

qt := setQueryType(xplac)
queryClient := core.NewIXplaClient(xplac, qt)

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")
}
return controller.Controller().Get(xplac.GetModule()).NewQueryRouter(*queryClient)
}

func setQueryType(xplac *XplaClient) uint8 {
Expand Down
171 changes: 3 additions & 168 deletions client/tx_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@ import (
"math/big"
"os"

mauthz "github.com/xpladev/xpla.go/core/authz"
mbank "github.com/xpladev/xpla.go/core/bank"
mcrisis "github.com/xpladev/xpla.go/core/crisis"
mdist "github.com/xpladev/xpla.go/core/distribution"
mfeegrant "github.com/xpladev/xpla.go/core/feegrant"
mgov "github.com/xpladev/xpla.go/core/gov"
mparams "github.com/xpladev/xpla.go/core/params"
mreward "github.com/xpladev/xpla.go/core/reward"
mslashing "github.com/xpladev/xpla.go/core/slashing"
mstaking "github.com/xpladev/xpla.go/core/staking"
mupgrade "github.com/xpladev/xpla.go/core/upgrade"
mwasm "github.com/xpladev/xpla.go/core/wasm"
"github.com/xpladev/xpla.go/controller"
"github.com/xpladev/xpla.go/key"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"

"github.com/CosmWasm/wasmd/x/wasm"
cmclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand All @@ -32,18 +20,9 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/common"
evmtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
rewardtypes "github.com/xpladev/xpla/x/reward/types"
)

// Set message for transaction builder.
Expand All @@ -55,152 +34,8 @@ func setTxBuilderMsg(xplac *XplaClient) (cmclient.TxBuilder, error) {

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

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
82 changes: 82 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package controller

import (
"sync"

"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
var cc *coreController

// Controller is able to contol modules in the core package.
// Route Tx & Query logic by message type.
// If need to add new modules of XPLA, insert NewCoreModule in the core controller.
type coreController struct {
cores map[string]core.CoreModule
}

func init() {
Controller()
}

// Set core controller only once as singleton, and get core controller.
func Controller() *coreController {
once.Do(
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
}

func NewCoreController(coreModules ...core.CoreModule) *coreController {
m := make(map[string]core.CoreModule)
for _, coreModule := range coreModules {
m[coreModule.Name()] = coreModule
}

return &coreController{
cores: m,
}
}

func (c coreController) Get(moduleName string) core.CoreModule {
return c.cores[moduleName]
}

0 comments on commit b9402bd

Please sign in to comment.