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.

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