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.

544 lines
15 KiB

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/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/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/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
7 years ago
7 years ago
7 years ago
7 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/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/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/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/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/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/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
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
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
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
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "sync"
  9. "time"
  10. "github.com/gorilla/websocket"
  11. metrics "github.com/rcrowley/go-metrics"
  12. tmrand "github.com/tendermint/tendermint/libs/rand"
  13. "github.com/tendermint/tendermint/libs/service"
  14. tmsync "github.com/tendermint/tendermint/libs/sync"
  15. types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  16. )
  17. const (
  18. defaultMaxReconnectAttempts = 25
  19. defaultWriteWait = 0
  20. defaultReadWait = 0
  21. defaultPingPeriod = 0
  22. )
  23. // WSClient is a JSON-RPC client, which uses WebSocket for communication with
  24. // the remote server.
  25. //
  26. // WSClient is safe for concurrent use by multiple goroutines.
  27. type WSClient struct { // nolint: maligned
  28. conn *websocket.Conn
  29. Address string // IP:PORT or /path/to/socket
  30. Endpoint string // /websocket/url/endpoint
  31. Dialer func(string, string) (net.Conn, error)
  32. // Single user facing channel to read RPCResponses from, closed only when the
  33. // client is being stopped.
  34. ResponsesCh chan types.RPCResponse
  35. // Callback, which will be called each time after successful reconnect.
  36. onReconnect func()
  37. // internal channels
  38. send chan types.RPCRequest // user requests
  39. backlog chan types.RPCRequest // stores a single user request received during a conn failure
  40. reconnectAfter chan error // reconnect requests
  41. readRoutineQuit chan struct{} // a way for readRoutine to close writeRoutine
  42. // Maximum reconnect attempts (0 or greater; default: 25).
  43. maxReconnectAttempts int
  44. // Support both ws and wss protocols
  45. protocol string
  46. wg sync.WaitGroup
  47. mtx tmsync.RWMutex
  48. sentLastPingAt time.Time
  49. reconnecting bool
  50. nextReqID int
  51. // sentIDs map[types.JSONRPCIntID]bool // IDs of the requests currently in flight
  52. // Time allowed to write a message to the server. 0 means block until operation succeeds.
  53. writeWait time.Duration
  54. // Time allowed to read the next message from the server. 0 means block until operation succeeds.
  55. readWait time.Duration
  56. // Send pings to server with this period. Must be less than readWait. If 0, no pings will be sent.
  57. pingPeriod time.Duration
  58. service.BaseService
  59. // Time between sending a ping and receiving a pong. See
  60. // https://godoc.org/github.com/rcrowley/go-metrics#Timer.
  61. PingPongLatencyTimer metrics.Timer
  62. }
  63. // NewWS returns a new client. See the commentary on the func(*WSClient)
  64. // functions for a detailed description of how to configure ping period and
  65. // pong wait time. The endpoint argument must begin with a `/`.
  66. // An error is returned on invalid remote. The function panics when remote is nil.
  67. func NewWS(remoteAddr, endpoint string, options ...func(*WSClient)) (*WSClient, error) {
  68. parsedURL, err := newParsedURL(remoteAddr)
  69. if err != nil {
  70. return nil, err
  71. }
  72. // default to ws protocol, unless wss is explicitly specified
  73. if parsedURL.Scheme != protoWSS {
  74. parsedURL.Scheme = protoWS
  75. }
  76. dialFn, err := makeHTTPDialer(remoteAddr)
  77. if err != nil {
  78. return nil, err
  79. }
  80. c := &WSClient{
  81. Address: parsedURL.GetTrimmedHostWithPath(),
  82. Dialer: dialFn,
  83. Endpoint: endpoint,
  84. PingPongLatencyTimer: metrics.NewTimer(),
  85. maxReconnectAttempts: defaultMaxReconnectAttempts,
  86. readWait: defaultReadWait,
  87. writeWait: defaultWriteWait,
  88. pingPeriod: defaultPingPeriod,
  89. protocol: parsedURL.Scheme,
  90. // sentIDs: make(map[types.JSONRPCIntID]bool),
  91. }
  92. c.BaseService = *service.NewBaseService(nil, "WSClient", c)
  93. for _, option := range options {
  94. option(c)
  95. }
  96. return c, nil
  97. }
  98. // MaxReconnectAttempts sets the maximum number of reconnect attempts before returning an error.
  99. // It should only be used in the constructor and is not Goroutine-safe.
  100. func MaxReconnectAttempts(max int) func(*WSClient) {
  101. return func(c *WSClient) {
  102. c.maxReconnectAttempts = max
  103. }
  104. }
  105. // ReadWait sets the amount of time to wait before a websocket read times out.
  106. // It should only be used in the constructor and is not Goroutine-safe.
  107. func ReadWait(readWait time.Duration) func(*WSClient) {
  108. return func(c *WSClient) {
  109. c.readWait = readWait
  110. }
  111. }
  112. // WriteWait sets the amount of time to wait before a websocket write times out.
  113. // It should only be used in the constructor and is not Goroutine-safe.
  114. func WriteWait(writeWait time.Duration) func(*WSClient) {
  115. return func(c *WSClient) {
  116. c.writeWait = writeWait
  117. }
  118. }
  119. // PingPeriod sets the duration for sending websocket pings.
  120. // It should only be used in the constructor - not Goroutine-safe.
  121. func PingPeriod(pingPeriod time.Duration) func(*WSClient) {
  122. return func(c *WSClient) {
  123. c.pingPeriod = pingPeriod
  124. }
  125. }
  126. // OnReconnect sets the callback, which will be called every time after
  127. // successful reconnect.
  128. func OnReconnect(cb func()) func(*WSClient) {
  129. return func(c *WSClient) {
  130. c.onReconnect = cb
  131. }
  132. }
  133. // String returns WS client full address.
  134. func (c *WSClient) String() string {
  135. return fmt.Sprintf("WSClient{%s (%s)}", c.Address, c.Endpoint)
  136. }
  137. // OnStart implements service.Service by dialing a server and creating read and
  138. // write routines.
  139. func (c *WSClient) OnStart() error {
  140. err := c.dial()
  141. if err != nil {
  142. return err
  143. }
  144. c.ResponsesCh = make(chan types.RPCResponse)
  145. c.send = make(chan types.RPCRequest)
  146. // 1 additional error may come from the read/write
  147. // goroutine depending on which failed first.
  148. c.reconnectAfter = make(chan error, 1)
  149. // capacity for 1 request. a user won't be able to send more because the send
  150. // channel is unbuffered.
  151. c.backlog = make(chan types.RPCRequest, 1)
  152. c.startReadWriteRoutines()
  153. go c.reconnectRoutine()
  154. return nil
  155. }
  156. // Stop overrides service.Service#Stop. There is no other way to wait until Quit
  157. // channel is closed.
  158. func (c *WSClient) Stop() error {
  159. if err := c.BaseService.Stop(); err != nil {
  160. return err
  161. }
  162. // only close user-facing channels when we can't write to them
  163. c.wg.Wait()
  164. close(c.ResponsesCh)
  165. return nil
  166. }
  167. // IsReconnecting returns true if the client is reconnecting right now.
  168. func (c *WSClient) IsReconnecting() bool {
  169. c.mtx.RLock()
  170. defer c.mtx.RUnlock()
  171. return c.reconnecting
  172. }
  173. // IsActive returns true if the client is running and not reconnecting.
  174. func (c *WSClient) IsActive() bool {
  175. return c.IsRunning() && !c.IsReconnecting()
  176. }
  177. // Send the given RPC request to the server. Results will be available on
  178. // ResponsesCh, errors, if any, on ErrorsCh. Will block until send succeeds or
  179. // ctx.Done is closed.
  180. func (c *WSClient) Send(ctx context.Context, request types.RPCRequest) error {
  181. select {
  182. case c.send <- request:
  183. c.Logger.Info("sent a request", "req", request)
  184. // c.mtx.Lock()
  185. // c.sentIDs[request.ID.(types.JSONRPCIntID)] = true
  186. // c.mtx.Unlock()
  187. return nil
  188. case <-ctx.Done():
  189. return ctx.Err()
  190. }
  191. }
  192. // Call enqueues a call request onto the Send queue. Requests are JSON encoded.
  193. func (c *WSClient) Call(ctx context.Context, method string, params map[string]interface{}) error {
  194. request, err := types.MapToRequest(c.nextRequestID(), method, params)
  195. if err != nil {
  196. return err
  197. }
  198. return c.Send(ctx, request)
  199. }
  200. // CallWithArrayParams enqueues a call request onto the Send queue. Params are
  201. // in a form of array (e.g. []interface{}{"abcd"}). Requests are JSON encoded.
  202. func (c *WSClient) CallWithArrayParams(ctx context.Context, method string, params []interface{}) error {
  203. request, err := types.ArrayToRequest(c.nextRequestID(), method, params)
  204. if err != nil {
  205. return err
  206. }
  207. return c.Send(ctx, request)
  208. }
  209. // Private methods
  210. func (c *WSClient) nextRequestID() types.JSONRPCIntID {
  211. c.mtx.Lock()
  212. id := c.nextReqID
  213. c.nextReqID++
  214. c.mtx.Unlock()
  215. return types.JSONRPCIntID(id)
  216. }
  217. func (c *WSClient) dial() error {
  218. dialer := &websocket.Dialer{
  219. NetDial: c.Dialer,
  220. Proxy: http.ProxyFromEnvironment,
  221. }
  222. rHeader := http.Header{}
  223. conn, _, err := dialer.Dial(c.protocol+"://"+c.Address+c.Endpoint, rHeader) // nolint:bodyclose
  224. if err != nil {
  225. return err
  226. }
  227. c.conn = conn
  228. return nil
  229. }
  230. // reconnect tries to redial up to maxReconnectAttempts with exponential
  231. // backoff.
  232. func (c *WSClient) reconnect() error {
  233. attempt := 0
  234. c.mtx.Lock()
  235. c.reconnecting = true
  236. c.mtx.Unlock()
  237. defer func() {
  238. c.mtx.Lock()
  239. c.reconnecting = false
  240. c.mtx.Unlock()
  241. }()
  242. for {
  243. jitter := time.Duration(tmrand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
  244. backoffDuration := jitter + ((1 << uint(attempt)) * time.Second)
  245. c.Logger.Info("reconnecting", "attempt", attempt+1, "backoff_duration", backoffDuration)
  246. time.Sleep(backoffDuration)
  247. err := c.dial()
  248. if err != nil {
  249. c.Logger.Error("failed to redial", "err", err)
  250. } else {
  251. c.Logger.Info("reconnected")
  252. if c.onReconnect != nil {
  253. go c.onReconnect()
  254. }
  255. return nil
  256. }
  257. attempt++
  258. if attempt > c.maxReconnectAttempts {
  259. return fmt.Errorf("reached maximum reconnect attempts: %w", err)
  260. }
  261. }
  262. }
  263. func (c *WSClient) startReadWriteRoutines() {
  264. c.wg.Add(2)
  265. c.readRoutineQuit = make(chan struct{})
  266. go c.readRoutine()
  267. go c.writeRoutine()
  268. }
  269. func (c *WSClient) processBacklog() error {
  270. select {
  271. case request := <-c.backlog:
  272. if c.writeWait > 0 {
  273. if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)); err != nil {
  274. c.Logger.Error("failed to set write deadline", "err", err)
  275. }
  276. }
  277. if err := c.conn.WriteJSON(request); err != nil {
  278. c.Logger.Error("failed to resend request", "err", err)
  279. c.reconnectAfter <- err
  280. // requeue request
  281. c.backlog <- request
  282. return err
  283. }
  284. c.Logger.Info("resend a request", "req", request)
  285. default:
  286. }
  287. return nil
  288. }
  289. func (c *WSClient) reconnectRoutine() {
  290. for {
  291. select {
  292. case originalError := <-c.reconnectAfter:
  293. // wait until writeRoutine and readRoutine finish
  294. c.wg.Wait()
  295. if err := c.reconnect(); err != nil {
  296. c.Logger.Error("failed to reconnect", "err", err, "original_err", originalError)
  297. if err = c.Stop(); err != nil {
  298. c.Logger.Error("failed to stop conn", "error", err)
  299. }
  300. return
  301. }
  302. // drain reconnectAfter
  303. LOOP:
  304. for {
  305. select {
  306. case <-c.reconnectAfter:
  307. default:
  308. break LOOP
  309. }
  310. }
  311. err := c.processBacklog()
  312. if err == nil {
  313. c.startReadWriteRoutines()
  314. }
  315. case <-c.Quit():
  316. return
  317. }
  318. }
  319. }
  320. // The client ensures that there is at most one writer to a connection by
  321. // executing all writes from this goroutine.
  322. func (c *WSClient) writeRoutine() {
  323. var ticker *time.Ticker
  324. if c.pingPeriod > 0 {
  325. // ticker with a predefined period
  326. ticker = time.NewTicker(c.pingPeriod)
  327. } else {
  328. // ticker that never fires
  329. ticker = &time.Ticker{C: make(<-chan time.Time)}
  330. }
  331. defer func() {
  332. ticker.Stop()
  333. c.conn.Close()
  334. // err != nil {
  335. // ignore error; it will trigger in tests
  336. // likely because it's closing an already closed connection
  337. // }
  338. c.wg.Done()
  339. }()
  340. for {
  341. select {
  342. case request := <-c.send:
  343. if c.writeWait > 0 {
  344. if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)); err != nil {
  345. c.Logger.Error("failed to set write deadline", "err", err)
  346. }
  347. }
  348. if err := c.conn.WriteJSON(request); err != nil {
  349. c.Logger.Error("failed to send request", "err", err)
  350. c.reconnectAfter <- err
  351. // add request to the backlog, so we don't lose it
  352. c.backlog <- request
  353. return
  354. }
  355. case <-ticker.C:
  356. if c.writeWait > 0 {
  357. if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)); err != nil {
  358. c.Logger.Error("failed to set write deadline", "err", err)
  359. }
  360. }
  361. if err := c.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
  362. c.Logger.Error("failed to write ping", "err", err)
  363. c.reconnectAfter <- err
  364. return
  365. }
  366. c.mtx.Lock()
  367. c.sentLastPingAt = time.Now()
  368. c.mtx.Unlock()
  369. c.Logger.Debug("sent ping")
  370. case <-c.readRoutineQuit:
  371. return
  372. case <-c.Quit():
  373. if err := c.conn.WriteMessage(
  374. websocket.CloseMessage,
  375. websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
  376. ); err != nil {
  377. c.Logger.Error("failed to write message", "err", err)
  378. }
  379. return
  380. }
  381. }
  382. }
  383. // The client ensures that there is at most one reader to a connection by
  384. // executing all reads from this goroutine.
  385. func (c *WSClient) readRoutine() {
  386. defer func() {
  387. c.conn.Close()
  388. // err != nil {
  389. // ignore error; it will trigger in tests
  390. // likely because it's closing an already closed connection
  391. // }
  392. c.wg.Done()
  393. }()
  394. c.conn.SetPongHandler(func(string) error {
  395. // gather latency stats
  396. c.mtx.RLock()
  397. t := c.sentLastPingAt
  398. c.mtx.RUnlock()
  399. c.PingPongLatencyTimer.UpdateSince(t)
  400. c.Logger.Debug("got pong")
  401. return nil
  402. })
  403. for {
  404. // reset deadline for every message type (control or data)
  405. if c.readWait > 0 {
  406. if err := c.conn.SetReadDeadline(time.Now().Add(c.readWait)); err != nil {
  407. c.Logger.Error("failed to set read deadline", "err", err)
  408. }
  409. }
  410. _, data, err := c.conn.ReadMessage()
  411. if err != nil {
  412. if !websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
  413. return
  414. }
  415. c.Logger.Error("failed to read response", "err", err)
  416. close(c.readRoutineQuit)
  417. c.reconnectAfter <- err
  418. return
  419. }
  420. var response types.RPCResponse
  421. err = json.Unmarshal(data, &response)
  422. if err != nil {
  423. c.Logger.Error("failed to parse response", "err", err, "data", string(data))
  424. continue
  425. }
  426. if err = validateResponseID(response.ID); err != nil {
  427. c.Logger.Error("error in response ID", "id", response.ID, "err", err)
  428. continue
  429. }
  430. // TODO: events resulting from /subscribe do not work with ->
  431. // because they are implemented as responses with the subscribe request's
  432. // ID. According to the spec, they should be notifications (requests
  433. // without IDs).
  434. // https://github.com/tendermint/tendermint/issues/2949
  435. // c.mtx.Lock()
  436. // if _, ok := c.sentIDs[response.ID.(types.JSONRPCIntID)]; !ok {
  437. // c.Logger.Error("unsolicited response ID", "id", response.ID, "expected", c.sentIDs)
  438. // c.mtx.Unlock()
  439. // continue
  440. // }
  441. // delete(c.sentIDs, response.ID.(types.JSONRPCIntID))
  442. // c.mtx.Unlock()
  443. // Combine a non-blocking read on BaseService.Quit with a non-blocking write on ResponsesCh to avoid blocking
  444. // c.wg.Wait() in c.Stop(). Note we rely on Quit being closed so that it sends unlimited Quit signals to stop
  445. // both readRoutine and writeRoutine
  446. c.Logger.Info("got response", "id", response.ID, "result", fmt.Sprintf("%X", response.Result))
  447. select {
  448. case <-c.Quit():
  449. case c.ResponsesCh <- response:
  450. }
  451. }
  452. }
  453. // Predefined methods
  454. // Subscribe to a query. Note the server must have a "subscribe" route
  455. // defined.
  456. func (c *WSClient) Subscribe(ctx context.Context, query string) error {
  457. params := map[string]interface{}{"query": query}
  458. return c.Call(ctx, "subscribe", params)
  459. }
  460. // Unsubscribe from a query. Note the server must have a "unsubscribe" route
  461. // defined.
  462. func (c *WSClient) Unsubscribe(ctx context.Context, query string) error {
  463. params := map[string]interface{}{"query": query}
  464. return c.Call(ctx, "unsubscribe", params)
  465. }
  466. // UnsubscribeAll from all. Note the server must have a "unsubscribe_all" route
  467. // defined.
  468. func (c *WSClient) UnsubscribeAll(ctx context.Context) error {
  469. params := map[string]interface{}{}
  470. return c.Call(ctx, "unsubscribe_all", params)
  471. }