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.

1105 lines
35 KiB

cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
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
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
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
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
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
6 years ago
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
6 years ago
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
6 years ago
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
  1. package consensus
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "testing"
  12. "time"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "sort"
  16. "github.com/tendermint/tendermint/abci/example/kvstore"
  17. abci "github.com/tendermint/tendermint/abci/types"
  18. cfg "github.com/tendermint/tendermint/config"
  19. "github.com/tendermint/tendermint/crypto"
  20. cmn "github.com/tendermint/tendermint/libs/common"
  21. dbm "github.com/tendermint/tendermint/libs/db"
  22. "github.com/tendermint/tendermint/libs/log"
  23. "github.com/tendermint/tendermint/mock"
  24. "github.com/tendermint/tendermint/privval"
  25. "github.com/tendermint/tendermint/proxy"
  26. sm "github.com/tendermint/tendermint/state"
  27. "github.com/tendermint/tendermint/types"
  28. tmtime "github.com/tendermint/tendermint/types/time"
  29. "github.com/tendermint/tendermint/version"
  30. )
  31. func TestMain(m *testing.M) {
  32. config = ResetConfig("consensus_reactor_test")
  33. consensusReplayConfig = ResetConfig("consensus_replay_test")
  34. configStateTest := ResetConfig("consensus_state_test")
  35. configMempoolTest := ResetConfig("consensus_mempool_test")
  36. configByzantineTest := ResetConfig("consensus_byzantine_test")
  37. code := m.Run()
  38. os.RemoveAll(config.RootDir)
  39. os.RemoveAll(consensusReplayConfig.RootDir)
  40. os.RemoveAll(configStateTest.RootDir)
  41. os.RemoveAll(configMempoolTest.RootDir)
  42. os.RemoveAll(configByzantineTest.RootDir)
  43. os.Exit(code)
  44. }
  45. // These tests ensure we can always recover from failure at any part of the consensus process.
  46. // There are two general failure scenarios: failure during consensus, and failure while applying the block.
  47. // Only the latter interacts with the app and store,
  48. // but the former has to deal with restrictions on re-use of priv_validator keys.
  49. // The `WAL Tests` are for failures during the consensus;
  50. // the `Handshake Tests` are for failures in applying the block.
  51. // With the help of the WAL, we can recover from it all!
  52. //------------------------------------------------------------------------------------------
  53. // WAL Tests
  54. // TODO: It would be better to verify explicitly which states we can recover from without the wal
  55. // and which ones we need the wal for - then we'd also be able to only flush the
  56. // wal writer when we need to, instead of with every message.
  57. func startNewConsensusStateAndWaitForBlock(t *testing.T, consensusReplayConfig *cfg.Config,
  58. lastBlockHeight int64, blockDB dbm.DB, stateDB dbm.DB) {
  59. logger := log.TestingLogger()
  60. state, _ := sm.LoadStateFromDBOrGenesisFile(stateDB, consensusReplayConfig.GenesisFile())
  61. privValidator := loadPrivValidator(consensusReplayConfig)
  62. cs := newConsensusStateWithConfigAndBlockStore(consensusReplayConfig, state, privValidator, kvstore.NewKVStoreApplication(), blockDB)
  63. cs.SetLogger(logger)
  64. bytes, _ := ioutil.ReadFile(cs.config.WalFile())
  65. t.Logf("====== WAL: \n\r%X\n", bytes)
  66. err := cs.Start()
  67. require.NoError(t, err)
  68. defer cs.Stop()
  69. // This is just a signal that we haven't halted; its not something contained
  70. // in the WAL itself. Assuming the consensus state is running, replay of any
  71. // WAL, including the empty one, should eventually be followed by a new
  72. // block, or else something is wrong.
  73. newBlockSub, err := cs.eventBus.Subscribe(context.Background(), testSubscriber, types.EventQueryNewBlock)
  74. require.NoError(t, err)
  75. select {
  76. case <-newBlockSub.Out():
  77. case <-newBlockSub.Cancelled():
  78. t.Fatal("newBlockSub was cancelled")
  79. case <-time.After(120 * time.Second):
  80. t.Fatal("Timed out waiting for new block (see trace above)")
  81. }
  82. }
  83. func sendTxs(ctx context.Context, cs *ConsensusState) {
  84. for i := 0; i < 256; i++ {
  85. select {
  86. case <-ctx.Done():
  87. return
  88. default:
  89. tx := []byte{byte(i)}
  90. assertMempool(cs.txNotifier).CheckTx(tx, nil)
  91. i++
  92. }
  93. }
  94. }
  95. // TestWALCrash uses crashing WAL to test we can recover from any WAL failure.
  96. func TestWALCrash(t *testing.T) {
  97. testCases := []struct {
  98. name string
  99. initFn func(dbm.DB, *ConsensusState, context.Context)
  100. heightToStop int64
  101. }{
  102. {"empty block",
  103. func(stateDB dbm.DB, cs *ConsensusState, ctx context.Context) {},
  104. 1},
  105. {"many non-empty blocks",
  106. func(stateDB dbm.DB, cs *ConsensusState, ctx context.Context) {
  107. go sendTxs(ctx, cs)
  108. },
  109. 3},
  110. }
  111. for i, tc := range testCases {
  112. consensusReplayConfig := ResetConfig(fmt.Sprintf("%s_%d", t.Name(), i))
  113. t.Run(tc.name, func(t *testing.T) {
  114. crashWALandCheckLiveness(t, consensusReplayConfig, tc.initFn, tc.heightToStop)
  115. })
  116. }
  117. }
  118. func crashWALandCheckLiveness(t *testing.T, consensusReplayConfig *cfg.Config,
  119. initFn func(dbm.DB, *ConsensusState, context.Context), heightToStop int64) {
  120. walPanicked := make(chan error)
  121. crashingWal := &crashingWAL{panicCh: walPanicked, heightToStop: heightToStop}
  122. i := 1
  123. LOOP:
  124. for {
  125. t.Logf("====== LOOP %d\n", i)
  126. // create consensus state from a clean slate
  127. logger := log.NewNopLogger()
  128. blockDB := dbm.NewMemDB()
  129. stateDB := blockDB
  130. state, _ := sm.MakeGenesisStateFromFile(consensusReplayConfig.GenesisFile())
  131. privValidator := loadPrivValidator(consensusReplayConfig)
  132. cs := newConsensusStateWithConfigAndBlockStore(consensusReplayConfig, state, privValidator, kvstore.NewKVStoreApplication(), blockDB)
  133. cs.SetLogger(logger)
  134. // start sending transactions
  135. ctx, cancel := context.WithCancel(context.Background())
  136. initFn(stateDB, cs, ctx)
  137. // clean up WAL file from the previous iteration
  138. walFile := cs.config.WalFile()
  139. os.Remove(walFile)
  140. // set crashing WAL
  141. csWal, err := cs.OpenWAL(walFile)
  142. require.NoError(t, err)
  143. crashingWal.next = csWal
  144. // reset the message counter
  145. crashingWal.msgIndex = 1
  146. cs.wal = crashingWal
  147. // start consensus state
  148. err = cs.Start()
  149. require.NoError(t, err)
  150. i++
  151. select {
  152. case err := <-walPanicked:
  153. t.Logf("WAL panicked: %v", err)
  154. // make sure we can make blocks after a crash
  155. startNewConsensusStateAndWaitForBlock(t, consensusReplayConfig, cs.Height, blockDB, stateDB)
  156. // stop consensus state and transactions sender (initFn)
  157. cs.Stop()
  158. cancel()
  159. // if we reached the required height, exit
  160. if _, ok := err.(ReachedHeightToStopError); ok {
  161. break LOOP
  162. }
  163. case <-time.After(10 * time.Second):
  164. t.Fatal("WAL did not panic for 10 seconds (check the log)")
  165. }
  166. }
  167. }
  168. // crashingWAL is a WAL which crashes or rather simulates a crash during Save
  169. // (before and after). It remembers a message for which we last panicked
  170. // (lastPanickedForMsgIndex), so we don't panic for it in subsequent iterations.
  171. type crashingWAL struct {
  172. next WAL
  173. panicCh chan error
  174. heightToStop int64
  175. msgIndex int // current message index
  176. lastPanickedForMsgIndex int // last message for which we panicked
  177. }
  178. var _ WAL = &crashingWAL{}
  179. // WALWriteError indicates a WAL crash.
  180. type WALWriteError struct {
  181. msg string
  182. }
  183. func (e WALWriteError) Error() string {
  184. return e.msg
  185. }
  186. // ReachedHeightToStopError indicates we've reached the required consensus
  187. // height and may exit.
  188. type ReachedHeightToStopError struct {
  189. height int64
  190. }
  191. func (e ReachedHeightToStopError) Error() string {
  192. return fmt.Sprintf("reached height to stop %d", e.height)
  193. }
  194. // Write simulate WAL's crashing by sending an error to the panicCh and then
  195. // exiting the cs.receiveRoutine.
  196. func (w *crashingWAL) Write(m WALMessage) {
  197. if endMsg, ok := m.(EndHeightMessage); ok {
  198. if endMsg.Height == w.heightToStop {
  199. w.panicCh <- ReachedHeightToStopError{endMsg.Height}
  200. runtime.Goexit()
  201. } else {
  202. w.next.Write(m)
  203. }
  204. return
  205. }
  206. if w.msgIndex > w.lastPanickedForMsgIndex {
  207. w.lastPanickedForMsgIndex = w.msgIndex
  208. _, file, line, _ := runtime.Caller(1)
  209. w.panicCh <- WALWriteError{fmt.Sprintf("failed to write %T to WAL (fileline: %s:%d)", m, file, line)}
  210. runtime.Goexit()
  211. } else {
  212. w.msgIndex++
  213. w.next.Write(m)
  214. }
  215. }
  216. func (w *crashingWAL) WriteSync(m WALMessage) {
  217. w.Write(m)
  218. }
  219. func (w *crashingWAL) FlushAndSync() error { return w.next.FlushAndSync() }
  220. func (w *crashingWAL) SearchForEndHeight(height int64, options *WALSearchOptions) (rd io.ReadCloser, found bool, err error) {
  221. return w.next.SearchForEndHeight(height, options)
  222. }
  223. func (w *crashingWAL) Start() error { return w.next.Start() }
  224. func (w *crashingWAL) Stop() error { return w.next.Stop() }
  225. func (w *crashingWAL) Wait() { w.next.Wait() }
  226. //------------------------------------------------------------------------------------------
  227. type testSim struct {
  228. GenesisState sm.State
  229. Config *cfg.Config
  230. Chain []*types.Block
  231. Commits []*types.Commit
  232. CleanupFunc cleanupFunc
  233. }
  234. const (
  235. numBlocks = 6
  236. )
  237. var (
  238. mempool = mock.Mempool{}
  239. evpool = sm.MockEvidencePool{}
  240. sim testSim
  241. )
  242. //---------------------------------------
  243. // Test handshake/replay
  244. // 0 - all synced up
  245. // 1 - saved block but app and state are behind
  246. // 2 - save block and committed but state is behind
  247. var modes = []uint{0, 1, 2}
  248. // This is actually not a test, it's for storing validator change tx data for testHandshakeReplay
  249. func TestSimulateValidatorsChange(t *testing.T) {
  250. nPeers := 7
  251. nVals := 4
  252. css, genDoc, config, cleanup := randConsensusNetWithPeers(nVals, nPeers, "replay_test", newMockTickerFunc(true), newPersistentKVStoreWithPath)
  253. sim.Config = config
  254. sim.GenesisState, _ = sm.MakeGenesisState(genDoc)
  255. sim.CleanupFunc = cleanup
  256. partSize := types.BlockPartSizeBytes
  257. newRoundCh := subscribe(css[0].eventBus, types.EventQueryNewRound)
  258. proposalCh := subscribe(css[0].eventBus, types.EventQueryCompleteProposal)
  259. vss := make([]*validatorStub, nPeers)
  260. for i := 0; i < nPeers; i++ {
  261. vss[i] = NewValidatorStub(css[i].privValidator, i)
  262. }
  263. height, round := css[0].Height, css[0].Round
  264. // start the machine
  265. startTestRound(css[0], height, round)
  266. incrementHeight(vss...)
  267. ensureNewRound(newRoundCh, height, 0)
  268. ensureNewProposal(proposalCh, height, round)
  269. rs := css[0].GetRoundState()
  270. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
  271. ensureNewRound(newRoundCh, height+1, 0)
  272. //height 2
  273. height++
  274. incrementHeight(vss...)
  275. newValidatorPubKey1 := css[nVals].privValidator.GetPubKey()
  276. valPubKey1ABCI := types.TM2PB.PubKey(newValidatorPubKey1)
  277. newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower)
  278. err := assertMempool(css[0].txNotifier).CheckTx(newValidatorTx1, nil)
  279. assert.Nil(t, err)
  280. propBlock, _ := css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  281. propBlockParts := propBlock.MakePartSet(partSize)
  282. blockID := types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  283. proposal := types.NewProposal(vss[1].Height, round, -1, blockID)
  284. if err := vss[1].SignProposal(config.ChainID(), proposal); err != nil {
  285. t.Fatal("failed to sign bad proposal", err)
  286. }
  287. // set the proposal block
  288. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  289. t.Fatal(err)
  290. }
  291. ensureNewProposal(proposalCh, height, round)
  292. rs = css[0].GetRoundState()
  293. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
  294. ensureNewRound(newRoundCh, height+1, 0)
  295. //height 3
  296. height++
  297. incrementHeight(vss...)
  298. updateValidatorPubKey1 := css[nVals].privValidator.GetPubKey()
  299. updatePubKey1ABCI := types.TM2PB.PubKey(updateValidatorPubKey1)
  300. updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
  301. err = assertMempool(css[0].txNotifier).CheckTx(updateValidatorTx1, nil)
  302. assert.Nil(t, err)
  303. propBlock, _ = css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  304. propBlockParts = propBlock.MakePartSet(partSize)
  305. blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  306. proposal = types.NewProposal(vss[2].Height, round, -1, blockID)
  307. if err := vss[2].SignProposal(config.ChainID(), proposal); err != nil {
  308. t.Fatal("failed to sign bad proposal", err)
  309. }
  310. // set the proposal block
  311. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  312. t.Fatal(err)
  313. }
  314. ensureNewProposal(proposalCh, height, round)
  315. rs = css[0].GetRoundState()
  316. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
  317. ensureNewRound(newRoundCh, height+1, 0)
  318. //height 4
  319. height++
  320. incrementHeight(vss...)
  321. newValidatorPubKey2 := css[nVals+1].privValidator.GetPubKey()
  322. newVal2ABCI := types.TM2PB.PubKey(newValidatorPubKey2)
  323. newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower)
  324. err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx2, nil)
  325. assert.Nil(t, err)
  326. newValidatorPubKey3 := css[nVals+2].privValidator.GetPubKey()
  327. newVal3ABCI := types.TM2PB.PubKey(newValidatorPubKey3)
  328. newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower)
  329. err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx3, nil)
  330. assert.Nil(t, err)
  331. propBlock, _ = css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  332. propBlockParts = propBlock.MakePartSet(partSize)
  333. blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  334. newVss := make([]*validatorStub, nVals+1)
  335. copy(newVss, vss[:nVals+1])
  336. sort.Sort(ValidatorStubsByAddress(newVss))
  337. selfIndex := 0
  338. for i, vs := range newVss {
  339. if vs.GetPubKey().Equals(css[0].privValidator.GetPubKey()) {
  340. selfIndex = i
  341. break
  342. }
  343. }
  344. proposal = types.NewProposal(vss[3].Height, round, -1, blockID)
  345. if err := vss[3].SignProposal(config.ChainID(), proposal); err != nil {
  346. t.Fatal("failed to sign bad proposal", err)
  347. }
  348. // set the proposal block
  349. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  350. t.Fatal(err)
  351. }
  352. ensureNewProposal(proposalCh, height, round)
  353. removeValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, 0)
  354. err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx2, nil)
  355. assert.Nil(t, err)
  356. rs = css[0].GetRoundState()
  357. for i := 0; i < nVals+1; i++ {
  358. if i == selfIndex {
  359. continue
  360. }
  361. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), newVss[i])
  362. }
  363. ensureNewRound(newRoundCh, height+1, 0)
  364. //height 5
  365. height++
  366. incrementHeight(vss...)
  367. ensureNewProposal(proposalCh, height, round)
  368. rs = css[0].GetRoundState()
  369. for i := 0; i < nVals+1; i++ {
  370. if i == selfIndex {
  371. continue
  372. }
  373. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), newVss[i])
  374. }
  375. ensureNewRound(newRoundCh, height+1, 0)
  376. //height 6
  377. height++
  378. incrementHeight(vss...)
  379. removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0)
  380. err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx3, nil)
  381. assert.Nil(t, err)
  382. propBlock, _ = css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  383. propBlockParts = propBlock.MakePartSet(partSize)
  384. blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  385. newVss = make([]*validatorStub, nVals+3)
  386. copy(newVss, vss[:nVals+3])
  387. sort.Sort(ValidatorStubsByAddress(newVss))
  388. for i, vs := range newVss {
  389. if vs.GetPubKey().Equals(css[0].privValidator.GetPubKey()) {
  390. selfIndex = i
  391. break
  392. }
  393. }
  394. proposal = types.NewProposal(vss[1].Height, round, -1, blockID)
  395. if err := vss[1].SignProposal(config.ChainID(), proposal); err != nil {
  396. t.Fatal("failed to sign bad proposal", err)
  397. }
  398. // set the proposal block
  399. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  400. t.Fatal(err)
  401. }
  402. ensureNewProposal(proposalCh, height, round)
  403. rs = css[0].GetRoundState()
  404. for i := 0; i < nVals+3; i++ {
  405. if i == selfIndex {
  406. continue
  407. }
  408. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), newVss[i])
  409. }
  410. ensureNewRound(newRoundCh, height+1, 0)
  411. sim.Chain = make([]*types.Block, 0)
  412. sim.Commits = make([]*types.Commit, 0)
  413. for i := 1; i <= numBlocks; i++ {
  414. sim.Chain = append(sim.Chain, css[0].blockStore.LoadBlock(int64(i)))
  415. sim.Commits = append(sim.Commits, css[0].blockStore.LoadBlockCommit(int64(i)))
  416. }
  417. }
  418. // Sync from scratch
  419. func TestHandshakeReplayAll(t *testing.T) {
  420. for _, m := range modes {
  421. testHandshakeReplay(t, config, 0, m, false)
  422. }
  423. for _, m := range modes {
  424. testHandshakeReplay(t, config, 0, m, true)
  425. }
  426. }
  427. // Sync many, not from scratch
  428. func TestHandshakeReplaySome(t *testing.T) {
  429. for _, m := range modes {
  430. testHandshakeReplay(t, config, 1, m, false)
  431. }
  432. for _, m := range modes {
  433. testHandshakeReplay(t, config, 1, m, true)
  434. }
  435. }
  436. // Sync from lagging by one
  437. func TestHandshakeReplayOne(t *testing.T) {
  438. for _, m := range modes {
  439. testHandshakeReplay(t, config, numBlocks-1, m, false)
  440. }
  441. for _, m := range modes {
  442. testHandshakeReplay(t, config, numBlocks-1, m, true)
  443. }
  444. }
  445. // Sync from caught up
  446. func TestHandshakeReplayNone(t *testing.T) {
  447. for _, m := range modes {
  448. testHandshakeReplay(t, config, numBlocks, m, false)
  449. }
  450. for _, m := range modes {
  451. testHandshakeReplay(t, config, numBlocks, m, true)
  452. }
  453. }
  454. // Test mockProxyApp should not panic when app return ABCIResponses with some empty ResponseDeliverTx
  455. func TestMockProxyApp(t *testing.T) {
  456. sim.CleanupFunc() //clean the test env created in TestSimulateValidatorsChange
  457. logger := log.TestingLogger()
  458. var validTxs, invalidTxs = 0, 0
  459. txIndex := 0
  460. assert.NotPanics(t, func() {
  461. abciResWithEmptyDeliverTx := new(sm.ABCIResponses)
  462. abciResWithEmptyDeliverTx.DeliverTx = make([]*abci.ResponseDeliverTx, 0)
  463. abciResWithEmptyDeliverTx.DeliverTx = append(abciResWithEmptyDeliverTx.DeliverTx, &abci.ResponseDeliverTx{})
  464. // called when saveABCIResponses:
  465. bytes := cdc.MustMarshalBinaryBare(abciResWithEmptyDeliverTx)
  466. loadedAbciRes := new(sm.ABCIResponses)
  467. // this also happens sm.LoadABCIResponses
  468. err := cdc.UnmarshalBinaryBare(bytes, loadedAbciRes)
  469. require.NoError(t, err)
  470. mock := newMockProxyApp([]byte("mock_hash"), loadedAbciRes)
  471. abciRes := new(sm.ABCIResponses)
  472. abciRes.DeliverTx = make([]*abci.ResponseDeliverTx, len(loadedAbciRes.DeliverTx))
  473. // Execute transactions and get hash.
  474. proxyCb := func(req *abci.Request, res *abci.Response) {
  475. switch r := res.Value.(type) {
  476. case *abci.Response_DeliverTx:
  477. // TODO: make use of res.Log
  478. // TODO: make use of this info
  479. // Blocks may include invalid txs.
  480. txRes := r.DeliverTx
  481. if txRes.Code == abci.CodeTypeOK {
  482. validTxs++
  483. } else {
  484. logger.Debug("Invalid tx", "code", txRes.Code, "log", txRes.Log)
  485. invalidTxs++
  486. }
  487. abciRes.DeliverTx[txIndex] = txRes
  488. txIndex++
  489. }
  490. }
  491. mock.SetResponseCallback(proxyCb)
  492. someTx := []byte("tx")
  493. mock.DeliverTxAsync(abci.RequestDeliverTx{Tx: someTx})
  494. })
  495. assert.True(t, validTxs == 1)
  496. assert.True(t, invalidTxs == 0)
  497. }
  498. func tempWALWithData(data []byte) string {
  499. walFile, err := ioutil.TempFile("", "wal")
  500. if err != nil {
  501. panic(fmt.Sprintf("failed to create temp WAL file: %v", err))
  502. }
  503. _, err = walFile.Write(data)
  504. if err != nil {
  505. panic(fmt.Sprintf("failed to write to temp WAL file: %v", err))
  506. }
  507. if err := walFile.Close(); err != nil {
  508. panic(fmt.Sprintf("failed to close temp WAL file: %v", err))
  509. }
  510. return walFile.Name()
  511. }
  512. // Make some blocks. Start a fresh app and apply nBlocks blocks. Then restart the app and sync it up with the remaining blocks
  513. func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uint, testValidatorsChange bool) {
  514. var chain []*types.Block
  515. var commits []*types.Commit
  516. var store *mockBlockStore
  517. var stateDB dbm.DB
  518. var genisisState sm.State
  519. if testValidatorsChange {
  520. testConfig := ResetConfig(fmt.Sprintf("%s_%v_m", t.Name(), mode))
  521. defer os.RemoveAll(testConfig.RootDir)
  522. stateDB = dbm.NewMemDB()
  523. genisisState = sim.GenesisState
  524. config = sim.Config
  525. chain = sim.Chain
  526. commits = sim.Commits
  527. store = newMockBlockStore(config, genisisState.ConsensusParams)
  528. } else { //test single node
  529. testConfig := ResetConfig(fmt.Sprintf("%s_%v_s", t.Name(), mode))
  530. defer os.RemoveAll(testConfig.RootDir)
  531. walBody, err := WALWithNBlocks(t, numBlocks)
  532. require.NoError(t, err)
  533. walFile := tempWALWithData(walBody)
  534. config.Consensus.SetWalFile(walFile)
  535. privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  536. wal, err := NewWAL(walFile)
  537. require.NoError(t, err)
  538. wal.SetLogger(log.TestingLogger())
  539. err = wal.Start()
  540. require.NoError(t, err)
  541. defer wal.Stop()
  542. chain, commits, err = makeBlockchainFromWAL(wal)
  543. require.NoError(t, err)
  544. stateDB, genisisState, store = stateAndStore(config, privVal.GetPubKey(), kvstore.ProtocolVersion)
  545. }
  546. store.chain = chain
  547. store.commits = commits
  548. state := genisisState.Copy()
  549. // run the chain through state.ApplyBlock to build up the tendermint state
  550. state = buildTMStateFromChain(config, stateDB, state, chain, nBlocks, mode)
  551. latestAppHash := state.AppHash
  552. // make a new client creator
  553. kvstoreApp := kvstore.NewPersistentKVStoreApplication(filepath.Join(config.DBDir(), fmt.Sprintf("replay_test_%d_%d_a", nBlocks, mode)))
  554. clientCreator2 := proxy.NewLocalClientCreator(kvstoreApp)
  555. if nBlocks > 0 {
  556. // run nBlocks against a new client to build up the app state.
  557. // use a throwaway tendermint state
  558. proxyApp := proxy.NewAppConns(clientCreator2)
  559. stateDB1 := dbm.NewMemDB()
  560. sm.SaveState(stateDB1, genisisState)
  561. buildAppStateFromChain(proxyApp, stateDB1, genisisState, chain, nBlocks, mode)
  562. }
  563. // now start the app using the handshake - it should sync
  564. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  565. handshaker := NewHandshaker(stateDB, state, store, genDoc)
  566. proxyApp := proxy.NewAppConns(clientCreator2)
  567. if err := proxyApp.Start(); err != nil {
  568. t.Fatalf("Error starting proxy app connections: %v", err)
  569. }
  570. defer proxyApp.Stop()
  571. if err := handshaker.Handshake(proxyApp); err != nil {
  572. t.Fatalf("Error on abci handshake: %v", err)
  573. }
  574. // get the latest app hash from the app
  575. res, err := proxyApp.Query().InfoSync(abci.RequestInfo{Version: ""})
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. // the app hash should be synced up
  580. if !bytes.Equal(latestAppHash, res.LastBlockAppHash) {
  581. t.Fatalf("Expected app hashes to match after handshake/replay. got %X, expected %X", res.LastBlockAppHash, latestAppHash)
  582. }
  583. expectedBlocksToSync := numBlocks - nBlocks
  584. if nBlocks == numBlocks && mode > 0 {
  585. expectedBlocksToSync++
  586. } else if nBlocks > 0 && mode == 1 {
  587. expectedBlocksToSync++
  588. }
  589. if handshaker.NBlocks() != expectedBlocksToSync {
  590. t.Fatalf("Expected handshake to sync %d blocks, got %d", expectedBlocksToSync, handshaker.NBlocks())
  591. }
  592. }
  593. func applyBlock(stateDB dbm.DB, st sm.State, blk *types.Block, proxyApp proxy.AppConns) sm.State {
  594. testPartSize := types.BlockPartSizeBytes
  595. blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
  596. blkID := types.BlockID{Hash: blk.Hash(), PartsHeader: blk.MakePartSet(testPartSize).Header()}
  597. newState, err := blockExec.ApplyBlock(st, blkID, blk)
  598. if err != nil {
  599. panic(err)
  600. }
  601. return newState
  602. }
  603. func buildAppStateFromChain(proxyApp proxy.AppConns, stateDB dbm.DB,
  604. state sm.State, chain []*types.Block, nBlocks int, mode uint) {
  605. // start a new app without handshake, play nBlocks blocks
  606. if err := proxyApp.Start(); err != nil {
  607. panic(err)
  608. }
  609. defer proxyApp.Stop()
  610. state.Version.Consensus.App = kvstore.ProtocolVersion //simulate handshake, receive app version
  611. validators := types.TM2PB.ValidatorUpdates(state.Validators)
  612. if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{
  613. Validators: validators,
  614. }); err != nil {
  615. panic(err)
  616. }
  617. sm.SaveState(stateDB, state) //save height 1's validatorsInfo
  618. switch mode {
  619. case 0:
  620. for i := 0; i < nBlocks; i++ {
  621. block := chain[i]
  622. state = applyBlock(stateDB, state, block, proxyApp)
  623. }
  624. case 1, 2:
  625. for i := 0; i < nBlocks-1; i++ {
  626. block := chain[i]
  627. state = applyBlock(stateDB, state, block, proxyApp)
  628. }
  629. if mode == 2 {
  630. // update the kvstore height and apphash
  631. // as if we ran commit but not
  632. state = applyBlock(stateDB, state, chain[nBlocks-1], proxyApp)
  633. }
  634. }
  635. }
  636. func buildTMStateFromChain(config *cfg.Config, stateDB dbm.DB, state sm.State, chain []*types.Block, nBlocks int, mode uint) sm.State {
  637. // run the whole chain against this client to build up the tendermint state
  638. clientCreator := proxy.NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(filepath.Join(config.DBDir(), fmt.Sprintf("replay_test_%d_%d_t", nBlocks, mode))))
  639. proxyApp := proxy.NewAppConns(clientCreator)
  640. if err := proxyApp.Start(); err != nil {
  641. panic(err)
  642. }
  643. defer proxyApp.Stop()
  644. state.Version.Consensus.App = kvstore.ProtocolVersion //simulate handshake, receive app version
  645. validators := types.TM2PB.ValidatorUpdates(state.Validators)
  646. if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{
  647. Validators: validators,
  648. }); err != nil {
  649. panic(err)
  650. }
  651. sm.SaveState(stateDB, state) //save height 1's validatorsInfo
  652. switch mode {
  653. case 0:
  654. // sync right up
  655. for _, block := range chain {
  656. state = applyBlock(stateDB, state, block, proxyApp)
  657. }
  658. case 1, 2:
  659. // sync up to the penultimate as if we stored the block.
  660. // whether we commit or not depends on the appHash
  661. for _, block := range chain[:len(chain)-1] {
  662. state = applyBlock(stateDB, state, block, proxyApp)
  663. }
  664. // apply the final block to a state copy so we can
  665. // get the right next appHash but keep the state back
  666. applyBlock(stateDB, state, chain[len(chain)-1], proxyApp)
  667. }
  668. return state
  669. }
  670. func TestHandshakePanicsIfAppReturnsWrongAppHash(t *testing.T) {
  671. // 1. Initialize tendermint and commit 3 blocks with the following app hashes:
  672. // - 0x01
  673. // - 0x02
  674. // - 0x03
  675. config := ResetConfig("handshake_test_")
  676. defer os.RemoveAll(config.RootDir)
  677. privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  678. const appVersion = 0x0
  679. stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), appVersion)
  680. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  681. state.LastValidators = state.Validators.Copy()
  682. // mode = 0 for committing all the blocks
  683. blocks := makeBlocks(3, &state, privVal)
  684. store.chain = blocks
  685. // 2. Tendermint must panic if app returns wrong hash for the first block
  686. // - RANDOM HASH
  687. // - 0x02
  688. // - 0x03
  689. {
  690. app := &badApp{numBlocks: 3, allHashesAreWrong: true}
  691. clientCreator := proxy.NewLocalClientCreator(app)
  692. proxyApp := proxy.NewAppConns(clientCreator)
  693. err := proxyApp.Start()
  694. require.NoError(t, err)
  695. defer proxyApp.Stop()
  696. assert.Panics(t, func() {
  697. h := NewHandshaker(stateDB, state, store, genDoc)
  698. h.Handshake(proxyApp)
  699. })
  700. }
  701. // 3. Tendermint must panic if app returns wrong hash for the last block
  702. // - 0x01
  703. // - 0x02
  704. // - RANDOM HASH
  705. {
  706. app := &badApp{numBlocks: 3, onlyLastHashIsWrong: true}
  707. clientCreator := proxy.NewLocalClientCreator(app)
  708. proxyApp := proxy.NewAppConns(clientCreator)
  709. err := proxyApp.Start()
  710. require.NoError(t, err)
  711. defer proxyApp.Stop()
  712. assert.Panics(t, func() {
  713. h := NewHandshaker(stateDB, state, store, genDoc)
  714. h.Handshake(proxyApp)
  715. })
  716. }
  717. }
  718. func makeBlocks(n int, state *sm.State, privVal types.PrivValidator) []*types.Block {
  719. blocks := make([]*types.Block, 0)
  720. var (
  721. prevBlock *types.Block
  722. prevBlockMeta *types.BlockMeta
  723. )
  724. appHeight := byte(0x01)
  725. for i := 0; i < n; i++ {
  726. height := int64(i + 1)
  727. block, parts := makeBlock(*state, prevBlock, prevBlockMeta, privVal, height)
  728. blocks = append(blocks, block)
  729. prevBlock = block
  730. prevBlockMeta = types.NewBlockMeta(block, parts)
  731. // update state
  732. state.AppHash = []byte{appHeight}
  733. appHeight++
  734. state.LastBlockHeight = height
  735. }
  736. return blocks
  737. }
  738. func makeVote(header *types.Header, blockID types.BlockID, valset *types.ValidatorSet, privVal types.PrivValidator) *types.Vote {
  739. addr := privVal.GetPubKey().Address()
  740. idx, _ := valset.GetByAddress(addr)
  741. vote := &types.Vote{
  742. ValidatorAddress: addr,
  743. ValidatorIndex: idx,
  744. Height: header.Height,
  745. Round: 1,
  746. Timestamp: tmtime.Now(),
  747. Type: types.PrecommitType,
  748. BlockID: blockID,
  749. }
  750. privVal.SignVote(header.ChainID, vote)
  751. return vote
  752. }
  753. func makeBlock(state sm.State, lastBlock *types.Block, lastBlockMeta *types.BlockMeta,
  754. privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) {
  755. lastCommit := types.NewCommit(types.BlockID{}, nil)
  756. if height > 1 {
  757. vote := makeVote(&lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVal).CommitSig()
  758. lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{vote})
  759. }
  760. return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
  761. }
  762. type badApp struct {
  763. abci.BaseApplication
  764. numBlocks byte
  765. height byte
  766. allHashesAreWrong bool
  767. onlyLastHashIsWrong bool
  768. }
  769. func (app *badApp) Commit() abci.ResponseCommit {
  770. app.height++
  771. if app.onlyLastHashIsWrong {
  772. if app.height == app.numBlocks {
  773. return abci.ResponseCommit{Data: cmn.RandBytes(8)}
  774. }
  775. return abci.ResponseCommit{Data: []byte{app.height}}
  776. } else if app.allHashesAreWrong {
  777. return abci.ResponseCommit{Data: cmn.RandBytes(8)}
  778. }
  779. panic("either allHashesAreWrong or onlyLastHashIsWrong must be set")
  780. }
  781. //--------------------------
  782. // utils for making blocks
  783. func makeBlockchainFromWAL(wal WAL) ([]*types.Block, []*types.Commit, error) {
  784. var height int64
  785. // Search for height marker
  786. gr, found, err := wal.SearchForEndHeight(height, &WALSearchOptions{})
  787. if err != nil {
  788. return nil, nil, err
  789. }
  790. if !found {
  791. return nil, nil, fmt.Errorf("WAL does not contain height %d", height)
  792. }
  793. defer gr.Close() // nolint: errcheck
  794. // log.Notice("Build a blockchain by reading from the WAL")
  795. var (
  796. blocks []*types.Block
  797. commits []*types.Commit
  798. thisBlockParts *types.PartSet
  799. thisBlockCommit *types.Commit
  800. )
  801. dec := NewWALDecoder(gr)
  802. for {
  803. msg, err := dec.Decode()
  804. if err == io.EOF {
  805. break
  806. } else if err != nil {
  807. return nil, nil, err
  808. }
  809. piece := readPieceFromWAL(msg)
  810. if piece == nil {
  811. continue
  812. }
  813. switch p := piece.(type) {
  814. case EndHeightMessage:
  815. // if its not the first one, we have a full block
  816. if thisBlockParts != nil {
  817. var block = new(types.Block)
  818. _, err = cdc.UnmarshalBinaryLengthPrefixedReader(thisBlockParts.GetReader(), block, 0)
  819. if err != nil {
  820. panic(err)
  821. }
  822. if block.Height != height+1 {
  823. panic(fmt.Sprintf("read bad block from wal. got height %d, expected %d", block.Height, height+1))
  824. }
  825. commitHeight := thisBlockCommit.Precommits[0].Height
  826. if commitHeight != height+1 {
  827. panic(fmt.Sprintf("commit doesnt match. got height %d, expected %d", commitHeight, height+1))
  828. }
  829. blocks = append(blocks, block)
  830. commits = append(commits, thisBlockCommit)
  831. height++
  832. }
  833. case *types.PartSetHeader:
  834. thisBlockParts = types.NewPartSetFromHeader(*p)
  835. case *types.Part:
  836. _, err := thisBlockParts.AddPart(p)
  837. if err != nil {
  838. return nil, nil, err
  839. }
  840. case *types.Vote:
  841. if p.Type == types.PrecommitType {
  842. commitSigs := []*types.CommitSig{p.CommitSig()}
  843. thisBlockCommit = types.NewCommit(p.BlockID, commitSigs)
  844. }
  845. }
  846. }
  847. // grab the last block too
  848. var block = new(types.Block)
  849. _, err = cdc.UnmarshalBinaryLengthPrefixedReader(thisBlockParts.GetReader(), block, 0)
  850. if err != nil {
  851. panic(err)
  852. }
  853. if block.Height != height+1 {
  854. panic(fmt.Sprintf("read bad block from wal. got height %d, expected %d", block.Height, height+1))
  855. }
  856. commitHeight := thisBlockCommit.Precommits[0].Height
  857. if commitHeight != height+1 {
  858. panic(fmt.Sprintf("commit doesnt match. got height %d, expected %d", commitHeight, height+1))
  859. }
  860. blocks = append(blocks, block)
  861. commits = append(commits, thisBlockCommit)
  862. return blocks, commits, nil
  863. }
  864. func readPieceFromWAL(msg *TimedWALMessage) interface{} {
  865. // for logging
  866. switch m := msg.Msg.(type) {
  867. case msgInfo:
  868. switch msg := m.Msg.(type) {
  869. case *ProposalMessage:
  870. return &msg.Proposal.BlockID.PartsHeader
  871. case *BlockPartMessage:
  872. return msg.Part
  873. case *VoteMessage:
  874. return msg.Vote
  875. }
  876. case EndHeightMessage:
  877. return m
  878. }
  879. return nil
  880. }
  881. // fresh state and mock store
  882. func stateAndStore(config *cfg.Config, pubKey crypto.PubKey, appVersion version.Protocol) (dbm.DB, sm.State, *mockBlockStore) {
  883. stateDB := dbm.NewMemDB()
  884. state, _ := sm.MakeGenesisStateFromFile(config.GenesisFile())
  885. state.Version.Consensus.App = appVersion
  886. store := newMockBlockStore(config, state.ConsensusParams)
  887. sm.SaveState(stateDB, state)
  888. return stateDB, state, store
  889. }
  890. //----------------------------------
  891. // mock block store
  892. type mockBlockStore struct {
  893. config *cfg.Config
  894. params types.ConsensusParams
  895. chain []*types.Block
  896. commits []*types.Commit
  897. }
  898. // TODO: NewBlockStore(db.NewMemDB) ...
  899. func newMockBlockStore(config *cfg.Config, params types.ConsensusParams) *mockBlockStore {
  900. return &mockBlockStore{config, params, nil, nil}
  901. }
  902. func (bs *mockBlockStore) Height() int64 { return int64(len(bs.chain)) }
  903. func (bs *mockBlockStore) LoadBlock(height int64) *types.Block { return bs.chain[height-1] }
  904. func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
  905. block := bs.chain[height-1]
  906. return &types.BlockMeta{
  907. BlockID: types.BlockID{Hash: block.Hash(), PartsHeader: block.MakePartSet(types.BlockPartSizeBytes).Header()},
  908. Header: block.Header,
  909. }
  910. }
  911. func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
  912. func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  913. }
  914. func (bs *mockBlockStore) LoadBlockCommit(height int64) *types.Commit {
  915. return bs.commits[height-1]
  916. }
  917. func (bs *mockBlockStore) LoadSeenCommit(height int64) *types.Commit {
  918. return bs.commits[height-1]
  919. }
  920. //---------------------------------------
  921. // Test handshake/init chain
  922. func TestHandshakeUpdatesValidators(t *testing.T) {
  923. val, _ := types.RandValidator(true, 10)
  924. vals := types.NewValidatorSet([]*types.Validator{val})
  925. app := &initChainApp{vals: types.TM2PB.ValidatorUpdates(vals)}
  926. clientCreator := proxy.NewLocalClientCreator(app)
  927. config := ResetConfig("handshake_test_")
  928. defer os.RemoveAll(config.RootDir)
  929. privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  930. stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), 0x0)
  931. oldValAddr := state.Validators.Validators[0].Address
  932. // now start the app using the handshake - it should sync
  933. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  934. handshaker := NewHandshaker(stateDB, state, store, genDoc)
  935. proxyApp := proxy.NewAppConns(clientCreator)
  936. if err := proxyApp.Start(); err != nil {
  937. t.Fatalf("Error starting proxy app connections: %v", err)
  938. }
  939. defer proxyApp.Stop()
  940. if err := handshaker.Handshake(proxyApp); err != nil {
  941. t.Fatalf("Error on abci handshake: %v", err)
  942. }
  943. // reload the state, check the validator set was updated
  944. state = sm.LoadState(stateDB)
  945. newValAddr := state.Validators.Validators[0].Address
  946. expectValAddr := val.Address
  947. assert.NotEqual(t, oldValAddr, newValAddr)
  948. assert.Equal(t, newValAddr, expectValAddr)
  949. }
  950. // returns the vals on InitChain
  951. type initChainApp struct {
  952. abci.BaseApplication
  953. vals []abci.ValidatorUpdate
  954. }
  955. func (ica *initChainApp) InitChain(req abci.RequestInitChain) abci.ResponseInitChain {
  956. return abci.ResponseInitChain{
  957. Validators: ica.vals,
  958. }
  959. }