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.

667 lines
19 KiB

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. "github.com/tendermint/tendermint/internal/evidence"
  21. "github.com/tendermint/tendermint/internal/mempool"
  22. mempoolv0 "github.com/tendermint/tendermint/internal/mempool/v0"
  23. "github.com/tendermint/tendermint/internal/test/factory"
  24. "github.com/tendermint/tendermint/libs/log"
  25. tmrand "github.com/tendermint/tendermint/libs/rand"
  26. tmtime "github.com/tendermint/tendermint/libs/time"
  27. "github.com/tendermint/tendermint/privval"
  28. "github.com/tendermint/tendermint/proxy"
  29. sm "github.com/tendermint/tendermint/state"
  30. "github.com/tendermint/tendermint/state/indexer"
  31. "github.com/tendermint/tendermint/store"
  32. "github.com/tendermint/tendermint/types"
  33. )
  34. func TestNodeStartStop(t *testing.T) {
  35. config := cfg.ResetTestRoot("node_node_test")
  36. defer os.RemoveAll(config.RootDir)
  37. // create & start node
  38. ns, err := newDefaultNode(config, log.TestingLogger())
  39. require.NoError(t, err)
  40. require.NoError(t, ns.Start())
  41. n, ok := ns.(*nodeImpl)
  42. require.True(t, ok)
  43. // wait for the node to produce a block
  44. blocksSub, err := n.EventBus().Subscribe(context.Background(), "node_test", types.EventQueryNewBlock)
  45. require.NoError(t, err)
  46. select {
  47. case <-blocksSub.Out():
  48. case <-blocksSub.Canceled():
  49. t.Fatal("blocksSub was canceled")
  50. case <-time.After(10 * time.Second):
  51. t.Fatal("timed out waiting for the node to produce a block")
  52. }
  53. // stop the node
  54. go func() {
  55. err = n.Stop()
  56. require.NoError(t, err)
  57. }()
  58. select {
  59. case <-n.Quit():
  60. case <-time.After(5 * time.Second):
  61. pid := os.Getpid()
  62. p, err := os.FindProcess(pid)
  63. if err != nil {
  64. panic(err)
  65. }
  66. err = p.Signal(syscall.SIGABRT)
  67. fmt.Println(err)
  68. t.Fatal("timed out waiting for shutdown")
  69. }
  70. }
  71. func getTestNode(t *testing.T, conf *cfg.Config, logger log.Logger) *nodeImpl {
  72. t.Helper()
  73. ns, err := newDefaultNode(conf, logger)
  74. require.NoError(t, err)
  75. n, ok := ns.(*nodeImpl)
  76. require.True(t, ok)
  77. return n
  78. }
  79. func TestNodeDelayedStart(t *testing.T) {
  80. config := cfg.ResetTestRoot("node_delayed_start_test")
  81. defer os.RemoveAll(config.RootDir)
  82. now := tmtime.Now()
  83. // create & start node
  84. n := getTestNode(t, config, log.TestingLogger())
  85. n.GenesisDoc().GenesisTime = now.Add(2 * time.Second)
  86. require.NoError(t, n.Start())
  87. defer n.Stop() //nolint:errcheck // ignore for tests
  88. startTime := tmtime.Now()
  89. assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime))
  90. }
  91. func TestNodeSetAppVersion(t *testing.T) {
  92. config := cfg.ResetTestRoot("node_app_version_test")
  93. defer os.RemoveAll(config.RootDir)
  94. // create node
  95. n := getTestNode(t, config, log.TestingLogger())
  96. // default config uses the kvstore app
  97. var appVersion uint64 = kvstore.ProtocolVersion
  98. // check version is set in state
  99. state, err := n.stateStore.Load()
  100. require.NoError(t, err)
  101. assert.Equal(t, state.Version.Consensus.App, appVersion)
  102. // check version is set in node info
  103. assert.Equal(t, n.nodeInfo.ProtocolVersion.App, appVersion)
  104. }
  105. func TestNodeSetPrivValTCP(t *testing.T) {
  106. addr := "tcp://" + testFreeAddr(t)
  107. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  108. defer os.RemoveAll(config.RootDir)
  109. config.PrivValidator.ListenAddr = addr
  110. dialer := privval.DialTCPFn(addr, 100*time.Millisecond, ed25519.GenPrivKey())
  111. dialerEndpoint := privval.NewSignerDialerEndpoint(
  112. log.TestingLogger(),
  113. dialer,
  114. )
  115. privval.SignerDialerEndpointTimeoutReadWrite(100 * time.Millisecond)(dialerEndpoint)
  116. signerServer := privval.NewSignerServer(
  117. dialerEndpoint,
  118. config.ChainID(),
  119. types.NewMockPV(),
  120. )
  121. go func() {
  122. err := signerServer.Start()
  123. if err != nil {
  124. panic(err)
  125. }
  126. }()
  127. defer signerServer.Stop() //nolint:errcheck // ignore for tests
  128. n := getTestNode(t, config, log.TestingLogger())
  129. assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator())
  130. }
  131. // address without a protocol must result in error
  132. func TestPrivValidatorListenAddrNoProtocol(t *testing.T) {
  133. addrNoPrefix := testFreeAddr(t)
  134. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  135. defer os.RemoveAll(config.RootDir)
  136. config.PrivValidator.ListenAddr = addrNoPrefix
  137. _, err := newDefaultNode(config, log.TestingLogger())
  138. assert.Error(t, err)
  139. }
  140. func TestNodeSetPrivValIPC(t *testing.T) {
  141. tmpfile := "/tmp/kms." + tmrand.Str(6) + ".sock"
  142. defer os.Remove(tmpfile) // clean up
  143. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  144. defer os.RemoveAll(config.RootDir)
  145. config.PrivValidator.ListenAddr = "unix://" + tmpfile
  146. dialer := privval.DialUnixFn(tmpfile)
  147. dialerEndpoint := privval.NewSignerDialerEndpoint(
  148. log.TestingLogger(),
  149. dialer,
  150. )
  151. privval.SignerDialerEndpointTimeoutReadWrite(100 * time.Millisecond)(dialerEndpoint)
  152. pvsc := privval.NewSignerServer(
  153. dialerEndpoint,
  154. config.ChainID(),
  155. types.NewMockPV(),
  156. )
  157. go func() {
  158. err := pvsc.Start()
  159. require.NoError(t, err)
  160. }()
  161. defer pvsc.Stop() //nolint:errcheck // ignore for tests
  162. n := getTestNode(t, config, log.TestingLogger())
  163. assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator())
  164. }
  165. // testFreeAddr claims a free port so we don't block on listener being ready.
  166. func testFreeAddr(t *testing.T) string {
  167. ln, err := net.Listen("tcp", "127.0.0.1:0")
  168. require.NoError(t, err)
  169. defer ln.Close()
  170. return fmt.Sprintf("127.0.0.1:%d", ln.Addr().(*net.TCPAddr).Port)
  171. }
  172. // create a proposal block using real and full
  173. // mempool and evidence pool and validate it.
  174. func TestCreateProposalBlock(t *testing.T) {
  175. config := cfg.ResetTestRoot("node_create_proposal")
  176. defer os.RemoveAll(config.RootDir)
  177. cc := proxy.NewLocalClientCreator(kvstore.NewApplication())
  178. proxyApp := proxy.NewAppConns(cc)
  179. err := proxyApp.Start()
  180. require.Nil(t, err)
  181. defer proxyApp.Stop() //nolint:errcheck // ignore for tests
  182. logger := log.TestingLogger()
  183. const height int64 = 1
  184. state, stateDB, privVals := state(1, height)
  185. stateStore := sm.NewStore(stateDB)
  186. maxBytes := 16384
  187. const partSize uint32 = 256
  188. maxEvidenceBytes := int64(maxBytes / 2)
  189. state.ConsensusParams.Block.MaxBytes = int64(maxBytes)
  190. state.ConsensusParams.Evidence.MaxBytes = maxEvidenceBytes
  191. proposerAddr, _ := state.Validators.GetByIndex(0)
  192. mp := mempoolv0.NewCListMempool(
  193. config.Mempool,
  194. proxyApp.Mempool(),
  195. state.LastBlockHeight,
  196. mempoolv0.WithMetrics(mempool.NopMetrics()),
  197. mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
  198. mempoolv0.WithPostCheck(sm.TxPostCheck(state)),
  199. )
  200. mp.SetLogger(logger)
  201. // Make EvidencePool
  202. evidenceDB := dbm.NewMemDB()
  203. blockStore := store.NewBlockStore(dbm.NewMemDB())
  204. evidencePool, err := evidence.NewPool(logger, evidenceDB, stateStore, blockStore)
  205. require.NoError(t, err)
  206. // fill the evidence pool with more evidence
  207. // than can fit in a block
  208. var currentBytes int64 = 0
  209. for currentBytes <= maxEvidenceBytes {
  210. ev := types.NewMockDuplicateVoteEvidenceWithValidator(height, time.Now(), privVals[0], "test-chain")
  211. currentBytes += int64(len(ev.Bytes()))
  212. evidencePool.ReportConflictingVotes(ev.VoteA, ev.VoteB)
  213. }
  214. evList, size := evidencePool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  215. require.Less(t, size, state.ConsensusParams.Evidence.MaxBytes+1)
  216. evData := &types.EvidenceData{Evidence: evList}
  217. require.EqualValues(t, size, evData.ByteSize())
  218. // fill the mempool with more txs
  219. // than can fit in a block
  220. txLength := 100
  221. for i := 0; i <= maxBytes/txLength; i++ {
  222. tx := tmrand.Bytes(txLength)
  223. err := mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  224. assert.NoError(t, err)
  225. }
  226. blockExec := sm.NewBlockExecutor(
  227. stateStore,
  228. logger,
  229. proxyApp.Consensus(),
  230. mp,
  231. evidencePool,
  232. blockStore,
  233. )
  234. commit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
  235. block, _ := blockExec.CreateProposalBlock(
  236. height,
  237. state, commit,
  238. proposerAddr,
  239. )
  240. // check that the part set does not exceed the maximum block size
  241. partSet := block.MakePartSet(partSize)
  242. assert.Less(t, partSet.ByteSize(), int64(maxBytes))
  243. partSetFromHeader := types.NewPartSetFromHeader(partSet.Header())
  244. for partSetFromHeader.Count() < partSetFromHeader.Total() {
  245. added, err := partSetFromHeader.AddPart(partSet.GetPart(int(partSetFromHeader.Count())))
  246. require.NoError(t, err)
  247. require.True(t, added)
  248. }
  249. assert.EqualValues(t, partSetFromHeader.ByteSize(), partSet.ByteSize())
  250. err = blockExec.ValidateBlock(state, block)
  251. assert.NoError(t, err)
  252. }
  253. func TestMaxTxsProposalBlockSize(t *testing.T) {
  254. config := cfg.ResetTestRoot("node_create_proposal")
  255. defer os.RemoveAll(config.RootDir)
  256. cc := proxy.NewLocalClientCreator(kvstore.NewApplication())
  257. proxyApp := proxy.NewAppConns(cc)
  258. err := proxyApp.Start()
  259. require.Nil(t, err)
  260. defer proxyApp.Stop() //nolint:errcheck // ignore for tests
  261. logger := log.TestingLogger()
  262. const height int64 = 1
  263. state, stateDB, _ := state(1, height)
  264. stateStore := sm.NewStore(stateDB)
  265. blockStore := store.NewBlockStore(dbm.NewMemDB())
  266. const maxBytes int64 = 16384
  267. const partSize uint32 = 256
  268. state.ConsensusParams.Block.MaxBytes = maxBytes
  269. proposerAddr, _ := state.Validators.GetByIndex(0)
  270. // Make Mempool
  271. mp := mempoolv0.NewCListMempool(
  272. config.Mempool,
  273. proxyApp.Mempool(),
  274. state.LastBlockHeight,
  275. mempoolv0.WithMetrics(mempool.NopMetrics()),
  276. mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
  277. mempoolv0.WithPostCheck(sm.TxPostCheck(state)),
  278. )
  279. mp.SetLogger(logger)
  280. // fill the mempool with one txs just below the maximum size
  281. txLength := int(types.MaxDataBytesNoEvidence(maxBytes, 1))
  282. tx := tmrand.Bytes(txLength - 4) // to account for the varint
  283. err = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  284. assert.NoError(t, err)
  285. blockExec := sm.NewBlockExecutor(
  286. stateStore,
  287. logger,
  288. proxyApp.Consensus(),
  289. mp,
  290. sm.EmptyEvidencePool{},
  291. blockStore,
  292. )
  293. commit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
  294. block, _ := blockExec.CreateProposalBlock(
  295. height,
  296. state, commit,
  297. proposerAddr,
  298. )
  299. pb, err := block.ToProto()
  300. require.NoError(t, err)
  301. assert.Less(t, int64(pb.Size()), maxBytes)
  302. // check that the part set does not exceed the maximum block size
  303. partSet := block.MakePartSet(partSize)
  304. assert.EqualValues(t, partSet.ByteSize(), int64(pb.Size()))
  305. }
  306. func TestMaxProposalBlockSize(t *testing.T) {
  307. config := cfg.ResetTestRoot("node_create_proposal")
  308. defer os.RemoveAll(config.RootDir)
  309. cc := proxy.NewLocalClientCreator(kvstore.NewApplication())
  310. proxyApp := proxy.NewAppConns(cc)
  311. err := proxyApp.Start()
  312. require.Nil(t, err)
  313. defer proxyApp.Stop() //nolint:errcheck // ignore for tests
  314. logger := log.TestingLogger()
  315. state, stateDB, _ := state(types.MaxVotesCount, int64(1))
  316. stateStore := sm.NewStore(stateDB)
  317. blockStore := store.NewBlockStore(dbm.NewMemDB())
  318. const maxBytes int64 = 1024 * 1024 * 2
  319. state.ConsensusParams.Block.MaxBytes = maxBytes
  320. proposerAddr, _ := state.Validators.GetByIndex(0)
  321. // Make Mempool
  322. mp := mempoolv0.NewCListMempool(
  323. config.Mempool,
  324. proxyApp.Mempool(),
  325. state.LastBlockHeight,
  326. mempoolv0.WithMetrics(mempool.NopMetrics()),
  327. mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
  328. mempoolv0.WithPostCheck(sm.TxPostCheck(state)),
  329. )
  330. mp.SetLogger(logger)
  331. // fill the mempool with one txs just below the maximum size
  332. txLength := int(types.MaxDataBytesNoEvidence(maxBytes, types.MaxVotesCount))
  333. tx := tmrand.Bytes(txLength - 6) // to account for the varint
  334. err = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  335. assert.NoError(t, err)
  336. // now produce more txs than what a normal block can hold with 10 smaller txs
  337. // At the end of the test, only the single big tx should be added
  338. for i := 0; i < 10; i++ {
  339. tx := tmrand.Bytes(10)
  340. err = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{})
  341. assert.NoError(t, err)
  342. }
  343. blockExec := sm.NewBlockExecutor(
  344. stateStore,
  345. logger,
  346. proxyApp.Consensus(),
  347. mp,
  348. sm.EmptyEvidencePool{},
  349. blockStore,
  350. )
  351. blockID := types.BlockID{
  352. Hash: tmhash.Sum([]byte("blockID_hash")),
  353. PartSetHeader: types.PartSetHeader{
  354. Total: math.MaxInt32,
  355. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  356. },
  357. }
  358. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  359. // change state in order to produce the largest accepted header
  360. state.LastBlockID = blockID
  361. state.LastBlockHeight = math.MaxInt64 - 1
  362. state.LastBlockTime = timestamp
  363. state.LastResultsHash = tmhash.Sum([]byte("last_results_hash"))
  364. state.AppHash = tmhash.Sum([]byte("app_hash"))
  365. state.Version.Consensus.Block = math.MaxInt64
  366. state.Version.Consensus.App = math.MaxInt64
  367. maxChainID := ""
  368. for i := 0; i < types.MaxChainIDLen; i++ {
  369. maxChainID += "𠜎"
  370. }
  371. state.ChainID = maxChainID
  372. cs := types.CommitSig{
  373. BlockIDFlag: types.BlockIDFlagNil,
  374. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  375. Timestamp: timestamp,
  376. Signature: crypto.CRandBytes(types.MaxSignatureSize),
  377. }
  378. commit := &types.Commit{
  379. Height: math.MaxInt64,
  380. Round: math.MaxInt32,
  381. BlockID: blockID,
  382. }
  383. // add maximum amount of signatures to a single commit
  384. for i := 0; i < types.MaxVotesCount; i++ {
  385. commit.Signatures = append(commit.Signatures, cs)
  386. }
  387. block, partSet := blockExec.CreateProposalBlock(
  388. math.MaxInt64,
  389. state, commit,
  390. proposerAddr,
  391. )
  392. // this ensures that the header is at max size
  393. block.Header.Time = timestamp
  394. pb, err := block.ToProto()
  395. require.NoError(t, err)
  396. // require that the header and commit be the max possible size
  397. require.Equal(t, int64(pb.Header.Size()), types.MaxHeaderBytes)
  398. require.Equal(t, int64(pb.LastCommit.Size()), types.MaxCommitBytes(types.MaxVotesCount))
  399. // make sure that the block is less than the max possible size
  400. assert.Equal(t, int64(pb.Size()), maxBytes)
  401. // because of the proto overhead we expect the part set bytes to be equal or
  402. // less than the pb block size
  403. assert.LessOrEqual(t, partSet.ByteSize(), int64(pb.Size()))
  404. }
  405. func TestNodeNewSeedNode(t *testing.T) {
  406. config := cfg.ResetTestRoot("node_new_node_custom_reactors_test")
  407. config.Mode = cfg.ModeSeed
  408. defer os.RemoveAll(config.RootDir)
  409. nodeKey, err := types.LoadOrGenNodeKey(config.NodeKeyFile())
  410. require.NoError(t, err)
  411. ns, err := makeSeedNode(config,
  412. cfg.DefaultDBProvider,
  413. nodeKey,
  414. defaultGenesisDocProviderFunc(config),
  415. log.TestingLogger(),
  416. )
  417. require.NoError(t, err)
  418. n, ok := ns.(*nodeImpl)
  419. require.True(t, ok)
  420. err = n.Start()
  421. require.NoError(t, err)
  422. assert.True(t, n.pexReactor.IsRunning())
  423. }
  424. func TestNodeSetEventSink(t *testing.T) {
  425. config := cfg.ResetTestRoot("node_app_version_test")
  426. defer os.RemoveAll(config.RootDir)
  427. logger := log.TestingLogger()
  428. setupTest := func(t *testing.T, conf *cfg.Config) []indexer.EventSink {
  429. eventBus, err := createAndStartEventBus(logger)
  430. require.NoError(t, err)
  431. genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
  432. require.NoError(t, err)
  433. indexService, eventSinks, err := createAndStartIndexerService(config,
  434. cfg.DefaultDBProvider, eventBus, logger, genDoc.ChainID)
  435. require.NoError(t, err)
  436. t.Cleanup(func() { require.NoError(t, indexService.Stop()) })
  437. return eventSinks
  438. }
  439. eventSinks := setupTest(t, config)
  440. assert.Equal(t, 1, len(eventSinks))
  441. assert.Equal(t, indexer.KV, eventSinks[0].Type())
  442. config.TxIndex.Indexer = []string{"null"}
  443. eventSinks = setupTest(t, config)
  444. assert.Equal(t, 1, len(eventSinks))
  445. assert.Equal(t, indexer.NULL, eventSinks[0].Type())
  446. config.TxIndex.Indexer = []string{"null", "kv"}
  447. eventSinks = setupTest(t, config)
  448. assert.Equal(t, 1, len(eventSinks))
  449. assert.Equal(t, indexer.NULL, eventSinks[0].Type())
  450. config.TxIndex.Indexer = []string{"kvv"}
  451. ns, err := newDefaultNode(config, logger)
  452. assert.Nil(t, ns)
  453. assert.Equal(t, errors.New("unsupported event sink type"), err)
  454. config.TxIndex.Indexer = []string{}
  455. eventSinks = setupTest(t, config)
  456. assert.Equal(t, 1, len(eventSinks))
  457. assert.Equal(t, indexer.NULL, eventSinks[0].Type())
  458. config.TxIndex.Indexer = []string{"psql"}
  459. ns, err = newDefaultNode(config, logger)
  460. assert.Nil(t, ns)
  461. assert.Equal(t, errors.New("the psql connection settings cannot be empty"), err)
  462. var psqlConn = "test"
  463. config.TxIndex.Indexer = []string{"psql"}
  464. config.TxIndex.PsqlConn = psqlConn
  465. eventSinks = setupTest(t, config)
  466. assert.Equal(t, 1, len(eventSinks))
  467. assert.Equal(t, indexer.PSQL, eventSinks[0].Type())
  468. config.TxIndex.Indexer = []string{"psql", "kv"}
  469. config.TxIndex.PsqlConn = psqlConn
  470. eventSinks = setupTest(t, config)
  471. assert.Equal(t, 2, len(eventSinks))
  472. // we use map to filter the duplicated sinks, so it's not guarantee the order when append sinks.
  473. if eventSinks[0].Type() == indexer.KV {
  474. assert.Equal(t, indexer.PSQL, eventSinks[1].Type())
  475. } else {
  476. assert.Equal(t, indexer.PSQL, eventSinks[0].Type())
  477. assert.Equal(t, indexer.KV, eventSinks[1].Type())
  478. }
  479. config.TxIndex.Indexer = []string{"kv", "psql"}
  480. config.TxIndex.PsqlConn = psqlConn
  481. eventSinks = setupTest(t, config)
  482. assert.Equal(t, 2, len(eventSinks))
  483. if eventSinks[0].Type() == indexer.KV {
  484. assert.Equal(t, indexer.PSQL, eventSinks[1].Type())
  485. } else {
  486. assert.Equal(t, indexer.PSQL, eventSinks[0].Type())
  487. assert.Equal(t, indexer.KV, eventSinks[1].Type())
  488. }
  489. var e = errors.New("found duplicated sinks, please check the tx-index section in the config.toml")
  490. config.TxIndex.Indexer = []string{"psql", "kv", "Kv"}
  491. config.TxIndex.PsqlConn = psqlConn
  492. _, err = newDefaultNode(config, logger)
  493. require.Error(t, err)
  494. assert.Equal(t, e, err)
  495. config.TxIndex.Indexer = []string{"Psql", "kV", "kv", "pSql"}
  496. config.TxIndex.PsqlConn = psqlConn
  497. _, err = newDefaultNode(config, logger)
  498. require.Error(t, err)
  499. assert.Equal(t, e, err)
  500. }
  501. func state(nVals int, height int64) (sm.State, dbm.DB, []types.PrivValidator) {
  502. privVals := make([]types.PrivValidator, nVals)
  503. vals := make([]types.GenesisValidator, nVals)
  504. for i := 0; i < nVals; i++ {
  505. privVal := types.NewMockPV()
  506. privVals[i] = privVal
  507. vals[i] = types.GenesisValidator{
  508. Address: privVal.PrivKey.PubKey().Address(),
  509. PubKey: privVal.PrivKey.PubKey(),
  510. Power: 1000,
  511. Name: fmt.Sprintf("test%d", i),
  512. }
  513. }
  514. s, _ := sm.MakeGenesisState(&types.GenesisDoc{
  515. ChainID: "test-chain",
  516. Validators: vals,
  517. AppHash: nil,
  518. })
  519. // save validators to db for 2 heights
  520. stateDB := dbm.NewMemDB()
  521. stateStore := sm.NewStore(stateDB)
  522. if err := stateStore.Save(s); err != nil {
  523. panic(err)
  524. }
  525. for i := 1; i < int(height); i++ {
  526. s.LastBlockHeight++
  527. s.LastValidators = s.Validators.Copy()
  528. if err := stateStore.Save(s); err != nil {
  529. panic(err)
  530. }
  531. }
  532. return s, stateDB, privVals
  533. }
  534. func TestLoadStateFromGenesis(t *testing.T) {
  535. _ = loadStatefromGenesis(t)
  536. }
  537. func loadStatefromGenesis(t *testing.T) sm.State {
  538. t.Helper()
  539. stateDB := dbm.NewMemDB()
  540. stateStore := sm.NewStore(stateDB)
  541. config := cfg.ResetTestRoot("load_state_from_genesis")
  542. loadedState, err := stateStore.Load()
  543. require.NoError(t, err)
  544. require.True(t, loadedState.IsEmpty())
  545. genDoc, _ := factory.RandGenesisDoc(config, 0, false, 10)
  546. state, err := loadStateFromDBOrGenesisDocProvider(
  547. stateStore,
  548. genDoc,
  549. )
  550. require.NoError(t, err)
  551. require.NotNil(t, state)
  552. return state
  553. }