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.

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