2020-10-30 05:32:02 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-12 06:22:54 -08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-10-30 05:32:02 -07:00
|
|
|
|
|
|
|
gogogrpc "github.com/gogo/protobuf/grpc"
|
2021-04-08 12:22:00 -07:00
|
|
|
"github.com/golang/protobuf/proto" // nolint: staticcheck
|
2020-10-30 05:32:02 -07:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-11-12 06:22:54 -08:00
|
|
|
pagination "github.com/cosmos/cosmos-sdk/types/query"
|
2020-10-30 05:32:02 -07:00
|
|
|
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// baseAppSimulateFn is the signature of the Baseapp#Simulate function.
|
|
|
|
type baseAppSimulateFn func(txBytes []byte) (sdk.GasInfo, *sdk.Result, error)
|
|
|
|
|
|
|
|
// txServer is the server for the protobuf Tx service.
|
|
|
|
type txServer struct {
|
|
|
|
clientCtx client.Context
|
|
|
|
simulate baseAppSimulateFn
|
|
|
|
interfaceRegistry codectypes.InterfaceRegistry
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTxServer creates a new Tx service server.
|
|
|
|
func NewTxServer(clientCtx client.Context, simulate baseAppSimulateFn, interfaceRegistry codectypes.InterfaceRegistry) txtypes.ServiceServer {
|
|
|
|
return txServer{
|
|
|
|
clientCtx: clientCtx,
|
|
|
|
simulate: simulate,
|
|
|
|
interfaceRegistry: interfaceRegistry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ txtypes.ServiceServer = txServer{}
|
|
|
|
|
2020-11-12 06:22:54 -08:00
|
|
|
const (
|
|
|
|
eventFormat = "{eventType}.{eventAttribute}={value}"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TxsByEvents implements the ServiceServer.TxsByEvents RPC method.
|
|
|
|
func (s txServer) GetTxsEvent(ctx context.Context, req *txtypes.GetTxsEventRequest) (*txtypes.GetTxsEventResponse, error) {
|
2020-12-02 09:50:40 -08:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "request cannot be nil")
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:18:35 -08:00
|
|
|
page, limit, err := pagination.ParsePagination(req.Pagination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-12 06:22:54 -08:00
|
|
|
}
|
2021-03-12 10:47:10 -08:00
|
|
|
orderBy := parseOrderBy(req.OrderBy)
|
2020-11-12 06:22:54 -08:00
|
|
|
|
2020-11-30 20:18:35 -08:00
|
|
|
if len(req.Events) == 0 {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "must declare at least one event to search")
|
2020-11-12 06:22:54 -08:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:57:28 -08:00
|
|
|
for _, event := range req.Events {
|
|
|
|
if !strings.Contains(event, "=") || strings.Count(event, "=") > 1 {
|
2020-11-12 06:22:54 -08:00
|
|
|
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid event; event %s should be of the format: %s", event, eventFormat))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:47:10 -08:00
|
|
|
result, err := QueryTxsByEvents(s.clientCtx, req.Events, page, limit, orderBy)
|
2020-11-12 06:22:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a proto codec, we need it to unmarshal the tx bytes.
|
|
|
|
txsList := make([]*txtypes.Tx, len(result.Txs))
|
|
|
|
|
|
|
|
for i, tx := range result.Txs {
|
2021-03-01 07:57:28 -08:00
|
|
|
protoTx, ok := tx.Tx.GetCachedValue().(*txtypes.Tx)
|
|
|
|
if !ok {
|
|
|
|
return nil, status.Errorf(codes.Internal, "expected %T, got %T", txtypes.Tx{}, tx.Tx.GetCachedValue())
|
2020-11-12 06:22:54 -08:00
|
|
|
}
|
2021-03-01 07:57:28 -08:00
|
|
|
|
|
|
|
txsList[i] = protoTx
|
2020-11-12 06:22:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &txtypes.GetTxsEventResponse{
|
|
|
|
Txs: txsList,
|
2021-03-01 07:57:28 -08:00
|
|
|
TxResponses: result.Txs,
|
2020-11-12 06:22:54 -08:00
|
|
|
Pagination: &pagination.PageResponse{
|
2021-03-01 07:57:28 -08:00
|
|
|
Total: result.TotalCount,
|
2020-11-12 06:22:54 -08:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-30 05:32:02 -07:00
|
|
|
// Simulate implements the ServiceServer.Simulate RPC method.
|
|
|
|
func (s txServer) Simulate(ctx context.Context, req *txtypes.SimulateRequest) (*txtypes.SimulateResponse, error) {
|
2021-03-25 03:52:54 -07:00
|
|
|
if req == nil {
|
2020-10-30 05:32:02 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid empty tx")
|
|
|
|
}
|
|
|
|
|
2021-03-25 03:52:54 -07:00
|
|
|
txBytes := req.TxBytes
|
|
|
|
if txBytes == nil && req.Tx != nil {
|
|
|
|
// This block is for backwards-compatibility.
|
|
|
|
// We used to support passing a `Tx` in req. But if we do that, sig
|
|
|
|
// verification might not pass, because the .Marshal() below might not
|
|
|
|
// be the same marshaling done by the client.
|
|
|
|
var err error
|
|
|
|
txBytes, err = proto.Marshal(req.Tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid tx; %v", err)
|
|
|
|
}
|
2020-10-30 05:32:02 -07:00
|
|
|
}
|
2021-03-25 03:52:54 -07:00
|
|
|
|
|
|
|
if txBytes == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty txBytes is not allowed")
|
2020-10-30 05:32:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gasInfo, result, err := s.simulate(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &txtypes.SimulateResponse{
|
|
|
|
GasInfo: &gasInfo,
|
|
|
|
Result: result,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTx implements the ServiceServer.GetTx RPC method.
|
|
|
|
func (s txServer) GetTx(ctx context.Context, req *txtypes.GetTxRequest) (*txtypes.GetTxResponse, error) {
|
2020-12-02 09:50:40 -08:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "request cannot be nil")
|
|
|
|
}
|
|
|
|
|
2020-10-30 05:32:02 -07:00
|
|
|
// TODO We should also check the proof flag in gRPC header.
|
|
|
|
// https://github.com/cosmos/cosmos-sdk/issues/7036.
|
2021-03-01 07:57:28 -08:00
|
|
|
result, err := QueryTx(s.clientCtx, req.Hash)
|
2020-10-30 05:32:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:57:28 -08:00
|
|
|
protoTx, ok := result.Tx.GetCachedValue().(*txtypes.Tx)
|
|
|
|
if !ok {
|
|
|
|
return nil, status.Errorf(codes.Internal, "expected %T, got %T", txtypes.Tx{}, result.Tx.GetCachedValue())
|
2020-10-30 05:32:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return &txtypes.GetTxResponse{
|
2021-03-01 07:57:28 -08:00
|
|
|
Tx: protoTx,
|
|
|
|
TxResponse: result,
|
2020-10-30 05:32:02 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:50:40 -08:00
|
|
|
func (s txServer) BroadcastTx(ctx context.Context, req *txtypes.BroadcastTxRequest) (*txtypes.BroadcastTxResponse, error) {
|
|
|
|
return client.TxServiceBroadcast(ctx, s.clientCtx, req)
|
|
|
|
}
|
|
|
|
|
2020-10-30 05:32:02 -07:00
|
|
|
// RegisterTxService registers the tx service on the gRPC router.
|
|
|
|
func RegisterTxService(
|
|
|
|
qrt gogogrpc.Server,
|
|
|
|
clientCtx client.Context,
|
|
|
|
simulateFn baseAppSimulateFn,
|
|
|
|
interfaceRegistry codectypes.InterfaceRegistry,
|
|
|
|
) {
|
|
|
|
txtypes.RegisterServiceServer(
|
|
|
|
qrt,
|
|
|
|
NewTxServer(clientCtx, simulateFn, interfaceRegistry),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterGRPCGatewayRoutes mounts the tx service's GRPC-gateway routes on the
|
|
|
|
// given Mux.
|
|
|
|
func RegisterGRPCGatewayRoutes(clientConn gogogrpc.ClientConn, mux *runtime.ServeMux) {
|
|
|
|
txtypes.RegisterServiceHandlerClient(context.Background(), mux, txtypes.NewServiceClient(clientConn))
|
|
|
|
}
|
2021-03-12 10:47:10 -08:00
|
|
|
|
|
|
|
func parseOrderBy(orderBy txtypes.OrderBy) string {
|
|
|
|
switch orderBy {
|
|
|
|
case txtypes.OrderBy_ORDER_BY_ASC:
|
|
|
|
return "asc"
|
|
|
|
case txtypes.OrderBy_ORDER_BY_DESC:
|
|
|
|
return "desc"
|
|
|
|
default:
|
2021-03-15 10:56:33 -07:00
|
|
|
return "" // Defaults to Tendermint's default, which is `asc` now.
|
2021-03-12 10:47:10 -08:00
|
|
|
}
|
|
|
|
}
|