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.

722 lines
20 KiB

rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
8 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
rpc: 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
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
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
  1. package client_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/rand"
  6. "net/http"
  7. "strings"
  8. "sync"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. abci "github.com/tendermint/tendermint/abci/types"
  14. "github.com/tendermint/tendermint/crypto/ed25519"
  15. "github.com/tendermint/tendermint/crypto/tmhash"
  16. "github.com/tendermint/tendermint/libs/log"
  17. tmmath "github.com/tendermint/tendermint/libs/math"
  18. mempl "github.com/tendermint/tendermint/mempool"
  19. "github.com/tendermint/tendermint/privval"
  20. "github.com/tendermint/tendermint/rpc/client"
  21. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  22. rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
  23. rpctest "github.com/tendermint/tendermint/rpc/test"
  24. "github.com/tendermint/tendermint/types"
  25. )
  26. func getHTTPClient() *client.HTTP {
  27. rpcAddr := rpctest.GetConfig().RPC.ListenAddress
  28. c, err := client.NewHTTP(rpcAddr, "/websocket")
  29. if err != nil {
  30. panic(err)
  31. }
  32. c.SetLogger(log.TestingLogger())
  33. return c
  34. }
  35. func getLocalClient() *client.Local {
  36. return client.NewLocal(node)
  37. }
  38. // GetClients returns a slice of clients for table-driven tests
  39. func GetClients() []client.Client {
  40. return []client.Client{
  41. getHTTPClient(),
  42. getLocalClient(),
  43. }
  44. }
  45. func TestNilCustomHTTPClient(t *testing.T) {
  46. require.Panics(t, func() {
  47. _, _ = client.NewHTTPWithClient("http://example.com", "/websocket", nil)
  48. })
  49. require.Panics(t, func() {
  50. _, _ = rpcclient.NewJSONRPCClientWithHTTPClient("http://example.com", nil)
  51. })
  52. }
  53. func TestCustomHTTPClient(t *testing.T) {
  54. remote := rpctest.GetConfig().RPC.ListenAddress
  55. c, err := client.NewHTTPWithClient(remote, "/websocket", http.DefaultClient)
  56. require.Nil(t, err)
  57. status, err := c.Status()
  58. require.NoError(t, err)
  59. require.NotNil(t, status)
  60. }
  61. func TestCorsEnabled(t *testing.T) {
  62. origin := rpctest.GetConfig().RPC.CORSAllowedOrigins[0]
  63. remote := strings.Replace(rpctest.GetConfig().RPC.ListenAddress, "tcp", "http", -1)
  64. req, err := http.NewRequest("GET", remote, nil)
  65. require.Nil(t, err, "%+v", err)
  66. req.Header.Set("Origin", origin)
  67. c := &http.Client{}
  68. resp, err := c.Do(req)
  69. require.Nil(t, err, "%+v", err)
  70. defer resp.Body.Close()
  71. assert.Equal(t, resp.Header.Get("Access-Control-Allow-Origin"), origin)
  72. }
  73. // Make sure status is correct (we connect properly)
  74. func TestStatus(t *testing.T) {
  75. for i, c := range GetClients() {
  76. moniker := rpctest.GetConfig().Moniker
  77. status, err := c.Status()
  78. require.Nil(t, err, "%d: %+v", i, err)
  79. assert.Equal(t, moniker, status.NodeInfo.Moniker)
  80. }
  81. }
  82. // Make sure info is correct (we connect properly)
  83. func TestInfo(t *testing.T) {
  84. for i, c := range GetClients() {
  85. // status, err := c.Status()
  86. // require.Nil(t, err, "%+v", err)
  87. info, err := c.ABCIInfo()
  88. require.Nil(t, err, "%d: %+v", i, err)
  89. // TODO: this is not correct - fix merkleeyes!
  90. // assert.EqualValues(t, status.SyncInfo.LatestBlockHeight, info.Response.LastBlockHeight)
  91. assert.True(t, strings.Contains(info.Response.Data, "size"))
  92. }
  93. }
  94. func TestNetInfo(t *testing.T) {
  95. for i, c := range GetClients() {
  96. nc, ok := c.(client.NetworkClient)
  97. require.True(t, ok, "%d", i)
  98. netinfo, err := nc.NetInfo()
  99. require.Nil(t, err, "%d: %+v", i, err)
  100. assert.True(t, netinfo.Listening)
  101. assert.Equal(t, 0, len(netinfo.Peers))
  102. }
  103. }
  104. func TestDumpConsensusState(t *testing.T) {
  105. for i, c := range GetClients() {
  106. // FIXME: fix server so it doesn't panic on invalid input
  107. nc, ok := c.(client.NetworkClient)
  108. require.True(t, ok, "%d", i)
  109. cons, err := nc.DumpConsensusState()
  110. require.Nil(t, err, "%d: %+v", i, err)
  111. assert.NotEmpty(t, cons.RoundState)
  112. assert.Empty(t, cons.Peers)
  113. }
  114. }
  115. func TestConsensusState(t *testing.T) {
  116. for i, c := range GetClients() {
  117. // FIXME: fix server so it doesn't panic on invalid input
  118. nc, ok := c.(client.NetworkClient)
  119. require.True(t, ok, "%d", i)
  120. cons, err := nc.ConsensusState()
  121. require.Nil(t, err, "%d: %+v", i, err)
  122. assert.NotEmpty(t, cons.RoundState)
  123. }
  124. }
  125. func TestHealth(t *testing.T) {
  126. for i, c := range GetClients() {
  127. nc, ok := c.(client.NetworkClient)
  128. require.True(t, ok, "%d", i)
  129. _, err := nc.Health()
  130. require.Nil(t, err, "%d: %+v", i, err)
  131. }
  132. }
  133. func TestGenesisAndValidators(t *testing.T) {
  134. for i, c := range GetClients() {
  135. // make sure this is the right genesis file
  136. gen, err := c.Genesis()
  137. require.Nil(t, err, "%d: %+v", i, err)
  138. // get the genesis validator
  139. require.Equal(t, 1, len(gen.Genesis.Validators))
  140. gval := gen.Genesis.Validators[0]
  141. // get the current validators
  142. vals, err := c.Validators(nil, 0, 0)
  143. require.Nil(t, err, "%d: %+v", i, err)
  144. require.Equal(t, 1, len(vals.Validators))
  145. val := vals.Validators[0]
  146. // make sure the current set is also the genesis set
  147. assert.Equal(t, gval.Power, val.VotingPower)
  148. assert.Equal(t, gval.PubKey, val.PubKey)
  149. }
  150. }
  151. func TestABCIQuery(t *testing.T) {
  152. for i, c := range GetClients() {
  153. // write something
  154. k, v, tx := MakeTxKV()
  155. bres, err := c.BroadcastTxCommit(tx)
  156. require.Nil(t, err, "%d: %+v", i, err)
  157. apph := bres.Height + 1 // this is where the tx will be applied to the state
  158. // wait before querying
  159. client.WaitForHeight(c, apph, nil)
  160. res, err := c.ABCIQuery("/key", k)
  161. qres := res.Response
  162. if assert.Nil(t, err) && assert.True(t, qres.IsOK()) {
  163. assert.EqualValues(t, v, qres.Value)
  164. }
  165. }
  166. }
  167. // Make some app checks
  168. func TestAppCalls(t *testing.T) {
  169. assert, require := assert.New(t), require.New(t)
  170. for i, c := range GetClients() {
  171. // get an offset of height to avoid racing and guessing
  172. s, err := c.Status()
  173. require.Nil(err, "%d: %+v", i, err)
  174. // sh is start height or status height
  175. sh := s.SyncInfo.LatestBlockHeight
  176. // look for the future
  177. h := sh + 2
  178. _, err = c.Block(&h)
  179. assert.NotNil(err) // no block yet
  180. // write something
  181. k, v, tx := MakeTxKV()
  182. bres, err := c.BroadcastTxCommit(tx)
  183. require.Nil(err, "%d: %+v", i, err)
  184. require.True(bres.DeliverTx.IsOK())
  185. txh := bres.Height
  186. apph := txh + 1 // this is where the tx will be applied to the state
  187. // wait before querying
  188. if err := client.WaitForHeight(c, apph, nil); err != nil {
  189. t.Error(err)
  190. }
  191. _qres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Prove: false})
  192. qres := _qres.Response
  193. if assert.Nil(err) && assert.True(qres.IsOK()) {
  194. assert.Equal(k, qres.Key)
  195. assert.EqualValues(v, qres.Value)
  196. }
  197. // make sure we can lookup the tx with proof
  198. ptx, err := c.Tx(bres.Hash, true)
  199. require.Nil(err, "%d: %+v", i, err)
  200. assert.EqualValues(txh, ptx.Height)
  201. assert.EqualValues(tx, ptx.Tx)
  202. // and we can even check the block is added
  203. block, err := c.Block(&apph)
  204. require.Nil(err, "%d: %+v", i, err)
  205. appHash := block.Block.Header.AppHash
  206. assert.True(len(appHash) > 0)
  207. assert.EqualValues(apph, block.Block.Header.Height)
  208. // now check the results
  209. blockResults, err := c.BlockResults(&txh)
  210. require.Nil(err, "%d: %+v", i, err)
  211. assert.Equal(txh, blockResults.Height)
  212. if assert.Equal(1, len(blockResults.TxsResults)) {
  213. // check success code
  214. assert.EqualValues(0, blockResults.TxsResults[0].Code)
  215. }
  216. // check blockchain info, now that we know there is info
  217. info, err := c.BlockchainInfo(apph, apph)
  218. require.Nil(err, "%d: %+v", i, err)
  219. assert.True(info.LastHeight >= apph)
  220. if assert.Equal(1, len(info.BlockMetas)) {
  221. lastMeta := info.BlockMetas[0]
  222. assert.EqualValues(apph, lastMeta.Header.Height)
  223. blockData := block.Block
  224. assert.Equal(blockData.Header.AppHash, lastMeta.Header.AppHash)
  225. assert.Equal(block.BlockID, lastMeta.BlockID)
  226. }
  227. // and get the corresponding commit with the same apphash
  228. commit, err := c.Commit(&apph)
  229. require.Nil(err, "%d: %+v", i, err)
  230. cappHash := commit.Header.AppHash
  231. assert.Equal(appHash, cappHash)
  232. assert.NotNil(commit.Commit)
  233. // compare the commits (note Commit(2) has commit from Block(3))
  234. h = apph - 1
  235. commit2, err := c.Commit(&h)
  236. require.Nil(err, "%d: %+v", i, err)
  237. assert.Equal(block.Block.LastCommit, commit2.Commit)
  238. // and we got a proof that works!
  239. _pres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Prove: true})
  240. pres := _pres.Response
  241. assert.Nil(err)
  242. assert.True(pres.IsOK())
  243. // XXX Test proof
  244. }
  245. }
  246. func TestBroadcastTxSync(t *testing.T) {
  247. require := require.New(t)
  248. // TODO (melekes): use mempool which is set on RPC rather than getting it from node
  249. mempool := node.Mempool()
  250. initMempoolSize := mempool.Size()
  251. for i, c := range GetClients() {
  252. _, _, tx := MakeTxKV()
  253. bres, err := c.BroadcastTxSync(tx)
  254. require.Nil(err, "%d: %+v", i, err)
  255. require.Equal(bres.Code, abci.CodeTypeOK) // FIXME
  256. require.Equal(initMempoolSize+1, mempool.Size())
  257. txs := mempool.ReapMaxTxs(len(tx))
  258. require.EqualValues(tx, txs[0])
  259. mempool.Flush()
  260. }
  261. }
  262. func TestBroadcastTxCommit(t *testing.T) {
  263. require := require.New(t)
  264. mempool := node.Mempool()
  265. for i, c := range GetClients() {
  266. _, _, tx := MakeTxKV()
  267. bres, err := c.BroadcastTxCommit(tx)
  268. require.Nil(err, "%d: %+v", i, err)
  269. require.True(bres.CheckTx.IsOK())
  270. require.True(bres.DeliverTx.IsOK())
  271. require.Equal(0, mempool.Size())
  272. }
  273. }
  274. func TestUnconfirmedTxs(t *testing.T) {
  275. _, _, tx := MakeTxKV()
  276. mempool := node.Mempool()
  277. _ = mempool.CheckTx(tx, nil, mempl.TxInfo{})
  278. for i, c := range GetClients() {
  279. mc, ok := c.(client.MempoolClient)
  280. require.True(t, ok, "%d", i)
  281. res, err := mc.UnconfirmedTxs(1)
  282. require.Nil(t, err, "%d: %+v", i, err)
  283. assert.Equal(t, 1, res.Count)
  284. assert.Equal(t, 1, res.Total)
  285. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  286. assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs))
  287. }
  288. mempool.Flush()
  289. }
  290. func TestNumUnconfirmedTxs(t *testing.T) {
  291. _, _, tx := MakeTxKV()
  292. mempool := node.Mempool()
  293. _ = mempool.CheckTx(tx, nil, mempl.TxInfo{})
  294. mempoolSize := mempool.Size()
  295. for i, c := range GetClients() {
  296. mc, ok := c.(client.MempoolClient)
  297. require.True(t, ok, "%d", i)
  298. res, err := mc.NumUnconfirmedTxs()
  299. require.Nil(t, err, "%d: %+v", i, err)
  300. assert.Equal(t, mempoolSize, res.Count)
  301. assert.Equal(t, mempoolSize, res.Total)
  302. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  303. }
  304. mempool.Flush()
  305. }
  306. func TestTx(t *testing.T) {
  307. // first we broadcast a tx
  308. c := getHTTPClient()
  309. _, _, tx := MakeTxKV()
  310. bres, err := c.BroadcastTxCommit(tx)
  311. require.Nil(t, err, "%+v", err)
  312. txHeight := bres.Height
  313. txHash := bres.Hash
  314. anotherTxHash := types.Tx("a different tx").Hash()
  315. cases := []struct {
  316. valid bool
  317. prove bool
  318. hash []byte
  319. }{
  320. // only valid if correct hash provided
  321. {true, false, txHash},
  322. {true, true, txHash},
  323. {false, false, anotherTxHash},
  324. {false, true, anotherTxHash},
  325. {false, false, nil},
  326. {false, true, nil},
  327. }
  328. for i, c := range GetClients() {
  329. for j, tc := range cases {
  330. t.Logf("client %d, case %d", i, j)
  331. // now we query for the tx.
  332. // since there's only one tx, we know index=0.
  333. ptx, err := c.Tx(tc.hash, tc.prove)
  334. if !tc.valid {
  335. require.NotNil(t, err)
  336. } else {
  337. require.Nil(t, err, "%+v", err)
  338. assert.EqualValues(t, txHeight, ptx.Height)
  339. assert.EqualValues(t, tx, ptx.Tx)
  340. assert.Zero(t, ptx.Index)
  341. assert.True(t, ptx.TxResult.IsOK())
  342. assert.EqualValues(t, txHash, ptx.Hash)
  343. // time to verify the proof
  344. proof := ptx.Proof
  345. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  346. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  347. }
  348. }
  349. }
  350. }
  351. }
  352. func TestTxSearch(t *testing.T) {
  353. // first we broadcast a tx
  354. c := getHTTPClient()
  355. _, _, tx := MakeTxKV()
  356. bres, err := c.BroadcastTxCommit(tx)
  357. require.Nil(t, err, "%+v", err)
  358. txHeight := bres.Height
  359. txHash := bres.Hash
  360. anotherTxHash := types.Tx("a different tx").Hash()
  361. for i, c := range GetClients() {
  362. t.Logf("client %d", i)
  363. // now we query for the tx.
  364. // since there's only one tx, we know index=0.
  365. result, err := c.TxSearch(fmt.Sprintf("tx.hash='%v'", txHash), true, 1, 30)
  366. require.Nil(t, err, "%+v", err)
  367. require.Len(t, result.Txs, 1)
  368. ptx := result.Txs[0]
  369. assert.EqualValues(t, txHeight, ptx.Height)
  370. assert.EqualValues(t, tx, ptx.Tx)
  371. assert.Zero(t, ptx.Index)
  372. assert.True(t, ptx.TxResult.IsOK())
  373. assert.EqualValues(t, txHash, ptx.Hash)
  374. // time to verify the proof
  375. proof := ptx.Proof
  376. if assert.EqualValues(t, tx, proof.Data) {
  377. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  378. }
  379. // query by height
  380. result, err = c.TxSearch(fmt.Sprintf("tx.height=%d", txHeight), true, 1, 30)
  381. require.Nil(t, err, "%+v", err)
  382. require.Len(t, result.Txs, 1)
  383. // query for non existing tx
  384. result, err = c.TxSearch(fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, 1, 30)
  385. require.Nil(t, err, "%+v", err)
  386. require.Len(t, result.Txs, 0)
  387. // query using a compositeKey (see kvstore application)
  388. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko'", false, 1, 30)
  389. require.Nil(t, err, "%+v", err)
  390. if len(result.Txs) == 0 {
  391. t.Fatal("expected a lot of transactions")
  392. }
  393. // query using a compositeKey (see kvstore application) and height
  394. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, 1, 30)
  395. require.Nil(t, err, "%+v", err)
  396. if len(result.Txs) == 0 {
  397. t.Fatal("expected a lot of transactions")
  398. }
  399. // query a non existing tx with page 1 and txsPerPage 1
  400. result, err = c.TxSearch("app.creator='Cosmoshi Neetowoko'", true, 1, 1)
  401. require.Nil(t, err, "%+v", err)
  402. require.Len(t, result.Txs, 0)
  403. }
  404. }
  405. func deepcpVote(vote *types.Vote) (res *types.Vote) {
  406. res = &types.Vote{
  407. ValidatorAddress: make([]byte, len(vote.ValidatorAddress)),
  408. ValidatorIndex: vote.ValidatorIndex,
  409. Height: vote.Height,
  410. Round: vote.Round,
  411. Type: vote.Type,
  412. Timestamp: vote.Timestamp,
  413. BlockID: types.BlockID{
  414. Hash: make([]byte, len(vote.BlockID.Hash)),
  415. PartsHeader: vote.BlockID.PartsHeader,
  416. },
  417. Signature: make([]byte, len(vote.Signature)),
  418. }
  419. copy(res.ValidatorAddress, vote.ValidatorAddress)
  420. copy(res.BlockID.Hash, vote.BlockID.Hash)
  421. copy(res.Signature, vote.Signature)
  422. return
  423. }
  424. func newEvidence(
  425. t *testing.T,
  426. val *privval.FilePV,
  427. vote *types.Vote,
  428. vote2 *types.Vote,
  429. chainID string,
  430. ) types.DuplicateVoteEvidence {
  431. var err error
  432. deepcpVote2 := deepcpVote(vote2)
  433. deepcpVote2.Signature, err = val.Key.PrivKey.Sign(deepcpVote2.SignBytes(chainID))
  434. require.NoError(t, err)
  435. return *types.NewDuplicateVoteEvidence(val.Key.PubKey, vote, deepcpVote2)
  436. }
  437. func makeEvidences(
  438. t *testing.T,
  439. val *privval.FilePV,
  440. chainID string,
  441. ) (ev types.DuplicateVoteEvidence, fakes []types.DuplicateVoteEvidence) {
  442. vote := &types.Vote{
  443. ValidatorAddress: val.Key.Address,
  444. ValidatorIndex: 0,
  445. Height: 1,
  446. Round: 0,
  447. Type: types.PrevoteType,
  448. Timestamp: time.Now().UTC(),
  449. BlockID: types.BlockID{
  450. Hash: tmhash.Sum([]byte("blockhash")),
  451. PartsHeader: types.PartSetHeader{
  452. Total: 1000,
  453. Hash: tmhash.Sum([]byte("partset")),
  454. },
  455. },
  456. }
  457. var err error
  458. vote.Signature, err = val.Key.PrivKey.Sign(vote.SignBytes(chainID))
  459. require.NoError(t, err)
  460. vote2 := deepcpVote(vote)
  461. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  462. ev = newEvidence(t, val, vote, vote2, chainID)
  463. fakes = make([]types.DuplicateVoteEvidence, 42)
  464. // different address
  465. vote2 = deepcpVote(vote)
  466. for i := 0; i < 10; i++ {
  467. rand.Read(vote2.ValidatorAddress) // nolint: gosec
  468. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  469. }
  470. // different index
  471. vote2 = deepcpVote(vote)
  472. for i := 10; i < 20; i++ {
  473. vote2.ValidatorIndex = rand.Int()%100 + 1 // nolint: gosec
  474. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  475. }
  476. // different height
  477. vote2 = deepcpVote(vote)
  478. for i := 20; i < 30; i++ {
  479. vote2.Height = rand.Int63()%1000 + 100 // nolint: gosec
  480. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  481. }
  482. // different round
  483. vote2 = deepcpVote(vote)
  484. for i := 30; i < 40; i++ {
  485. vote2.Round = rand.Int()%10 + 1 // nolint: gosec
  486. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  487. }
  488. // different type
  489. vote2 = deepcpVote(vote)
  490. vote2.Type = types.PrecommitType
  491. fakes[40] = newEvidence(t, val, vote, vote2, chainID)
  492. // exactly same vote
  493. vote2 = deepcpVote(vote)
  494. fakes[41] = newEvidence(t, val, vote, vote2, chainID)
  495. return ev, fakes
  496. }
  497. func TestBroadcastEvidenceDuplicateVote(t *testing.T) {
  498. config := rpctest.GetConfig()
  499. chainID := config.ChainID()
  500. pvKeyFile := config.PrivValidatorKeyFile()
  501. pvKeyStateFile := config.PrivValidatorStateFile()
  502. pv := privval.LoadOrGenFilePV(pvKeyFile, pvKeyStateFile)
  503. ev, fakes := makeEvidences(t, pv, chainID)
  504. t.Logf("evidence %v", ev)
  505. for i, c := range GetClients() {
  506. t.Logf("client %d", i)
  507. result, err := c.BroadcastEvidence(&ev)
  508. require.Nil(t, err)
  509. require.Equal(t, ev.Hash(), result.Hash, "Invalid response, result %+v", result)
  510. status, err := c.Status()
  511. require.NoError(t, err)
  512. client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  513. ed25519pub := ev.PubKey.(ed25519.PubKeyEd25519)
  514. rawpub := ed25519pub[:]
  515. result2, err := c.ABCIQuery("/val", rawpub)
  516. require.Nil(t, err, "Error querying evidence, err %v", err)
  517. qres := result2.Response
  518. require.True(t, qres.IsOK(), "Response not OK")
  519. var v abci.ValidatorUpdate
  520. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  521. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  522. require.EqualValues(t, rawpub, v.PubKey.Data, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  523. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  524. for _, fake := range fakes {
  525. _, err := c.BroadcastEvidence(&types.DuplicateVoteEvidence{
  526. PubKey: fake.PubKey,
  527. VoteA: fake.VoteA,
  528. VoteB: fake.VoteB})
  529. require.Error(t, err, "Broadcasting fake evidence succeed: %s", fake.String())
  530. }
  531. }
  532. }
  533. func TestBatchedJSONRPCCalls(t *testing.T) {
  534. c := getHTTPClient()
  535. testBatchedJSONRPCCalls(t, c)
  536. }
  537. func testBatchedJSONRPCCalls(t *testing.T, c *client.HTTP) {
  538. k1, v1, tx1 := MakeTxKV()
  539. k2, v2, tx2 := MakeTxKV()
  540. batch := c.NewBatch()
  541. r1, err := batch.BroadcastTxCommit(tx1)
  542. require.NoError(t, err)
  543. r2, err := batch.BroadcastTxCommit(tx2)
  544. require.NoError(t, err)
  545. require.Equal(t, 2, batch.Count())
  546. bresults, err := batch.Send()
  547. require.NoError(t, err)
  548. require.Len(t, bresults, 2)
  549. require.Equal(t, 0, batch.Count())
  550. bresult1, ok := bresults[0].(*ctypes.ResultBroadcastTxCommit)
  551. require.True(t, ok)
  552. require.Equal(t, *bresult1, *r1)
  553. bresult2, ok := bresults[1].(*ctypes.ResultBroadcastTxCommit)
  554. require.True(t, ok)
  555. require.Equal(t, *bresult2, *r2)
  556. apph := tmmath.MaxInt64(bresult1.Height, bresult2.Height) + 1
  557. client.WaitForHeight(c, apph, nil)
  558. q1, err := batch.ABCIQuery("/key", k1)
  559. require.NoError(t, err)
  560. q2, err := batch.ABCIQuery("/key", k2)
  561. require.NoError(t, err)
  562. require.Equal(t, 2, batch.Count())
  563. qresults, err := batch.Send()
  564. require.NoError(t, err)
  565. require.Len(t, qresults, 2)
  566. require.Equal(t, 0, batch.Count())
  567. qresult1, ok := qresults[0].(*ctypes.ResultABCIQuery)
  568. require.True(t, ok)
  569. require.Equal(t, *qresult1, *q1)
  570. qresult2, ok := qresults[1].(*ctypes.ResultABCIQuery)
  571. require.True(t, ok)
  572. require.Equal(t, *qresult2, *q2)
  573. require.Equal(t, qresult1.Response.Key, k1)
  574. require.Equal(t, qresult2.Response.Key, k2)
  575. require.Equal(t, qresult1.Response.Value, v1)
  576. require.Equal(t, qresult2.Response.Value, v2)
  577. }
  578. func TestBatchedJSONRPCCallsCancellation(t *testing.T) {
  579. c := getHTTPClient()
  580. _, _, tx1 := MakeTxKV()
  581. _, _, tx2 := MakeTxKV()
  582. batch := c.NewBatch()
  583. _, err := batch.BroadcastTxCommit(tx1)
  584. require.NoError(t, err)
  585. _, err = batch.BroadcastTxCommit(tx2)
  586. require.NoError(t, err)
  587. // we should have 2 requests waiting
  588. require.Equal(t, 2, batch.Count())
  589. // we want to make sure we cleared 2 pending requests
  590. require.Equal(t, 2, batch.Clear())
  591. // now there should be no batched requests
  592. require.Equal(t, 0, batch.Count())
  593. }
  594. func TestSendingEmptyJSONRPCRequestBatch(t *testing.T) {
  595. c := getHTTPClient()
  596. batch := c.NewBatch()
  597. _, err := batch.Send()
  598. require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error")
  599. }
  600. func TestClearingEmptyJSONRPCRequestBatch(t *testing.T) {
  601. c := getHTTPClient()
  602. batch := c.NewBatch()
  603. require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result")
  604. }
  605. func TestConcurrentJSONRPCBatching(t *testing.T) {
  606. var wg sync.WaitGroup
  607. c := getHTTPClient()
  608. for i := 0; i < 50; i++ {
  609. wg.Add(1)
  610. go func() {
  611. defer wg.Done()
  612. testBatchedJSONRPCCalls(t, c)
  613. }()
  614. }
  615. wg.Wait()
  616. }