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