github.com/Oyster-zx/tendermint@v0.34.24-fork/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/tendermint/tendermint/abci/types" 18 tmjson "github.com/tendermint/tendermint/libs/json" 19 "github.com/tendermint/tendermint/libs/log" 20 tmmath "github.com/tendermint/tendermint/libs/math" 21 mempl "github.com/tendermint/tendermint/mempool" 22 "github.com/tendermint/tendermint/rpc/client" 23 rpchttp "github.com/tendermint/tendermint/rpc/client/http" 24 rpclocal "github.com/tendermint/tendermint/rpc/client/local" 25 ctypes "github.com/tendermint/tendermint/rpc/core/types" 26 rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client" 27 rpctest "github.com/tendermint/tendermint/rpc/test" 28 "github.com/tendermint/tendermint/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, tmjson.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 // now check the results 289 blockResults, err := c.BlockResults(context.Background(), &txh) 290 require.Nil(err, "%d: %+v", i, err) 291 assert.Equal(txh, blockResults.Height) 292 if assert.Equal(1, len(blockResults.TxsResults)) { 293 // check success code 294 assert.EqualValues(0, blockResults.TxsResults[0].Code) 295 } 296 297 // check blockchain info, now that we know there is info 298 info, err := c.BlockchainInfo(context.Background(), apph, apph) 299 require.NoError(err) 300 assert.True(info.LastHeight >= apph) 301 if assert.Equal(1, len(info.BlockMetas)) { 302 lastMeta := info.BlockMetas[0] 303 assert.EqualValues(apph, lastMeta.Header.Height) 304 blockData := block.Block 305 assert.Equal(blockData.Header.AppHash, lastMeta.Header.AppHash) 306 assert.Equal(block.BlockID, lastMeta.BlockID) 307 } 308 309 // and get the corresponding commit with the same apphash 310 commit, err := c.Commit(context.Background(), &apph) 311 require.NoError(err) 312 cappHash := commit.Header.AppHash 313 assert.Equal(appHash, cappHash) 314 assert.NotNil(commit.Commit) 315 316 // compare the commits (note Commit(2) has commit from Block(3)) 317 h = apph - 1 318 commit2, err := c.Commit(context.Background(), &h) 319 require.NoError(err) 320 assert.Equal(block.Block.LastCommitHash, commit2.Commit.Hash()) 321 322 // and we got a proof that works! 323 _pres, err := c.ABCIQueryWithOptions(context.Background(), "/key", k, client.ABCIQueryOptions{Prove: true}) 324 require.NoError(err) 325 pres := _pres.Response 326 assert.True(pres.IsOK()) 327 328 // XXX Test proof 329 } 330 } 331 332 func TestBroadcastTxSync(t *testing.T) { 333 require := require.New(t) 334 335 // TODO (melekes): use mempool which is set on RPC rather than getting it from node 336 mempool := node.Mempool() 337 initMempoolSize := mempool.Size() 338 339 for i, c := range GetClients() { 340 _, _, tx := MakeTxKV() 341 bres, err := c.BroadcastTxSync(context.Background(), tx) 342 require.Nil(err, "%d: %+v", i, err) 343 require.Equal(bres.Code, abci.CodeTypeOK) // FIXME 344 345 require.Equal(initMempoolSize+1, mempool.Size()) 346 347 txs := mempool.ReapMaxTxs(len(tx)) 348 require.EqualValues(tx, txs[0]) 349 mempool.Flush() 350 } 351 } 352 353 func TestBroadcastTxCommit(t *testing.T) { 354 require := require.New(t) 355 356 mempool := node.Mempool() 357 for i, c := range GetClients() { 358 _, _, tx := MakeTxKV() 359 bres, err := c.BroadcastTxCommit(context.Background(), tx) 360 require.Nil(err, "%d: %+v", i, err) 361 require.True(bres.CheckTx.IsOK()) 362 require.True(bres.DeliverTx.IsOK()) 363 364 require.Equal(0, mempool.Size()) 365 } 366 } 367 368 func TestUnconfirmedTxs(t *testing.T) { 369 _, _, tx := MakeTxKV() 370 371 ch := make(chan *abci.Response, 1) 372 mempool := node.Mempool() 373 err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{}) 374 require.NoError(t, err) 375 376 // wait for tx to arrive in mempoool. 377 select { 378 case <-ch: 379 case <-time.After(5 * time.Second): 380 t.Error("Timed out waiting for CheckTx callback") 381 } 382 383 for _, c := range GetClients() { 384 mc := c.(client.MempoolClient) 385 limit := 1 386 res, err := mc.UnconfirmedTxs(context.Background(), &limit) 387 require.NoError(t, err) 388 389 assert.Equal(t, 1, res.Count) 390 assert.Equal(t, 1, res.Total) 391 assert.Equal(t, mempool.SizeBytes(), res.TotalBytes) 392 assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs)) 393 } 394 395 mempool.Flush() 396 } 397 398 func TestNumUnconfirmedTxs(t *testing.T) { 399 _, _, tx := MakeTxKV() 400 401 ch := make(chan *abci.Response, 1) 402 mempool := node.Mempool() 403 err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{}) 404 require.NoError(t, err) 405 406 // wait for tx to arrive in mempoool. 407 select { 408 case <-ch: 409 case <-time.After(5 * time.Second): 410 t.Error("Timed out waiting for CheckTx callback") 411 } 412 413 mempoolSize := mempool.Size() 414 for i, c := range GetClients() { 415 mc, ok := c.(client.MempoolClient) 416 require.True(t, ok, "%d", i) 417 res, err := mc.NumUnconfirmedTxs(context.Background()) 418 require.Nil(t, err, "%d: %+v", i, err) 419 420 assert.Equal(t, mempoolSize, res.Count) 421 assert.Equal(t, mempoolSize, res.Total) 422 assert.Equal(t, mempool.SizeBytes(), res.TotalBytes) 423 } 424 425 mempool.Flush() 426 } 427 428 func TestCheckTx(t *testing.T) { 429 mempool := node.Mempool() 430 431 for _, c := range GetClients() { 432 _, _, tx := MakeTxKV() 433 434 res, err := c.CheckTx(context.Background(), tx) 435 require.NoError(t, err) 436 assert.Equal(t, abci.CodeTypeOK, res.Code) 437 438 assert.Equal(t, 0, mempool.Size(), "mempool must be empty") 439 } 440 } 441 442 func TestTx(t *testing.T) { 443 // first we broadcast a tx 444 c := getHTTPClient() 445 _, _, tx := MakeTxKV() 446 bres, err := c.BroadcastTxCommit(context.Background(), tx) 447 require.Nil(t, err, "%+v", err) 448 449 txHeight := bres.Height 450 txHash := bres.Hash 451 452 anotherTxHash := types.Tx("a different tx").Hash() 453 454 cases := []struct { 455 valid bool 456 prove bool 457 hash []byte 458 }{ 459 // only valid if correct hash provided 460 {true, false, txHash}, 461 {true, true, txHash}, 462 {false, false, anotherTxHash}, 463 {false, true, anotherTxHash}, 464 {false, false, nil}, 465 {false, true, nil}, 466 } 467 468 for i, c := range GetClients() { 469 for j, tc := range cases { 470 t.Logf("client %d, case %d", i, j) 471 472 // now we query for the tx. 473 // since there's only one tx, we know index=0. 474 ptx, err := c.Tx(context.Background(), tc.hash, tc.prove) 475 476 if !tc.valid { 477 require.NotNil(t, err) 478 } else { 479 require.Nil(t, err, "%+v", err) 480 assert.EqualValues(t, txHeight, ptx.Height) 481 assert.EqualValues(t, tx, ptx.Tx) 482 assert.Zero(t, ptx.Index) 483 assert.True(t, ptx.TxResult.IsOK()) 484 assert.EqualValues(t, txHash, ptx.Hash) 485 486 // time to verify the proof 487 proof := ptx.Proof 488 if tc.prove && assert.EqualValues(t, tx, proof.Data) { 489 assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash)) 490 } 491 } 492 } 493 } 494 } 495 496 func TestTxSearchWithTimeout(t *testing.T) { 497 // Get a client with a time-out of 10 secs. 498 timeoutClient := getHTTPClientWithTimeout(10) 499 500 _, _, tx := MakeTxKV() 501 _, err := timeoutClient.BroadcastTxCommit(context.Background(), tx) 502 require.NoError(t, err) 503 504 // query using a compositeKey (see kvstore application) 505 result, err := timeoutClient.TxSearch(context.Background(), "app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc") 506 require.Nil(t, err) 507 require.Greater(t, len(result.Txs), 0, "expected a lot of transactions") 508 } 509 510 func TestTxSearch(t *testing.T) { 511 c := getHTTPClient() 512 513 // first we broadcast a few txs 514 for i := 0; i < 10; i++ { 515 _, _, tx := MakeTxKV() 516 _, err := c.BroadcastTxCommit(context.Background(), tx) 517 require.NoError(t, err) 518 } 519 520 // since we're not using an isolated test server, we'll have lingering transactions 521 // from other tests as well 522 result, err := c.TxSearch(context.Background(), "tx.height >= 0", true, nil, nil, "asc") 523 require.NoError(t, err) 524 txCount := len(result.Txs) 525 526 // pick out the last tx to have something to search for in tests 527 find := result.Txs[len(result.Txs)-1] 528 anotherTxHash := types.Tx("a different tx").Hash() 529 530 for i, c := range GetClients() { 531 t.Logf("client %d", i) 532 533 // now we query for the tx. 534 result, err := c.TxSearch(context.Background(), fmt.Sprintf("tx.hash='%v'", find.Hash), true, nil, nil, "asc") 535 require.Nil(t, err) 536 require.Len(t, result.Txs, 1) 537 require.Equal(t, find.Hash, result.Txs[0].Hash) 538 539 ptx := result.Txs[0] 540 assert.EqualValues(t, find.Height, ptx.Height) 541 assert.EqualValues(t, find.Tx, ptx.Tx) 542 assert.Zero(t, ptx.Index) 543 assert.True(t, ptx.TxResult.IsOK()) 544 assert.EqualValues(t, find.Hash, ptx.Hash) 545 546 // time to verify the proof 547 if assert.EqualValues(t, find.Tx, ptx.Proof.Data) { 548 assert.NoError(t, ptx.Proof.Proof.Verify(ptx.Proof.RootHash, find.Hash)) 549 } 550 551 // query by height 552 result, err = c.TxSearch(context.Background(), fmt.Sprintf("tx.height=%d", find.Height), true, nil, nil, "asc") 553 require.Nil(t, err) 554 require.Len(t, result.Txs, 1) 555 556 // query for non existing tx 557 result, err = c.TxSearch(context.Background(), fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, nil, nil, "asc") 558 require.Nil(t, err) 559 require.Len(t, result.Txs, 0) 560 561 // query using a compositeKey (see kvstore application) 562 result, err = c.TxSearch(context.Background(), "app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc") 563 require.Nil(t, err) 564 require.Greater(t, len(result.Txs), 0, "expected a lot of transactions") 565 566 // query using an index key 567 result, err = c.TxSearch(context.Background(), "app.index_key='index is working'", false, nil, nil, "asc") 568 require.Nil(t, err) 569 require.Greater(t, len(result.Txs), 0, "expected a lot of transactions") 570 571 // query using an noindex key 572 result, err = c.TxSearch(context.Background(), "app.noindex_key='index is working'", false, nil, nil, "asc") 573 require.Nil(t, err) 574 require.Equal(t, len(result.Txs), 0, "expected a lot of transactions") 575 576 // query using a compositeKey (see kvstore application) and height 577 result, err = c.TxSearch(context.Background(), 578 "app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, nil, nil, "asc") 579 require.Nil(t, err) 580 require.Greater(t, len(result.Txs), 0, "expected a lot of transactions") 581 582 // query a non existing tx with page 1 and txsPerPage 1 583 perPage := 1 584 result, err = c.TxSearch(context.Background(), "app.creator='Cosmoshi Neetowoko'", true, nil, &perPage, "asc") 585 require.Nil(t, err) 586 require.Len(t, result.Txs, 0) 587 588 // check sorting 589 result, err = c.TxSearch(context.Background(), "tx.height >= 1", false, nil, nil, "asc") 590 require.Nil(t, err) 591 for k := 0; k < len(result.Txs)-1; k++ { 592 require.LessOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height) 593 require.LessOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index) 594 } 595 596 result, err = c.TxSearch(context.Background(), "tx.height >= 1", false, nil, nil, "desc") 597 require.Nil(t, err) 598 for k := 0; k < len(result.Txs)-1; k++ { 599 require.GreaterOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height) 600 require.GreaterOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index) 601 } 602 // check pagination 603 perPage = 3 604 var ( 605 seen = map[int64]bool{} 606 maxHeight int64 607 pages = int(math.Ceil(float64(txCount) / float64(perPage))) 608 ) 609 610 for page := 1; page <= pages; page++ { 611 page := page 612 result, err := c.TxSearch(context.Background(), "tx.height >= 1", false, &page, &perPage, "asc") 613 require.NoError(t, err) 614 if page < pages { 615 require.Len(t, result.Txs, perPage) 616 } else { 617 require.LessOrEqual(t, len(result.Txs), perPage) 618 } 619 require.Equal(t, txCount, result.TotalCount) 620 for _, tx := range result.Txs { 621 require.False(t, seen[tx.Height], 622 "Found duplicate height %v in page %v", tx.Height, page) 623 require.Greater(t, tx.Height, maxHeight, 624 "Found decreasing height %v (max seen %v) in page %v", tx.Height, maxHeight, page) 625 seen[tx.Height] = true 626 maxHeight = tx.Height 627 } 628 } 629 require.Len(t, seen, txCount) 630 } 631 } 632 633 func TestBatchedJSONRPCCalls(t *testing.T) { 634 c := getHTTPClient() 635 testBatchedJSONRPCCalls(t, c) 636 } 637 638 func testBatchedJSONRPCCalls(t *testing.T, c *rpchttp.HTTP) { 639 k1, v1, tx1 := MakeTxKV() 640 k2, v2, tx2 := MakeTxKV() 641 642 batch := c.NewBatch() 643 r1, err := batch.BroadcastTxCommit(context.Background(), tx1) 644 require.NoError(t, err) 645 r2, err := batch.BroadcastTxCommit(context.Background(), tx2) 646 require.NoError(t, err) 647 require.Equal(t, 2, batch.Count()) 648 bresults, err := batch.Send(ctx) 649 require.NoError(t, err) 650 require.Len(t, bresults, 2) 651 require.Equal(t, 0, batch.Count()) 652 653 bresult1, ok := bresults[0].(*ctypes.ResultBroadcastTxCommit) 654 require.True(t, ok) 655 require.Equal(t, *bresult1, *r1) 656 bresult2, ok := bresults[1].(*ctypes.ResultBroadcastTxCommit) 657 require.True(t, ok) 658 require.Equal(t, *bresult2, *r2) 659 apph := tmmath.MaxInt64(bresult1.Height, bresult2.Height) + 1 660 661 err = client.WaitForHeight(c, apph, nil) 662 require.NoError(t, err) 663 664 q1, err := batch.ABCIQuery(context.Background(), "/key", k1) 665 require.NoError(t, err) 666 q2, err := batch.ABCIQuery(context.Background(), "/key", k2) 667 require.NoError(t, err) 668 require.Equal(t, 2, batch.Count()) 669 qresults, err := batch.Send(ctx) 670 require.NoError(t, err) 671 require.Len(t, qresults, 2) 672 require.Equal(t, 0, batch.Count()) 673 674 qresult1, ok := qresults[0].(*ctypes.ResultABCIQuery) 675 require.True(t, ok) 676 require.Equal(t, *qresult1, *q1) 677 qresult2, ok := qresults[1].(*ctypes.ResultABCIQuery) 678 require.True(t, ok) 679 require.Equal(t, *qresult2, *q2) 680 681 require.Equal(t, qresult1.Response.Key, k1) 682 require.Equal(t, qresult2.Response.Key, k2) 683 require.Equal(t, qresult1.Response.Value, v1) 684 require.Equal(t, qresult2.Response.Value, v2) 685 } 686 687 func TestBatchedJSONRPCCallsCancellation(t *testing.T) { 688 c := getHTTPClient() 689 _, _, tx1 := MakeTxKV() 690 _, _, tx2 := MakeTxKV() 691 692 batch := c.NewBatch() 693 _, err := batch.BroadcastTxCommit(context.Background(), tx1) 694 require.NoError(t, err) 695 _, err = batch.BroadcastTxCommit(context.Background(), tx2) 696 require.NoError(t, err) 697 // we should have 2 requests waiting 698 require.Equal(t, 2, batch.Count()) 699 // we want to make sure we cleared 2 pending requests 700 require.Equal(t, 2, batch.Clear()) 701 // now there should be no batched requests 702 require.Equal(t, 0, batch.Count()) 703 } 704 705 func TestSendingEmptyRequestBatch(t *testing.T) { 706 c := getHTTPClient() 707 batch := c.NewBatch() 708 _, err := batch.Send(ctx) 709 require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error") 710 } 711 712 func TestClearingEmptyRequestBatch(t *testing.T) { 713 c := getHTTPClient() 714 batch := c.NewBatch() 715 require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result") 716 } 717 718 func TestConcurrentJSONRPCBatching(t *testing.T) { 719 var wg sync.WaitGroup 720 c := getHTTPClient() 721 for i := 0; i < 50; i++ { 722 wg.Add(1) 723 go func() { 724 defer wg.Done() 725 testBatchedJSONRPCCalls(t, c) 726 }() 727 } 728 wg.Wait() 729 }