github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/rpc/client/rpc_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"math"
     8  	"net/http"
     9  	"strings"
    10  	"sync"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	abci "github.com/badrootd/nibiru-cometbft/abci/types"
    18  	cmtjson "github.com/badrootd/nibiru-cometbft/libs/json"
    19  	"github.com/badrootd/nibiru-cometbft/libs/log"
    20  	cmtmath "github.com/badrootd/nibiru-cometbft/libs/math"
    21  	mempl "github.com/badrootd/nibiru-cometbft/mempool"
    22  	"github.com/badrootd/nibiru-cometbft/rpc/client"
    23  	rpchttp "github.com/badrootd/nibiru-cometbft/rpc/client/http"
    24  	rpclocal "github.com/badrootd/nibiru-cometbft/rpc/client/local"
    25  	ctypes "github.com/badrootd/nibiru-cometbft/rpc/core/types"
    26  	rpcclient "github.com/badrootd/nibiru-cometbft/rpc/jsonrpc/client"
    27  	rpctest "github.com/badrootd/nibiru-cometbft/rpc/test"
    28  	"github.com/badrootd/nibiru-cometbft/types"
    29  )
    30  
    31  var (
    32  	ctx = context.Background()
    33  )
    34  
    35  func getHTTPClient() *rpchttp.HTTP {
    36  	rpcAddr := rpctest.GetConfig().RPC.ListenAddress
    37  	c, err := rpchttp.New(rpcAddr, "/websocket")
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	c.SetLogger(log.TestingLogger())
    42  	return c
    43  }
    44  
    45  func getHTTPClientWithTimeout(timeout uint) *rpchttp.HTTP {
    46  	rpcAddr := rpctest.GetConfig().RPC.ListenAddress
    47  	c, err := rpchttp.NewWithTimeout(rpcAddr, "/websocket", timeout)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	c.SetLogger(log.TestingLogger())
    52  	return c
    53  }
    54  
    55  func getLocalClient() *rpclocal.Local {
    56  	return rpclocal.New(node)
    57  }
    58  
    59  // GetClients returns a slice of clients for table-driven tests
    60  func GetClients() []client.Client {
    61  	return []client.Client{
    62  		getHTTPClient(),
    63  		getLocalClient(),
    64  	}
    65  }
    66  
    67  func TestNilCustomHTTPClient(t *testing.T) {
    68  	require.Panics(t, func() {
    69  		_, _ = rpchttp.NewWithClient("http://example.com", "/websocket", nil)
    70  	})
    71  	require.Panics(t, func() {
    72  		_, _ = rpcclient.NewWithHTTPClient("http://example.com", nil)
    73  	})
    74  }
    75  
    76  func TestCustomHTTPClient(t *testing.T) {
    77  	remote := rpctest.GetConfig().RPC.ListenAddress
    78  	c, err := rpchttp.NewWithClient(remote, "/websocket", http.DefaultClient)
    79  	require.Nil(t, err)
    80  	status, err := c.Status(context.Background())
    81  	require.NoError(t, err)
    82  	require.NotNil(t, status)
    83  }
    84  
    85  func TestCorsEnabled(t *testing.T) {
    86  	origin := rpctest.GetConfig().RPC.CORSAllowedOrigins[0]
    87  	remote := strings.ReplaceAll(rpctest.GetConfig().RPC.ListenAddress, "tcp", "http")
    88  
    89  	req, err := http.NewRequest("GET", remote, nil)
    90  	require.Nil(t, err, "%+v", err)
    91  	req.Header.Set("Origin", origin)
    92  	c := &http.Client{}
    93  	resp, err := c.Do(req)
    94  	require.Nil(t, err, "%+v", err)
    95  	defer resp.Body.Close()
    96  
    97  	assert.Equal(t, resp.Header.Get("Access-Control-Allow-Origin"), origin)
    98  }
    99  
   100  // Make sure status is correct (we connect properly)
   101  func TestStatus(t *testing.T) {
   102  	for i, c := range GetClients() {
   103  		moniker := rpctest.GetConfig().Moniker
   104  		status, err := c.Status(context.Background())
   105  		require.Nil(t, err, "%d: %+v", i, err)
   106  		assert.Equal(t, moniker, status.NodeInfo.Moniker)
   107  	}
   108  }
   109  
   110  // Make sure info is correct (we connect properly)
   111  func TestInfo(t *testing.T) {
   112  	for i, c := range GetClients() {
   113  		// status, err := c.Status()
   114  		// require.Nil(t, err, "%+v", err)
   115  		info, err := c.ABCIInfo(context.Background())
   116  		require.Nil(t, err, "%d: %+v", i, err)
   117  		// TODO: this is not correct - fix merkleeyes!
   118  		// assert.EqualValues(t, status.SyncInfo.LatestBlockHeight, info.Response.LastBlockHeight)
   119  		assert.True(t, strings.Contains(info.Response.Data, "size"))
   120  	}
   121  }
   122  
   123  func TestNetInfo(t *testing.T) {
   124  	for i, c := range GetClients() {
   125  		nc, ok := c.(client.NetworkClient)
   126  		require.True(t, ok, "%d", i)
   127  		netinfo, err := nc.NetInfo(context.Background())
   128  		require.Nil(t, err, "%d: %+v", i, err)
   129  		assert.True(t, netinfo.Listening)
   130  		assert.Equal(t, 0, len(netinfo.Peers))
   131  	}
   132  }
   133  
   134  func TestDumpConsensusState(t *testing.T) {
   135  	for i, c := range GetClients() {
   136  		// FIXME: fix server so it doesn't panic on invalid input
   137  		nc, ok := c.(client.NetworkClient)
   138  		require.True(t, ok, "%d", i)
   139  		cons, err := nc.DumpConsensusState(context.Background())
   140  		require.Nil(t, err, "%d: %+v", i, err)
   141  		assert.NotEmpty(t, cons.RoundState)
   142  		assert.Empty(t, cons.Peers)
   143  	}
   144  }
   145  
   146  func TestConsensusState(t *testing.T) {
   147  	for i, c := range GetClients() {
   148  		// FIXME: fix server so it doesn't panic on invalid input
   149  		nc, ok := c.(client.NetworkClient)
   150  		require.True(t, ok, "%d", i)
   151  		cons, err := nc.ConsensusState(context.Background())
   152  		require.Nil(t, err, "%d: %+v", i, err)
   153  		assert.NotEmpty(t, cons.RoundState)
   154  	}
   155  }
   156  
   157  func TestHealth(t *testing.T) {
   158  	for i, c := range GetClients() {
   159  		nc, ok := c.(client.NetworkClient)
   160  		require.True(t, ok, "%d", i)
   161  		_, err := nc.Health(context.Background())
   162  		require.Nil(t, err, "%d: %+v", i, err)
   163  	}
   164  }
   165  
   166  func TestGenesisAndValidators(t *testing.T) {
   167  	for i, c := range GetClients() {
   168  
   169  		// make sure this is the right genesis file
   170  		gen, err := c.Genesis(context.Background())
   171  		require.Nil(t, err, "%d: %+v", i, err)
   172  		// get the genesis validator
   173  		require.Equal(t, 1, len(gen.Genesis.Validators))
   174  		gval := gen.Genesis.Validators[0]
   175  
   176  		// get the current validators
   177  		h := int64(1)
   178  		vals, err := c.Validators(context.Background(), &h, nil, nil)
   179  		require.Nil(t, err, "%d: %+v", i, err)
   180  		require.Equal(t, 1, len(vals.Validators))
   181  		require.Equal(t, 1, vals.Count)
   182  		require.Equal(t, 1, vals.Total)
   183  		val := vals.Validators[0]
   184  
   185  		// make sure the current set is also the genesis set
   186  		assert.Equal(t, gval.Power, val.VotingPower)
   187  		assert.Equal(t, gval.PubKey, val.PubKey)
   188  	}
   189  }
   190  
   191  func TestGenesisChunked(t *testing.T) {
   192  	ctx, cancel := context.WithCancel(context.Background())
   193  	defer cancel()
   194  
   195  	for _, c := range GetClients() {
   196  		first, err := c.GenesisChunked(ctx, 0)
   197  		require.NoError(t, err)
   198  
   199  		decoded := make([]string, 0, first.TotalChunks)
   200  		for i := 0; i < first.TotalChunks; i++ {
   201  			chunk, err := c.GenesisChunked(ctx, uint(i))
   202  			require.NoError(t, err)
   203  			data, err := base64.StdEncoding.DecodeString(chunk.Data)
   204  			require.NoError(t, err)
   205  			decoded = append(decoded, string(data))
   206  
   207  		}
   208  		doc := []byte(strings.Join(decoded, ""))
   209  
   210  		var out types.GenesisDoc
   211  		require.NoError(t, cmtjson.Unmarshal(doc, &out),
   212  			"first: %+v, doc: %s", first, string(doc))
   213  	}
   214  }
   215  
   216  func TestABCIQuery(t *testing.T) {
   217  	for i, c := range GetClients() {
   218  		// write something
   219  		k, v, tx := MakeTxKV()
   220  		bres, err := c.BroadcastTxCommit(context.Background(), tx)
   221  		require.Nil(t, err, "%d: %+v", i, err)
   222  		apph := bres.Height + 1 // this is where the tx will be applied to the state
   223  
   224  		// wait before querying
   225  		err = client.WaitForHeight(c, apph, nil)
   226  		require.NoError(t, err)
   227  		res, err := c.ABCIQuery(context.Background(), "/key", k)
   228  		qres := res.Response
   229  		if assert.Nil(t, err) && assert.True(t, qres.IsOK()) {
   230  			assert.EqualValues(t, v, qres.Value)
   231  		}
   232  	}
   233  }
   234  
   235  // Make some app checks
   236  func TestAppCalls(t *testing.T) {
   237  	assert, require := assert.New(t), require.New(t)
   238  	for i, c := range GetClients() {
   239  
   240  		// get an offset of height to avoid racing and guessing
   241  		s, err := c.Status(context.Background())
   242  		require.NoError(err)
   243  		// sh is start height or status height
   244  		sh := s.SyncInfo.LatestBlockHeight
   245  
   246  		// look for the future
   247  		h := sh + 20
   248  		_, err = c.Block(context.Background(), &h)
   249  		require.Error(err) // no block yet
   250  
   251  		// write something
   252  		k, v, tx := MakeTxKV()
   253  		bres, err := c.BroadcastTxCommit(context.Background(), tx)
   254  		require.NoError(err)
   255  		require.True(bres.DeliverTx.IsOK())
   256  		txh := bres.Height
   257  		apph := txh + 1 // this is where the tx will be applied to the state
   258  
   259  		// wait before querying
   260  		err = client.WaitForHeight(c, apph, nil)
   261  		require.NoError(err)
   262  
   263  		_qres, err := c.ABCIQueryWithOptions(context.Background(), "/key", k, client.ABCIQueryOptions{Prove: false})
   264  		require.NoError(err)
   265  		qres := _qres.Response
   266  		if assert.True(qres.IsOK()) {
   267  			assert.Equal(k, qres.Key)
   268  			assert.EqualValues(v, qres.Value)
   269  		}
   270  
   271  		// make sure we can lookup the tx with proof
   272  		ptx, err := c.Tx(context.Background(), bres.Hash, true)
   273  		require.NoError(err)
   274  		assert.EqualValues(txh, ptx.Height)
   275  		assert.EqualValues(tx, ptx.Tx)
   276  
   277  		// and we can even check the block is added
   278  		block, err := c.Block(context.Background(), &apph)
   279  		require.NoError(err)
   280  		appHash := block.Block.Header.AppHash
   281  		assert.True(len(appHash) > 0)
   282  		assert.EqualValues(apph, block.Block.Header.Height)
   283  
   284  		blockByHash, err := c.BlockByHash(context.Background(), block.BlockID.Hash)
   285  		require.NoError(err)
   286  		require.Equal(block, blockByHash)
   287  
   288  		// check that the header matches the block hash
   289  		header, err := c.Header(context.Background(), &apph)
   290  		require.NoError(err)
   291  		require.Equal(block.Block.Header, *header.Header)
   292  
   293  		headerByHash, err := c.HeaderByHash(context.Background(), block.BlockID.Hash)
   294  		require.NoError(err)
   295  		require.Equal(header, headerByHash)
   296  
   297  		// now check the results
   298  		blockResults, err := c.BlockResults(context.Background(), &txh)
   299  		require.Nil(err, "%d: %+v", i, err)
   300  		assert.Equal(txh, blockResults.Height)
   301  		if assert.Equal(1, len(blockResults.TxsResults)) {
   302  			// check success code
   303  			assert.EqualValues(0, blockResults.TxsResults[0].Code)
   304  		}
   305  
   306  		// check blockchain info, now that we know there is info
   307  		info, err := c.BlockchainInfo(context.Background(), apph, apph)
   308  		require.NoError(err)
   309  		assert.True(info.LastHeight >= apph)
   310  		if assert.Equal(1, len(info.BlockMetas)) {
   311  			lastMeta := info.BlockMetas[0]
   312  			assert.EqualValues(apph, lastMeta.Header.Height)
   313  			blockData := block.Block
   314  			assert.Equal(blockData.Header.AppHash, lastMeta.Header.AppHash)
   315  			assert.Equal(block.BlockID, lastMeta.BlockID)
   316  		}
   317  
   318  		// and get the corresponding commit with the same apphash
   319  		commit, err := c.Commit(context.Background(), &apph)
   320  		require.NoError(err)
   321  		cappHash := commit.Header.AppHash
   322  		assert.Equal(appHash, cappHash)
   323  		assert.NotNil(commit.Commit)
   324  
   325  		// compare the commits (note Commit(2) has commit from Block(3))
   326  		h = apph - 1
   327  		commit2, err := c.Commit(context.Background(), &h)
   328  		require.NoError(err)
   329  		assert.Equal(block.Block.LastCommitHash, commit2.Commit.Hash())
   330  
   331  		// and we got a proof that works!
   332  		_pres, err := c.ABCIQueryWithOptions(context.Background(), "/key", k, client.ABCIQueryOptions{Prove: true})
   333  		require.NoError(err)
   334  		pres := _pres.Response
   335  		assert.True(pres.IsOK())
   336  
   337  		// XXX Test proof
   338  	}
   339  }
   340  
   341  func TestBroadcastTxSync(t *testing.T) {
   342  	require := require.New(t)
   343  
   344  	// TODO (melekes): use mempool which is set on RPC rather than getting it from node
   345  	mempool := node.Mempool()
   346  	initMempoolSize := mempool.Size()
   347  
   348  	for i, c := range GetClients() {
   349  		_, _, tx := MakeTxKV()
   350  		bres, err := c.BroadcastTxSync(context.Background(), tx)
   351  		require.Nil(err, "%d: %+v", i, err)
   352  		require.Equal(bres.Code, abci.CodeTypeOK) // FIXME
   353  
   354  		require.Equal(initMempoolSize+1, mempool.Size())
   355  
   356  		txs := mempool.ReapMaxTxs(len(tx))
   357  		require.EqualValues(tx, txs[0])
   358  		mempool.Flush()
   359  	}
   360  }
   361  
   362  func TestBroadcastTxCommit(t *testing.T) {
   363  	require := require.New(t)
   364  
   365  	mempool := node.Mempool()
   366  	for i, c := range GetClients() {
   367  		_, _, tx := MakeTxKV()
   368  		bres, err := c.BroadcastTxCommit(context.Background(), tx)
   369  		require.Nil(err, "%d: %+v", i, err)
   370  		require.True(bres.CheckTx.IsOK())
   371  		require.True(bres.DeliverTx.IsOK())
   372  
   373  		require.Equal(0, mempool.Size())
   374  	}
   375  }
   376  
   377  func TestUnconfirmedTxs(t *testing.T) {
   378  	_, _, tx := MakeTxKV()
   379  
   380  	ch := make(chan *abci.Response, 1)
   381  	mempool := node.Mempool()
   382  	err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
   383  	require.NoError(t, err)
   384  
   385  	// wait for tx to arrive in mempoool.
   386  	select {
   387  	case <-ch:
   388  	case <-time.After(5 * time.Second):
   389  		t.Error("Timed out waiting for CheckTx callback")
   390  	}
   391  
   392  	for _, c := range GetClients() {
   393  		mc := c.(client.MempoolClient)
   394  		limit := 1
   395  		res, err := mc.UnconfirmedTxs(context.Background(), &limit)
   396  		require.NoError(t, err)
   397  
   398  		assert.Equal(t, 1, res.Count)
   399  		assert.Equal(t, 1, res.Total)
   400  		assert.Equal(t, mempool.SizeBytes(), res.TotalBytes)
   401  		assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs))
   402  	}
   403  
   404  	mempool.Flush()
   405  }
   406  
   407  func TestNumUnconfirmedTxs(t *testing.T) {
   408  	_, _, tx := MakeTxKV()
   409  
   410  	ch := make(chan *abci.Response, 1)
   411  	mempool := node.Mempool()
   412  	err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
   413  	require.NoError(t, err)
   414  
   415  	// wait for tx to arrive in mempoool.
   416  	select {
   417  	case <-ch:
   418  	case <-time.After(5 * time.Second):
   419  		t.Error("Timed out waiting for CheckTx callback")
   420  	}
   421  
   422  	mempoolSize := mempool.Size()
   423  	for i, c := range GetClients() {
   424  		mc, ok := c.(client.MempoolClient)
   425  		require.True(t, ok, "%d", i)
   426  		res, err := mc.NumUnconfirmedTxs(context.Background())
   427  		require.Nil(t, err, "%d: %+v", i, err)
   428  
   429  		assert.Equal(t, mempoolSize, res.Count)
   430  		assert.Equal(t, mempoolSize, res.Total)
   431  		assert.Equal(t, mempool.SizeBytes(), res.TotalBytes)
   432  	}
   433  
   434  	mempool.Flush()
   435  }
   436  
   437  func TestCheckTx(t *testing.T) {
   438  	mempool := node.Mempool()
   439  
   440  	for _, c := range GetClients() {
   441  		_, _, tx := MakeTxKV()
   442  
   443  		res, err := c.CheckTx(context.Background(), tx)
   444  		require.NoError(t, err)
   445  		assert.Equal(t, abci.CodeTypeOK, res.Code)
   446  
   447  		assert.Equal(t, 0, mempool.Size(), "mempool must be empty")
   448  	}
   449  }
   450  
   451  func TestTx(t *testing.T) {
   452  	// first we broadcast a tx
   453  	c := getHTTPClient()
   454  	_, _, tx := MakeTxKV()
   455  	bres, err := c.BroadcastTxCommit(context.Background(), tx)
   456  	require.Nil(t, err, "%+v", err)
   457  
   458  	txHeight := bres.Height
   459  	txHash := bres.Hash
   460  
   461  	anotherTxHash := types.Tx("a different tx").Hash()
   462  
   463  	cases := []struct {
   464  		valid bool
   465  		prove bool
   466  		hash  []byte
   467  	}{
   468  		// only valid if correct hash provided
   469  		{true, false, txHash},
   470  		{true, true, txHash},
   471  		{false, false, anotherTxHash},
   472  		{false, true, anotherTxHash},
   473  		{false, false, nil},
   474  		{false, true, nil},
   475  	}
   476  
   477  	for i, c := range GetClients() {
   478  		for j, tc := range cases {
   479  			t.Logf("client %d, case %d", i, j)
   480  
   481  			// now we query for the tx.
   482  			// since there's only one tx, we know index=0.
   483  			ptx, err := c.Tx(context.Background(), tc.hash, tc.prove)
   484  
   485  			if !tc.valid {
   486  				require.NotNil(t, err)
   487  			} else {
   488  				require.Nil(t, err, "%+v", err)
   489  				assert.EqualValues(t, txHeight, ptx.Height)
   490  				assert.EqualValues(t, tx, ptx.Tx)
   491  				assert.Zero(t, ptx.Index)
   492  				assert.True(t, ptx.TxResult.IsOK())
   493  				assert.EqualValues(t, txHash, ptx.Hash)
   494  
   495  				// time to verify the proof
   496  				proof := ptx.Proof
   497  				if tc.prove && assert.EqualValues(t, tx, proof.Data) {
   498  					assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
   499  				}
   500  			}
   501  		}
   502  	}
   503  }
   504  
   505  func TestTxSearchWithTimeout(t *testing.T) {
   506  	// Get a client with a time-out of 10 secs.
   507  	timeoutClient := getHTTPClientWithTimeout(10)
   508  
   509  	_, _, tx := MakeTxKV()
   510  	_, err := timeoutClient.BroadcastTxCommit(context.Background(), tx)
   511  	require.NoError(t, err)
   512  
   513  	// query using a compositeKey (see kvstore application)
   514  	result, err := timeoutClient.TxSearch(context.Background(), "app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
   515  	require.Nil(t, err)
   516  	require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
   517  }
   518  
   519  // This test does nothing if we do not call app.SetGenBlockEvents() within main_test.go
   520  // It will nevertheless pass as there are no events being generated.
   521  func TestBlockSearch(t *testing.T) {
   522  	c := getHTTPClient()
   523  
   524  	// first we broadcast a few txs
   525  	for i := 0; i < 10; i++ {
   526  		_, _, tx := MakeTxKV()
   527  
   528  		_, err := c.BroadcastTxCommit(context.Background(), tx)
   529  		require.NoError(t, err)
   530  	}
   531  	require.NoError(t, client.WaitForHeight(c, 5, nil))
   532  	result, err := c.BlockSearch(context.Background(), "begin_event.foo = 100", nil, nil, "asc")
   533  	require.NoError(t, err)
   534  	blockCount := len(result.Blocks)
   535  	// if we generate block events within the test (by uncommenting
   536  	// the code in line main_test.go:L23) then we expect len(result.Blocks)
   537  	// to be at least 5
   538  	// require.GreaterOrEqual(t, blockCount, 5)
   539  
   540  	// otherwise it is 0
   541  	require.Equal(t, blockCount, 0)
   542  
   543  }
   544  func TestTxSearch(t *testing.T) {
   545  	c := getHTTPClient()
   546  
   547  	// first we broadcast a few txs
   548  	for i := 0; i < 10; i++ {
   549  		_, _, tx := MakeTxKV()
   550  		_, err := c.BroadcastTxCommit(context.Background(), tx)
   551  		require.NoError(t, err)
   552  	}
   553  
   554  	// since we're not using an isolated test server, we'll have lingering transactions
   555  	// from other tests as well
   556  	result, err := c.TxSearch(context.Background(), "tx.height >= 0", true, nil, nil, "asc")
   557  	require.NoError(t, err)
   558  	txCount := len(result.Txs)
   559  
   560  	// pick out the last tx to have something to search for in tests
   561  	find := result.Txs[len(result.Txs)-1]
   562  	anotherTxHash := types.Tx("a different tx").Hash()
   563  
   564  	for _, c := range GetClients() {
   565  
   566  		// now we query for the tx.
   567  		result, err := c.TxSearch(context.Background(), fmt.Sprintf("tx.hash='%v'", find.Hash), true, nil, nil, "asc")
   568  		require.Nil(t, err)
   569  		require.Len(t, result.Txs, 1)
   570  		require.Equal(t, find.Hash, result.Txs[0].Hash)
   571  
   572  		ptx := result.Txs[0]
   573  		assert.EqualValues(t, find.Height, ptx.Height)
   574  		assert.EqualValues(t, find.Tx, ptx.Tx)
   575  		assert.Zero(t, ptx.Index)
   576  		assert.True(t, ptx.TxResult.IsOK())
   577  		assert.EqualValues(t, find.Hash, ptx.Hash)
   578  
   579  		// time to verify the proof
   580  		if assert.EqualValues(t, find.Tx, ptx.Proof.Data) {
   581  			assert.NoError(t, ptx.Proof.Proof.Verify(ptx.Proof.RootHash, find.Hash))
   582  		}
   583  
   584  		// query by height
   585  		result, err = c.TxSearch(context.Background(), fmt.Sprintf("tx.height=%d", find.Height), true, nil, nil, "asc")
   586  		require.Nil(t, err)
   587  		require.Len(t, result.Txs, 1)
   588  
   589  		// query for non existing tx
   590  		result, err = c.TxSearch(context.Background(), fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, nil, nil, "asc")
   591  		require.Nil(t, err)
   592  		require.Len(t, result.Txs, 0)
   593  
   594  		// query using a compositeKey (see kvstore application)
   595  		result, err = c.TxSearch(context.Background(), "app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
   596  		require.Nil(t, err)
   597  		require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
   598  
   599  		// query using an index key
   600  		result, err = c.TxSearch(context.Background(), "app.index_key='index is working'", false, nil, nil, "asc")
   601  		require.Nil(t, err)
   602  		require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
   603  
   604  		// query using an noindex key
   605  		result, err = c.TxSearch(context.Background(), "app.noindex_key='index is working'", false, nil, nil, "asc")
   606  		require.Nil(t, err)
   607  		require.Equal(t, len(result.Txs), 0, "expected a lot of transactions")
   608  
   609  		// query using a compositeKey (see kvstore application) and height
   610  		result, err = c.TxSearch(context.Background(),
   611  			"app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, nil, nil, "asc")
   612  		require.Nil(t, err)
   613  		require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
   614  
   615  		// query a non existing tx with page 1 and txsPerPage 1
   616  		perPage := 1
   617  		result, err = c.TxSearch(context.Background(), "app.creator='Cosmoshi Neetowoko'", true, nil, &perPage, "asc")
   618  		require.Nil(t, err)
   619  		require.Len(t, result.Txs, 0)
   620  
   621  		// check sorting
   622  		result, err = c.TxSearch(context.Background(), "tx.height >= 1", false, nil, nil, "asc")
   623  		require.Nil(t, err)
   624  		for k := 0; k < len(result.Txs)-1; k++ {
   625  			require.LessOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
   626  			require.LessOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
   627  		}
   628  
   629  		result, err = c.TxSearch(context.Background(), "tx.height >= 1", false, nil, nil, "desc")
   630  		require.Nil(t, err)
   631  		for k := 0; k < len(result.Txs)-1; k++ {
   632  			require.GreaterOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
   633  			require.GreaterOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
   634  		}
   635  		// check pagination
   636  		perPage = 3
   637  		var (
   638  			seen      = map[int64]bool{}
   639  			maxHeight int64
   640  			pages     = int(math.Ceil(float64(txCount) / float64(perPage)))
   641  		)
   642  
   643  		totalTx := 0
   644  		for page := 1; page <= pages; page++ {
   645  			page := page
   646  			result, err := c.TxSearch(context.Background(), "tx.height >= 1", true, &page, &perPage, "asc")
   647  			require.NoError(t, err)
   648  			if page < pages {
   649  				require.Len(t, result.Txs, perPage)
   650  			} else {
   651  				require.LessOrEqual(t, len(result.Txs), perPage)
   652  			}
   653  			totalTx = totalTx + len(result.Txs)
   654  			for _, tx := range result.Txs {
   655  				require.False(t, seen[tx.Height],
   656  					"Found duplicate height %v in page %v", tx.Height, page)
   657  				require.Greater(t, tx.Height, maxHeight,
   658  					"Found decreasing height %v (max seen %v) in page %v", tx.Height, maxHeight, page)
   659  				seen[tx.Height] = true
   660  				maxHeight = tx.Height
   661  			}
   662  		}
   663  		require.Equal(t, txCount, totalTx)
   664  		require.Len(t, seen, txCount)
   665  	}
   666  }
   667  
   668  func TestBatchedJSONRPCCalls(t *testing.T) {
   669  	c := getHTTPClient()
   670  	testBatchedJSONRPCCalls(t, c)
   671  }
   672  
   673  func testBatchedJSONRPCCalls(t *testing.T, c *rpchttp.HTTP) {
   674  	k1, v1, tx1 := MakeTxKV()
   675  	k2, v2, tx2 := MakeTxKV()
   676  
   677  	batch := c.NewBatch()
   678  	r1, err := batch.BroadcastTxCommit(context.Background(), tx1)
   679  	require.NoError(t, err)
   680  	r2, err := batch.BroadcastTxCommit(context.Background(), tx2)
   681  	require.NoError(t, err)
   682  	require.Equal(t, 2, batch.Count())
   683  	bresults, err := batch.Send(ctx)
   684  	require.NoError(t, err)
   685  	require.Len(t, bresults, 2)
   686  	require.Equal(t, 0, batch.Count())
   687  
   688  	bresult1, ok := bresults[0].(*ctypes.ResultBroadcastTxCommit)
   689  	require.True(t, ok)
   690  	require.Equal(t, *bresult1, *r1)
   691  	bresult2, ok := bresults[1].(*ctypes.ResultBroadcastTxCommit)
   692  	require.True(t, ok)
   693  	require.Equal(t, *bresult2, *r2)
   694  	apph := cmtmath.MaxInt64(bresult1.Height, bresult2.Height) + 1
   695  
   696  	err = client.WaitForHeight(c, apph, nil)
   697  	require.NoError(t, err)
   698  
   699  	q1, err := batch.ABCIQuery(context.Background(), "/key", k1)
   700  	require.NoError(t, err)
   701  	q2, err := batch.ABCIQuery(context.Background(), "/key", k2)
   702  	require.NoError(t, err)
   703  	require.Equal(t, 2, batch.Count())
   704  	qresults, err := batch.Send(ctx)
   705  	require.NoError(t, err)
   706  	require.Len(t, qresults, 2)
   707  	require.Equal(t, 0, batch.Count())
   708  
   709  	qresult1, ok := qresults[0].(*ctypes.ResultABCIQuery)
   710  	require.True(t, ok)
   711  	require.Equal(t, *qresult1, *q1)
   712  	qresult2, ok := qresults[1].(*ctypes.ResultABCIQuery)
   713  	require.True(t, ok)
   714  	require.Equal(t, *qresult2, *q2)
   715  
   716  	require.Equal(t, qresult1.Response.Key, k1)
   717  	require.Equal(t, qresult2.Response.Key, k2)
   718  	require.Equal(t, qresult1.Response.Value, v1)
   719  	require.Equal(t, qresult2.Response.Value, v2)
   720  }
   721  
   722  func TestBatchedJSONRPCCallsCancellation(t *testing.T) {
   723  	c := getHTTPClient()
   724  	_, _, tx1 := MakeTxKV()
   725  	_, _, tx2 := MakeTxKV()
   726  
   727  	batch := c.NewBatch()
   728  	_, err := batch.BroadcastTxCommit(context.Background(), tx1)
   729  	require.NoError(t, err)
   730  	_, err = batch.BroadcastTxCommit(context.Background(), tx2)
   731  	require.NoError(t, err)
   732  	// we should have 2 requests waiting
   733  	require.Equal(t, 2, batch.Count())
   734  	// we want to make sure we cleared 2 pending requests
   735  	require.Equal(t, 2, batch.Clear())
   736  	// now there should be no batched requests
   737  	require.Equal(t, 0, batch.Count())
   738  }
   739  
   740  func TestSendingEmptyRequestBatch(t *testing.T) {
   741  	c := getHTTPClient()
   742  	batch := c.NewBatch()
   743  	_, err := batch.Send(ctx)
   744  	require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error")
   745  }
   746  
   747  func TestClearingEmptyRequestBatch(t *testing.T) {
   748  	c := getHTTPClient()
   749  	batch := c.NewBatch()
   750  	require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result")
   751  }
   752  
   753  func TestConcurrentJSONRPCBatching(t *testing.T) {
   754  	var wg sync.WaitGroup
   755  	c := getHTTPClient()
   756  	for i := 0; i < 50; i++ {
   757  		wg.Add(1)
   758  		go func() {
   759  			defer wg.Done()
   760  			testBatchedJSONRPCCalls(t, c)
   761  		}()
   762  	}
   763  	wg.Wait()
   764  }