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.

694 lines
20 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 TestCheckTx(t *testing.T) {
  336. mempool := node.Mempool()
  337. for _, c := range GetClients() {
  338. _, _, tx := MakeTxKV()
  339. res, err := c.CheckTx(tx)
  340. require.NoError(t, err)
  341. assert.Equal(t, abci.CodeTypeOK, res.Code)
  342. assert.Equal(t, 0, mempool.Size(), "mempool must be empty")
  343. }
  344. }
  345. func TestTx(t *testing.T) {
  346. // first we broadcast a tx
  347. c := getHTTPClient()
  348. _, _, tx := MakeTxKV()
  349. bres, err := c.BroadcastTxCommit(tx)
  350. require.Nil(t, err, "%+v", err)
  351. txHeight := bres.Height
  352. txHash := bres.Hash
  353. anotherTxHash := types.Tx("a different tx").Hash()
  354. cases := []struct {
  355. valid bool
  356. prove bool
  357. hash []byte
  358. }{
  359. // only valid if correct hash provided
  360. {true, false, txHash},
  361. {true, true, txHash},
  362. {false, false, anotherTxHash},
  363. {false, true, anotherTxHash},
  364. {false, false, nil},
  365. {false, true, nil},
  366. }
  367. for i, c := range GetClients() {
  368. for j, tc := range cases {
  369. t.Logf("client %d, case %d", i, j)
  370. // now we query for the tx.
  371. // since there's only one tx, we know index=0.
  372. ptx, err := c.Tx(tc.hash, tc.prove)
  373. if !tc.valid {
  374. require.NotNil(t, err)
  375. } else {
  376. require.Nil(t, err, "%+v", err)
  377. assert.EqualValues(t, txHeight, ptx.Height)
  378. assert.EqualValues(t, tx, ptx.Tx)
  379. assert.Zero(t, ptx.Index)
  380. assert.True(t, ptx.TxResult.IsOK())
  381. assert.EqualValues(t, txHash, ptx.Hash)
  382. // time to verify the proof
  383. proof := ptx.Proof
  384. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  385. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  386. }
  387. }
  388. }
  389. }
  390. }
  391. func TestTxSearchWithTimeout(t *testing.T) {
  392. // Get a client with a time-out of 10 secs.
  393. timeoutClient := getHTTPClientWithTimeout(10)
  394. _, _, tx := MakeTxKV()
  395. _, err := timeoutClient.BroadcastTxCommit(tx)
  396. require.NoError(t, err)
  397. // query using a compositeKey (see kvstore application)
  398. result, err := timeoutClient.TxSearch("app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
  399. require.Nil(t, err)
  400. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  401. }
  402. func TestTxSearch(t *testing.T) {
  403. c := getHTTPClient()
  404. // first we broadcast a few txs
  405. for i := 0; i < 10; i++ {
  406. _, _, tx := MakeTxKV()
  407. _, err := c.BroadcastTxCommit(tx)
  408. require.NoError(t, err)
  409. }
  410. // since we're not using an isolated test server, we'll have lingering transactions
  411. // from other tests as well
  412. result, err := c.TxSearch("tx.height >= 0", true, nil, nil, "asc")
  413. require.NoError(t, err)
  414. txCount := len(result.Txs)
  415. // pick out the last tx to have something to search for in tests
  416. find := result.Txs[len(result.Txs)-1]
  417. anotherTxHash := types.Tx("a different tx").Hash()
  418. for i, c := range GetClients() {
  419. t.Logf("client %d", i)
  420. // now we query for the tx.
  421. result, err := c.TxSearch(fmt.Sprintf("tx.hash='%v'", find.Hash), true, nil, nil, "asc")
  422. require.Nil(t, err)
  423. require.Len(t, result.Txs, 1)
  424. require.Equal(t, find.Hash, result.Txs[0].Hash)
  425. ptx := result.Txs[0]
  426. assert.EqualValues(t, find.Height, ptx.Height)
  427. assert.EqualValues(t, find.Tx, ptx.Tx)
  428. assert.Zero(t, ptx.Index)
  429. assert.True(t, ptx.TxResult.IsOK())
  430. assert.EqualValues(t, find.Hash, ptx.Hash)
  431. // time to verify the proof
  432. if assert.EqualValues(t, find.Tx, ptx.Proof.Data) {
  433. assert.NoError(t, ptx.Proof.Proof.Verify(ptx.Proof.RootHash, find.Hash))
  434. }
  435. // query by height
  436. result, err = c.TxSearch(fmt.Sprintf("tx.height=%d", find.Height), true, nil, nil, "asc")
  437. require.Nil(t, err)
  438. require.Len(t, result.Txs, 1)
  439. // query for non existing tx
  440. result, err = c.TxSearch(fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, nil, nil, "asc")
  441. require.Nil(t, err)
  442. require.Len(t, result.Txs, 0)
  443. // query using a compositeKey (see kvstore application)
  444. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
  445. require.Nil(t, err)
  446. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  447. // query using an index key
  448. result, err = c.TxSearch("app.index_key='index is working'", false, nil, nil, "asc")
  449. require.Nil(t, err)
  450. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  451. // query using an noindex key
  452. result, err = c.TxSearch("app.noindex_key='index is working'", false, nil, nil, "asc")
  453. require.Nil(t, err)
  454. require.Equal(t, len(result.Txs), 0, "expected a lot of transactions")
  455. // query using a compositeKey (see kvstore application) and height
  456. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, nil, nil, "asc")
  457. require.Nil(t, err)
  458. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  459. // query a non existing tx with page 1 and txsPerPage 1
  460. perPage := 1
  461. result, err = c.TxSearch("app.creator='Cosmoshi Neetowoko'", true, nil, &perPage, "asc")
  462. require.Nil(t, err)
  463. require.Len(t, result.Txs, 0)
  464. // check sorting
  465. result, err = c.TxSearch("tx.height >= 1", false, nil, nil, "asc")
  466. require.Nil(t, err)
  467. for k := 0; k < len(result.Txs)-1; k++ {
  468. require.LessOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
  469. require.LessOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
  470. }
  471. result, err = c.TxSearch("tx.height >= 1", false, nil, nil, "desc")
  472. require.Nil(t, err)
  473. for k := 0; k < len(result.Txs)-1; k++ {
  474. require.GreaterOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
  475. require.GreaterOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
  476. }
  477. // check pagination
  478. perPage = 3
  479. var (
  480. seen = map[int64]bool{}
  481. maxHeight int64
  482. pages = int(math.Ceil(float64(txCount) / float64(perPage)))
  483. )
  484. for page := 1; page <= pages; page++ {
  485. page := page
  486. result, err := c.TxSearch("tx.height >= 1", false, &page, &perPage, "asc")
  487. require.NoError(t, err)
  488. if page < pages {
  489. require.Len(t, result.Txs, perPage)
  490. } else {
  491. require.LessOrEqual(t, len(result.Txs), perPage)
  492. }
  493. require.Equal(t, txCount, result.TotalCount)
  494. for _, tx := range result.Txs {
  495. require.False(t, seen[tx.Height],
  496. "Found duplicate height %v in page %v", tx.Height, page)
  497. require.Greater(t, tx.Height, maxHeight,
  498. "Found decreasing height %v (max seen %v) in page %v", tx.Height, maxHeight, page)
  499. seen[tx.Height] = true
  500. maxHeight = tx.Height
  501. }
  502. }
  503. require.Len(t, seen, txCount)
  504. }
  505. }
  506. func TestBatchedJSONRPCCalls(t *testing.T) {
  507. c := getHTTPClient()
  508. testBatchedJSONRPCCalls(t, c)
  509. }
  510. func testBatchedJSONRPCCalls(t *testing.T, c *rpchttp.HTTP) {
  511. k1, v1, tx1 := MakeTxKV()
  512. k2, v2, tx2 := MakeTxKV()
  513. batch := c.NewBatch()
  514. r1, err := batch.BroadcastTxCommit(tx1)
  515. require.NoError(t, err)
  516. r2, err := batch.BroadcastTxCommit(tx2)
  517. require.NoError(t, err)
  518. require.Equal(t, 2, batch.Count())
  519. bresults, err := batch.Send()
  520. require.NoError(t, err)
  521. require.Len(t, bresults, 2)
  522. require.Equal(t, 0, batch.Count())
  523. bresult1, ok := bresults[0].(*ctypes.ResultBroadcastTxCommit)
  524. require.True(t, ok)
  525. require.Equal(t, *bresult1, *r1)
  526. bresult2, ok := bresults[1].(*ctypes.ResultBroadcastTxCommit)
  527. require.True(t, ok)
  528. require.Equal(t, *bresult2, *r2)
  529. apph := tmmath.MaxInt64(bresult1.Height, bresult2.Height) + 1
  530. client.WaitForHeight(c, apph, nil)
  531. q1, err := batch.ABCIQuery("/key", k1)
  532. require.NoError(t, err)
  533. q2, err := batch.ABCIQuery("/key", k2)
  534. require.NoError(t, err)
  535. require.Equal(t, 2, batch.Count())
  536. qresults, err := batch.Send()
  537. require.NoError(t, err)
  538. require.Len(t, qresults, 2)
  539. require.Equal(t, 0, batch.Count())
  540. qresult1, ok := qresults[0].(*ctypes.ResultABCIQuery)
  541. require.True(t, ok)
  542. require.Equal(t, *qresult1, *q1)
  543. qresult2, ok := qresults[1].(*ctypes.ResultABCIQuery)
  544. require.True(t, ok)
  545. require.Equal(t, *qresult2, *q2)
  546. require.Equal(t, qresult1.Response.Key, k1)
  547. require.Equal(t, qresult2.Response.Key, k2)
  548. require.Equal(t, qresult1.Response.Value, v1)
  549. require.Equal(t, qresult2.Response.Value, v2)
  550. }
  551. func TestBatchedJSONRPCCallsCancellation(t *testing.T) {
  552. c := getHTTPClient()
  553. _, _, tx1 := MakeTxKV()
  554. _, _, tx2 := MakeTxKV()
  555. batch := c.NewBatch()
  556. _, err := batch.BroadcastTxCommit(tx1)
  557. require.NoError(t, err)
  558. _, err = batch.BroadcastTxCommit(tx2)
  559. require.NoError(t, err)
  560. // we should have 2 requests waiting
  561. require.Equal(t, 2, batch.Count())
  562. // we want to make sure we cleared 2 pending requests
  563. require.Equal(t, 2, batch.Clear())
  564. // now there should be no batched requests
  565. require.Equal(t, 0, batch.Count())
  566. }
  567. func TestSendingEmptyRequestBatch(t *testing.T) {
  568. c := getHTTPClient()
  569. batch := c.NewBatch()
  570. _, err := batch.Send()
  571. require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error")
  572. }
  573. func TestClearingEmptyRequestBatch(t *testing.T) {
  574. c := getHTTPClient()
  575. batch := c.NewBatch()
  576. require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result")
  577. }
  578. func TestConcurrentJSONRPCBatching(t *testing.T) {
  579. var wg sync.WaitGroup
  580. c := getHTTPClient()
  581. for i := 0; i < 50; i++ {
  582. wg.Add(1)
  583. go func() {
  584. defer wg.Done()
  585. testBatchedJSONRPCCalls(t, c)
  586. }()
  587. }
  588. wg.Wait()
  589. }