|  | 
|  | 1 | +package cosmosclient | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | + | 
|  | 6 | +	"github.com/pkg/errors" | 
|  | 7 | +	"github.com/tendermint/tendermint/libs/bytes" | 
|  | 8 | +	"github.com/tendermint/tendermint/rpc/client" | 
|  | 9 | +	rpcclient "github.com/tendermint/tendermint/rpc/client" | 
|  | 10 | +	ctypes "github.com/tendermint/tendermint/rpc/core/types" | 
|  | 11 | +	"github.com/tendermint/tendermint/types" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +// rpcWrapper is a rpclient.Client but with more contextualized errors. | 
|  | 15 | +// Useful because the original implementation may return JSON errors when the | 
|  | 16 | +// requested node is busy, which is confusing for the user. With rpcWrapper, | 
|  | 17 | +// the error is prefixed with 'error while requesting node xxx: JSON error'. | 
|  | 18 | +// TODO(tb): we may remove this wrapper once https://github.com/tendermint/tendermint/issues/9312 is fixed. | 
|  | 19 | +type rpcWrapper struct { | 
|  | 20 | +	rpcclient.Client | 
|  | 21 | +	nodeAddress string | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +func rpcError(node string, err error) error { | 
|  | 25 | +	return errors.Wrapf(err, "error while requesting node '%s'", node) | 
|  | 26 | +} | 
|  | 27 | + | 
|  | 28 | +func (rpc rpcWrapper) ABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error) { | 
|  | 29 | +	res, err := rpc.Client.ABCIInfo(ctx) | 
|  | 30 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +func (rpc rpcWrapper) ABCIQuery(ctx context.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) { | 
|  | 34 | +	res, err := rpc.Client.ABCIQuery(ctx, path, data) | 
|  | 35 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +func (rpc rpcWrapper) ABCIQueryWithOptions(ctx context.Context, path string, data bytes.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { | 
|  | 39 | +	res, err := rpc.Client.ABCIQueryWithOptions(ctx, path, data, opts) | 
|  | 40 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 41 | +} | 
|  | 42 | + | 
|  | 43 | +func (rpc rpcWrapper) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { | 
|  | 44 | +	res, err := rpc.Client.BroadcastTxCommit(ctx, tx) | 
|  | 45 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +func (rpc rpcWrapper) BroadcastTxAsync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) { | 
|  | 49 | +	res, err := rpc.Client.BroadcastTxAsync(ctx, tx) | 
|  | 50 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | +func (rpc rpcWrapper) BroadcastTxSync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) { | 
|  | 54 | +	res, err := rpc.Client.BroadcastTxSync(ctx, tx) | 
|  | 55 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +func (rpc rpcWrapper) GenesisChunked(ctx context.Context, n uint) (*ctypes.ResultGenesisChunk, error) { | 
|  | 59 | +	res, err := rpc.Client.GenesisChunked(ctx, n) | 
|  | 60 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 61 | +} | 
|  | 62 | + | 
|  | 63 | +func (rpc rpcWrapper) BlockchainInfo(ctx context.Context, minHeight int64, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { | 
|  | 64 | +	res, err := rpc.Client.BlockchainInfo(ctx, minHeight, maxHeight) | 
|  | 65 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 66 | +} | 
|  | 67 | + | 
|  | 68 | +func (rpc rpcWrapper) NetInfo(ctx context.Context) (*ctypes.ResultNetInfo, error) { | 
|  | 69 | +	res, err := rpc.Client.NetInfo(ctx) | 
|  | 70 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 71 | +} | 
|  | 72 | + | 
|  | 73 | +func (rpc rpcWrapper) DumpConsensusState(ctx context.Context) (*ctypes.ResultDumpConsensusState, error) { | 
|  | 74 | +	res, err := rpc.Client.DumpConsensusState(ctx) | 
|  | 75 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 76 | +} | 
|  | 77 | + | 
|  | 78 | +func (rpc rpcWrapper) ConsensusState(ctx context.Context) (*ctypes.ResultConsensusState, error) { | 
|  | 79 | +	res, err := rpc.Client.ConsensusState(ctx) | 
|  | 80 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +func (rpc rpcWrapper) ConsensusParams(ctx context.Context, height *int64) (*ctypes.ResultConsensusParams, error) { | 
|  | 84 | +	res, err := rpc.Client.ConsensusParams(ctx, height) | 
|  | 85 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +func (rpc rpcWrapper) Health(ctx context.Context) (*ctypes.ResultHealth, error) { | 
|  | 89 | +	res, err := rpc.Client.Health(ctx) | 
|  | 90 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 91 | +} | 
|  | 92 | + | 
|  | 93 | +func (rpc rpcWrapper) Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) { | 
|  | 94 | +	res, err := rpc.Client.Block(ctx, height) | 
|  | 95 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 96 | +} | 
|  | 97 | + | 
|  | 98 | +func (rpc rpcWrapper) BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBlock, error) { | 
|  | 99 | +	res, err := rpc.Client.BlockByHash(ctx, hash) | 
|  | 100 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 101 | +} | 
|  | 102 | + | 
|  | 103 | +func (rpc rpcWrapper) BlockResults(ctx context.Context, height *int64) (*ctypes.ResultBlockResults, error) { | 
|  | 104 | +	res, err := rpc.Client.BlockResults(ctx, height) | 
|  | 105 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 106 | +} | 
|  | 107 | + | 
|  | 108 | +func (rpc rpcWrapper) Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error) { | 
|  | 109 | +	res, err := rpc.Client.Commit(ctx, height) | 
|  | 110 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 111 | +} | 
|  | 112 | + | 
|  | 113 | +func (rpc rpcWrapper) Validators(ctx context.Context, height *int64, page *int, perPage *int) (*ctypes.ResultValidators, error) { | 
|  | 114 | +	res, err := rpc.Client.Validators(ctx, height, page, perPage) | 
|  | 115 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 116 | +} | 
|  | 117 | + | 
|  | 118 | +func (rpc rpcWrapper) Tx(ctx context.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) { | 
|  | 119 | +	res, err := rpc.Client.Tx(ctx, hash, prove) | 
|  | 120 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 121 | +} | 
|  | 122 | + | 
|  | 123 | +func (rpc rpcWrapper) TxSearch(ctx context.Context, query string, prove bool, page *int, perPage *int, orderBy string) (*ctypes.ResultTxSearch, error) { | 
|  | 124 | +	res, err := rpc.Client.TxSearch(ctx, query, prove, page, perPage, orderBy) | 
|  | 125 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 126 | +} | 
|  | 127 | + | 
|  | 128 | +func (rpc rpcWrapper) BlockSearch(ctx context.Context, query string, page *int, perPage *int, orderBy string) (*ctypes.ResultBlockSearch, error) { | 
|  | 129 | +	res, err := rpc.Client.BlockSearch(ctx, query, page, perPage, orderBy) | 
|  | 130 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 131 | +} | 
|  | 132 | + | 
|  | 133 | +func (rpc rpcWrapper) Status(ctx context.Context) (*ctypes.ResultStatus, error) { | 
|  | 134 | +	res, err := rpc.Client.Status(ctx) | 
|  | 135 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 136 | +} | 
|  | 137 | + | 
|  | 138 | +func (rpc rpcWrapper) BroadcastEvidence(ctx context.Context, e types.Evidence) (*ctypes.ResultBroadcastEvidence, error) { | 
|  | 139 | +	res, err := rpc.Client.BroadcastEvidence(ctx, e) | 
|  | 140 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 141 | +} | 
|  | 142 | + | 
|  | 143 | +func (rpc rpcWrapper) UnconfirmedTxs(ctx context.Context, limit *int) (*ctypes.ResultUnconfirmedTxs, error) { | 
|  | 144 | +	res, err := rpc.Client.UnconfirmedTxs(ctx, limit) | 
|  | 145 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 146 | +} | 
|  | 147 | + | 
|  | 148 | +func (rpc rpcWrapper) NumUnconfirmedTxs(ctx context.Context) (*ctypes.ResultUnconfirmedTxs, error) { | 
|  | 149 | +	res, err := rpc.Client.NumUnconfirmedTxs(ctx) | 
|  | 150 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 151 | +} | 
|  | 152 | + | 
|  | 153 | +func (rpc rpcWrapper) CheckTx(ctx context.Context, tx types.Tx) (*ctypes.ResultCheckTx, error) { | 
|  | 154 | +	res, err := rpc.Client.CheckTx(ctx, tx) | 
|  | 155 | +	return res, rpcError(rpc.nodeAddress, err) | 
|  | 156 | +} | 
0 commit comments