You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

687 lines
19 KiB

rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
8 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
rpc/test: wait for mempool CheckTx callback (#4908) Fixes race conditions causing the following test failures: ``` === RUN TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs(nil) Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} -} +(types.Txs) <nil> Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:342: Error Trace: rpc_test.go:342 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:343: Error Trace: rpc_test.go:343 Error: Not equal: expected: 1 actual : 0 Test: TestUnconfirmedTxs TestUnconfirmedTxs: rpc_test.go:345: Error Trace: rpc_test.go:345 Error: Not equal: expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}} actual : types.Txs{} Diff: --- Expected +++ Actual @@ -1,3 +1,2 @@ -(types.Txs) (len=1) { - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958} +(types.Txs) { } Test: TestUnconfirmedTxs --- FAIL: TestUnconfirmedTxs (0.20s) === RUN TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:364: Error Trace: rpc_test.go:364 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs TestNumUnconfirmedTxs: rpc_test.go:365: Error Trace: rpc_test.go:365 Error: Not equal: expected: 1 actual : 0 Test: TestNumUnconfirmedTxs --- FAIL: TestNumUnconfirmedTxs (0.09s) ```
5 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
  1. package client_test
  2. import (
  3. "fmt"
  4. "math"
  5. "net/http"
  6. "strings"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. abci "github.com/tendermint/tendermint/abci/types"
  13. "github.com/tendermint/tendermint/libs/log"
  14. tmmath "github.com/tendermint/tendermint/libs/math"
  15. mempl "github.com/tendermint/tendermint/mempool"
  16. "github.com/tendermint/tendermint/rpc/client"
  17. rpchttp "github.com/tendermint/tendermint/rpc/client/http"
  18. rpclocal "github.com/tendermint/tendermint/rpc/client/local"
  19. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  20. rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
  21. rpctest "github.com/tendermint/tendermint/rpc/test"
  22. "github.com/tendermint/tendermint/types"
  23. )
  24. func getHTTPClient() *rpchttp.HTTP {
  25. rpcAddr := rpctest.GetConfig().RPC.ListenAddress
  26. c, err := rpchttp.New(rpcAddr, "/websocket")
  27. if err != nil {
  28. panic(err)
  29. }
  30. c.SetLogger(log.TestingLogger())
  31. return c
  32. }
  33. func getHTTPClientWithTimeout(timeout uint) *rpchttp.HTTP {
  34. rpcAddr := rpctest.GetConfig().RPC.ListenAddress
  35. c, err := rpchttp.NewWithTimeout(rpcAddr, "/websocket", timeout)
  36. if err != nil {
  37. panic(err)
  38. }
  39. c.SetLogger(log.TestingLogger())
  40. return c
  41. }
  42. func getLocalClient() *rpclocal.Local {
  43. return rpclocal.New(node)
  44. }
  45. // GetClients returns a slice of clients for table-driven tests
  46. func GetClients() []client.Client {
  47. return []client.Client{
  48. getHTTPClient(),
  49. getLocalClient(),
  50. }
  51. }
  52. func TestNilCustomHTTPClient(t *testing.T) {
  53. require.Panics(t, func() {
  54. _, _ = rpchttp.NewWithClient("http://example.com", "/websocket", nil)
  55. })
  56. require.Panics(t, func() {
  57. _, _ = rpcclient.NewWithHTTPClient("http://example.com", nil)
  58. })
  59. }
  60. func TestCustomHTTPClient(t *testing.T) {
  61. remote := rpctest.GetConfig().RPC.ListenAddress
  62. c, err := rpchttp.NewWithClient(remote, "/websocket", http.DefaultClient)
  63. require.Nil(t, err)
  64. status, err := c.Status()
  65. require.NoError(t, err)
  66. require.NotNil(t, status)
  67. }
  68. func TestCorsEnabled(t *testing.T) {
  69. origin := rpctest.GetConfig().RPC.CORSAllowedOrigins[0]
  70. remote := strings.Replace(rpctest.GetConfig().RPC.ListenAddress, "tcp", "http", -1)
  71. req, err := http.NewRequest("GET", remote, nil)
  72. require.Nil(t, err, "%+v", err)
  73. req.Header.Set("Origin", origin)
  74. c := &http.Client{}
  75. resp, err := c.Do(req)
  76. require.Nil(t, err, "%+v", err)
  77. defer resp.Body.Close()
  78. assert.Equal(t, resp.Header.Get("Access-Control-Allow-Origin"), origin)
  79. }
  80. // Make sure status is correct (we connect properly)
  81. func TestStatus(t *testing.T) {
  82. for i, c := range GetClients() {
  83. moniker := rpctest.GetConfig().Moniker
  84. status, err := c.Status()
  85. require.Nil(t, err, "%d: %+v", i, err)
  86. assert.Equal(t, moniker, status.NodeInfo.Moniker)
  87. }
  88. }
  89. // Make sure info is correct (we connect properly)
  90. func TestInfo(t *testing.T) {
  91. for i, c := range GetClients() {
  92. // status, err := c.Status()
  93. // require.Nil(t, err, "%+v", err)
  94. info, err := c.ABCIInfo()
  95. require.Nil(t, err, "%d: %+v", i, err)
  96. // TODO: this is not correct - fix merkleeyes!
  97. // assert.EqualValues(t, status.SyncInfo.LatestBlockHeight, info.Response.LastBlockHeight)
  98. assert.True(t, strings.Contains(info.Response.Data, "size"))
  99. }
  100. }
  101. func TestNetInfo(t *testing.T) {
  102. for i, c := range GetClients() {
  103. nc, ok := c.(client.NetworkClient)
  104. require.True(t, ok, "%d", i)
  105. netinfo, err := nc.NetInfo()
  106. require.Nil(t, err, "%d: %+v", i, err)
  107. assert.True(t, netinfo.Listening)
  108. assert.Equal(t, 0, len(netinfo.Peers))
  109. }
  110. }
  111. func TestDumpConsensusState(t *testing.T) {
  112. for i, c := range GetClients() {
  113. // FIXME: fix server so it doesn't panic on invalid input
  114. nc, ok := c.(client.NetworkClient)
  115. require.True(t, ok, "%d", i)
  116. cons, err := nc.DumpConsensusState()
  117. require.Nil(t, err, "%d: %+v", i, err)
  118. assert.NotEmpty(t, cons.RoundState)
  119. assert.Empty(t, cons.Peers)
  120. }
  121. }
  122. func TestConsensusState(t *testing.T) {
  123. for i, c := range GetClients() {
  124. // FIXME: fix server so it doesn't panic on invalid input
  125. nc, ok := c.(client.NetworkClient)
  126. require.True(t, ok, "%d", i)
  127. cons, err := nc.ConsensusState()
  128. require.Nil(t, err, "%d: %+v", i, err)
  129. assert.NotEmpty(t, cons.RoundState)
  130. }
  131. }
  132. func TestHealth(t *testing.T) {
  133. for i, c := range GetClients() {
  134. nc, ok := c.(client.NetworkClient)
  135. require.True(t, ok, "%d", i)
  136. _, err := nc.Health()
  137. require.Nil(t, err, "%d: %+v", i, err)
  138. }
  139. }
  140. func TestGenesisAndValidators(t *testing.T) {
  141. for i, c := range GetClients() {
  142. // make sure this is the right genesis file
  143. gen, err := c.Genesis()
  144. require.Nil(t, err, "%d: %+v", i, err)
  145. // get the genesis validator
  146. require.Equal(t, 1, len(gen.Genesis.Validators))
  147. gval := gen.Genesis.Validators[0]
  148. // get the current validators
  149. h := int64(1)
  150. vals, err := c.Validators(&h, nil, nil)
  151. require.Nil(t, err, "%d: %+v", i, err)
  152. require.Equal(t, 1, len(vals.Validators))
  153. require.Equal(t, 1, vals.Count)
  154. require.Equal(t, 1, vals.Total)
  155. val := vals.Validators[0]
  156. // make sure the current set is also the genesis set
  157. assert.Equal(t, gval.Power, val.VotingPower)
  158. assert.Equal(t, gval.PubKey, val.PubKey)
  159. }
  160. }
  161. func TestABCIQuery(t *testing.T) {
  162. for i, c := range GetClients() {
  163. // write something
  164. k, v, tx := MakeTxKV()
  165. bres, err := c.BroadcastTxCommit(tx)
  166. require.Nil(t, err, "%d: %+v", i, err)
  167. apph := bres.Height + 1 // this is where the tx will be applied to the state
  168. // wait before querying
  169. client.WaitForHeight(c, apph, nil)
  170. res, err := c.ABCIQuery("/key", k)
  171. qres := res.Response
  172. if assert.Nil(t, err) && assert.True(t, qres.IsOK()) {
  173. assert.EqualValues(t, v, qres.Value)
  174. }
  175. }
  176. }
  177. // Make some app checks
  178. func TestAppCalls(t *testing.T) {
  179. assert, require := assert.New(t), require.New(t)
  180. for i, c := range GetClients() {
  181. // get an offset of height to avoid racing and guessing
  182. s, err := c.Status()
  183. require.NoError(err)
  184. // sh is start height or status height
  185. sh := s.SyncInfo.LatestBlockHeight
  186. // look for the future
  187. h := sh + 20
  188. _, err = c.Block(&h)
  189. require.Error(err) // no block yet
  190. // write something
  191. k, v, tx := MakeTxKV()
  192. bres, err := c.BroadcastTxCommit(tx)
  193. require.NoError(err)
  194. require.True(bres.DeliverTx.IsOK())
  195. txh := bres.Height
  196. apph := txh + 1 // this is where the tx will be applied to the state
  197. // wait before querying
  198. err = client.WaitForHeight(c, apph, nil)
  199. require.NoError(err)
  200. _qres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Prove: false})
  201. require.NoError(err)
  202. qres := _qres.Response
  203. if assert.True(qres.IsOK()) {
  204. assert.Equal(k, qres.Key)
  205. assert.EqualValues(v, qres.Value)
  206. }
  207. // make sure we can lookup the tx with proof
  208. ptx, err := c.Tx(bres.Hash, true)
  209. require.NoError(err)
  210. assert.EqualValues(txh, ptx.Height)
  211. assert.EqualValues(tx, ptx.Tx)
  212. // and we can even check the block is added
  213. block, err := c.Block(&apph)
  214. require.NoError(err)
  215. appHash := block.Block.Header.AppHash
  216. assert.True(len(appHash) > 0)
  217. assert.EqualValues(apph, block.Block.Header.Height)
  218. blockByHash, err := c.BlockByHash(block.BlockID.Hash)
  219. require.NoError(err)
  220. require.Equal(block, blockByHash)
  221. // now check the results
  222. blockResults, err := c.BlockResults(&txh)
  223. require.Nil(err, "%d: %+v", i, err)
  224. assert.Equal(txh, blockResults.Height)
  225. if assert.Equal(1, len(blockResults.TxsResults)) {
  226. // check success code
  227. assert.EqualValues(0, blockResults.TxsResults[0].Code)
  228. }
  229. // check blockchain info, now that we know there is info
  230. info, err := c.BlockchainInfo(apph, apph)
  231. require.NoError(err)
  232. assert.True(info.LastHeight >= apph)
  233. if assert.Equal(1, len(info.BlockMetas)) {
  234. lastMeta := info.BlockMetas[0]
  235. assert.EqualValues(apph, lastMeta.Header.Height)
  236. blockData := block.Block
  237. assert.Equal(blockData.Header.AppHash, lastMeta.Header.AppHash)
  238. assert.Equal(block.BlockID, lastMeta.BlockID)
  239. }
  240. // and get the corresponding commit with the same apphash
  241. commit, err := c.Commit(&apph)
  242. require.NoError(err)
  243. cappHash := commit.Header.AppHash
  244. assert.Equal(appHash, cappHash)
  245. assert.NotNil(commit.Commit)
  246. // compare the commits (note Commit(2) has commit from Block(3))
  247. h = apph - 1
  248. commit2, err := c.Commit(&h)
  249. require.NoError(err)
  250. assert.Equal(block.Block.LastCommit, commit2.Commit)
  251. // and we got a proof that works!
  252. _pres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Prove: true})
  253. require.NoError(err)
  254. pres := _pres.Response
  255. assert.True(pres.IsOK())
  256. // XXX Test proof
  257. }
  258. }
  259. func TestBroadcastTxSync(t *testing.T) {
  260. require := require.New(t)
  261. // TODO (melekes): use mempool which is set on RPC rather than getting it from node
  262. mempool := node.Mempool()
  263. initMempoolSize := mempool.Size()
  264. for i, c := range GetClients() {
  265. _, _, tx := MakeTxKV()
  266. bres, err := c.BroadcastTxSync(tx)
  267. require.Nil(err, "%d: %+v", i, err)
  268. require.Equal(bres.Code, abci.CodeTypeOK) // FIXME
  269. require.Equal(initMempoolSize+1, mempool.Size())
  270. txs := mempool.ReapMaxTxs(len(tx))
  271. require.EqualValues(tx, txs[0])
  272. mempool.Flush()
  273. }
  274. }
  275. func TestBroadcastTxCommit(t *testing.T) {
  276. require := require.New(t)
  277. mempool := node.Mempool()
  278. for i, c := range GetClients() {
  279. _, _, tx := MakeTxKV()
  280. bres, err := c.BroadcastTxCommit(tx)
  281. require.Nil(err, "%d: %+v", i, err)
  282. require.True(bres.CheckTx.IsOK())
  283. require.True(bres.DeliverTx.IsOK())
  284. require.Equal(0, mempool.Size())
  285. }
  286. }
  287. func TestUnconfirmedTxs(t *testing.T) {
  288. _, _, tx := MakeTxKV()
  289. ch := make(chan *abci.Response, 1)
  290. mempool := node.Mempool()
  291. err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
  292. require.NoError(t, err)
  293. // wait for tx to arrive in mempoool.
  294. select {
  295. case <-ch:
  296. case <-time.After(5 * time.Second):
  297. t.Error("Timed out waiting for CheckTx callback")
  298. }
  299. for _, c := range GetClients() {
  300. mc := c.(client.MempoolClient)
  301. limit := 1
  302. res, err := mc.UnconfirmedTxs(&limit)
  303. require.NoError(t, err)
  304. assert.Equal(t, 1, res.Count)
  305. assert.Equal(t, 1, res.Total)
  306. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  307. assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs))
  308. }
  309. mempool.Flush()
  310. }
  311. func TestNumUnconfirmedTxs(t *testing.T) {
  312. _, _, tx := MakeTxKV()
  313. ch := make(chan *abci.Response, 1)
  314. mempool := node.Mempool()
  315. err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
  316. require.NoError(t, err)
  317. // wait for tx to arrive in mempoool.
  318. select {
  319. case <-ch:
  320. case <-time.After(5 * time.Second):
  321. t.Error("Timed out waiting for CheckTx callback")
  322. }
  323. mempoolSize := mempool.Size()
  324. for i, c := range GetClients() {
  325. mc, ok := c.(client.MempoolClient)
  326. require.True(t, ok, "%d", i)
  327. res, err := mc.NumUnconfirmedTxs()
  328. require.Nil(t, err, "%d: %+v", i, err)
  329. assert.Equal(t, mempoolSize, res.Count)
  330. assert.Equal(t, mempoolSize, res.Total)
  331. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  332. }
  333. mempool.Flush()
  334. }
  335. func TestTx(t *testing.T) {
  336. // first we broadcast a tx
  337. c := getHTTPClient()
  338. _, _, tx := MakeTxKV()
  339. bres, err := c.BroadcastTxCommit(tx)
  340. require.Nil(t, err, "%+v", err)
  341. txHeight := bres.Height
  342. txHash := bres.Hash
  343. anotherTxHash := types.Tx("a different tx").Hash()
  344. cases := []struct {
  345. valid bool
  346. prove bool
  347. hash []byte
  348. }{
  349. // only valid if correct hash provided
  350. {true, false, txHash},
  351. {true, true, txHash},
  352. {false, false, anotherTxHash},
  353. {false, true, anotherTxHash},
  354. {false, false, nil},
  355. {false, true, nil},
  356. }
  357. for i, c := range GetClients() {
  358. for j, tc := range cases {
  359. t.Logf("client %d, case %d", i, j)
  360. // now we query for the tx.
  361. // since there's only one tx, we know index=0.
  362. ptx, err := c.Tx(tc.hash, tc.prove)
  363. if !tc.valid {
  364. require.NotNil(t, err)
  365. } else {
  366. require.Nil(t, err, "%+v", err)
  367. assert.EqualValues(t, txHeight, ptx.Height)
  368. assert.EqualValues(t, tx, ptx.Tx)
  369. assert.Zero(t, ptx.Index)
  370. assert.True(t, ptx.TxResult.IsOK())
  371. assert.EqualValues(t, txHash, ptx.Hash)
  372. // time to verify the proof
  373. proof := ptx.Proof
  374. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  375. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  376. }
  377. }
  378. }
  379. }
  380. }
  381. func TestTxSearchWithTimeout(t *testing.T) {
  382. // Get a client with a time-out of 10 secs.
  383. timeoutClient := getHTTPClientWithTimeout(10)
  384. // query using a compositeKey (see kvstore application)
  385. result, err := timeoutClient.TxSearch("app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
  386. require.Nil(t, err)
  387. if len(result.Txs) == 0 {
  388. t.Fatal("expected a lot of transactions")
  389. }
  390. }
  391. func TestTxSearch(t *testing.T) {
  392. c := getHTTPClient()
  393. // first we broadcast a few txs
  394. for i := 0; i < 10; i++ {
  395. _, _, tx := MakeTxKV()
  396. _, err := c.BroadcastTxCommit(tx)
  397. require.NoError(t, err)
  398. }
  399. // since we're not using an isolated test server, we'll have lingering transactions
  400. // from other tests as well
  401. result, err := c.TxSearch("tx.height >= 0", true, nil, nil, "asc")
  402. require.NoError(t, err)
  403. txCount := len(result.Txs)
  404. // pick out the last tx to have something to search for in tests
  405. find := result.Txs[len(result.Txs)-1]
  406. anotherTxHash := types.Tx("a different tx").Hash()
  407. for i, c := range GetClients() {
  408. t.Logf("client %d", i)
  409. // now we query for the tx.
  410. result, err := c.TxSearch(fmt.Sprintf("tx.hash='%v'", find.Hash), true, nil, nil, "asc")
  411. require.Nil(t, err)
  412. require.Len(t, result.Txs, 1)
  413. require.Equal(t, find.Hash, result.Txs[0].Hash)
  414. ptx := result.Txs[0]
  415. assert.EqualValues(t, find.Height, ptx.Height)
  416. assert.EqualValues(t, find.Tx, ptx.Tx)
  417. assert.Zero(t, ptx.Index)
  418. assert.True(t, ptx.TxResult.IsOK())
  419. assert.EqualValues(t, find.Hash, ptx.Hash)
  420. // time to verify the proof
  421. if assert.EqualValues(t, find.Tx, ptx.Proof.Data) {
  422. assert.NoError(t, ptx.Proof.Proof.Verify(ptx.Proof.RootHash, find.Hash))
  423. }
  424. // query by height
  425. result, err = c.TxSearch(fmt.Sprintf("tx.height=%d", find.Height), true, nil, nil, "asc")
  426. require.Nil(t, err)
  427. require.Len(t, result.Txs, 1)
  428. // query for non existing tx
  429. result, err = c.TxSearch(fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, nil, nil, "asc")
  430. require.Nil(t, err)
  431. require.Len(t, result.Txs, 0)
  432. // query using a compositeKey (see kvstore application)
  433. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
  434. require.Nil(t, err)
  435. if len(result.Txs) == 0 {
  436. t.Fatal("expected a lot of transactions")
  437. }
  438. // query using an index key
  439. result, err = c.TxSearch("app.index_key='index is working'", false, nil, nil, "asc")
  440. require.Nil(t, err)
  441. if len(result.Txs) == 0 {
  442. t.Fatal("expected a lot of transactions")
  443. }
  444. // query using an noindex key
  445. result, err = c.TxSearch("app.noindex_key='index is working'", false, nil, nil, "asc")
  446. require.Nil(t, err)
  447. if len(result.Txs) != 0 {
  448. t.Fatal("expected no transaction")
  449. }
  450. // query using a compositeKey (see kvstore application) and height
  451. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, nil, nil, "asc")
  452. require.Nil(t, err)
  453. if len(result.Txs) == 0 {
  454. t.Fatal("expected a lot of transactions")
  455. }
  456. // query a non existing tx with page 1 and txsPerPage 1
  457. perPage := 1
  458. result, err = c.TxSearch("app.creator='Cosmoshi Neetowoko'", true, nil, &perPage, "asc")
  459. require.Nil(t, err)
  460. require.Len(t, result.Txs, 0)
  461. // check sorting
  462. result, err = c.TxSearch("tx.height >= 1", false, nil, nil, "asc")
  463. require.Nil(t, err)
  464. for k := 0; k < len(result.Txs)-1; k++ {
  465. require.LessOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
  466. require.LessOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
  467. }
  468. result, err = c.TxSearch("tx.height >= 1", false, nil, nil, "desc")
  469. require.Nil(t, err)
  470. for k := 0; k < len(result.Txs)-1; k++ {
  471. require.GreaterOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
  472. require.GreaterOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
  473. }
  474. // check pagination
  475. perPage = 3
  476. var (
  477. seen = map[int64]bool{}
  478. maxHeight int64
  479. pages = int(math.Ceil(float64(txCount) / float64(perPage)))
  480. )
  481. for page := 1; page <= pages; page++ {
  482. page := page
  483. result, err = c.TxSearch("tx.height >= 1", false, &page, &perPage, "asc")
  484. require.NoError(t, err)
  485. if page < pages {
  486. require.Len(t, result.Txs, perPage)
  487. } else {
  488. require.LessOrEqual(t, len(result.Txs), perPage)
  489. }
  490. require.Equal(t, txCount, result.TotalCount)
  491. for _, tx := range result.Txs {
  492. require.False(t, seen[tx.Height],
  493. "Found duplicate height %v in page %v", tx.Height, page)
  494. require.Greater(t, tx.Height, maxHeight,
  495. "Found decreasing height %v (max seen %v) in page %v", tx.Height, maxHeight, page)
  496. seen[tx.Height] = true
  497. maxHeight = tx.Height
  498. }
  499. }
  500. require.Len(t, seen, txCount)
  501. }
  502. }
  503. func TestBatchedJSONRPCCalls(t *testing.T) {
  504. c := getHTTPClient()
  505. testBatchedJSONRPCCalls(t, c)
  506. }
  507. func testBatchedJSONRPCCalls(t *testing.T, c *rpchttp.HTTP) {
  508. k1, v1, tx1 := MakeTxKV()
  509. k2, v2, tx2 := MakeTxKV()
  510. batch := c.NewBatch()
  511. r1, err := batch.BroadcastTxCommit(tx1)
  512. require.NoError(t, err)
  513. r2, err := batch.BroadcastTxCommit(tx2)
  514. require.NoError(t, err)
  515. require.Equal(t, 2, batch.Count())
  516. bresults, err := batch.Send()
  517. require.NoError(t, err)
  518. require.Len(t, bresults, 2)
  519. require.Equal(t, 0, batch.Count())
  520. bresult1, ok := bresults[0].(*ctypes.ResultBroadcastTxCommit)
  521. require.True(t, ok)
  522. require.Equal(t, *bresult1, *r1)
  523. bresult2, ok := bresults[1].(*ctypes.ResultBroadcastTxCommit)
  524. require.True(t, ok)
  525. require.Equal(t, *bresult2, *r2)
  526. apph := tmmath.MaxInt64(bresult1.Height, bresult2.Height) + 1
  527. client.WaitForHeight(c, apph, nil)
  528. q1, err := batch.ABCIQuery("/key", k1)
  529. require.NoError(t, err)
  530. q2, err := batch.ABCIQuery("/key", k2)
  531. require.NoError(t, err)
  532. require.Equal(t, 2, batch.Count())
  533. qresults, err := batch.Send()
  534. require.NoError(t, err)
  535. require.Len(t, qresults, 2)
  536. require.Equal(t, 0, batch.Count())
  537. qresult1, ok := qresults[0].(*ctypes.ResultABCIQuery)
  538. require.True(t, ok)
  539. require.Equal(t, *qresult1, *q1)
  540. qresult2, ok := qresults[1].(*ctypes.ResultABCIQuery)
  541. require.True(t, ok)
  542. require.Equal(t, *qresult2, *q2)
  543. require.Equal(t, qresult1.Response.Key, k1)
  544. require.Equal(t, qresult2.Response.Key, k2)
  545. require.Equal(t, qresult1.Response.Value, v1)
  546. require.Equal(t, qresult2.Response.Value, v2)
  547. }
  548. func TestBatchedJSONRPCCallsCancellation(t *testing.T) {
  549. c := getHTTPClient()
  550. _, _, tx1 := MakeTxKV()
  551. _, _, tx2 := MakeTxKV()
  552. batch := c.NewBatch()
  553. _, err := batch.BroadcastTxCommit(tx1)
  554. require.NoError(t, err)
  555. _, err = batch.BroadcastTxCommit(tx2)
  556. require.NoError(t, err)
  557. // we should have 2 requests waiting
  558. require.Equal(t, 2, batch.Count())
  559. // we want to make sure we cleared 2 pending requests
  560. require.Equal(t, 2, batch.Clear())
  561. // now there should be no batched requests
  562. require.Equal(t, 0, batch.Count())
  563. }
  564. func TestSendingEmptyRequestBatch(t *testing.T) {
  565. c := getHTTPClient()
  566. batch := c.NewBatch()
  567. _, err := batch.Send()
  568. require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error")
  569. }
  570. func TestClearingEmptyRequestBatch(t *testing.T) {
  571. c := getHTTPClient()
  572. batch := c.NewBatch()
  573. require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result")
  574. }
  575. func TestConcurrentJSONRPCBatching(t *testing.T) {
  576. var wg sync.WaitGroup
  577. c := getHTTPClient()
  578. for i := 0; i < 50; i++ {
  579. wg.Add(1)
  580. go func() {
  581. defer wg.Done()
  582. testBatchedJSONRPCCalls(t, c)
  583. }()
  584. }
  585. wg.Wait()
  586. }