Skip to content

Commit

Permalink
init client context & tx service
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed Dec 10, 2024
1 parent 3110801 commit 9b8f115
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ import (
coreserver "cosmossdk.io/core/server"
"cosmossdk.io/core/transaction"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
)

const (
Expand Down Expand Up @@ -288,7 +291,7 @@ func (c *consensus[T]) maybeRunGRPCQuery(ctx context.Context, req *abci.QueryReq

// Handle node service
if strings.Contains(req.Path, "/cosmos.base.node.v1beta1.Service") {
nodeQService := nodeServer[transaction.Tx]{c.cfgMap, c.cfg.AppTomlConfig, c}
nodeQService := nodeServer[T]{c.cfgMap, c.cfg.AppTomlConfig, c}
paths := strings.Split(req.Path, "/")

var resp transaction.Msg
Expand All @@ -308,6 +311,64 @@ func (c *consensus[T]) maybeRunGRPCQuery(ctx context.Context, req *abci.QueryReq
return res, true, err
}

// Handle tx service
if strings.Contains(req.Path, "/cosmos.tx.v1beta1.Service") {
// init simple client context
amino := codec.NewLegacyAmino()
std.RegisterLegacyAminoCodec(amino)
txConfig := authtx.NewTxConfig(
c.appCodec,
c.appCodec.InterfaceRegistry().SigningContext().AddressCodec(),
c.appCodec.InterfaceRegistry().SigningContext().ValidatorAddressCodec(),
authtx.DefaultSignModes,
)
rpcClient, _ := client.NewClientFromNode(c.cfg.AppTomlConfig.Address)

clientCtx := client.Context{}.
WithLegacyAmino(amino).
WithCodec(c.appCodec).
WithTxConfig(txConfig).
WithNodeURI(c.cfg.AppTomlConfig.Address).
WithClient(rpcClient)

txService := txServer[T]{
clientCtx: clientCtx,
txCodec: c.txCodec,
app: c.app,
}
paths := strings.Split(req.Path, "/")

var resp transaction.Msg
var err error
switch paths[2] {
case "Simulate":
resp, err = handleCometService(ctx, req, txService.Simulate)
case "GetTx":
resp, err = handleCometService(ctx, req, txService.GetTx)
case "BroadcastTx":
resp, err = handleCometService(ctx, req, txService.BroadcastTx)
case "GetTxsEvent":
resp, err = handleCometService(ctx, req, txService.GetTxsEvent)
case "GetBlockWithTxs":
resp, err = handleCometService(ctx, req, txService.GetBlockWithTxs)
case "TxDecode":
resp, err = handleCometService(ctx, req, txService.TxDecode)
case "TxEncode":
resp, err = handleCometService(ctx, req, txService.TxEncode)
case "TxEncodeAmino":
resp, err = handleCometService(ctx, req, txService.TxEncodeAmino)
case "TxDecodeAmino":
resp, err = handleCometService(ctx, req, txService.Simulate)
}

if err != nil {
return nil, true, err
}

res, err := queryResponse(resp, req.Height)
return res, true, err
}

// special case for simulation as it is an external gRPC registered on the grpc server component
// and not on the app itself, so it won't pass the router afterwards.
if req.Path == "/cosmos.tx.v1beta1.Service/Simulate" {
Expand Down

0 comments on commit 9b8f115

Please sign in to comment.