Unify RPC method signatures and parameter decoding (#8397)

Pass all parameters from JSON-RPC requests to their corresponding handlers
using struct types instead of positional parameters. This allows us to control
encoding of arguments using only the standard library, and to eliminate the
remaining special-purpose JSON encoding hooks in the server.

To support existing use, the server still allows arguments to be encoded in
JSON as either an array or an object.

Related changes:

- Rework the RPCFunc constructor to reduce reflection during RPC call service.
- Add request parameter wrappers for each RPC service method.
- Update the RPC Environment methods to use these types.
- Update the interfaces and shims derived from Environment to the new
  signatures.
- Update and extend test cases.
This commit is contained in:
M. J. Fromberger
2022-04-27 07:53:51 -07:00
committed by GitHub
parent e741d01231
commit da1b871808
30 changed files with 983 additions and 779 deletions

View File

@@ -2,60 +2,148 @@ package proxy
import (
"context"
"time"
"github.com/tendermint/tendermint/internal/eventlog/cursor"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
lrpc "github.com/tendermint/tendermint/light/rpc"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/coretypes"
)
// proxyService wraps a light RPC client to export the RPC service interfaces.
// This is needed because the service and the client use different signatures
// for some of the methods.
// The interfaces are implemented by delegating to the underlying node via the
// specified client.
type proxyService struct {
*lrpc.Client
Client *lrpc.Client
}
func (p proxyService) ABCIQuery(ctx context.Context, path string, data tmbytes.HexBytes, height int64, prove bool) (*coretypes.ResultABCIQuery, error) {
return p.ABCIQueryWithOptions(ctx, path, data, rpcclient.ABCIQueryOptions{
Height: height,
Prove: prove,
func (p proxyService) ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, error) { panic("ok") }
func (p proxyService) ABCIQuery(ctx context.Context, req *coretypes.RequestABCIQuery) (*coretypes.ResultABCIQuery, error) {
return p.Client.ABCIQueryWithOptions(ctx, req.Path, req.Data, rpcclient.ABCIQueryOptions{
Height: int64(req.Height),
Prove: req.Prove,
})
}
func (p proxyService) Block(ctx context.Context, req *coretypes.RequestBlockInfo) (*coretypes.ResultBlock, error) {
return p.Client.Block(ctx, (*int64)(req.Height))
}
func (p proxyService) BlockByHash(ctx context.Context, req *coretypes.RequestBlockByHash) (*coretypes.ResultBlock, error) {
return p.Client.BlockByHash(ctx, req.Hash)
}
func (p proxyService) BlockResults(ctx context.Context, req *coretypes.RequestBlockInfo) (*coretypes.ResultBlockResults, error) {
return p.Client.BlockResults(ctx, (*int64)(req.Height))
}
func (p proxyService) BlockSearch(ctx context.Context, req *coretypes.RequestBlockSearch) (*coretypes.ResultBlockSearch, error) {
return p.Client.BlockSearch(ctx, req.Query, req.Page.IntPtr(), req.PerPage.IntPtr(), req.OrderBy)
}
func (p proxyService) BlockchainInfo(ctx context.Context, req *coretypes.RequestBlockchainInfo) (*coretypes.ResultBlockchainInfo, error) {
return p.Client.BlockchainInfo(ctx, int64(req.MinHeight), int64(req.MaxHeight))
}
func (p proxyService) BroadcastEvidence(ctx context.Context, req *coretypes.RequestBroadcastEvidence) (*coretypes.ResultBroadcastEvidence, error) {
return p.Client.BroadcastEvidence(ctx, req.Evidence)
}
func (p proxyService) BroadcastTxAsync(ctx context.Context, req *coretypes.RequestBroadcastTx) (*coretypes.ResultBroadcastTx, error) {
return p.Client.BroadcastTxAsync(ctx, req.Tx)
}
func (p proxyService) BroadcastTxCommit(ctx context.Context, req *coretypes.RequestBroadcastTx) (*coretypes.ResultBroadcastTxCommit, error) {
return p.Client.BroadcastTxCommit(ctx, req.Tx)
}
func (p proxyService) BroadcastTxSync(ctx context.Context, req *coretypes.RequestBroadcastTx) (*coretypes.ResultBroadcastTx, error) {
return p.Client.BroadcastTxSync(ctx, req.Tx)
}
func (p proxyService) CheckTx(ctx context.Context, req *coretypes.RequestCheckTx) (*coretypes.ResultCheckTx, error) {
return p.Client.CheckTx(ctx, req.Tx)
}
func (p proxyService) Commit(ctx context.Context, req *coretypes.RequestBlockInfo) (*coretypes.ResultCommit, error) {
return p.Client.Commit(ctx, (*int64)(req.Height))
}
func (p proxyService) ConsensusParams(ctx context.Context, req *coretypes.RequestConsensusParams) (*coretypes.ResultConsensusParams, error) {
return p.Client.ConsensusParams(ctx, (*int64)(req.Height))
}
func (p proxyService) DumpConsensusState(ctx context.Context) (*coretypes.ResultDumpConsensusState, error) {
return p.Client.DumpConsensusState(ctx)
}
func (p proxyService) Events(ctx context.Context, req *coretypes.RequestEvents) (*coretypes.ResultEvents, error) {
return p.Client.Events(ctx, req)
}
func (p proxyService) Genesis(ctx context.Context) (*coretypes.ResultGenesis, error) {
return p.Client.Genesis(ctx)
}
func (p proxyService) GenesisChunked(ctx context.Context, req *coretypes.RequestGenesisChunked) (*coretypes.ResultGenesisChunk, error) {
return p.Client.GenesisChunked(ctx, uint(req.Chunk))
}
func (p proxyService) GetConsensusState(ctx context.Context) (*coretypes.ResultConsensusState, error) {
return p.ConsensusState(ctx)
return p.Client.ConsensusState(ctx)
}
func (p proxyService) Events(ctx context.Context,
filter *coretypes.EventFilter,
maxItems int,
before, after cursor.Cursor,
waitTime time.Duration,
) (*coretypes.ResultEvents, error) {
return p.Client.Events(ctx, &coretypes.RequestEvents{
Filter: filter,
MaxItems: maxItems,
Before: before.String(),
After: after.String(),
WaitTime: waitTime,
})
func (p proxyService) Header(ctx context.Context, req *coretypes.RequestBlockInfo) (*coretypes.ResultHeader, error) {
return p.Client.Header(ctx, (*int64)(req.Height))
}
func (p proxyService) Subscribe(ctx context.Context, query string) (*coretypes.ResultSubscribe, error) {
return p.SubscribeWS(ctx, query)
func (p proxyService) HeaderByHash(ctx context.Context, req *coretypes.RequestBlockByHash) (*coretypes.ResultHeader, error) {
return p.Client.HeaderByHash(ctx, req.Hash)
}
func (p proxyService) Unsubscribe(ctx context.Context, query string) (*coretypes.ResultUnsubscribe, error) {
return p.UnsubscribeWS(ctx, query)
func (p proxyService) Health(ctx context.Context) (*coretypes.ResultHealth, error) {
return p.Client.Health(ctx)
}
func (p proxyService) NetInfo(ctx context.Context) (*coretypes.ResultNetInfo, error) {
return p.Client.NetInfo(ctx)
}
func (p proxyService) NumUnconfirmedTxs(ctx context.Context) (*coretypes.ResultUnconfirmedTxs, error) {
return p.Client.NumUnconfirmedTxs(ctx)
}
func (p proxyService) RemoveTx(ctx context.Context, req *coretypes.RequestRemoveTx) error {
return p.Client.RemoveTx(ctx, req.TxKey)
}
func (p proxyService) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
return p.Client.Status(ctx)
}
func (p proxyService) Subscribe(ctx context.Context, req *coretypes.RequestSubscribe) (*coretypes.ResultSubscribe, error) {
return p.Client.SubscribeWS(ctx, req.Query)
}
func (p proxyService) Tx(ctx context.Context, req *coretypes.RequestTx) (*coretypes.ResultTx, error) {
return p.Client.Tx(ctx, req.Hash, req.Prove)
}
func (p proxyService) TxSearch(ctx context.Context, req *coretypes.RequestTxSearch) (*coretypes.ResultTxSearch, error) {
return p.Client.TxSearch(ctx, req.Query, req.Prove, req.Page.IntPtr(), req.PerPage.IntPtr(), req.OrderBy)
}
func (p proxyService) UnconfirmedTxs(ctx context.Context, req *coretypes.RequestUnconfirmedTxs) (*coretypes.ResultUnconfirmedTxs, error) {
return p.Client.UnconfirmedTxs(ctx, req.Page.IntPtr(), req.PerPage.IntPtr())
}
func (p proxyService) Unsubscribe(ctx context.Context, req *coretypes.RequestUnsubscribe) (*coretypes.ResultUnsubscribe, error) {
return p.Client.UnsubscribeWS(ctx, req.Query)
}
func (p proxyService) UnsubscribeAll(ctx context.Context) (*coretypes.ResultUnsubscribe, error) {
return p.UnsubscribeAllWS(ctx)
return p.Client.UnsubscribeAllWS(ctx)
}
func (p proxyService) BroadcastEvidence(ctx context.Context, ev coretypes.Evidence) (*coretypes.ResultBroadcastEvidence, error) {
return p.Client.BroadcastEvidence(ctx, ev.Value)
func (p proxyService) Validators(ctx context.Context, req *coretypes.RequestValidators) (*coretypes.ResultValidators, error) {
return p.Client.Validators(ctx, (*int64)(req.Height), req.Page.IntPtr(), req.PerPage.IntPtr())
}