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.

889 lines
28 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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: 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
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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "fmt"
  7. "math"
  8. "net/http"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "time"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. abci "github.com/tendermint/tendermint/abci/types"
  16. "github.com/tendermint/tendermint/config"
  17. "github.com/tendermint/tendermint/crypto/ed25519"
  18. "github.com/tendermint/tendermint/crypto/encoding"
  19. "github.com/tendermint/tendermint/internal/mempool"
  20. tmjson "github.com/tendermint/tendermint/libs/json"
  21. "github.com/tendermint/tendermint/libs/log"
  22. tmmath "github.com/tendermint/tendermint/libs/math"
  23. "github.com/tendermint/tendermint/libs/service"
  24. "github.com/tendermint/tendermint/privval"
  25. "github.com/tendermint/tendermint/rpc/client"
  26. rpchttp "github.com/tendermint/tendermint/rpc/client/http"
  27. rpclocal "github.com/tendermint/tendermint/rpc/client/local"
  28. "github.com/tendermint/tendermint/rpc/coretypes"
  29. rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
  30. "github.com/tendermint/tendermint/types"
  31. )
  32. func getHTTPClient(t *testing.T, conf *config.Config) *rpchttp.HTTP {
  33. t.Helper()
  34. rpcAddr := conf.RPC.ListenAddress
  35. c, err := rpchttp.NewWithClient(rpcAddr, http.DefaultClient)
  36. require.NoError(t, err)
  37. c.Logger = log.TestingLogger()
  38. t.Cleanup(func() {
  39. if c.IsRunning() {
  40. require.NoError(t, c.Stop())
  41. }
  42. })
  43. return c
  44. }
  45. func getHTTPClientWithTimeout(t *testing.T, conf *config.Config, timeout time.Duration) *rpchttp.HTTP {
  46. t.Helper()
  47. rpcAddr := conf.RPC.ListenAddress
  48. http.DefaultClient.Timeout = timeout
  49. c, err := rpchttp.NewWithClient(rpcAddr, http.DefaultClient)
  50. require.NoError(t, err)
  51. c.Logger = log.TestingLogger()
  52. t.Cleanup(func() {
  53. http.DefaultClient.Timeout = 0
  54. if c.IsRunning() {
  55. require.NoError(t, c.Stop())
  56. }
  57. })
  58. return c
  59. }
  60. // GetClients returns a slice of clients for table-driven tests
  61. func GetClients(t *testing.T, ns service.Service, conf *config.Config) []client.Client {
  62. t.Helper()
  63. node, ok := ns.(rpclocal.NodeService)
  64. require.True(t, ok)
  65. ncl, err := rpclocal.New(node)
  66. require.NoError(t, err)
  67. return []client.Client{
  68. ncl,
  69. getHTTPClient(t, conf),
  70. }
  71. }
  72. func TestClientOperations(t *testing.T) {
  73. ctx, cancel := context.WithCancel(context.Background())
  74. defer cancel()
  75. _, conf := NodeSuite(t)
  76. t.Run("NilCustomHTTPClient", func(t *testing.T) {
  77. _, err := rpchttp.NewWithClient("http://example.com", nil)
  78. require.Error(t, err)
  79. _, err = rpcclient.NewWithHTTPClient("http://example.com", nil)
  80. require.Error(t, err)
  81. })
  82. t.Run("ParseInvalidAddress", func(t *testing.T) {
  83. // should remove trailing /
  84. invalidRemote := conf.RPC.ListenAddress + "/"
  85. _, err := rpchttp.New(invalidRemote)
  86. require.NoError(t, err)
  87. })
  88. t.Run("CustomHTTPClient", func(t *testing.T) {
  89. remote := conf.RPC.ListenAddress
  90. c, err := rpchttp.NewWithClient(remote, http.DefaultClient)
  91. require.Nil(t, err)
  92. status, err := c.Status(ctx)
  93. require.NoError(t, err)
  94. require.NotNil(t, status)
  95. })
  96. t.Run("CorsEnabled", func(t *testing.T) {
  97. origin := conf.RPC.CORSAllowedOrigins[0]
  98. remote := strings.ReplaceAll(conf.RPC.ListenAddress, "tcp", "http")
  99. req, err := http.NewRequestWithContext(ctx, "GET", remote, nil)
  100. require.Nil(t, err, "%+v", err)
  101. req.Header.Set("Origin", origin)
  102. resp, err := http.DefaultClient.Do(req)
  103. require.Nil(t, err, "%+v", err)
  104. defer resp.Body.Close()
  105. assert.Equal(t, resp.Header.Get("Access-Control-Allow-Origin"), origin)
  106. })
  107. t.Run("Batching", func(t *testing.T) {
  108. t.Run("JSONRPCCalls", func(t *testing.T) {
  109. c := getHTTPClient(t, conf)
  110. testBatchedJSONRPCCalls(ctx, t, c)
  111. })
  112. t.Run("JSONRPCCallsCancellation", func(t *testing.T) {
  113. _, _, tx1 := MakeTxKV()
  114. _, _, tx2 := MakeTxKV()
  115. c := getHTTPClient(t, conf)
  116. batch := c.NewBatch()
  117. _, err := batch.BroadcastTxCommit(ctx, tx1)
  118. require.NoError(t, err)
  119. _, err = batch.BroadcastTxCommit(ctx, tx2)
  120. require.NoError(t, err)
  121. // we should have 2 requests waiting
  122. require.Equal(t, 2, batch.Count())
  123. // we want to make sure we cleared 2 pending requests
  124. require.Equal(t, 2, batch.Clear())
  125. // now there should be no batched requests
  126. require.Equal(t, 0, batch.Count())
  127. })
  128. t.Run("SendingEmptyRequest", func(t *testing.T) {
  129. c := getHTTPClient(t, conf)
  130. batch := c.NewBatch()
  131. _, err := batch.Send(ctx)
  132. require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error")
  133. })
  134. t.Run("ClearingEmptyRequest", func(t *testing.T) {
  135. c := getHTTPClient(t, conf)
  136. batch := c.NewBatch()
  137. require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result")
  138. })
  139. t.Run("ConcurrentJSONRPC", func(t *testing.T) {
  140. var wg sync.WaitGroup
  141. c := getHTTPClient(t, conf)
  142. for i := 0; i < 50; i++ {
  143. wg.Add(1)
  144. go func() {
  145. defer wg.Done()
  146. testBatchedJSONRPCCalls(ctx, t, c)
  147. }()
  148. }
  149. wg.Wait()
  150. })
  151. })
  152. t.Run("HTTPReturnsErrorIfClientIsNotRunning", func(t *testing.T) {
  153. c := getHTTPClientWithTimeout(t, conf, 100*time.Millisecond)
  154. // on Subscribe
  155. _, err := c.Subscribe(ctx, "TestHeaderEvents",
  156. types.QueryForEvent(types.EventNewBlockHeaderValue).String())
  157. assert.Error(t, err)
  158. // on Unsubscribe
  159. err = c.Unsubscribe(ctx, "TestHeaderEvents",
  160. types.QueryForEvent(types.EventNewBlockHeaderValue).String())
  161. assert.Error(t, err)
  162. // on UnsubscribeAll
  163. err = c.UnsubscribeAll(ctx, "TestHeaderEvents")
  164. assert.Error(t, err)
  165. })
  166. }
  167. // Make sure info is correct (we connect properly)
  168. func TestClientMethodCalls(t *testing.T) {
  169. ctx, cancel := context.WithCancel(context.Background())
  170. defer cancel()
  171. n, conf := NodeSuite(t)
  172. // for broadcast tx tests
  173. pool := getMempool(t, n)
  174. // for evidence tests
  175. pv, err := privval.LoadOrGenFilePV(conf.PrivValidator.KeyFile(), conf.PrivValidator.StateFile())
  176. require.NoError(t, err)
  177. for i, c := range GetClients(t, n, conf) {
  178. t.Run(fmt.Sprintf("%T", c), func(t *testing.T) {
  179. t.Run("Status", func(t *testing.T) {
  180. status, err := c.Status(ctx)
  181. require.Nil(t, err, "%d: %+v", i, err)
  182. assert.Equal(t, conf.Moniker, status.NodeInfo.Moniker)
  183. })
  184. t.Run("Info", func(t *testing.T) {
  185. info, err := c.ABCIInfo(ctx)
  186. require.NoError(t, err)
  187. status, err := c.Status(ctx)
  188. require.NoError(t, err)
  189. assert.GreaterOrEqual(t, status.SyncInfo.LatestBlockHeight, info.Response.LastBlockHeight)
  190. assert.True(t, strings.Contains(info.Response.Data, "size"))
  191. })
  192. t.Run("NetInfo", func(t *testing.T) {
  193. nc, ok := c.(client.NetworkClient)
  194. require.True(t, ok, "%d", i)
  195. netinfo, err := nc.NetInfo(ctx)
  196. require.Nil(t, err, "%d: %+v", i, err)
  197. assert.True(t, netinfo.Listening)
  198. assert.Equal(t, 0, len(netinfo.Peers))
  199. })
  200. t.Run("DumpConsensusState", func(t *testing.T) {
  201. // FIXME: fix server so it doesn't panic on invalid input
  202. nc, ok := c.(client.NetworkClient)
  203. require.True(t, ok, "%d", i)
  204. cons, err := nc.DumpConsensusState(ctx)
  205. require.Nil(t, err, "%d: %+v", i, err)
  206. assert.NotEmpty(t, cons.RoundState)
  207. assert.Empty(t, cons.Peers)
  208. })
  209. t.Run("ConsensusState", func(t *testing.T) {
  210. // FIXME: fix server so it doesn't panic on invalid input
  211. nc, ok := c.(client.NetworkClient)
  212. require.True(t, ok, "%d", i)
  213. cons, err := nc.ConsensusState(ctx)
  214. require.Nil(t, err, "%d: %+v", i, err)
  215. assert.NotEmpty(t, cons.RoundState)
  216. })
  217. t.Run("Health", func(t *testing.T) {
  218. nc, ok := c.(client.NetworkClient)
  219. require.True(t, ok, "%d", i)
  220. _, err := nc.Health(ctx)
  221. require.Nil(t, err, "%d: %+v", i, err)
  222. })
  223. t.Run("GenesisAndValidators", func(t *testing.T) {
  224. // make sure this is the right genesis file
  225. gen, err := c.Genesis(ctx)
  226. require.Nil(t, err, "%d: %+v", i, err)
  227. // get the genesis validator
  228. require.Equal(t, 1, len(gen.Genesis.Validators))
  229. gval := gen.Genesis.Validators[0]
  230. // get the current validators
  231. h := int64(1)
  232. vals, err := c.Validators(ctx, &h, nil, nil)
  233. require.Nil(t, err, "%d: %+v", i, err)
  234. require.Equal(t, 1, len(vals.Validators))
  235. require.Equal(t, 1, vals.Count)
  236. require.Equal(t, 1, vals.Total)
  237. val := vals.Validators[0]
  238. // make sure the current set is also the genesis set
  239. assert.Equal(t, gval.Power, val.VotingPower)
  240. assert.Equal(t, gval.PubKey, val.PubKey)
  241. })
  242. t.Run("GenesisChunked", func(t *testing.T) {
  243. first, err := c.GenesisChunked(ctx, 0)
  244. require.NoError(t, err)
  245. decoded := make([]string, 0, first.TotalChunks)
  246. for i := 0; i < first.TotalChunks; i++ {
  247. chunk, err := c.GenesisChunked(ctx, uint(i))
  248. require.NoError(t, err)
  249. data, err := base64.StdEncoding.DecodeString(chunk.Data)
  250. require.NoError(t, err)
  251. decoded = append(decoded, string(data))
  252. }
  253. doc := []byte(strings.Join(decoded, ""))
  254. var out types.GenesisDoc
  255. require.NoError(t, tmjson.Unmarshal(doc, &out),
  256. "first: %+v, doc: %s", first, string(doc))
  257. })
  258. t.Run("ABCIQuery", func(t *testing.T) {
  259. // write something
  260. k, v, tx := MakeTxKV()
  261. status, err := c.Status(ctx)
  262. require.NoError(t, err)
  263. _, err = c.BroadcastTxSync(ctx, tx)
  264. require.NoError(t, err, "%d: %+v", i, err)
  265. apph := status.SyncInfo.LatestBlockHeight + 2 // this is where the tx will be applied to the state
  266. // wait before querying
  267. err = client.WaitForHeight(c, apph, nil)
  268. require.NoError(t, err)
  269. res, err := c.ABCIQuery(ctx, "/key", k)
  270. qres := res.Response
  271. if assert.NoError(t, err) && assert.True(t, qres.IsOK()) {
  272. assert.EqualValues(t, v, qres.Value)
  273. }
  274. })
  275. t.Run("AppCalls", func(t *testing.T) {
  276. // get an offset of height to avoid racing and guessing
  277. s, err := c.Status(ctx)
  278. require.NoError(t, err)
  279. // sh is start height or status height
  280. sh := s.SyncInfo.LatestBlockHeight
  281. // look for the future
  282. h := sh + 20
  283. _, err = c.Block(ctx, &h)
  284. require.Error(t, err) // no block yet
  285. // write something
  286. k, v, tx := MakeTxKV()
  287. bres, err := c.BroadcastTxCommit(ctx, tx)
  288. require.NoError(t, err)
  289. require.True(t, bres.DeliverTx.IsOK())
  290. txh := bres.Height
  291. apph := txh + 1 // this is where the tx will be applied to the state
  292. // wait before querying
  293. err = client.WaitForHeight(c, apph, nil)
  294. require.NoError(t, err)
  295. _qres, err := c.ABCIQueryWithOptions(ctx, "/key", k, client.ABCIQueryOptions{Prove: false})
  296. require.NoError(t, err)
  297. qres := _qres.Response
  298. if assert.True(t, qres.IsOK()) {
  299. assert.Equal(t, k, qres.Key)
  300. assert.EqualValues(t, v, qres.Value)
  301. }
  302. // make sure we can lookup the tx with proof
  303. ptx, err := c.Tx(ctx, bres.Hash, true)
  304. require.NoError(t, err)
  305. assert.EqualValues(t, txh, ptx.Height)
  306. assert.EqualValues(t, tx, ptx.Tx)
  307. // and we can even check the block is added
  308. block, err := c.Block(ctx, &apph)
  309. require.NoError(t, err)
  310. appHash := block.Block.Header.AppHash
  311. assert.True(t, len(appHash) > 0)
  312. assert.EqualValues(t, apph, block.Block.Header.Height)
  313. blockByHash, err := c.BlockByHash(ctx, block.BlockID.Hash)
  314. require.NoError(t, err)
  315. require.Equal(t, block, blockByHash)
  316. // check that the header matches the block hash
  317. header, err := c.Header(ctx, &apph)
  318. require.NoError(t, err)
  319. require.Equal(t, block.Block.Header, *header.Header)
  320. headerByHash, err := c.HeaderByHash(ctx, block.BlockID.Hash)
  321. require.NoError(t, err)
  322. require.Equal(t, header, headerByHash)
  323. // now check the results
  324. blockResults, err := c.BlockResults(ctx, &txh)
  325. require.NoError(t, err, "%d: %+v", i, err)
  326. assert.Equal(t, txh, blockResults.Height)
  327. if assert.Equal(t, 1, len(blockResults.TxsResults)) {
  328. // check success code
  329. assert.EqualValues(t, 0, blockResults.TxsResults[0].Code)
  330. }
  331. // check blockchain info, now that we know there is info
  332. info, err := c.BlockchainInfo(ctx, apph, apph)
  333. require.NoError(t, err)
  334. assert.True(t, info.LastHeight >= apph)
  335. if assert.Equal(t, 1, len(info.BlockMetas)) {
  336. lastMeta := info.BlockMetas[0]
  337. assert.EqualValues(t, apph, lastMeta.Header.Height)
  338. blockData := block.Block
  339. assert.Equal(t, blockData.Header.AppHash, lastMeta.Header.AppHash)
  340. assert.Equal(t, block.BlockID, lastMeta.BlockID)
  341. }
  342. // and get the corresponding commit with the same apphash
  343. commit, err := c.Commit(ctx, &apph)
  344. require.NoError(t, err)
  345. cappHash := commit.Header.AppHash
  346. assert.Equal(t, appHash, cappHash)
  347. assert.NotNil(t, commit.Commit)
  348. // compare the commits (note Commit(2) has commit from Block(3))
  349. h = apph - 1
  350. commit2, err := c.Commit(ctx, &h)
  351. require.NoError(t, err)
  352. assert.Equal(t, block.Block.LastCommitHash, commit2.Commit.Hash())
  353. // and we got a proof that works!
  354. _pres, err := c.ABCIQueryWithOptions(ctx, "/key", k, client.ABCIQueryOptions{Prove: true})
  355. require.NoError(t, err)
  356. pres := _pres.Response
  357. assert.True(t, pres.IsOK())
  358. // XXX Test proof
  359. })
  360. t.Run("BlockchainInfo", func(t *testing.T) {
  361. err := client.WaitForHeight(c, 10, nil)
  362. require.NoError(t, err)
  363. res, err := c.BlockchainInfo(ctx, 0, 0)
  364. require.Nil(t, err, "%d: %+v", i, err)
  365. assert.True(t, res.LastHeight > 0)
  366. assert.True(t, len(res.BlockMetas) > 0)
  367. res, err = c.BlockchainInfo(ctx, 1, 1)
  368. require.Nil(t, err, "%d: %+v", i, err)
  369. assert.True(t, res.LastHeight > 0)
  370. assert.True(t, len(res.BlockMetas) == 1)
  371. res, err = c.BlockchainInfo(ctx, 1, 10000)
  372. require.Nil(t, err, "%d: %+v", i, err)
  373. assert.True(t, res.LastHeight > 0)
  374. assert.True(t, len(res.BlockMetas) < 100)
  375. for _, m := range res.BlockMetas {
  376. assert.NotNil(t, m)
  377. }
  378. res, err = c.BlockchainInfo(ctx, 10000, 1)
  379. require.NotNil(t, err)
  380. assert.Nil(t, res)
  381. assert.Contains(t, err.Error(), "can't be greater than max")
  382. })
  383. t.Run("BroadcastTxCommit", func(t *testing.T) {
  384. _, _, tx := MakeTxKV()
  385. bres, err := c.BroadcastTxCommit(ctx, tx)
  386. require.Nil(t, err, "%d: %+v", i, err)
  387. require.True(t, bres.CheckTx.IsOK())
  388. require.True(t, bres.DeliverTx.IsOK())
  389. require.Equal(t, 0, pool.Size())
  390. })
  391. t.Run("BroadcastTxSync", func(t *testing.T) {
  392. _, _, tx := MakeTxKV()
  393. initMempoolSize := pool.Size()
  394. bres, err := c.BroadcastTxSync(ctx, tx)
  395. require.Nil(t, err, "%d: %+v", i, err)
  396. require.Equal(t, bres.Code, abci.CodeTypeOK) // FIXME
  397. require.Equal(t, initMempoolSize+1, pool.Size())
  398. txs := pool.ReapMaxTxs(len(tx))
  399. require.EqualValues(t, tx, txs[0])
  400. pool.Flush()
  401. })
  402. t.Run("CheckTx", func(t *testing.T) {
  403. _, _, tx := MakeTxKV()
  404. res, err := c.CheckTx(ctx, tx)
  405. require.NoError(t, err)
  406. assert.Equal(t, abci.CodeTypeOK, res.Code)
  407. assert.Equal(t, 0, pool.Size(), "mempool must be empty")
  408. })
  409. t.Run("Events", func(t *testing.T) {
  410. // start for this test it if it wasn't already running
  411. if !c.IsRunning() {
  412. ctx, cancel := context.WithCancel(ctx)
  413. defer cancel()
  414. // if so, then we start it, listen, and stop it.
  415. err := c.Start(ctx)
  416. require.Nil(t, err)
  417. }
  418. t.Run("Header", func(t *testing.T) {
  419. evt, err := client.WaitForOneEvent(c, types.EventNewBlockHeaderValue, waitForEventTimeout)
  420. require.Nil(t, err, "%d: %+v", i, err)
  421. _, ok := evt.(types.EventDataNewBlockHeader)
  422. require.True(t, ok, "%d: %#v", i, evt)
  423. // TODO: more checks...
  424. })
  425. t.Run("Block", func(t *testing.T) {
  426. const subscriber = "TestBlockEvents"
  427. eventCh, err := c.Subscribe(ctx, subscriber, types.QueryForEvent(types.EventNewBlockValue).String())
  428. require.NoError(t, err)
  429. t.Cleanup(func() {
  430. if err := c.UnsubscribeAll(ctx, subscriber); err != nil {
  431. t.Error(err)
  432. }
  433. })
  434. var firstBlockHeight int64
  435. for i := int64(0); i < 3; i++ {
  436. event := <-eventCh
  437. blockEvent, ok := event.Data.(types.EventDataNewBlock)
  438. require.True(t, ok)
  439. block := blockEvent.Block
  440. if firstBlockHeight == 0 {
  441. firstBlockHeight = block.Header.Height
  442. }
  443. require.Equal(t, firstBlockHeight+i, block.Header.Height)
  444. }
  445. })
  446. t.Run("BroadcastTxAsync", func(t *testing.T) {
  447. testTxEventsSent(ctx, t, "async", c)
  448. })
  449. t.Run("BroadcastTxSync", func(t *testing.T) {
  450. testTxEventsSent(ctx, t, "sync", c)
  451. })
  452. })
  453. t.Run("Evidence", func(t *testing.T) {
  454. t.Run("BraodcastDuplicateVote", func(t *testing.T) {
  455. chainID := conf.ChainID()
  456. correct, fakes := makeEvidences(t, pv, chainID)
  457. // make sure that the node has produced enough blocks
  458. waitForBlock(ctx, t, c, 2)
  459. result, err := c.BroadcastEvidence(ctx, correct)
  460. require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
  461. assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
  462. status, err := c.Status(ctx)
  463. require.NoError(t, err)
  464. err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  465. require.NoError(t, err)
  466. ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
  467. rawpub := ed25519pub.Bytes()
  468. result2, err := c.ABCIQuery(ctx, "/val", rawpub)
  469. require.NoError(t, err)
  470. qres := result2.Response
  471. require.True(t, qres.IsOK())
  472. var v abci.ValidatorUpdate
  473. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  474. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  475. pk, err := encoding.PubKeyFromProto(v.PubKey)
  476. require.NoError(t, err)
  477. require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  478. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  479. for _, fake := range fakes {
  480. _, err := c.BroadcastEvidence(ctx, fake)
  481. require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
  482. }
  483. })
  484. t.Run("BroadcastEmpty", func(t *testing.T) {
  485. _, err := c.BroadcastEvidence(ctx, nil)
  486. assert.Error(t, err)
  487. })
  488. })
  489. })
  490. }
  491. }
  492. func getMempool(t *testing.T, srv service.Service) mempool.Mempool {
  493. t.Helper()
  494. n, ok := srv.(interface {
  495. Mempool() mempool.Mempool
  496. })
  497. require.True(t, ok)
  498. return n.Mempool()
  499. }
  500. // these cases are roughly the same as the TestClientMethodCalls, but
  501. // they have to loop over their clients in the individual test cases,
  502. // so making a separate suite makes more sense, though isn't strictly
  503. // speaking desirable.
  504. func TestClientMethodCallsAdvanced(t *testing.T) {
  505. ctx, cancel := context.WithCancel(context.Background())
  506. defer cancel()
  507. n, conf := NodeSuite(t)
  508. pool := getMempool(t, n)
  509. t.Run("UnconfirmedTxs", func(t *testing.T) {
  510. _, _, tx := MakeTxKV()
  511. ch := make(chan struct{})
  512. err := pool.CheckTx(ctx, tx, func(_ *abci.Response) { close(ch) }, mempool.TxInfo{})
  513. require.NoError(t, err)
  514. // wait for tx to arrive in mempoool.
  515. select {
  516. case <-ch:
  517. case <-time.After(5 * time.Second):
  518. t.Error("Timed out waiting for CheckTx callback")
  519. }
  520. for _, c := range GetClients(t, n, conf) {
  521. mc := c.(client.MempoolClient)
  522. limit := 1
  523. res, err := mc.UnconfirmedTxs(ctx, &limit)
  524. require.NoError(t, err)
  525. assert.Equal(t, 1, res.Count)
  526. assert.Equal(t, 1, res.Total)
  527. assert.Equal(t, pool.SizeBytes(), res.TotalBytes)
  528. assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs))
  529. }
  530. pool.Flush()
  531. })
  532. t.Run("NumUnconfirmedTxs", func(t *testing.T) {
  533. ch := make(chan struct{})
  534. pool := getMempool(t, n)
  535. _, _, tx := MakeTxKV()
  536. err := pool.CheckTx(ctx, tx, func(_ *abci.Response) { close(ch) }, mempool.TxInfo{})
  537. require.NoError(t, err)
  538. // wait for tx to arrive in mempoool.
  539. select {
  540. case <-ch:
  541. case <-time.After(5 * time.Second):
  542. t.Error("Timed out waiting for CheckTx callback")
  543. }
  544. mempoolSize := pool.Size()
  545. for i, c := range GetClients(t, n, conf) {
  546. mc, ok := c.(client.MempoolClient)
  547. require.True(t, ok, "%d", i)
  548. res, err := mc.NumUnconfirmedTxs(ctx)
  549. require.Nil(t, err, "%d: %+v", i, err)
  550. assert.Equal(t, mempoolSize, res.Count)
  551. assert.Equal(t, mempoolSize, res.Total)
  552. assert.Equal(t, pool.SizeBytes(), res.TotalBytes)
  553. }
  554. pool.Flush()
  555. })
  556. t.Run("Tx", func(t *testing.T) {
  557. c := getHTTPClient(t, conf)
  558. // first we broadcast a tx
  559. _, _, tx := MakeTxKV()
  560. bres, err := c.BroadcastTxCommit(ctx, tx)
  561. require.Nil(t, err, "%+v", err)
  562. txHeight := bres.Height
  563. txHash := bres.Hash
  564. anotherTxHash := types.Tx("a different tx").Hash()
  565. cases := []struct {
  566. valid bool
  567. prove bool
  568. hash []byte
  569. }{
  570. // only valid if correct hash provided
  571. {true, false, txHash},
  572. {true, true, txHash},
  573. {false, false, anotherTxHash},
  574. {false, true, anotherTxHash},
  575. {false, false, nil},
  576. {false, true, nil},
  577. }
  578. for _, c := range GetClients(t, n, conf) {
  579. t.Run(fmt.Sprintf("%T", c), func(t *testing.T) {
  580. for j, tc := range cases {
  581. t.Run(fmt.Sprintf("Case%d", j), func(t *testing.T) {
  582. // now we query for the tx.
  583. // since there's only one tx, we know index=0.
  584. ptx, err := c.Tx(ctx, tc.hash, tc.prove)
  585. if !tc.valid {
  586. require.NotNil(t, err)
  587. } else {
  588. require.Nil(t, err, "%+v", err)
  589. assert.EqualValues(t, txHeight, ptx.Height)
  590. assert.EqualValues(t, tx, ptx.Tx)
  591. assert.Zero(t, ptx.Index)
  592. assert.True(t, ptx.TxResult.IsOK())
  593. assert.EqualValues(t, txHash, ptx.Hash)
  594. // time to verify the proof
  595. proof := ptx.Proof
  596. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  597. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  598. }
  599. }
  600. })
  601. }
  602. })
  603. }
  604. })
  605. t.Run("TxSearchWithTimeout", func(t *testing.T) {
  606. timeoutClient := getHTTPClientWithTimeout(t, conf, 10*time.Second)
  607. _, _, tx := MakeTxKV()
  608. _, err := timeoutClient.BroadcastTxCommit(ctx, tx)
  609. require.NoError(t, err)
  610. // query using a compositeKey (see kvstore application)
  611. result, err := timeoutClient.TxSearch(ctx, "app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
  612. require.Nil(t, err)
  613. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  614. })
  615. t.Run("TxSearch", func(t *testing.T) {
  616. t.Skip("Test Asserts Non-Deterministic Results")
  617. c := getHTTPClient(t, conf)
  618. // first we broadcast a few txs
  619. for i := 0; i < 10; i++ {
  620. _, _, tx := MakeTxKV()
  621. _, err := c.BroadcastTxSync(ctx, tx)
  622. require.NoError(t, err)
  623. }
  624. // since we're not using an isolated test server, we'll have lingering transactions
  625. // from other tests as well
  626. result, err := c.TxSearch(ctx, "tx.height >= 0", true, nil, nil, "asc")
  627. require.NoError(t, err)
  628. txCount := len(result.Txs)
  629. // pick out the last tx to have something to search for in tests
  630. find := result.Txs[len(result.Txs)-1]
  631. anotherTxHash := types.Tx("a different tx").Hash()
  632. for _, c := range GetClients(t, n, conf) {
  633. t.Run(fmt.Sprintf("%T", c), func(t *testing.T) {
  634. // now we query for the tx.
  635. result, err := c.TxSearch(ctx, fmt.Sprintf("tx.hash='%v'", find.Hash), true, nil, nil, "asc")
  636. require.Nil(t, err)
  637. require.Len(t, result.Txs, 1)
  638. require.Equal(t, find.Hash, result.Txs[0].Hash)
  639. ptx := result.Txs[0]
  640. assert.EqualValues(t, find.Height, ptx.Height)
  641. assert.EqualValues(t, find.Tx, ptx.Tx)
  642. assert.Zero(t, ptx.Index)
  643. assert.True(t, ptx.TxResult.IsOK())
  644. assert.EqualValues(t, find.Hash, ptx.Hash)
  645. // time to verify the proof
  646. if assert.EqualValues(t, find.Tx, ptx.Proof.Data) {
  647. assert.NoError(t, ptx.Proof.Proof.Verify(ptx.Proof.RootHash, find.Hash))
  648. }
  649. // query by height
  650. result, err = c.TxSearch(ctx, fmt.Sprintf("tx.height=%d", find.Height), true, nil, nil, "asc")
  651. require.Nil(t, err)
  652. require.Len(t, result.Txs, 1)
  653. // query for non existing tx
  654. result, err = c.TxSearch(ctx, fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, nil, nil, "asc")
  655. require.Nil(t, err)
  656. require.Len(t, result.Txs, 0)
  657. // query using a compositeKey (see kvstore application)
  658. result, err = c.TxSearch(ctx, "app.creator='Cosmoshi Netowoko'", false, nil, nil, "asc")
  659. require.Nil(t, err)
  660. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  661. // query using an index key
  662. result, err = c.TxSearch(ctx, "app.index_key='index is working'", false, nil, nil, "asc")
  663. require.Nil(t, err)
  664. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  665. // query using an noindex key
  666. result, err = c.TxSearch(ctx, "app.noindex_key='index is working'", false, nil, nil, "asc")
  667. require.Nil(t, err)
  668. require.Equal(t, len(result.Txs), 0, "expected a lot of transactions")
  669. // query using a compositeKey (see kvstore application) and height
  670. result, err = c.TxSearch(ctx,
  671. "app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, nil, nil, "asc")
  672. require.Nil(t, err)
  673. require.Greater(t, len(result.Txs), 0, "expected a lot of transactions")
  674. // query a non existing tx with page 1 and txsPerPage 1
  675. perPage := 1
  676. result, err = c.TxSearch(ctx, "app.creator='Cosmoshi Neetowoko'", true, nil, &perPage, "asc")
  677. require.Nil(t, err)
  678. require.Len(t, result.Txs, 0)
  679. // check sorting
  680. result, err = c.TxSearch(ctx, "tx.height >= 1", false, nil, nil, "asc")
  681. require.Nil(t, err)
  682. for k := 0; k < len(result.Txs)-1; k++ {
  683. require.LessOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
  684. require.LessOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
  685. }
  686. result, err = c.TxSearch(ctx, "tx.height >= 1", false, nil, nil, "desc")
  687. require.Nil(t, err)
  688. for k := 0; k < len(result.Txs)-1; k++ {
  689. require.GreaterOrEqual(t, result.Txs[k].Height, result.Txs[k+1].Height)
  690. require.GreaterOrEqual(t, result.Txs[k].Index, result.Txs[k+1].Index)
  691. }
  692. // check pagination
  693. perPage = 3
  694. var (
  695. seen = map[int64]bool{}
  696. maxHeight int64
  697. pages = int(math.Ceil(float64(txCount) / float64(perPage)))
  698. )
  699. for page := 1; page <= pages; page++ {
  700. page := page
  701. result, err := c.TxSearch(ctx, "tx.height >= 1", false, &page, &perPage, "asc")
  702. require.NoError(t, err)
  703. if page < pages {
  704. require.Len(t, result.Txs, perPage)
  705. } else {
  706. require.LessOrEqual(t, len(result.Txs), perPage)
  707. }
  708. require.Equal(t, txCount, result.TotalCount)
  709. for _, tx := range result.Txs {
  710. require.False(t, seen[tx.Height],
  711. "Found duplicate height %v in page %v", tx.Height, page)
  712. require.Greater(t, tx.Height, maxHeight,
  713. "Found decreasing height %v (max seen %v) in page %v", tx.Height, maxHeight, page)
  714. seen[tx.Height] = true
  715. maxHeight = tx.Height
  716. }
  717. }
  718. require.Len(t, seen, txCount)
  719. })
  720. }
  721. })
  722. }
  723. func testBatchedJSONRPCCalls(ctx context.Context, t *testing.T, c *rpchttp.HTTP) {
  724. k1, v1, tx1 := MakeTxKV()
  725. k2, v2, tx2 := MakeTxKV()
  726. batch := c.NewBatch()
  727. r1, err := batch.BroadcastTxCommit(ctx, tx1)
  728. require.NoError(t, err)
  729. r2, err := batch.BroadcastTxCommit(ctx, tx2)
  730. require.NoError(t, err)
  731. require.Equal(t, 2, batch.Count())
  732. bresults, err := batch.Send(ctx)
  733. require.NoError(t, err)
  734. require.Len(t, bresults, 2)
  735. require.Equal(t, 0, batch.Count())
  736. bresult1, ok := bresults[0].(*coretypes.ResultBroadcastTxCommit)
  737. require.True(t, ok)
  738. require.Equal(t, *bresult1, *r1)
  739. bresult2, ok := bresults[1].(*coretypes.ResultBroadcastTxCommit)
  740. require.True(t, ok)
  741. require.Equal(t, *bresult2, *r2)
  742. apph := tmmath.MaxInt64(bresult1.Height, bresult2.Height) + 1
  743. err = client.WaitForHeight(c, apph, nil)
  744. require.NoError(t, err)
  745. q1, err := batch.ABCIQuery(ctx, "/key", k1)
  746. require.NoError(t, err)
  747. q2, err := batch.ABCIQuery(ctx, "/key", k2)
  748. require.NoError(t, err)
  749. require.Equal(t, 2, batch.Count())
  750. qresults, err := batch.Send(ctx)
  751. require.NoError(t, err)
  752. require.Len(t, qresults, 2)
  753. require.Equal(t, 0, batch.Count())
  754. qresult1, ok := qresults[0].(*coretypes.ResultABCIQuery)
  755. require.True(t, ok)
  756. require.Equal(t, *qresult1, *q1)
  757. qresult2, ok := qresults[1].(*coretypes.ResultABCIQuery)
  758. require.True(t, ok)
  759. require.Equal(t, *qresult2, *q2)
  760. require.Equal(t, qresult1.Response.Key, k1)
  761. require.Equal(t, qresult2.Response.Key, k2)
  762. require.Equal(t, qresult1.Response.Value, v1)
  763. require.Equal(t, qresult2.Response.Value, v2)
  764. }