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.

1106 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
  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) error {
  197. if endMsg, ok := m.(EndHeightMessage); ok {
  198. if endMsg.Height == w.heightToStop {
  199. w.panicCh <- ReachedHeightToStopError{endMsg.Height}
  200. runtime.Goexit()
  201. return nil
  202. }
  203. return w.next.Write(m)
  204. }
  205. if w.msgIndex > w.lastPanickedForMsgIndex {
  206. w.lastPanickedForMsgIndex = w.msgIndex
  207. _, file, line, _ := runtime.Caller(1)
  208. w.panicCh <- WALWriteError{fmt.Sprintf("failed to write %T to WAL (fileline: %s:%d)", m, file, line)}
  209. runtime.Goexit()
  210. return nil
  211. }
  212. w.msgIndex++
  213. return w.next.Write(m)
  214. }
  215. func (w *crashingWAL) WriteSync(m WALMessage) error {
  216. return w.Write(m)
  217. }
  218. func (w *crashingWAL) FlushAndSync() error { return w.next.FlushAndSync() }
  219. func (w *crashingWAL) SearchForEndHeight(height int64, options *WALSearchOptions) (rd io.ReadCloser, found bool, err error) {
  220. return w.next.SearchForEndHeight(height, options)
  221. }
  222. func (w *crashingWAL) Start() error { return w.next.Start() }
  223. func (w *crashingWAL) Stop() error { return w.next.Stop() }
  224. func (w *crashingWAL) Wait() { w.next.Wait() }
  225. //------------------------------------------------------------------------------------------
  226. type testSim struct {
  227. GenesisState sm.State
  228. Config *cfg.Config
  229. Chain []*types.Block
  230. Commits []*types.Commit
  231. CleanupFunc cleanupFunc
  232. }
  233. const (
  234. numBlocks = 6
  235. )
  236. var (
  237. mempool = mock.Mempool{}
  238. evpool = sm.MockEvidencePool{}
  239. sim testSim
  240. )
  241. //---------------------------------------
  242. // Test handshake/replay
  243. // 0 - all synced up
  244. // 1 - saved block but app and state are behind
  245. // 2 - save block and committed but state is behind
  246. var modes = []uint{0, 1, 2}
  247. // This is actually not a test, it's for storing validator change tx data for testHandshakeReplay
  248. func TestSimulateValidatorsChange(t *testing.T) {
  249. nPeers := 7
  250. nVals := 4
  251. css, genDoc, config, cleanup := randConsensusNetWithPeers(nVals, nPeers, "replay_test", newMockTickerFunc(true), newPersistentKVStoreWithPath)
  252. sim.Config = config
  253. sim.GenesisState, _ = sm.MakeGenesisState(genDoc)
  254. sim.CleanupFunc = cleanup
  255. partSize := types.BlockPartSizeBytes
  256. newRoundCh := subscribe(css[0].eventBus, types.EventQueryNewRound)
  257. proposalCh := subscribe(css[0].eventBus, types.EventQueryCompleteProposal)
  258. vss := make([]*validatorStub, nPeers)
  259. for i := 0; i < nPeers; i++ {
  260. vss[i] = NewValidatorStub(css[i].privValidator, i)
  261. }
  262. height, round := css[0].Height, css[0].Round
  263. // start the machine
  264. startTestRound(css[0], height, round)
  265. incrementHeight(vss...)
  266. ensureNewRound(newRoundCh, height, 0)
  267. ensureNewProposal(proposalCh, height, round)
  268. rs := css[0].GetRoundState()
  269. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
  270. ensureNewRound(newRoundCh, height+1, 0)
  271. //height 2
  272. height++
  273. incrementHeight(vss...)
  274. newValidatorPubKey1 := css[nVals].privValidator.GetPubKey()
  275. valPubKey1ABCI := types.TM2PB.PubKey(newValidatorPubKey1)
  276. newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower)
  277. err := assertMempool(css[0].txNotifier).CheckTx(newValidatorTx1, nil)
  278. assert.Nil(t, err)
  279. propBlock, _ := css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  280. propBlockParts := propBlock.MakePartSet(partSize)
  281. blockID := types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  282. proposal := types.NewProposal(vss[1].Height, round, -1, blockID)
  283. if err := vss[1].SignProposal(config.ChainID(), proposal); err != nil {
  284. t.Fatal("failed to sign bad proposal", err)
  285. }
  286. // set the proposal block
  287. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  288. t.Fatal(err)
  289. }
  290. ensureNewProposal(proposalCh, height, round)
  291. rs = css[0].GetRoundState()
  292. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
  293. ensureNewRound(newRoundCh, height+1, 0)
  294. //height 3
  295. height++
  296. incrementHeight(vss...)
  297. updateValidatorPubKey1 := css[nVals].privValidator.GetPubKey()
  298. updatePubKey1ABCI := types.TM2PB.PubKey(updateValidatorPubKey1)
  299. updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
  300. err = assertMempool(css[0].txNotifier).CheckTx(updateValidatorTx1, nil)
  301. assert.Nil(t, err)
  302. propBlock, _ = css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  303. propBlockParts = propBlock.MakePartSet(partSize)
  304. blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  305. proposal = types.NewProposal(vss[2].Height, round, -1, blockID)
  306. if err := vss[2].SignProposal(config.ChainID(), proposal); err != nil {
  307. t.Fatal("failed to sign bad proposal", err)
  308. }
  309. // set the proposal block
  310. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  311. t.Fatal(err)
  312. }
  313. ensureNewProposal(proposalCh, height, round)
  314. rs = css[0].GetRoundState()
  315. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
  316. ensureNewRound(newRoundCh, height+1, 0)
  317. //height 4
  318. height++
  319. incrementHeight(vss...)
  320. newValidatorPubKey2 := css[nVals+1].privValidator.GetPubKey()
  321. newVal2ABCI := types.TM2PB.PubKey(newValidatorPubKey2)
  322. newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower)
  323. err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx2, nil)
  324. assert.Nil(t, err)
  325. newValidatorPubKey3 := css[nVals+2].privValidator.GetPubKey()
  326. newVal3ABCI := types.TM2PB.PubKey(newValidatorPubKey3)
  327. newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower)
  328. err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx3, nil)
  329. assert.Nil(t, err)
  330. propBlock, _ = css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  331. propBlockParts = propBlock.MakePartSet(partSize)
  332. blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  333. newVss := make([]*validatorStub, nVals+1)
  334. copy(newVss, vss[:nVals+1])
  335. sort.Sort(ValidatorStubsByAddress(newVss))
  336. selfIndex := 0
  337. for i, vs := range newVss {
  338. if vs.GetPubKey().Equals(css[0].privValidator.GetPubKey()) {
  339. selfIndex = i
  340. break
  341. }
  342. }
  343. proposal = types.NewProposal(vss[3].Height, round, -1, blockID)
  344. if err := vss[3].SignProposal(config.ChainID(), proposal); err != nil {
  345. t.Fatal("failed to sign bad proposal", err)
  346. }
  347. // set the proposal block
  348. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  349. t.Fatal(err)
  350. }
  351. ensureNewProposal(proposalCh, height, round)
  352. removeValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, 0)
  353. err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx2, nil)
  354. assert.Nil(t, err)
  355. rs = css[0].GetRoundState()
  356. for i := 0; i < nVals+1; i++ {
  357. if i == selfIndex {
  358. continue
  359. }
  360. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), newVss[i])
  361. }
  362. ensureNewRound(newRoundCh, height+1, 0)
  363. //height 5
  364. height++
  365. incrementHeight(vss...)
  366. ensureNewProposal(proposalCh, height, round)
  367. rs = css[0].GetRoundState()
  368. for i := 0; i < nVals+1; i++ {
  369. if i == selfIndex {
  370. continue
  371. }
  372. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), newVss[i])
  373. }
  374. ensureNewRound(newRoundCh, height+1, 0)
  375. //height 6
  376. height++
  377. incrementHeight(vss...)
  378. removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0)
  379. err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx3, nil)
  380. assert.Nil(t, err)
  381. propBlock, _ = css[0].createProposalBlock() //changeProposer(t, cs1, vs2)
  382. propBlockParts = propBlock.MakePartSet(partSize)
  383. blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
  384. newVss = make([]*validatorStub, nVals+3)
  385. copy(newVss, vss[:nVals+3])
  386. sort.Sort(ValidatorStubsByAddress(newVss))
  387. for i, vs := range newVss {
  388. if vs.GetPubKey().Equals(css[0].privValidator.GetPubKey()) {
  389. selfIndex = i
  390. break
  391. }
  392. }
  393. proposal = types.NewProposal(vss[1].Height, round, -1, blockID)
  394. if err := vss[1].SignProposal(config.ChainID(), proposal); err != nil {
  395. t.Fatal("failed to sign bad proposal", err)
  396. }
  397. // set the proposal block
  398. if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
  399. t.Fatal(err)
  400. }
  401. ensureNewProposal(proposalCh, height, round)
  402. rs = css[0].GetRoundState()
  403. for i := 0; i < nVals+3; i++ {
  404. if i == selfIndex {
  405. continue
  406. }
  407. signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), newVss[i])
  408. }
  409. ensureNewRound(newRoundCh, height+1, 0)
  410. sim.Chain = make([]*types.Block, 0)
  411. sim.Commits = make([]*types.Commit, 0)
  412. for i := 1; i <= numBlocks; i++ {
  413. sim.Chain = append(sim.Chain, css[0].blockStore.LoadBlock(int64(i)))
  414. sim.Commits = append(sim.Commits, css[0].blockStore.LoadBlockCommit(int64(i)))
  415. }
  416. }
  417. // Sync from scratch
  418. func TestHandshakeReplayAll(t *testing.T) {
  419. for _, m := range modes {
  420. testHandshakeReplay(t, config, 0, m, false)
  421. }
  422. for _, m := range modes {
  423. testHandshakeReplay(t, config, 0, m, true)
  424. }
  425. }
  426. // Sync many, not from scratch
  427. func TestHandshakeReplaySome(t *testing.T) {
  428. for _, m := range modes {
  429. testHandshakeReplay(t, config, 1, m, false)
  430. }
  431. for _, m := range modes {
  432. testHandshakeReplay(t, config, 1, m, true)
  433. }
  434. }
  435. // Sync from lagging by one
  436. func TestHandshakeReplayOne(t *testing.T) {
  437. for _, m := range modes {
  438. testHandshakeReplay(t, config, numBlocks-1, m, false)
  439. }
  440. for _, m := range modes {
  441. testHandshakeReplay(t, config, numBlocks-1, m, true)
  442. }
  443. }
  444. // Sync from caught up
  445. func TestHandshakeReplayNone(t *testing.T) {
  446. for _, m := range modes {
  447. testHandshakeReplay(t, config, numBlocks, m, false)
  448. }
  449. for _, m := range modes {
  450. testHandshakeReplay(t, config, numBlocks, m, true)
  451. }
  452. }
  453. // Test mockProxyApp should not panic when app return ABCIResponses with some empty ResponseDeliverTx
  454. func TestMockProxyApp(t *testing.T) {
  455. sim.CleanupFunc() //clean the test env created in TestSimulateValidatorsChange
  456. logger := log.TestingLogger()
  457. var validTxs, invalidTxs = 0, 0
  458. txIndex := 0
  459. assert.NotPanics(t, func() {
  460. abciResWithEmptyDeliverTx := new(sm.ABCIResponses)
  461. abciResWithEmptyDeliverTx.DeliverTx = make([]*abci.ResponseDeliverTx, 0)
  462. abciResWithEmptyDeliverTx.DeliverTx = append(abciResWithEmptyDeliverTx.DeliverTx, &abci.ResponseDeliverTx{})
  463. // called when saveABCIResponses:
  464. bytes := cdc.MustMarshalBinaryBare(abciResWithEmptyDeliverTx)
  465. loadedAbciRes := new(sm.ABCIResponses)
  466. // this also happens sm.LoadABCIResponses
  467. err := cdc.UnmarshalBinaryBare(bytes, loadedAbciRes)
  468. require.NoError(t, err)
  469. mock := newMockProxyApp([]byte("mock_hash"), loadedAbciRes)
  470. abciRes := new(sm.ABCIResponses)
  471. abciRes.DeliverTx = make([]*abci.ResponseDeliverTx, len(loadedAbciRes.DeliverTx))
  472. // Execute transactions and get hash.
  473. proxyCb := func(req *abci.Request, res *abci.Response) {
  474. switch r := res.Value.(type) {
  475. case *abci.Response_DeliverTx:
  476. // TODO: make use of res.Log
  477. // TODO: make use of this info
  478. // Blocks may include invalid txs.
  479. txRes := r.DeliverTx
  480. if txRes.Code == abci.CodeTypeOK {
  481. validTxs++
  482. } else {
  483. logger.Debug("Invalid tx", "code", txRes.Code, "log", txRes.Log)
  484. invalidTxs++
  485. }
  486. abciRes.DeliverTx[txIndex] = txRes
  487. txIndex++
  488. }
  489. }
  490. mock.SetResponseCallback(proxyCb)
  491. someTx := []byte("tx")
  492. mock.DeliverTxAsync(someTx)
  493. })
  494. assert.True(t, validTxs == 1)
  495. assert.True(t, invalidTxs == 0)
  496. }
  497. func tempWALWithData(data []byte) string {
  498. walFile, err := ioutil.TempFile("", "wal")
  499. if err != nil {
  500. panic(fmt.Sprintf("failed to create temp WAL file: %v", err))
  501. }
  502. _, err = walFile.Write(data)
  503. if err != nil {
  504. panic(fmt.Sprintf("failed to write to temp WAL file: %v", err))
  505. }
  506. if err := walFile.Close(); err != nil {
  507. panic(fmt.Sprintf("failed to close temp WAL file: %v", err))
  508. }
  509. return walFile.Name()
  510. }
  511. // Make some blocks. Start a fresh app and apply nBlocks blocks. Then restart the app and sync it up with the remaining blocks
  512. func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uint, testValidatorsChange bool) {
  513. var chain []*types.Block
  514. var commits []*types.Commit
  515. var store *mockBlockStore
  516. var stateDB dbm.DB
  517. var genisisState sm.State
  518. if testValidatorsChange {
  519. testConfig := ResetConfig(fmt.Sprintf("%s_%v_m", t.Name(), mode))
  520. defer os.RemoveAll(testConfig.RootDir)
  521. stateDB = dbm.NewMemDB()
  522. genisisState = sim.GenesisState
  523. config = sim.Config
  524. chain = sim.Chain
  525. commits = sim.Commits
  526. store = newMockBlockStore(config, genisisState.ConsensusParams)
  527. } else { //test single node
  528. testConfig := ResetConfig(fmt.Sprintf("%s_%v_s", t.Name(), mode))
  529. defer os.RemoveAll(testConfig.RootDir)
  530. walBody, err := WALWithNBlocks(t, numBlocks)
  531. require.NoError(t, err)
  532. walFile := tempWALWithData(walBody)
  533. config.Consensus.SetWalFile(walFile)
  534. privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  535. wal, err := NewWAL(walFile)
  536. require.NoError(t, err)
  537. wal.SetLogger(log.TestingLogger())
  538. err = wal.Start()
  539. require.NoError(t, err)
  540. defer wal.Stop()
  541. chain, commits, err = makeBlockchainFromWAL(wal)
  542. require.NoError(t, err)
  543. stateDB, genisisState, store = stateAndStore(config, privVal.GetPubKey(), kvstore.ProtocolVersion)
  544. }
  545. store.chain = chain
  546. store.commits = commits
  547. state := genisisState.Copy()
  548. // run the chain through state.ApplyBlock to build up the tendermint state
  549. state = buildTMStateFromChain(config, stateDB, state, chain, nBlocks, mode)
  550. latestAppHash := state.AppHash
  551. // make a new client creator
  552. kvstoreApp := kvstore.NewPersistentKVStoreApplication(filepath.Join(config.DBDir(), fmt.Sprintf("replay_test_%d_%d_a", nBlocks, mode)))
  553. clientCreator2 := proxy.NewLocalClientCreator(kvstoreApp)
  554. if nBlocks > 0 {
  555. // run nBlocks against a new client to build up the app state.
  556. // use a throwaway tendermint state
  557. proxyApp := proxy.NewAppConns(clientCreator2)
  558. stateDB1 := dbm.NewMemDB()
  559. sm.SaveState(stateDB1, genisisState)
  560. buildAppStateFromChain(proxyApp, stateDB1, genisisState, chain, nBlocks, mode)
  561. }
  562. // now start the app using the handshake - it should sync
  563. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  564. handshaker := NewHandshaker(stateDB, state, store, genDoc)
  565. proxyApp := proxy.NewAppConns(clientCreator2)
  566. if err := proxyApp.Start(); err != nil {
  567. t.Fatalf("Error starting proxy app connections: %v", err)
  568. }
  569. defer proxyApp.Stop()
  570. if err := handshaker.Handshake(proxyApp); err != nil {
  571. t.Fatalf("Error on abci handshake: %v", err)
  572. }
  573. // get the latest app hash from the app
  574. res, err := proxyApp.Query().InfoSync(abci.RequestInfo{Version: ""})
  575. if err != nil {
  576. t.Fatal(err)
  577. }
  578. // the app hash should be synced up
  579. if !bytes.Equal(latestAppHash, res.LastBlockAppHash) {
  580. t.Fatalf("Expected app hashes to match after handshake/replay. got %X, expected %X", res.LastBlockAppHash, latestAppHash)
  581. }
  582. expectedBlocksToSync := numBlocks - nBlocks
  583. if nBlocks == numBlocks && mode > 0 {
  584. expectedBlocksToSync++
  585. } else if nBlocks > 0 && mode == 1 {
  586. expectedBlocksToSync++
  587. }
  588. if handshaker.NBlocks() != expectedBlocksToSync {
  589. t.Fatalf("Expected handshake to sync %d blocks, got %d", expectedBlocksToSync, handshaker.NBlocks())
  590. }
  591. }
  592. func applyBlock(stateDB dbm.DB, st sm.State, blk *types.Block, proxyApp proxy.AppConns) sm.State {
  593. testPartSize := types.BlockPartSizeBytes
  594. blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
  595. blkID := types.BlockID{Hash: blk.Hash(), PartsHeader: blk.MakePartSet(testPartSize).Header()}
  596. newState, err := blockExec.ApplyBlock(st, blkID, blk)
  597. if err != nil {
  598. panic(err)
  599. }
  600. return newState
  601. }
  602. func buildAppStateFromChain(proxyApp proxy.AppConns, stateDB dbm.DB,
  603. state sm.State, chain []*types.Block, nBlocks int, mode uint) {
  604. // start a new app without handshake, play nBlocks blocks
  605. if err := proxyApp.Start(); err != nil {
  606. panic(err)
  607. }
  608. defer proxyApp.Stop()
  609. state.Version.Consensus.App = kvstore.ProtocolVersion //simulate handshake, receive app version
  610. validators := types.TM2PB.ValidatorUpdates(state.Validators)
  611. if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{
  612. Validators: validators,
  613. }); err != nil {
  614. panic(err)
  615. }
  616. sm.SaveState(stateDB, state) //save height 1's validatorsInfo
  617. switch mode {
  618. case 0:
  619. for i := 0; i < nBlocks; i++ {
  620. block := chain[i]
  621. state = applyBlock(stateDB, state, block, proxyApp)
  622. }
  623. case 1, 2:
  624. for i := 0; i < nBlocks-1; i++ {
  625. block := chain[i]
  626. state = applyBlock(stateDB, state, block, proxyApp)
  627. }
  628. if mode == 2 {
  629. // update the kvstore height and apphash
  630. // as if we ran commit but not
  631. state = applyBlock(stateDB, state, chain[nBlocks-1], proxyApp)
  632. }
  633. }
  634. }
  635. func buildTMStateFromChain(config *cfg.Config, stateDB dbm.DB, state sm.State, chain []*types.Block, nBlocks int, mode uint) sm.State {
  636. // run the whole chain against this client to build up the tendermint state
  637. clientCreator := proxy.NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(filepath.Join(config.DBDir(), fmt.Sprintf("replay_test_%d_%d_t", nBlocks, mode))))
  638. proxyApp := proxy.NewAppConns(clientCreator)
  639. if err := proxyApp.Start(); err != nil {
  640. panic(err)
  641. }
  642. defer proxyApp.Stop()
  643. state.Version.Consensus.App = kvstore.ProtocolVersion //simulate handshake, receive app version
  644. validators := types.TM2PB.ValidatorUpdates(state.Validators)
  645. if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{
  646. Validators: validators,
  647. }); err != nil {
  648. panic(err)
  649. }
  650. sm.SaveState(stateDB, state) //save height 1's validatorsInfo
  651. switch mode {
  652. case 0:
  653. // sync right up
  654. for _, block := range chain {
  655. state = applyBlock(stateDB, state, block, proxyApp)
  656. }
  657. case 1, 2:
  658. // sync up to the penultimate as if we stored the block.
  659. // whether we commit or not depends on the appHash
  660. for _, block := range chain[:len(chain)-1] {
  661. state = applyBlock(stateDB, state, block, proxyApp)
  662. }
  663. // apply the final block to a state copy so we can
  664. // get the right next appHash but keep the state back
  665. applyBlock(stateDB, state, chain[len(chain)-1], proxyApp)
  666. }
  667. return state
  668. }
  669. func TestHandshakePanicsIfAppReturnsWrongAppHash(t *testing.T) {
  670. // 1. Initialize tendermint and commit 3 blocks with the following app hashes:
  671. // - 0x01
  672. // - 0x02
  673. // - 0x03
  674. config := ResetConfig("handshake_test_")
  675. defer os.RemoveAll(config.RootDir)
  676. privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  677. const appVersion = 0x0
  678. stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), appVersion)
  679. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  680. state.LastValidators = state.Validators.Copy()
  681. // mode = 0 for committing all the blocks
  682. blocks := makeBlocks(3, &state, privVal)
  683. store.chain = blocks
  684. // 2. Tendermint must panic if app returns wrong hash for the first block
  685. // - RANDOM HASH
  686. // - 0x02
  687. // - 0x03
  688. {
  689. app := &badApp{numBlocks: 3, allHashesAreWrong: true}
  690. clientCreator := proxy.NewLocalClientCreator(app)
  691. proxyApp := proxy.NewAppConns(clientCreator)
  692. err := proxyApp.Start()
  693. require.NoError(t, err)
  694. defer proxyApp.Stop()
  695. assert.Panics(t, func() {
  696. h := NewHandshaker(stateDB, state, store, genDoc)
  697. h.Handshake(proxyApp)
  698. })
  699. }
  700. // 3. Tendermint must panic if app returns wrong hash for the last block
  701. // - 0x01
  702. // - 0x02
  703. // - RANDOM HASH
  704. {
  705. app := &badApp{numBlocks: 3, onlyLastHashIsWrong: true}
  706. clientCreator := proxy.NewLocalClientCreator(app)
  707. proxyApp := proxy.NewAppConns(clientCreator)
  708. err := proxyApp.Start()
  709. require.NoError(t, err)
  710. defer proxyApp.Stop()
  711. assert.Panics(t, func() {
  712. h := NewHandshaker(stateDB, state, store, genDoc)
  713. h.Handshake(proxyApp)
  714. })
  715. }
  716. }
  717. func makeBlocks(n int, state *sm.State, privVal types.PrivValidator) []*types.Block {
  718. blocks := make([]*types.Block, 0)
  719. var (
  720. prevBlock *types.Block
  721. prevBlockMeta *types.BlockMeta
  722. )
  723. appHeight := byte(0x01)
  724. for i := 0; i < n; i++ {
  725. height := int64(i + 1)
  726. block, parts := makeBlock(*state, prevBlock, prevBlockMeta, privVal, height)
  727. blocks = append(blocks, block)
  728. prevBlock = block
  729. prevBlockMeta = types.NewBlockMeta(block, parts)
  730. // update state
  731. state.AppHash = []byte{appHeight}
  732. appHeight++
  733. state.LastBlockHeight = height
  734. }
  735. return blocks
  736. }
  737. func makeVote(header *types.Header, blockID types.BlockID, valset *types.ValidatorSet, privVal types.PrivValidator) *types.Vote {
  738. addr := privVal.GetPubKey().Address()
  739. idx, _ := valset.GetByAddress(addr)
  740. vote := &types.Vote{
  741. ValidatorAddress: addr,
  742. ValidatorIndex: idx,
  743. Height: header.Height,
  744. Round: 1,
  745. Timestamp: tmtime.Now(),
  746. Type: types.PrecommitType,
  747. BlockID: blockID,
  748. }
  749. privVal.SignVote(header.ChainID, vote)
  750. return vote
  751. }
  752. func makeBlock(state sm.State, lastBlock *types.Block, lastBlockMeta *types.BlockMeta,
  753. privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) {
  754. lastCommit := types.NewCommit(types.BlockID{}, nil)
  755. if height > 1 {
  756. vote := makeVote(&lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVal).CommitSig()
  757. lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{vote})
  758. }
  759. return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
  760. }
  761. type badApp struct {
  762. abci.BaseApplication
  763. numBlocks byte
  764. height byte
  765. allHashesAreWrong bool
  766. onlyLastHashIsWrong bool
  767. }
  768. func (app *badApp) Commit() abci.ResponseCommit {
  769. app.height++
  770. if app.onlyLastHashIsWrong {
  771. if app.height == app.numBlocks {
  772. return abci.ResponseCommit{Data: cmn.RandBytes(8)}
  773. }
  774. return abci.ResponseCommit{Data: []byte{app.height}}
  775. } else if app.allHashesAreWrong {
  776. return abci.ResponseCommit{Data: cmn.RandBytes(8)}
  777. }
  778. panic("either allHashesAreWrong or onlyLastHashIsWrong must be set")
  779. }
  780. //--------------------------
  781. // utils for making blocks
  782. func makeBlockchainFromWAL(wal WAL) ([]*types.Block, []*types.Commit, error) {
  783. var height int64
  784. // Search for height marker
  785. gr, found, err := wal.SearchForEndHeight(height, &WALSearchOptions{})
  786. if err != nil {
  787. return nil, nil, err
  788. }
  789. if !found {
  790. return nil, nil, fmt.Errorf("WAL does not contain height %d", height)
  791. }
  792. defer gr.Close() // nolint: errcheck
  793. // log.Notice("Build a blockchain by reading from the WAL")
  794. var (
  795. blocks []*types.Block
  796. commits []*types.Commit
  797. thisBlockParts *types.PartSet
  798. thisBlockCommit *types.Commit
  799. )
  800. dec := NewWALDecoder(gr)
  801. for {
  802. msg, err := dec.Decode()
  803. if err == io.EOF {
  804. break
  805. } else if err != nil {
  806. return nil, nil, err
  807. }
  808. piece := readPieceFromWAL(msg)
  809. if piece == nil {
  810. continue
  811. }
  812. switch p := piece.(type) {
  813. case EndHeightMessage:
  814. // if its not the first one, we have a full block
  815. if thisBlockParts != nil {
  816. var block = new(types.Block)
  817. _, err = cdc.UnmarshalBinaryLengthPrefixedReader(thisBlockParts.GetReader(), block, 0)
  818. if err != nil {
  819. panic(err)
  820. }
  821. if block.Height != height+1 {
  822. panic(fmt.Sprintf("read bad block from wal. got height %d, expected %d", block.Height, height+1))
  823. }
  824. commitHeight := thisBlockCommit.Precommits[0].Height
  825. if commitHeight != height+1 {
  826. panic(fmt.Sprintf("commit doesnt match. got height %d, expected %d", commitHeight, height+1))
  827. }
  828. blocks = append(blocks, block)
  829. commits = append(commits, thisBlockCommit)
  830. height++
  831. }
  832. case *types.PartSetHeader:
  833. thisBlockParts = types.NewPartSetFromHeader(*p)
  834. case *types.Part:
  835. _, err := thisBlockParts.AddPart(p)
  836. if err != nil {
  837. return nil, nil, err
  838. }
  839. case *types.Vote:
  840. if p.Type == types.PrecommitType {
  841. commitSigs := []*types.CommitSig{p.CommitSig()}
  842. thisBlockCommit = types.NewCommit(p.BlockID, commitSigs)
  843. }
  844. }
  845. }
  846. // grab the last block too
  847. var block = new(types.Block)
  848. _, err = cdc.UnmarshalBinaryLengthPrefixedReader(thisBlockParts.GetReader(), block, 0)
  849. if err != nil {
  850. panic(err)
  851. }
  852. if block.Height != height+1 {
  853. panic(fmt.Sprintf("read bad block from wal. got height %d, expected %d", block.Height, height+1))
  854. }
  855. commitHeight := thisBlockCommit.Precommits[0].Height
  856. if commitHeight != height+1 {
  857. panic(fmt.Sprintf("commit doesnt match. got height %d, expected %d", commitHeight, height+1))
  858. }
  859. blocks = append(blocks, block)
  860. commits = append(commits, thisBlockCommit)
  861. return blocks, commits, nil
  862. }
  863. func readPieceFromWAL(msg *TimedWALMessage) interface{} {
  864. // for logging
  865. switch m := msg.Msg.(type) {
  866. case msgInfo:
  867. switch msg := m.Msg.(type) {
  868. case *ProposalMessage:
  869. return &msg.Proposal.BlockID.PartsHeader
  870. case *BlockPartMessage:
  871. return msg.Part
  872. case *VoteMessage:
  873. return msg.Vote
  874. }
  875. case EndHeightMessage:
  876. return m
  877. }
  878. return nil
  879. }
  880. // fresh state and mock store
  881. func stateAndStore(config *cfg.Config, pubKey crypto.PubKey, appVersion version.Protocol) (dbm.DB, sm.State, *mockBlockStore) {
  882. stateDB := dbm.NewMemDB()
  883. state, _ := sm.MakeGenesisStateFromFile(config.GenesisFile())
  884. state.Version.Consensus.App = appVersion
  885. store := newMockBlockStore(config, state.ConsensusParams)
  886. sm.SaveState(stateDB, state)
  887. return stateDB, state, store
  888. }
  889. //----------------------------------
  890. // mock block store
  891. type mockBlockStore struct {
  892. config *cfg.Config
  893. params types.ConsensusParams
  894. chain []*types.Block
  895. commits []*types.Commit
  896. }
  897. // TODO: NewBlockStore(db.NewMemDB) ...
  898. func newMockBlockStore(config *cfg.Config, params types.ConsensusParams) *mockBlockStore {
  899. return &mockBlockStore{config, params, nil, nil}
  900. }
  901. func (bs *mockBlockStore) Height() int64 { return int64(len(bs.chain)) }
  902. func (bs *mockBlockStore) LoadBlock(height int64) *types.Block { return bs.chain[height-1] }
  903. func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
  904. block := bs.chain[height-1]
  905. return &types.BlockMeta{
  906. BlockID: types.BlockID{Hash: block.Hash(), PartsHeader: block.MakePartSet(types.BlockPartSizeBytes).Header()},
  907. Header: block.Header,
  908. }
  909. }
  910. func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
  911. func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  912. }
  913. func (bs *mockBlockStore) LoadBlockCommit(height int64) *types.Commit {
  914. return bs.commits[height-1]
  915. }
  916. func (bs *mockBlockStore) LoadSeenCommit(height int64) *types.Commit {
  917. return bs.commits[height-1]
  918. }
  919. //---------------------------------------
  920. // Test handshake/init chain
  921. func TestHandshakeUpdatesValidators(t *testing.T) {
  922. val, _ := types.RandValidator(true, 10)
  923. vals := types.NewValidatorSet([]*types.Validator{val})
  924. app := &initChainApp{vals: types.TM2PB.ValidatorUpdates(vals)}
  925. clientCreator := proxy.NewLocalClientCreator(app)
  926. config := ResetConfig("handshake_test_")
  927. defer os.RemoveAll(config.RootDir)
  928. privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  929. stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), 0x0)
  930. oldValAddr := state.Validators.Validators[0].Address
  931. // now start the app using the handshake - it should sync
  932. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  933. handshaker := NewHandshaker(stateDB, state, store, genDoc)
  934. proxyApp := proxy.NewAppConns(clientCreator)
  935. if err := proxyApp.Start(); err != nil {
  936. t.Fatalf("Error starting proxy app connections: %v", err)
  937. }
  938. defer proxyApp.Stop()
  939. if err := handshaker.Handshake(proxyApp); err != nil {
  940. t.Fatalf("Error on abci handshake: %v", err)
  941. }
  942. // reload the state, check the validator set was updated
  943. state = sm.LoadState(stateDB)
  944. newValAddr := state.Validators.Validators[0].Address
  945. expectValAddr := val.Address
  946. assert.NotEqual(t, oldValAddr, newValAddr)
  947. assert.Equal(t, newValAddr, expectValAddr)
  948. }
  949. // returns the vals on InitChain
  950. type initChainApp struct {
  951. abci.BaseApplication
  952. vals []abci.ValidatorUpdate
  953. }
  954. func (ica *initChainApp) InitChain(req abci.RequestInitChain) abci.ResponseInitChain {
  955. return abci.ResponseInitChain{
  956. Validators: ica.vals,
  957. }
  958. }