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.

734 lines
21 KiB

8 years ago
8 years ago
pubsub 2.0 (#3227) * green pubsub tests :OK: * get rid of clientToQueryMap * Subscribe and SubscribeUnbuffered * start adapting other pkgs to new pubsub * nope * rename MsgAndTags to Message * remove TagMap it does not bring any additional benefits * bring back EventSubscriber * fix test * fix data race in TestStartNextHeightCorrectly ``` Write at 0x00c0001c7418 by goroutine 796: github.com/tendermint/tendermint/consensus.TestStartNextHeightCorrectly() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:1296 +0xad testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Previous read at 0x00c0001c7418 by goroutine 858: github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1631 +0x1366 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1476 +0x8f github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /go/src/github.com/tendermint/tendermint/consensus/state.go:667 +0xa1e github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /go/src/github.com/tendermint/tendermint/consensus/state.go:628 +0x794 Goroutine 796 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1119 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 testing.runTests() /usr/local/go/src/testing/testing.go:1117 +0x4ee testing.(*M).Run() /usr/local/go/src/testing/testing.go:1034 +0x2ee main.main() _testmain.go:214 +0x332 Goroutine 858 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).startRoutines() /go/src/github.com/tendermint/tendermint/consensus/state.go:334 +0x221 github.com/tendermint/tendermint/consensus.startTestRound() /go/src/github.com/tendermint/tendermint/consensus/common_test.go:122 +0x63 github.com/tendermint/tendermint/consensus.TestStateFullRound1() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:255 +0x397 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ``` * fixes after my own review * fix formatting * wait 100ms before kicking a subscriber out + a test for indexer_service * fixes after my second review * no timeout * add changelog entries * fix merge conflicts * fix typos after Thane's review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * reformat code * rewrite indexer service in the attempt to fix failing test https://github.com/tendermint/tendermint/pull/3227/#issuecomment-462316527 * Revert "rewrite indexer service in the attempt to fix failing test" This reverts commit 0d9107a098230de7138abb1c201877c246e89ed1. * another attempt to fix indexer * fixes after Ethan's review * use unbuffered channel when indexing transactions Refs https://github.com/tendermint/tendermint/pull/3227#discussion_r258786716 * add a comment for EventBus#SubscribeUnbuffered * format code
6 years ago
pubsub 2.0 (#3227) * green pubsub tests :OK: * get rid of clientToQueryMap * Subscribe and SubscribeUnbuffered * start adapting other pkgs to new pubsub * nope * rename MsgAndTags to Message * remove TagMap it does not bring any additional benefits * bring back EventSubscriber * fix test * fix data race in TestStartNextHeightCorrectly ``` Write at 0x00c0001c7418 by goroutine 796: github.com/tendermint/tendermint/consensus.TestStartNextHeightCorrectly() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:1296 +0xad testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Previous read at 0x00c0001c7418 by goroutine 858: github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1631 +0x1366 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1476 +0x8f github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /go/src/github.com/tendermint/tendermint/consensus/state.go:667 +0xa1e github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /go/src/github.com/tendermint/tendermint/consensus/state.go:628 +0x794 Goroutine 796 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1119 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 testing.runTests() /usr/local/go/src/testing/testing.go:1117 +0x4ee testing.(*M).Run() /usr/local/go/src/testing/testing.go:1034 +0x2ee main.main() _testmain.go:214 +0x332 Goroutine 858 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).startRoutines() /go/src/github.com/tendermint/tendermint/consensus/state.go:334 +0x221 github.com/tendermint/tendermint/consensus.startTestRound() /go/src/github.com/tendermint/tendermint/consensus/common_test.go:122 +0x63 github.com/tendermint/tendermint/consensus.TestStateFullRound1() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:255 +0x397 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ``` * fixes after my own review * fix formatting * wait 100ms before kicking a subscriber out + a test for indexer_service * fixes after my second review * no timeout * add changelog entries * fix merge conflicts * fix typos after Thane's review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * reformat code * rewrite indexer service in the attempt to fix failing test https://github.com/tendermint/tendermint/pull/3227/#issuecomment-462316527 * Revert "rewrite indexer service in the attempt to fix failing test" This reverts commit 0d9107a098230de7138abb1c201877c246e89ed1. * another attempt to fix indexer * fixes after Ethan's review * use unbuffered channel when indexing transactions Refs https://github.com/tendermint/tendermint/pull/3227#discussion_r258786716 * add a comment for EventBus#SubscribeUnbuffered * format code
6 years ago
7 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
privval: refactor Remote signers (#3370) This PR is related to #3107 and a continuation of #3351 It is important to emphasise that in the privval original design, client/server and listening/dialing roles are inverted and do not follow a conventional interaction. Given two hosts A and B: Host A is listener/client Host B is dialer/server (contains the secret key) When A requires a signature, it needs to wait for B to dial in before it can issue a request. A only accepts a single connection and any failure leads to dropping the connection and waiting for B to reconnect. The original rationale behind this design was based on security. Host B only allows outbound connections to a list of whitelisted hosts. It is not possible to reach B unless B dials in. There are no listening/open ports in B. This PR results in the following changes: Refactors ping/heartbeat to avoid previously existing race conditions. Separates transport (dialer/listener) from signing (client/server) concerns to simplify workflow. Unifies and abstracts away the differences between unix and tcp sockets. A single signer endpoint implementation unifies connection handling code (read/write/close/connection obj) The signer request handler (server side) is customizable to increase testability. Updates and extends unit tests A high level overview of the classes is as follows: Transport (endpoints): The following classes take care of establishing a connection SignerDialerEndpoint SignerListeningEndpoint SignerEndpoint groups common functionality (read/write/timeouts/etc.) Signing (client/server): The following classes take care of exchanging request/responses SignerClient SignerServer This PR also closes #3601 Commits: * refactoring - work in progress * reworking unit tests * Encapsulating and fixing unit tests * Improve tests * Clean up * Fix/improve unit tests * clean up tests * Improving service endpoint * fixing unit test * fix linter issues * avoid invalid cache values (improve later?) * complete implementation * wip * improved connection loop * Improve reconnections + fixing unit tests * addressing comments * small formatting changes * clean up * Update node/node.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * check during initialization * dropping connecting when writing fails * removing break * use t.log instead * unifying and using cmn.GetFreePort() * review fixes * reordering and unifying drop connection * closing instead of signalling * refactored service loop * removed superfluous brackets * GetPubKey can return errors * Revert "GetPubKey can return errors" This reverts commit 68c06f19b4650389d7e5ab1659b318889028202c. * adding entry to changelog * Update CHANGELOG_PENDING.md Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * updating node.go * review fixes * fixes linter * fixing unit test * small fixes in comments * addressing review comments * addressing review comments 2 * reverting suggestion * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * do not expose brokenSignerDialerEndpoint * clean up logging * unifying methods shorten test time signer also drops * reenabling pings * improving testability + unit test * fixing go fmt + unit test * remove unused code * Addressing review comments * simplifying connection workflow * fix linter/go import issue * using base service quit * updating comment * Simplifying design + adjusting names * fixing linter issues * refactoring test harness + fixes * Addressing review comments * cleaning up * adding additional error check
5 years ago
privval: refactor Remote signers (#3370) This PR is related to #3107 and a continuation of #3351 It is important to emphasise that in the privval original design, client/server and listening/dialing roles are inverted and do not follow a conventional interaction. Given two hosts A and B: Host A is listener/client Host B is dialer/server (contains the secret key) When A requires a signature, it needs to wait for B to dial in before it can issue a request. A only accepts a single connection and any failure leads to dropping the connection and waiting for B to reconnect. The original rationale behind this design was based on security. Host B only allows outbound connections to a list of whitelisted hosts. It is not possible to reach B unless B dials in. There are no listening/open ports in B. This PR results in the following changes: Refactors ping/heartbeat to avoid previously existing race conditions. Separates transport (dialer/listener) from signing (client/server) concerns to simplify workflow. Unifies and abstracts away the differences between unix and tcp sockets. A single signer endpoint implementation unifies connection handling code (read/write/close/connection obj) The signer request handler (server side) is customizable to increase testability. Updates and extends unit tests A high level overview of the classes is as follows: Transport (endpoints): The following classes take care of establishing a connection SignerDialerEndpoint SignerListeningEndpoint SignerEndpoint groups common functionality (read/write/timeouts/etc.) Signing (client/server): The following classes take care of exchanging request/responses SignerClient SignerServer This PR also closes #3601 Commits: * refactoring - work in progress * reworking unit tests * Encapsulating and fixing unit tests * Improve tests * Clean up * Fix/improve unit tests * clean up tests * Improving service endpoint * fixing unit test * fix linter issues * avoid invalid cache values (improve later?) * complete implementation * wip * improved connection loop * Improve reconnections + fixing unit tests * addressing comments * small formatting changes * clean up * Update node/node.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * check during initialization * dropping connecting when writing fails * removing break * use t.log instead * unifying and using cmn.GetFreePort() * review fixes * reordering and unifying drop connection * closing instead of signalling * refactored service loop * removed superfluous brackets * GetPubKey can return errors * Revert "GetPubKey can return errors" This reverts commit 68c06f19b4650389d7e5ab1659b318889028202c. * adding entry to changelog * Update CHANGELOG_PENDING.md Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * updating node.go * review fixes * fixes linter * fixing unit test * small fixes in comments * addressing review comments * addressing review comments 2 * reverting suggestion * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * do not expose brokenSignerDialerEndpoint * clean up logging * unifying methods shorten test time signer also drops * reenabling pings * improving testability + unit test * fixing go fmt + unit test * remove unused code * Addressing review comments * simplifying connection workflow * fix linter/go import issue * using base service quit * updating comment * Simplifying design + adjusting names * fixing linter issues * refactoring test harness + fixes * Addressing review comments * cleaning up * adding additional error check
5 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
privval: refactor Remote signers (#3370) This PR is related to #3107 and a continuation of #3351 It is important to emphasise that in the privval original design, client/server and listening/dialing roles are inverted and do not follow a conventional interaction. Given two hosts A and B: Host A is listener/client Host B is dialer/server (contains the secret key) When A requires a signature, it needs to wait for B to dial in before it can issue a request. A only accepts a single connection and any failure leads to dropping the connection and waiting for B to reconnect. The original rationale behind this design was based on security. Host B only allows outbound connections to a list of whitelisted hosts. It is not possible to reach B unless B dials in. There are no listening/open ports in B. This PR results in the following changes: Refactors ping/heartbeat to avoid previously existing race conditions. Separates transport (dialer/listener) from signing (client/server) concerns to simplify workflow. Unifies and abstracts away the differences between unix and tcp sockets. A single signer endpoint implementation unifies connection handling code (read/write/close/connection obj) The signer request handler (server side) is customizable to increase testability. Updates and extends unit tests A high level overview of the classes is as follows: Transport (endpoints): The following classes take care of establishing a connection SignerDialerEndpoint SignerListeningEndpoint SignerEndpoint groups common functionality (read/write/timeouts/etc.) Signing (client/server): The following classes take care of exchanging request/responses SignerClient SignerServer This PR also closes #3601 Commits: * refactoring - work in progress * reworking unit tests * Encapsulating and fixing unit tests * Improve tests * Clean up * Fix/improve unit tests * clean up tests * Improving service endpoint * fixing unit test * fix linter issues * avoid invalid cache values (improve later?) * complete implementation * wip * improved connection loop * Improve reconnections + fixing unit tests * addressing comments * small formatting changes * clean up * Update node/node.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * check during initialization * dropping connecting when writing fails * removing break * use t.log instead * unifying and using cmn.GetFreePort() * review fixes * reordering and unifying drop connection * closing instead of signalling * refactored service loop * removed superfluous brackets * GetPubKey can return errors * Revert "GetPubKey can return errors" This reverts commit 68c06f19b4650389d7e5ab1659b318889028202c. * adding entry to changelog * Update CHANGELOG_PENDING.md Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * updating node.go * review fixes * fixes linter * fixing unit test * small fixes in comments * addressing review comments * addressing review comments 2 * reverting suggestion * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * do not expose brokenSignerDialerEndpoint * clean up logging * unifying methods shorten test time signer also drops * reenabling pings * improving testability + unit test * fixing go fmt + unit test * remove unused code * Addressing review comments * simplifying connection workflow * fix linter/go import issue * using base service quit * updating comment * Simplifying design + adjusting names * fixing linter issues * refactoring test harness + fixes * Addressing review comments * cleaning up * adding additional error check
5 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
privval: refactor Remote signers (#3370) This PR is related to #3107 and a continuation of #3351 It is important to emphasise that in the privval original design, client/server and listening/dialing roles are inverted and do not follow a conventional interaction. Given two hosts A and B: Host A is listener/client Host B is dialer/server (contains the secret key) When A requires a signature, it needs to wait for B to dial in before it can issue a request. A only accepts a single connection and any failure leads to dropping the connection and waiting for B to reconnect. The original rationale behind this design was based on security. Host B only allows outbound connections to a list of whitelisted hosts. It is not possible to reach B unless B dials in. There are no listening/open ports in B. This PR results in the following changes: Refactors ping/heartbeat to avoid previously existing race conditions. Separates transport (dialer/listener) from signing (client/server) concerns to simplify workflow. Unifies and abstracts away the differences between unix and tcp sockets. A single signer endpoint implementation unifies connection handling code (read/write/close/connection obj) The signer request handler (server side) is customizable to increase testability. Updates and extends unit tests A high level overview of the classes is as follows: Transport (endpoints): The following classes take care of establishing a connection SignerDialerEndpoint SignerListeningEndpoint SignerEndpoint groups common functionality (read/write/timeouts/etc.) Signing (client/server): The following classes take care of exchanging request/responses SignerClient SignerServer This PR also closes #3601 Commits: * refactoring - work in progress * reworking unit tests * Encapsulating and fixing unit tests * Improve tests * Clean up * Fix/improve unit tests * clean up tests * Improving service endpoint * fixing unit test * fix linter issues * avoid invalid cache values (improve later?) * complete implementation * wip * improved connection loop * Improve reconnections + fixing unit tests * addressing comments * small formatting changes * clean up * Update node/node.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * check during initialization * dropping connecting when writing fails * removing break * use t.log instead * unifying and using cmn.GetFreePort() * review fixes * reordering and unifying drop connection * closing instead of signalling * refactored service loop * removed superfluous brackets * GetPubKey can return errors * Revert "GetPubKey can return errors" This reverts commit 68c06f19b4650389d7e5ab1659b318889028202c. * adding entry to changelog * Update CHANGELOG_PENDING.md Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * updating node.go * review fixes * fixes linter * fixing unit test * small fixes in comments * addressing review comments * addressing review comments 2 * reverting suggestion * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * do not expose brokenSignerDialerEndpoint * clean up logging * unifying methods shorten test time signer also drops * reenabling pings * improving testability + unit test * fixing go fmt + unit test * remove unused code * Addressing review comments * simplifying connection workflow * fix linter/go import issue * using base service quit * updating comment * Simplifying design + adjusting names * fixing linter issues * refactoring test harness + fixes * Addressing review comments * cleaning up * adding additional error check
5 years ago
privval: refactor Remote signers (#3370) This PR is related to #3107 and a continuation of #3351 It is important to emphasise that in the privval original design, client/server and listening/dialing roles are inverted and do not follow a conventional interaction. Given two hosts A and B: Host A is listener/client Host B is dialer/server (contains the secret key) When A requires a signature, it needs to wait for B to dial in before it can issue a request. A only accepts a single connection and any failure leads to dropping the connection and waiting for B to reconnect. The original rationale behind this design was based on security. Host B only allows outbound connections to a list of whitelisted hosts. It is not possible to reach B unless B dials in. There are no listening/open ports in B. This PR results in the following changes: Refactors ping/heartbeat to avoid previously existing race conditions. Separates transport (dialer/listener) from signing (client/server) concerns to simplify workflow. Unifies and abstracts away the differences between unix and tcp sockets. A single signer endpoint implementation unifies connection handling code (read/write/close/connection obj) The signer request handler (server side) is customizable to increase testability. Updates and extends unit tests A high level overview of the classes is as follows: Transport (endpoints): The following classes take care of establishing a connection SignerDialerEndpoint SignerListeningEndpoint SignerEndpoint groups common functionality (read/write/timeouts/etc.) Signing (client/server): The following classes take care of exchanging request/responses SignerClient SignerServer This PR also closes #3601 Commits: * refactoring - work in progress * reworking unit tests * Encapsulating and fixing unit tests * Improve tests * Clean up * Fix/improve unit tests * clean up tests * Improving service endpoint * fixing unit test * fix linter issues * avoid invalid cache values (improve later?) * complete implementation * wip * improved connection loop * Improve reconnections + fixing unit tests * addressing comments * small formatting changes * clean up * Update node/node.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * check during initialization * dropping connecting when writing fails * removing break * use t.log instead * unifying and using cmn.GetFreePort() * review fixes * reordering and unifying drop connection * closing instead of signalling * refactored service loop * removed superfluous brackets * GetPubKey can return errors * Revert "GetPubKey can return errors" This reverts commit 68c06f19b4650389d7e5ab1659b318889028202c. * adding entry to changelog * Update CHANGELOG_PENDING.md Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_client.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_dialer_endpoint.go Co-Authored-By: jleni <juan.leni@zondax.ch> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: jleni <juan.leni@zondax.ch> * updating node.go * review fixes * fixes linter * fixing unit test * small fixes in comments * addressing review comments * addressing review comments 2 * reverting suggestion * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_client_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update privval/signer_listener_endpoint_test.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * do not expose brokenSignerDialerEndpoint * clean up logging * unifying methods shorten test time signer also drops * reenabling pings * improving testability + unit test * fixing go fmt + unit test * remove unused code * Addressing review comments * simplifying connection workflow * fix linter/go import issue * using base service quit * updating comment * Simplifying design + adjusting names * fixing linter issues * refactoring test harness + fixes * Addressing review comments * cleaning up * adding additional error check
5 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
  1. package node
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "math"
  7. "net"
  8. "os"
  9. "syscall"
  10. "testing"
  11. "time"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. dbm "github.com/tendermint/tm-db"
  15. "github.com/tendermint/tendermint/abci/example/kvstore"
  16. cfg "github.com/tendermint/tendermint/config"
  17. "github.com/tendermint/tendermint/crypto"
  18. "github.com/tendermint/tendermint/crypto/ed25519"
  19. "github.com/tendermint/tendermint/crypto/tmhash"
  20. consmocks "github.com/tendermint/tendermint/internal/consensus/mocks"
  21. ssmocks "github.com/tendermint/tendermint/internal/statesync/mocks"
  22. "github.com/tendermint/tendermint/internal/evidence"
  23. "github.com/tendermint/tendermint/internal/mempool"
  24. mempoolv0 "github.com/tendermint/tendermint/internal/mempool/v0"
  25. statesync "github.com/tendermint/tendermint/internal/statesync"
  26. "github.com/tendermint/tendermint/internal/test/factory"
  27. "github.com/tendermint/tendermint/libs/log"
  28. tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
  29. tmrand "github.com/tendermint/tendermint/libs/rand"
  30. tmtime "github.com/tendermint/tendermint/libs/time"
  31. "github.com/tendermint/tendermint/privval"
  32. "github.com/tendermint/tendermint/proxy"
  33. sm "github.com/tendermint/tendermint/state"
  34. "github.com/tendermint/tendermint/state/indexer"
  35. "github.com/tendermint/tendermint/store"
  36. "github.com/tendermint/tendermint/types"
  37. )
  38. func TestNodeStartStop(t *testing.T) {
  39. config := cfg.ResetTestRoot("node_node_test")
  40. defer os.RemoveAll(config.RootDir)
  41. // create & start node
  42. ns, err := newDefaultNode(config, log.TestingLogger())
  43. require.NoError(t, err)
  44. require.NoError(t, ns.Start())
  45. n, ok := ns.(*nodeImpl)
  46. require.True(t, ok)
  47. t.Logf("Started node %v", n.sw.NodeInfo())
  48. // wait for the node to produce a block
  49. blocksSub, err := n.EventBus().Subscribe(context.Background(), "node_test", types.EventQueryNewBlock)
  50. require.NoError(t, err)
  51. select {
  52. case <-blocksSub.Out():
  53. case <-blocksSub.Canceled():
  54. t.Fatal("blocksSub was canceled")
  55. case <-time.After(10 * time.Second):
  56. t.Fatal("timed out waiting for the node to produce a block")
  57. }
  58. // stop the node
  59. go func() {
  60. err = n.Stop()
  61. require.NoError(t, err)
  62. }()
  63. select {
  64. case <-n.Quit():
  65. case <-time.After(5 * time.Second):
  66. pid := os.Getpid()
  67. p, err := os.FindProcess(pid)
  68. if err != nil {
  69. panic(err)
  70. }
  71. err = p.Signal(syscall.SIGABRT)
  72. fmt.Println(err)
  73. t.Fatal("timed out waiting for shutdown")
  74. }
  75. }
  76. func getTestNode(t *testing.T, conf *cfg.Config, logger log.Logger) *nodeImpl {
  77. t.Helper()
  78. ns, err := newDefaultNode(conf, logger)
  79. require.NoError(t, err)
  80. n, ok := ns.(*nodeImpl)
  81. require.True(t, ok)
  82. return n
  83. }
  84. func TestNodeDelayedStart(t *testing.T) {
  85. config := cfg.ResetTestRoot("node_delayed_start_test")
  86. defer os.RemoveAll(config.RootDir)
  87. now := tmtime.Now()
  88. // create & start node
  89. n := getTestNode(t, config, log.TestingLogger())
  90. n.GenesisDoc().GenesisTime = now.Add(2 * time.Second)
  91. require.NoError(t, n.Start())
  92. defer n.Stop() //nolint:errcheck // ignore for tests
  93. startTime := tmtime.Now()
  94. assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime))
  95. }
  96. func TestNodeSetAppVersion(t *testing.T) {
  97. config := cfg.ResetTestRoot("node_app_version_test")
  98. defer os.RemoveAll(config.RootDir)
  99. // create node
  100. n := getTestNode(t, config, log.TestingLogger())
  101. // default config uses the kvstore app
  102. var appVersion uint64 = kvstore.ProtocolVersion
  103. // check version is set in state
  104. state, err := n.stateStore.Load()
  105. require.NoError(t, err)
  106. assert.Equal(t, state.Version.Consensus.App, appVersion)
  107. // check version is set in node info
  108. assert.Equal(t, n.nodeInfo.ProtocolVersion.App, appVersion)
  109. }
  110. func TestNodeSetPrivValTCP(t *testing.T) {
  111. addr := "tcp://" + testFreeAddr(t)
  112. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  113. defer os.RemoveAll(config.RootDir)
  114. config.PrivValidator.ListenAddr = addr
  115. dialer := privval.DialTCPFn(addr, 100*time.Millisecond, ed25519.GenPrivKey())
  116. dialerEndpoint := privval.NewSignerDialerEndpoint(
  117. log.TestingLogger(),
  118. dialer,
  119. )
  120. privval.SignerDialerEndpointTimeoutReadWrite(100 * time.Millisecond)(dialerEndpoint)
  121. signerServer := privval.NewSignerServer(
  122. dialerEndpoint,
  123. config.ChainID(),
  124. types.NewMockPV(),
  125. )
  126. go func() {
  127. err := signerServer.Start()
  128. if err != nil {
  129. panic(err)
  130. }
  131. }()
  132. defer signerServer.Stop() //nolint:errcheck // ignore for tests
  133. n := getTestNode(t, config, log.TestingLogger())
  134. assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator())
  135. }
  136. // address without a protocol must result in error
  137. func TestPrivValidatorListenAddrNoProtocol(t *testing.T) {
  138. addrNoPrefix := testFreeAddr(t)
  139. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  140. defer os.RemoveAll(config.RootDir)
  141. config.PrivValidator.ListenAddr = addrNoPrefix
  142. _, err := newDefaultNode(config, log.TestingLogger())
  143. assert.Error(t, err)
  144. }
  145. func TestNodeSetPrivValIPC(t *testing.T) {
  146. tmpfile := "/tmp/kms." + tmrand.Str(6) + ".sock"
  147. defer os.Remove(tmpfile) // clean up
  148. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  149. defer os.RemoveAll(config.RootDir)
  150. config.PrivValidator.ListenAddr = "unix://" + tmpfile
  151. dialer := privval.DialUnixFn(tmpfile)
  152. dialerEndpoint := privval.NewSignerDialerEndpoint(
  153. log.TestingLogger(),
  154. dialer,
  155. )
  156. privval.SignerDialerEndpointTimeoutReadWrite(100 * time.Millisecond)(dialerEndpoint)
  157. pvsc := privval.NewSignerServer(
  158. dialerEndpoint,
  159. config.ChainID(),
  160. types.NewMockPV(),
  161. )
  162. go func() {
  163. err := pvsc.Start()
  164. require.NoError(t, err)
  165. }()
  166. defer pvsc.Stop() //nolint:errcheck // ignore for tests
  167. n := getTestNode(t, config, log.TestingLogger())
  168. assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator())
  169. }
  170. // testFreeAddr claims a free port so we don't block on listener being ready.
  171. func testFreeAddr(t *testing.T) string {
  172. ln, err := net.Listen("tcp", "127.0.0.1:0")
  173. require.NoError(t, err)
  174. defer ln.Close()
  175. return fmt.Sprintf("127.0.0.1:%d", ln.Addr().(*net.TCPAddr).Port)
  176. }
  177. // create a proposal block using real and full
  178. // mempool and evidence pool and validate it.
  179. func TestCreateProposalBlock(t *testing.T) {
  180. config := cfg.ResetTestRoot("node_create_proposal")
  181. defer os.RemoveAll(config.RootDir)
  182. cc := proxy.NewLocalClientCreator(kvstore.NewApplication())
  183. proxyApp := proxy.NewAppConns(cc)
  184. err := proxyApp.Start()
  185. require.Nil(t, err)
  186. defer proxyApp.Stop() //nolint:errcheck // ignore for tests
  187. logger := log.TestingLogger()
  188. const height int64 = 1
  189. state, stateDB, privVals := state(1, height)
  190. stateStore := sm.NewStore(stateDB)
  191. maxBytes := 16384
  192. const partSize uint32 = 256
  193. maxEvidenceBytes := int64(maxBytes / 2)
  194. state.ConsensusParams.Block.MaxBytes = int64(maxBytes)
  195. state.ConsensusParams.Evidence.MaxBytes = maxEvidenceBytes
  196. proposerAddr, _ := state.Validators.GetByIndex(0)
  197. mp := mempoolv0.NewCListMempool(
  198. config.Mempool,
  199. proxyApp.Mempool(),
  200. state.LastBlockHeight,
  201. mempoolv0.WithMetrics(mempool.NopMetrics()),
  202. mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
  203. mempoolv0.WithPostCheck(sm.TxPostCheck(state)),
  204. )
  205. mp.SetLogger(logger)
  206. // Make EvidencePool
  207. evidenceDB := dbm.NewMemDB()
  208. blockStore := store.NewBlockStore(dbm.NewMemDB())
  209. evidencePool, err := evidence.NewPool(logger, evidenceDB, stateStore, blockStore)
  210. require.NoError(t, err)
  211. // fill the evidence pool with more evidence
  212. // than can fit in a block
  213. var currentBytes int64 = 0
  214. for currentBytes <= maxEvidenceBytes {
  215. ev := types.NewMockDuplicateVoteEvidenceWithValidator(height, time.Now(), privVals[0], "test-chain")
  216. currentBytes += int64(len(ev.Bytes()))
  217. evidencePool.ReportConflictingVotes(ev.VoteA, ev.VoteB)
  218. }
  219. evList, size := evidencePool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  220. require.Less(t, size, state.ConsensusParams.Evidence.MaxBytes+1)
  221. evData := &types.EvidenceData{Evidence: evList}
  222. require.EqualValues(t, size, evData.ByteSize())
  223. // fill the mempool with more txs
  224. // than can fit in a block
  225. txLength := 100
  226. for i := 0; i <= maxBytes/txLength; i++ {
  227. tx := tmrand.Bytes(txLength)
  228. err := mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  229. assert.NoError(t, err)
  230. }
  231. blockExec := sm.NewBlockExecutor(
  232. stateStore,
  233. logger,
  234. proxyApp.Consensus(),
  235. mp,
  236. evidencePool,
  237. blockStore,
  238. )
  239. commit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
  240. block, _ := blockExec.CreateProposalBlock(
  241. height,
  242. state, commit,
  243. proposerAddr,
  244. )
  245. // check that the part set does not exceed the maximum block size
  246. partSet := block.MakePartSet(partSize)
  247. assert.Less(t, partSet.ByteSize(), int64(maxBytes))
  248. partSetFromHeader := types.NewPartSetFromHeader(partSet.Header())
  249. for partSetFromHeader.Count() < partSetFromHeader.Total() {
  250. added, err := partSetFromHeader.AddPart(partSet.GetPart(int(partSetFromHeader.Count())))
  251. require.NoError(t, err)
  252. require.True(t, added)
  253. }
  254. assert.EqualValues(t, partSetFromHeader.ByteSize(), partSet.ByteSize())
  255. err = blockExec.ValidateBlock(state, block)
  256. assert.NoError(t, err)
  257. }
  258. func TestMaxTxsProposalBlockSize(t *testing.T) {
  259. config := cfg.ResetTestRoot("node_create_proposal")
  260. defer os.RemoveAll(config.RootDir)
  261. cc := proxy.NewLocalClientCreator(kvstore.NewApplication())
  262. proxyApp := proxy.NewAppConns(cc)
  263. err := proxyApp.Start()
  264. require.Nil(t, err)
  265. defer proxyApp.Stop() //nolint:errcheck // ignore for tests
  266. logger := log.TestingLogger()
  267. const height int64 = 1
  268. state, stateDB, _ := state(1, height)
  269. stateStore := sm.NewStore(stateDB)
  270. blockStore := store.NewBlockStore(dbm.NewMemDB())
  271. const maxBytes int64 = 16384
  272. const partSize uint32 = 256
  273. state.ConsensusParams.Block.MaxBytes = maxBytes
  274. proposerAddr, _ := state.Validators.GetByIndex(0)
  275. // Make Mempool
  276. mp := mempoolv0.NewCListMempool(
  277. config.Mempool,
  278. proxyApp.Mempool(),
  279. state.LastBlockHeight,
  280. mempoolv0.WithMetrics(mempool.NopMetrics()),
  281. mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
  282. mempoolv0.WithPostCheck(sm.TxPostCheck(state)),
  283. )
  284. mp.SetLogger(logger)
  285. // fill the mempool with one txs just below the maximum size
  286. txLength := int(types.MaxDataBytesNoEvidence(maxBytes, 1))
  287. tx := tmrand.Bytes(txLength - 4) // to account for the varint
  288. err = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  289. assert.NoError(t, err)
  290. blockExec := sm.NewBlockExecutor(
  291. stateStore,
  292. logger,
  293. proxyApp.Consensus(),
  294. mp,
  295. sm.EmptyEvidencePool{},
  296. blockStore,
  297. )
  298. commit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
  299. block, _ := blockExec.CreateProposalBlock(
  300. height,
  301. state, commit,
  302. proposerAddr,
  303. )
  304. pb, err := block.ToProto()
  305. require.NoError(t, err)
  306. assert.Less(t, int64(pb.Size()), maxBytes)
  307. // check that the part set does not exceed the maximum block size
  308. partSet := block.MakePartSet(partSize)
  309. assert.EqualValues(t, partSet.ByteSize(), int64(pb.Size()))
  310. }
  311. func TestMaxProposalBlockSize(t *testing.T) {
  312. config := cfg.ResetTestRoot("node_create_proposal")
  313. defer os.RemoveAll(config.RootDir)
  314. cc := proxy.NewLocalClientCreator(kvstore.NewApplication())
  315. proxyApp := proxy.NewAppConns(cc)
  316. err := proxyApp.Start()
  317. require.Nil(t, err)
  318. defer proxyApp.Stop() //nolint:errcheck // ignore for tests
  319. logger := log.TestingLogger()
  320. state, stateDB, _ := state(types.MaxVotesCount, int64(1))
  321. stateStore := sm.NewStore(stateDB)
  322. blockStore := store.NewBlockStore(dbm.NewMemDB())
  323. const maxBytes int64 = 1024 * 1024 * 2
  324. state.ConsensusParams.Block.MaxBytes = maxBytes
  325. proposerAddr, _ := state.Validators.GetByIndex(0)
  326. // Make Mempool
  327. mp := mempoolv0.NewCListMempool(
  328. config.Mempool,
  329. proxyApp.Mempool(),
  330. state.LastBlockHeight,
  331. mempoolv0.WithMetrics(mempool.NopMetrics()),
  332. mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
  333. mempoolv0.WithPostCheck(sm.TxPostCheck(state)),
  334. )
  335. mp.SetLogger(logger)
  336. // fill the mempool with one txs just below the maximum size
  337. txLength := int(types.MaxDataBytesNoEvidence(maxBytes, types.MaxVotesCount))
  338. tx := tmrand.Bytes(txLength - 6) // to account for the varint
  339. err = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  340. assert.NoError(t, err)
  341. // now produce more txs than what a normal block can hold with 10 smaller txs
  342. // At the end of the test, only the single big tx should be added
  343. for i := 0; i < 10; i++ {
  344. tx := tmrand.Bytes(10)
  345. err = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  346. assert.NoError(t, err)
  347. }
  348. blockExec := sm.NewBlockExecutor(
  349. stateStore,
  350. logger,
  351. proxyApp.Consensus(),
  352. mp,
  353. sm.EmptyEvidencePool{},
  354. blockStore,
  355. )
  356. blockID := types.BlockID{
  357. Hash: tmhash.Sum([]byte("blockID_hash")),
  358. PartSetHeader: types.PartSetHeader{
  359. Total: math.MaxInt32,
  360. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  361. },
  362. }
  363. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  364. // change state in order to produce the largest accepted header
  365. state.LastBlockID = blockID
  366. state.LastBlockHeight = math.MaxInt64 - 1
  367. state.LastBlockTime = timestamp
  368. state.LastResultsHash = tmhash.Sum([]byte("last_results_hash"))
  369. state.AppHash = tmhash.Sum([]byte("app_hash"))
  370. state.Version.Consensus.Block = math.MaxInt64
  371. state.Version.Consensus.App = math.MaxInt64
  372. maxChainID := ""
  373. for i := 0; i < types.MaxChainIDLen; i++ {
  374. maxChainID += "𠜎"
  375. }
  376. state.ChainID = maxChainID
  377. cs := types.CommitSig{
  378. BlockIDFlag: types.BlockIDFlagNil,
  379. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  380. Timestamp: timestamp,
  381. Signature: crypto.CRandBytes(types.MaxSignatureSize),
  382. }
  383. commit := &types.Commit{
  384. Height: math.MaxInt64,
  385. Round: math.MaxInt32,
  386. BlockID: blockID,
  387. }
  388. // add maximum amount of signatures to a single commit
  389. for i := 0; i < types.MaxVotesCount; i++ {
  390. commit.Signatures = append(commit.Signatures, cs)
  391. }
  392. block, partSet := blockExec.CreateProposalBlock(
  393. math.MaxInt64,
  394. state, commit,
  395. proposerAddr,
  396. )
  397. // this ensures that the header is at max size
  398. block.Header.Time = timestamp
  399. pb, err := block.ToProto()
  400. require.NoError(t, err)
  401. // require that the header and commit be the max possible size
  402. require.Equal(t, int64(pb.Header.Size()), types.MaxHeaderBytes)
  403. require.Equal(t, int64(pb.LastCommit.Size()), types.MaxCommitBytes(types.MaxVotesCount))
  404. // make sure that the block is less than the max possible size
  405. assert.Equal(t, int64(pb.Size()), maxBytes)
  406. // because of the proto overhead we expect the part set bytes to be equal or
  407. // less than the pb block size
  408. assert.LessOrEqual(t, partSet.ByteSize(), int64(pb.Size()))
  409. }
  410. func TestNodeNewSeedNode(t *testing.T) {
  411. config := cfg.ResetTestRoot("node_new_node_custom_reactors_test")
  412. config.Mode = cfg.ModeSeed
  413. defer os.RemoveAll(config.RootDir)
  414. nodeKey, err := types.LoadOrGenNodeKey(config.NodeKeyFile())
  415. require.NoError(t, err)
  416. ns, err := makeSeedNode(config,
  417. cfg.DefaultDBProvider,
  418. nodeKey,
  419. defaultGenesisDocProviderFunc(config),
  420. log.TestingLogger(),
  421. )
  422. require.NoError(t, err)
  423. n, ok := ns.(*nodeImpl)
  424. require.True(t, ok)
  425. err = n.Start()
  426. require.NoError(t, err)
  427. assert.True(t, n.pexReactor.IsRunning())
  428. }
  429. func TestNodeSetEventSink(t *testing.T) {
  430. config := cfg.ResetTestRoot("node_app_version_test")
  431. defer os.RemoveAll(config.RootDir)
  432. logger := log.TestingLogger()
  433. setupTest := func(t *testing.T, conf *cfg.Config) []indexer.EventSink {
  434. eventBus, err := createAndStartEventBus(logger)
  435. require.NoError(t, err)
  436. genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
  437. require.NoError(t, err)
  438. indexService, eventSinks, err := createAndStartIndexerService(config,
  439. cfg.DefaultDBProvider, eventBus, logger, genDoc.ChainID)
  440. require.NoError(t, err)
  441. t.Cleanup(func() { require.NoError(t, indexService.Stop()) })
  442. return eventSinks
  443. }
  444. eventSinks := setupTest(t, config)
  445. assert.Equal(t, 1, len(eventSinks))
  446. assert.Equal(t, indexer.KV, eventSinks[0].Type())
  447. config.TxIndex.Indexer = []string{"null"}
  448. eventSinks = setupTest(t, config)
  449. assert.Equal(t, 1, len(eventSinks))
  450. assert.Equal(t, indexer.NULL, eventSinks[0].Type())
  451. config.TxIndex.Indexer = []string{"null", "kv"}
  452. eventSinks = setupTest(t, config)
  453. assert.Equal(t, 1, len(eventSinks))
  454. assert.Equal(t, indexer.NULL, eventSinks[0].Type())
  455. config.TxIndex.Indexer = []string{"kvv"}
  456. ns, err := newDefaultNode(config, logger)
  457. assert.Nil(t, ns)
  458. assert.Equal(t, errors.New("unsupported event sink type"), err)
  459. config.TxIndex.Indexer = []string{}
  460. eventSinks = setupTest(t, config)
  461. assert.Equal(t, 1, len(eventSinks))
  462. assert.Equal(t, indexer.NULL, eventSinks[0].Type())
  463. config.TxIndex.Indexer = []string{"psql"}
  464. ns, err = newDefaultNode(config, logger)
  465. assert.Nil(t, ns)
  466. assert.Equal(t, errors.New("the psql connection settings cannot be empty"), err)
  467. var psqlConn = "test"
  468. config.TxIndex.Indexer = []string{"psql"}
  469. config.TxIndex.PsqlConn = psqlConn
  470. eventSinks = setupTest(t, config)
  471. assert.Equal(t, 1, len(eventSinks))
  472. assert.Equal(t, indexer.PSQL, eventSinks[0].Type())
  473. config.TxIndex.Indexer = []string{"psql", "kv"}
  474. config.TxIndex.PsqlConn = psqlConn
  475. eventSinks = setupTest(t, config)
  476. assert.Equal(t, 2, len(eventSinks))
  477. // we use map to filter the duplicated sinks, so it's not guarantee the order when append sinks.
  478. if eventSinks[0].Type() == indexer.KV {
  479. assert.Equal(t, indexer.PSQL, eventSinks[1].Type())
  480. } else {
  481. assert.Equal(t, indexer.PSQL, eventSinks[0].Type())
  482. assert.Equal(t, indexer.KV, eventSinks[1].Type())
  483. }
  484. config.TxIndex.Indexer = []string{"kv", "psql"}
  485. config.TxIndex.PsqlConn = psqlConn
  486. eventSinks = setupTest(t, config)
  487. assert.Equal(t, 2, len(eventSinks))
  488. if eventSinks[0].Type() == indexer.KV {
  489. assert.Equal(t, indexer.PSQL, eventSinks[1].Type())
  490. } else {
  491. assert.Equal(t, indexer.PSQL, eventSinks[0].Type())
  492. assert.Equal(t, indexer.KV, eventSinks[1].Type())
  493. }
  494. var e = errors.New("found duplicated sinks, please check the tx-index section in the config.toml")
  495. config.TxIndex.Indexer = []string{"psql", "kv", "Kv"}
  496. config.TxIndex.PsqlConn = psqlConn
  497. _, err = newDefaultNode(config, logger)
  498. require.Error(t, err)
  499. assert.Equal(t, e, err)
  500. config.TxIndex.Indexer = []string{"Psql", "kV", "kv", "pSql"}
  501. config.TxIndex.PsqlConn = psqlConn
  502. _, err = newDefaultNode(config, logger)
  503. require.Error(t, err)
  504. assert.Equal(t, e, err)
  505. }
  506. func state(nVals int, height int64) (sm.State, dbm.DB, []types.PrivValidator) {
  507. privVals := make([]types.PrivValidator, nVals)
  508. vals := make([]types.GenesisValidator, nVals)
  509. for i := 0; i < nVals; i++ {
  510. privVal := types.NewMockPV()
  511. privVals[i] = privVal
  512. vals[i] = types.GenesisValidator{
  513. Address: privVal.PrivKey.PubKey().Address(),
  514. PubKey: privVal.PrivKey.PubKey(),
  515. Power: 1000,
  516. Name: fmt.Sprintf("test%d", i),
  517. }
  518. }
  519. s, _ := sm.MakeGenesisState(&types.GenesisDoc{
  520. ChainID: "test-chain",
  521. Validators: vals,
  522. AppHash: nil,
  523. })
  524. // save validators to db for 2 heights
  525. stateDB := dbm.NewMemDB()
  526. stateStore := sm.NewStore(stateDB)
  527. if err := stateStore.Save(s); err != nil {
  528. panic(err)
  529. }
  530. for i := 1; i < int(height); i++ {
  531. s.LastBlockHeight++
  532. s.LastValidators = s.Validators.Copy()
  533. if err := stateStore.Save(s); err != nil {
  534. panic(err)
  535. }
  536. }
  537. return s, stateDB, privVals
  538. }
  539. func TestLoadStateFromGenesis(t *testing.T) {
  540. _ = loadStatefromGenesis(t)
  541. }
  542. func loadStatefromGenesis(t *testing.T) sm.State {
  543. t.Helper()
  544. stateDB := dbm.NewMemDB()
  545. stateStore := sm.NewStore(stateDB)
  546. config := cfg.ResetTestRoot("load_state_from_genesis")
  547. loadedState, err := stateStore.Load()
  548. require.NoError(t, err)
  549. require.True(t, loadedState.IsEmpty())
  550. genDoc, _ := factory.RandGenesisDoc(config, 0, false, 10)
  551. state, err := loadStateFromDBOrGenesisDocProvider(
  552. stateStore,
  553. genDoc,
  554. )
  555. require.NoError(t, err)
  556. require.NotNil(t, state)
  557. return state
  558. }
  559. func TestNodeStartStateSync(t *testing.T) {
  560. mockSSR := &statesync.MockSyncReactor{}
  561. mockFSR := &consmocks.BlockSyncReactor{}
  562. mockCSR := &consmocks.ConsSyncReactor{}
  563. mockSP := &ssmocks.StateProvider{}
  564. state := loadStatefromGenesis(t)
  565. config := cfg.ResetTestRoot("load_state_from_genesis")
  566. eventBus, err := createAndStartEventBus(log.TestingLogger())
  567. defer func() {
  568. err := eventBus.Stop()
  569. require.NoError(t, err)
  570. }()
  571. require.NoError(t, err)
  572. require.NotNil(t, eventBus)
  573. sub, err := eventBus.Subscribe(context.Background(), "test-client", types.EventQueryStateSyncStatus, 10)
  574. require.NoError(t, err)
  575. require.NotNil(t, sub)
  576. cfgSS := config.StateSync
  577. mockSSR.On("Sync", context.TODO(), mockSP, cfgSS.DiscoveryTime).Return(state, nil).
  578. On("Backfill", state).Return(nil)
  579. mockCSR.On("SetStateSyncingMetrics", float64(0)).Return().
  580. On("SwitchToConsensus", state, true).Return()
  581. require.NoError(t,
  582. startStateSync(mockSSR, mockFSR, mockCSR, mockSP, config.StateSync, false, state.InitialHeight, eventBus))
  583. for cnt := 0; cnt < 2; {
  584. select {
  585. case <-time.After(3 * time.Second):
  586. t.Errorf("StateSyncStatus timeout")
  587. case msg := <-sub.Out():
  588. if cnt == 0 {
  589. ensureStateSyncStatus(t, msg, false, state.InitialHeight)
  590. cnt++
  591. } else {
  592. // the state height = 0 because we are not actually update the state in this test
  593. ensureStateSyncStatus(t, msg, true, 0)
  594. cnt++
  595. }
  596. }
  597. }
  598. mockSSR.AssertNumberOfCalls(t, "Sync", 1)
  599. mockSSR.AssertNumberOfCalls(t, "Backfill", 1)
  600. mockCSR.AssertNumberOfCalls(t, "SetStateSyncingMetrics", 1)
  601. mockCSR.AssertNumberOfCalls(t, "SwitchToConsensus", 1)
  602. }
  603. func ensureStateSyncStatus(t *testing.T, msg tmpubsub.Message, complete bool, height int64) {
  604. t.Helper()
  605. status, ok := msg.Data().(types.EventDataStateSyncStatus)
  606. require.True(t, ok)
  607. require.Equal(t, complete, status.Complete)
  608. require.Equal(t, height, status.Height)
  609. }