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.

918 lines
28 KiB

state: add more tests for block validation (#3674) * Expose priv validators for use in testing * Generalize block header validation test past height 1 * Remove ineffectual assignment * Remove redundant SaveState call * Reorder comment for clarity * Use the block executor ApplyBlock function instead of implementing a stripped-down version of it * Remove commented-out code * Remove unnecessary test The required tests already appear to be implemented (implicitly) through the TestValidateBlockHeader test. * Allow for catching of specific error types during TestValidateBlockCommit * Make return error testable * Clean up and add TestValidateBlockCommit code * Fix formatting * Extract function to create a new mock test app * Update comment for clarity * Fix comment * Add skeleton code for evidence-related test * Allow for addressing priv val by address * Generalize test beyond a single validator * Generalize TestValidateBlockEvidence past first height * Reorder code to clearly separate tests and utility code * Use a common constant for stop height for testing in state/validation_test.go * Refactor errors to resemble existing conventions * Fix formatting * Extract common helper functions Having the tests littered with helper functions makes them less easily readable imho, so I've pulled them out into a separate file. This also makes it easier to see what helper functions are available during testing, so we minimize the chance of duplication when writing new tests. * Remove unused parameter * Remove unused parameters * Add field keys * Remove unused height constant * Fix typo * Fix incorrect return error * Add field keys * Use separate package for tests This refactors all of the state package's tests into a state_test package, so as to keep any usage of the state package's internal methods explicit. Any internal methods/constants used by tests are now explicitly exported in state/export_test.go * Refactor: extract helper function to make, validate, execute and commit a block * Rename state function to makeState * Remove redundant constant for number of validators * Refactor mock evidence registration into TestMain * Remove extraneous nVals variable * Replace function-level TODOs with file-level TODO and explanation * Remove extraneous comment * Fix linting issues brought up by GolangCI (pulled in from latest merge from develop)
5 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
state: add more tests for block validation (#3674) * Expose priv validators for use in testing * Generalize block header validation test past height 1 * Remove ineffectual assignment * Remove redundant SaveState call * Reorder comment for clarity * Use the block executor ApplyBlock function instead of implementing a stripped-down version of it * Remove commented-out code * Remove unnecessary test The required tests already appear to be implemented (implicitly) through the TestValidateBlockHeader test. * Allow for catching of specific error types during TestValidateBlockCommit * Make return error testable * Clean up and add TestValidateBlockCommit code * Fix formatting * Extract function to create a new mock test app * Update comment for clarity * Fix comment * Add skeleton code for evidence-related test * Allow for addressing priv val by address * Generalize test beyond a single validator * Generalize TestValidateBlockEvidence past first height * Reorder code to clearly separate tests and utility code * Use a common constant for stop height for testing in state/validation_test.go * Refactor errors to resemble existing conventions * Fix formatting * Extract common helper functions Having the tests littered with helper functions makes them less easily readable imho, so I've pulled them out into a separate file. This also makes it easier to see what helper functions are available during testing, so we minimize the chance of duplication when writing new tests. * Remove unused parameter * Remove unused parameters * Add field keys * Remove unused height constant * Fix typo * Fix incorrect return error * Add field keys * Use separate package for tests This refactors all of the state package's tests into a state_test package, so as to keep any usage of the state package's internal methods explicit. Any internal methods/constants used by tests are now explicitly exported in state/export_test.go * Refactor: extract helper function to make, validate, execute and commit a block * Rename state function to makeState * Remove redundant constant for number of validators * Refactor mock evidence registration into TestMain * Remove extraneous nVals variable * Replace function-level TODOs with file-level TODO and explanation * Remove extraneous comment * Fix linting issues brought up by GolangCI (pulled in from latest merge from develop)
5 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
6 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package state_test
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/mock"
  8. "github.com/stretchr/testify/require"
  9. dbm "github.com/tendermint/tm-db"
  10. abciclient "github.com/tendermint/tendermint/abci/client"
  11. abci "github.com/tendermint/tendermint/abci/types"
  12. abcimocks "github.com/tendermint/tendermint/abci/types/mocks"
  13. "github.com/tendermint/tendermint/crypto"
  14. "github.com/tendermint/tendermint/crypto/ed25519"
  15. "github.com/tendermint/tendermint/crypto/encoding"
  16. "github.com/tendermint/tendermint/crypto/tmhash"
  17. "github.com/tendermint/tendermint/internal/eventbus"
  18. mpmocks "github.com/tendermint/tendermint/internal/mempool/mocks"
  19. "github.com/tendermint/tendermint/internal/proxy"
  20. "github.com/tendermint/tendermint/internal/pubsub"
  21. sm "github.com/tendermint/tendermint/internal/state"
  22. "github.com/tendermint/tendermint/internal/state/mocks"
  23. sf "github.com/tendermint/tendermint/internal/state/test/factory"
  24. "github.com/tendermint/tendermint/internal/store"
  25. "github.com/tendermint/tendermint/internal/test/factory"
  26. "github.com/tendermint/tendermint/libs/log"
  27. "github.com/tendermint/tendermint/types"
  28. "github.com/tendermint/tendermint/version"
  29. )
  30. var (
  31. chainID = "execution_chain"
  32. testPartSize uint32 = 65536
  33. )
  34. func TestApplyBlock(t *testing.T) {
  35. app := &testApp{}
  36. logger := log.TestingLogger()
  37. cc := abciclient.NewLocalClient(logger, app)
  38. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  39. ctx, cancel := context.WithCancel(context.Background())
  40. defer cancel()
  41. require.NoError(t, proxyApp.Start(ctx))
  42. eventBus := eventbus.NewDefault(logger)
  43. require.NoError(t, eventBus.Start(ctx))
  44. state, stateDB, _ := makeState(t, 1, 1)
  45. stateStore := sm.NewStore(stateDB)
  46. blockStore := store.NewBlockStore(dbm.NewMemDB())
  47. mp := &mpmocks.Mempool{}
  48. mp.On("Lock").Return()
  49. mp.On("Unlock").Return()
  50. mp.On("FlushAppConn", mock.Anything).Return(nil)
  51. mp.On("Update",
  52. mock.Anything,
  53. mock.Anything,
  54. mock.Anything,
  55. mock.Anything,
  56. mock.Anything,
  57. mock.Anything).Return(nil)
  58. blockExec := sm.NewBlockExecutor(stateStore, logger, proxyApp, mp, sm.EmptyEvidencePool{}, blockStore, eventBus)
  59. block := sf.MakeBlock(state, 1, new(types.Commit))
  60. bps, err := block.MakePartSet(testPartSize)
  61. require.NoError(t, err)
  62. blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
  63. state, err = blockExec.ApplyBlock(ctx, state, blockID, block)
  64. require.NoError(t, err)
  65. // TODO check state and mempool
  66. assert.EqualValues(t, 1, state.Version.Consensus.App, "App version wasn't updated")
  67. }
  68. // TestFinalizeBlockDecidedLastCommit ensures we correctly send the DecidedLastCommit to the
  69. // application. The test ensures that the DecidedLastCommit properly reflects
  70. // which validators signed the preceding block.
  71. func TestFinalizeBlockDecidedLastCommit(t *testing.T) {
  72. ctx, cancel := context.WithCancel(context.Background())
  73. defer cancel()
  74. logger := log.TestingLogger()
  75. app := &testApp{}
  76. cc := abciclient.NewLocalClient(logger, app)
  77. appClient := proxy.New(cc, logger, proxy.NopMetrics())
  78. err := appClient.Start(ctx)
  79. require.NoError(t, err)
  80. state, stateDB, privVals := makeState(t, 7, 1)
  81. stateStore := sm.NewStore(stateDB)
  82. absentSig := types.NewCommitSigAbsent()
  83. testCases := []struct {
  84. name string
  85. absentCommitSigs map[int]bool
  86. }{
  87. {"none absent", map[int]bool{}},
  88. {"one absent", map[int]bool{1: true}},
  89. {"multiple absent", map[int]bool{1: true, 3: true}},
  90. }
  91. for _, tc := range testCases {
  92. t.Run(tc.name, func(t *testing.T) {
  93. blockStore := store.NewBlockStore(dbm.NewMemDB())
  94. evpool := &mocks.EvidencePool{}
  95. evpool.On("PendingEvidence", mock.Anything).Return([]types.Evidence{}, 0)
  96. evpool.On("Update", ctx, mock.Anything, mock.Anything).Return()
  97. evpool.On("CheckEvidence", ctx, mock.Anything).Return(nil)
  98. mp := &mpmocks.Mempool{}
  99. mp.On("Lock").Return()
  100. mp.On("Unlock").Return()
  101. mp.On("FlushAppConn", mock.Anything).Return(nil)
  102. mp.On("Update",
  103. mock.Anything,
  104. mock.Anything,
  105. mock.Anything,
  106. mock.Anything,
  107. mock.Anything,
  108. mock.Anything).Return(nil)
  109. eventBus := eventbus.NewDefault(logger)
  110. require.NoError(t, eventBus.Start(ctx))
  111. blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), appClient, mp, evpool, blockStore, eventBus)
  112. state, _, lastCommit := makeAndCommitGoodBlock(ctx, t, state, 1, new(types.Commit), state.NextValidators.Validators[0].Address, blockExec, privVals, nil)
  113. for idx, isAbsent := range tc.absentCommitSigs {
  114. if isAbsent {
  115. lastCommit.Signatures[idx] = absentSig
  116. }
  117. }
  118. // block for height 2
  119. block := sf.MakeBlock(state, 2, lastCommit)
  120. bps, err := block.MakePartSet(testPartSize)
  121. require.NoError(t, err)
  122. blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
  123. _, err = blockExec.ApplyBlock(ctx, state, blockID, block)
  124. require.NoError(t, err)
  125. // -> app receives a list of validators with a bool indicating if they signed
  126. for i, v := range app.CommitVotes {
  127. _, absent := tc.absentCommitSigs[i]
  128. assert.Equal(t, !absent, v.SignedLastBlock)
  129. }
  130. })
  131. }
  132. }
  133. // TestFinalizeBlockByzantineValidators ensures we send byzantine validators list.
  134. func TestFinalizeBlockByzantineValidators(t *testing.T) {
  135. ctx, cancel := context.WithCancel(context.Background())
  136. defer cancel()
  137. app := &testApp{}
  138. logger := log.TestingLogger()
  139. cc := abciclient.NewLocalClient(logger, app)
  140. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  141. err := proxyApp.Start(ctx)
  142. require.NoError(t, err)
  143. state, stateDB, privVals := makeState(t, 1, 1)
  144. stateStore := sm.NewStore(stateDB)
  145. defaultEvidenceTime := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
  146. privVal := privVals[state.Validators.Validators[0].Address.String()]
  147. blockID := makeBlockID([]byte("headerhash"), 1000, []byte("partshash"))
  148. header := &types.Header{
  149. Version: version.Consensus{Block: version.BlockProtocol, App: 1},
  150. ChainID: state.ChainID,
  151. Height: 10,
  152. Time: defaultEvidenceTime,
  153. LastBlockID: blockID,
  154. LastCommitHash: crypto.CRandBytes(tmhash.Size),
  155. DataHash: crypto.CRandBytes(tmhash.Size),
  156. ValidatorsHash: state.Validators.Hash(),
  157. NextValidatorsHash: state.Validators.Hash(),
  158. ConsensusHash: crypto.CRandBytes(tmhash.Size),
  159. AppHash: crypto.CRandBytes(tmhash.Size),
  160. LastResultsHash: crypto.CRandBytes(tmhash.Size),
  161. EvidenceHash: crypto.CRandBytes(tmhash.Size),
  162. ProposerAddress: crypto.CRandBytes(crypto.AddressSize),
  163. }
  164. // we don't need to worry about validating the evidence as long as they pass validate basic
  165. dve, err := types.NewMockDuplicateVoteEvidenceWithValidator(ctx, 3, defaultEvidenceTime, privVal, state.ChainID)
  166. require.NoError(t, err)
  167. dve.ValidatorPower = 1000
  168. lcae := &types.LightClientAttackEvidence{
  169. ConflictingBlock: &types.LightBlock{
  170. SignedHeader: &types.SignedHeader{
  171. Header: header,
  172. Commit: types.NewCommit(10, 0, makeBlockID(header.Hash(), 100, []byte("partshash")), []types.CommitSig{{
  173. BlockIDFlag: types.BlockIDFlagNil,
  174. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  175. Timestamp: defaultEvidenceTime,
  176. Signature: crypto.CRandBytes(types.MaxSignatureSize),
  177. }}),
  178. },
  179. ValidatorSet: state.Validators,
  180. },
  181. CommonHeight: 8,
  182. ByzantineValidators: []*types.Validator{state.Validators.Validators[0]},
  183. TotalVotingPower: 12,
  184. Timestamp: defaultEvidenceTime,
  185. }
  186. ev := []types.Evidence{dve, lcae}
  187. abciEv := []abci.Evidence{
  188. {
  189. Type: abci.EvidenceType_DUPLICATE_VOTE,
  190. Height: 3,
  191. Time: defaultEvidenceTime,
  192. Validator: types.TM2PB.Validator(state.Validators.Validators[0]),
  193. TotalVotingPower: 10,
  194. },
  195. {
  196. Type: abci.EvidenceType_LIGHT_CLIENT_ATTACK,
  197. Height: 8,
  198. Time: defaultEvidenceTime,
  199. Validator: types.TM2PB.Validator(state.Validators.Validators[0]),
  200. TotalVotingPower: 12,
  201. },
  202. }
  203. evpool := &mocks.EvidencePool{}
  204. evpool.On("PendingEvidence", mock.AnythingOfType("int64")).Return(ev, int64(100))
  205. evpool.On("Update", ctx, mock.AnythingOfType("state.State"), mock.AnythingOfType("types.EvidenceList")).Return()
  206. evpool.On("CheckEvidence", ctx, mock.AnythingOfType("types.EvidenceList")).Return(nil)
  207. mp := &mpmocks.Mempool{}
  208. mp.On("Lock").Return()
  209. mp.On("Unlock").Return()
  210. mp.On("FlushAppConn", mock.Anything).Return(nil)
  211. mp.On("Update",
  212. mock.Anything,
  213. mock.Anything,
  214. mock.Anything,
  215. mock.Anything,
  216. mock.Anything,
  217. mock.Anything).Return(nil)
  218. eventBus := eventbus.NewDefault(logger)
  219. require.NoError(t, eventBus.Start(ctx))
  220. blockStore := store.NewBlockStore(dbm.NewMemDB())
  221. blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp,
  222. mp, evpool, blockStore, eventBus)
  223. block := sf.MakeBlock(state, 1, new(types.Commit))
  224. block.Evidence = ev
  225. block.Header.EvidenceHash = block.Evidence.Hash()
  226. bps, err := block.MakePartSet(testPartSize)
  227. require.NoError(t, err)
  228. blockID = types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
  229. _, err = blockExec.ApplyBlock(ctx, state, blockID, block)
  230. require.NoError(t, err)
  231. // TODO check state and mempool
  232. assert.Equal(t, abciEv, app.ByzantineValidators)
  233. }
  234. func TestProcessProposal(t *testing.T) {
  235. const height = 2
  236. txs := factory.MakeTenTxs(height)
  237. ctx, cancel := context.WithCancel(context.Background())
  238. defer cancel()
  239. app := abcimocks.NewBaseMock()
  240. logger := log.TestingLogger()
  241. cc := abciclient.NewLocalClient(logger, app)
  242. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  243. err := proxyApp.Start(ctx)
  244. require.NoError(t, err)
  245. state, stateDB, privVals := makeState(t, 1, height)
  246. stateStore := sm.NewStore(stateDB)
  247. blockStore := store.NewBlockStore(dbm.NewMemDB())
  248. eventBus := eventbus.NewDefault(logger)
  249. require.NoError(t, eventBus.Start(ctx))
  250. blockExec := sm.NewBlockExecutor(
  251. stateStore,
  252. logger,
  253. proxyApp,
  254. new(mpmocks.Mempool),
  255. sm.EmptyEvidencePool{},
  256. blockStore,
  257. eventBus,
  258. )
  259. block0 := sf.MakeBlock(state, height-1, new(types.Commit))
  260. lastCommitSig := []types.CommitSig{}
  261. partSet, err := block0.MakePartSet(types.BlockPartSizeBytes)
  262. require.NoError(t, err)
  263. blockID := types.BlockID{Hash: block0.Hash(), PartSetHeader: partSet.Header()}
  264. voteInfos := []abci.VoteInfo{}
  265. for _, privVal := range privVals {
  266. vote, err := factory.MakeVote(ctx, privVal, block0.Header.ChainID, 0, 0, 0, 2, blockID, time.Now())
  267. require.NoError(t, err)
  268. pk, err := privVal.GetPubKey(ctx)
  269. require.NoError(t, err)
  270. addr := pk.Address()
  271. voteInfos = append(voteInfos,
  272. abci.VoteInfo{
  273. SignedLastBlock: true,
  274. Validator: abci.Validator{
  275. Address: addr,
  276. Power: 1000,
  277. },
  278. })
  279. lastCommitSig = append(lastCommitSig, vote.CommitSig())
  280. }
  281. lastCommit := types.NewCommit(height-1, 0, types.BlockID{}, lastCommitSig)
  282. block1 := sf.MakeBlock(state, height, lastCommit)
  283. block1.Txs = txs
  284. expectedRpp := abci.RequestProcessProposal{
  285. Hash: block1.Hash(),
  286. Header: *block1.Header.ToProto(),
  287. Txs: block1.Txs.ToSliceOfBytes(),
  288. ByzantineValidators: block1.Evidence.ToABCI(),
  289. ProposedLastCommit: abci.CommitInfo{
  290. Round: 0,
  291. Votes: voteInfos,
  292. },
  293. }
  294. app.On("ProcessProposal", mock.Anything).Return(abci.ResponseProcessProposal{Accept: true})
  295. acceptBlock, err := blockExec.ProcessProposal(ctx, block1, state)
  296. require.NoError(t, err)
  297. require.True(t, acceptBlock)
  298. app.AssertExpectations(t)
  299. app.AssertCalled(t, "ProcessProposal", expectedRpp)
  300. }
  301. func TestValidateValidatorUpdates(t *testing.T) {
  302. pubkey1 := ed25519.GenPrivKey().PubKey()
  303. pubkey2 := ed25519.GenPrivKey().PubKey()
  304. pk1, err := encoding.PubKeyToProto(pubkey1)
  305. assert.NoError(t, err)
  306. pk2, err := encoding.PubKeyToProto(pubkey2)
  307. assert.NoError(t, err)
  308. defaultValidatorParams := types.ValidatorParams{PubKeyTypes: []string{types.ABCIPubKeyTypeEd25519}}
  309. testCases := []struct {
  310. name string
  311. abciUpdates []abci.ValidatorUpdate
  312. validatorParams types.ValidatorParams
  313. shouldErr bool
  314. }{
  315. {
  316. "adding a validator is OK",
  317. []abci.ValidatorUpdate{{PubKey: pk2, Power: 20}},
  318. defaultValidatorParams,
  319. false,
  320. },
  321. {
  322. "updating a validator is OK",
  323. []abci.ValidatorUpdate{{PubKey: pk1, Power: 20}},
  324. defaultValidatorParams,
  325. false,
  326. },
  327. {
  328. "removing a validator is OK",
  329. []abci.ValidatorUpdate{{PubKey: pk2, Power: 0}},
  330. defaultValidatorParams,
  331. false,
  332. },
  333. {
  334. "adding a validator with negative power results in error",
  335. []abci.ValidatorUpdate{{PubKey: pk2, Power: -100}},
  336. defaultValidatorParams,
  337. true,
  338. },
  339. }
  340. for _, tc := range testCases {
  341. tc := tc
  342. t.Run(tc.name, func(t *testing.T) {
  343. err := sm.ValidateValidatorUpdates(tc.abciUpdates, tc.validatorParams)
  344. if tc.shouldErr {
  345. assert.Error(t, err)
  346. } else {
  347. assert.NoError(t, err)
  348. }
  349. })
  350. }
  351. }
  352. func TestUpdateValidators(t *testing.T) {
  353. pubkey1 := ed25519.GenPrivKey().PubKey()
  354. val1 := types.NewValidator(pubkey1, 10)
  355. pubkey2 := ed25519.GenPrivKey().PubKey()
  356. val2 := types.NewValidator(pubkey2, 20)
  357. pk, err := encoding.PubKeyToProto(pubkey1)
  358. require.NoError(t, err)
  359. pk2, err := encoding.PubKeyToProto(pubkey2)
  360. require.NoError(t, err)
  361. testCases := []struct {
  362. name string
  363. currentSet *types.ValidatorSet
  364. abciUpdates []abci.ValidatorUpdate
  365. resultingSet *types.ValidatorSet
  366. shouldErr bool
  367. }{
  368. {
  369. "adding a validator is OK",
  370. types.NewValidatorSet([]*types.Validator{val1}),
  371. []abci.ValidatorUpdate{{PubKey: pk2, Power: 20}},
  372. types.NewValidatorSet([]*types.Validator{val1, val2}),
  373. false,
  374. },
  375. {
  376. "updating a validator is OK",
  377. types.NewValidatorSet([]*types.Validator{val1}),
  378. []abci.ValidatorUpdate{{PubKey: pk, Power: 20}},
  379. types.NewValidatorSet([]*types.Validator{types.NewValidator(pubkey1, 20)}),
  380. false,
  381. },
  382. {
  383. "removing a validator is OK",
  384. types.NewValidatorSet([]*types.Validator{val1, val2}),
  385. []abci.ValidatorUpdate{{PubKey: pk2, Power: 0}},
  386. types.NewValidatorSet([]*types.Validator{val1}),
  387. false,
  388. },
  389. {
  390. "removing a non-existing validator results in error",
  391. types.NewValidatorSet([]*types.Validator{val1}),
  392. []abci.ValidatorUpdate{{PubKey: pk2, Power: 0}},
  393. types.NewValidatorSet([]*types.Validator{val1}),
  394. true,
  395. },
  396. }
  397. for _, tc := range testCases {
  398. tc := tc
  399. t.Run(tc.name, func(t *testing.T) {
  400. updates, err := types.PB2TM.ValidatorUpdates(tc.abciUpdates)
  401. assert.NoError(t, err)
  402. err = tc.currentSet.UpdateWithChangeSet(updates)
  403. if tc.shouldErr {
  404. assert.Error(t, err)
  405. } else {
  406. assert.NoError(t, err)
  407. require.Equal(t, tc.resultingSet.Size(), tc.currentSet.Size())
  408. assert.Equal(t, tc.resultingSet.TotalVotingPower(), tc.currentSet.TotalVotingPower())
  409. assert.Equal(t, tc.resultingSet.Validators[0].Address, tc.currentSet.Validators[0].Address)
  410. if tc.resultingSet.Size() > 1 {
  411. assert.Equal(t, tc.resultingSet.Validators[1].Address, tc.currentSet.Validators[1].Address)
  412. }
  413. }
  414. })
  415. }
  416. }
  417. // TestFinalizeBlockValidatorUpdates ensures we update validator set and send an event.
  418. func TestFinalizeBlockValidatorUpdates(t *testing.T) {
  419. ctx, cancel := context.WithCancel(context.Background())
  420. defer cancel()
  421. app := &testApp{}
  422. logger := log.TestingLogger()
  423. cc := abciclient.NewLocalClient(logger, app)
  424. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  425. err := proxyApp.Start(ctx)
  426. require.NoError(t, err)
  427. state, stateDB, _ := makeState(t, 1, 1)
  428. stateStore := sm.NewStore(stateDB)
  429. blockStore := store.NewBlockStore(dbm.NewMemDB())
  430. mp := &mpmocks.Mempool{}
  431. mp.On("Lock").Return()
  432. mp.On("Unlock").Return()
  433. mp.On("FlushAppConn", mock.Anything).Return(nil)
  434. mp.On("Update",
  435. mock.Anything,
  436. mock.Anything,
  437. mock.Anything,
  438. mock.Anything,
  439. mock.Anything,
  440. mock.Anything).Return(nil)
  441. mp.On("ReapMaxBytesMaxGas", mock.Anything, mock.Anything).Return(types.Txs{})
  442. eventBus := eventbus.NewDefault(logger)
  443. require.NoError(t, eventBus.Start(ctx))
  444. blockExec := sm.NewBlockExecutor(
  445. stateStore,
  446. logger,
  447. proxyApp,
  448. mp,
  449. sm.EmptyEvidencePool{},
  450. blockStore,
  451. eventBus,
  452. )
  453. updatesSub, err := eventBus.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
  454. ClientID: "TestFinalizeBlockValidatorUpdates",
  455. Query: types.EventQueryValidatorSetUpdates,
  456. })
  457. require.NoError(t, err)
  458. block := sf.MakeBlock(state, 1, new(types.Commit))
  459. bps, err := block.MakePartSet(testPartSize)
  460. require.NoError(t, err)
  461. blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
  462. pubkey := ed25519.GenPrivKey().PubKey()
  463. pk, err := encoding.PubKeyToProto(pubkey)
  464. require.NoError(t, err)
  465. app.ValidatorUpdates = []abci.ValidatorUpdate{
  466. {PubKey: pk, Power: 10},
  467. }
  468. state, err = blockExec.ApplyBlock(ctx, state, blockID, block)
  469. require.NoError(t, err)
  470. // test new validator was added to NextValidators
  471. if assert.Equal(t, state.Validators.Size()+1, state.NextValidators.Size()) {
  472. idx, _ := state.NextValidators.GetByAddress(pubkey.Address())
  473. if idx < 0 {
  474. t.Fatalf("can't find address %v in the set %v", pubkey.Address(), state.NextValidators)
  475. }
  476. }
  477. // test we threw an event
  478. ctx, cancel = context.WithTimeout(ctx, 1*time.Second)
  479. defer cancel()
  480. msg, err := updatesSub.Next(ctx)
  481. require.NoError(t, err)
  482. event, ok := msg.Data().(types.EventDataValidatorSetUpdates)
  483. require.True(t, ok, "Expected event of type EventDataValidatorSetUpdates, got %T", msg.Data())
  484. if assert.NotEmpty(t, event.ValidatorUpdates) {
  485. assert.Equal(t, pubkey, event.ValidatorUpdates[0].PubKey)
  486. assert.EqualValues(t, 10, event.ValidatorUpdates[0].VotingPower)
  487. }
  488. }
  489. // TestFinalizeBlockValidatorUpdatesResultingInEmptySet checks that processing validator updates that
  490. // would result in empty set causes no panic, an error is raised and NextValidators is not updated
  491. func TestFinalizeBlockValidatorUpdatesResultingInEmptySet(t *testing.T) {
  492. ctx, cancel := context.WithCancel(context.Background())
  493. defer cancel()
  494. app := &testApp{}
  495. logger := log.TestingLogger()
  496. cc := abciclient.NewLocalClient(logger, app)
  497. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  498. err := proxyApp.Start(ctx)
  499. require.NoError(t, err)
  500. eventBus := eventbus.NewDefault(logger)
  501. require.NoError(t, eventBus.Start(ctx))
  502. state, stateDB, _ := makeState(t, 1, 1)
  503. stateStore := sm.NewStore(stateDB)
  504. blockStore := store.NewBlockStore(dbm.NewMemDB())
  505. blockExec := sm.NewBlockExecutor(
  506. stateStore,
  507. log.TestingLogger(),
  508. proxyApp,
  509. new(mpmocks.Mempool),
  510. sm.EmptyEvidencePool{},
  511. blockStore,
  512. eventBus,
  513. )
  514. block := sf.MakeBlock(state, 1, new(types.Commit))
  515. bps, err := block.MakePartSet(testPartSize)
  516. require.NoError(t, err)
  517. blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
  518. vp, err := encoding.PubKeyToProto(state.Validators.Validators[0].PubKey)
  519. require.NoError(t, err)
  520. // Remove the only validator
  521. app.ValidatorUpdates = []abci.ValidatorUpdate{
  522. {PubKey: vp, Power: 0},
  523. }
  524. assert.NotPanics(t, func() { state, err = blockExec.ApplyBlock(ctx, state, blockID, block) })
  525. assert.Error(t, err)
  526. assert.NotEmpty(t, state.NextValidators.Validators)
  527. }
  528. func TestEmptyPrepareProposal(t *testing.T) {
  529. const height = 2
  530. ctx, cancel := context.WithCancel(context.Background())
  531. defer cancel()
  532. logger := log.TestingLogger()
  533. eventBus := eventbus.NewDefault(logger)
  534. require.NoError(t, eventBus.Start(ctx))
  535. app := abcimocks.NewBaseMock()
  536. cc := abciclient.NewLocalClient(logger, app)
  537. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  538. err := proxyApp.Start(ctx)
  539. require.NoError(t, err)
  540. state, stateDB, privVals := makeState(t, 1, height)
  541. stateStore := sm.NewStore(stateDB)
  542. mp := &mpmocks.Mempool{}
  543. mp.On("Lock").Return()
  544. mp.On("Unlock").Return()
  545. mp.On("FlushAppConn", mock.Anything).Return(nil)
  546. mp.On("Update",
  547. mock.Anything,
  548. mock.Anything,
  549. mock.Anything,
  550. mock.Anything,
  551. mock.Anything,
  552. mock.Anything).Return(nil)
  553. mp.On("ReapMaxBytesMaxGas", mock.Anything, mock.Anything).Return(types.Txs{})
  554. blockExec := sm.NewBlockExecutor(
  555. stateStore,
  556. logger,
  557. proxyApp,
  558. mp,
  559. sm.EmptyEvidencePool{},
  560. nil,
  561. eventBus,
  562. )
  563. pa, _ := state.Validators.GetByIndex(0)
  564. commit := makeValidCommit(ctx, t, height, types.BlockID{}, state.Validators, privVals)
  565. _, err = blockExec.CreateProposalBlock(ctx, height, state, commit, pa, nil)
  566. require.NoError(t, err)
  567. }
  568. // TestPrepareProposalRemoveTxs tests that any transactions marked as REMOVED
  569. // are not included in the block produced by CreateProposalBlock. The test also
  570. // ensures that any transactions removed are also removed from the mempool.
  571. func TestPrepareProposalRemoveTxs(t *testing.T) {
  572. const height = 2
  573. ctx, cancel := context.WithCancel(context.Background())
  574. defer cancel()
  575. logger := log.TestingLogger()
  576. eventBus := eventbus.NewDefault(logger)
  577. require.NoError(t, eventBus.Start(ctx))
  578. state, stateDB, privVals := makeState(t, 1, height)
  579. stateStore := sm.NewStore(stateDB)
  580. evpool := &mocks.EvidencePool{}
  581. evpool.On("PendingEvidence", mock.Anything).Return([]types.Evidence{}, int64(0))
  582. txs := factory.MakeTenTxs(height)
  583. mp := &mpmocks.Mempool{}
  584. mp.On("ReapMaxBytesMaxGas", mock.Anything, mock.Anything).Return(types.Txs(txs))
  585. trs := txsToTxRecords(types.Txs(txs))
  586. trs[0].Action = abci.TxRecord_REMOVED
  587. trs[1].Action = abci.TxRecord_REMOVED
  588. mp.On("RemoveTxByKey", mock.Anything).Return(nil).Twice()
  589. app := abcimocks.NewBaseMock()
  590. app.On("PrepareProposal", mock.Anything).Return(abci.ResponsePrepareProposal{
  591. ModifiedTx: true,
  592. TxRecords: trs,
  593. }, nil)
  594. cc := abciclient.NewLocalClient(logger, app)
  595. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  596. err := proxyApp.Start(ctx)
  597. require.NoError(t, err)
  598. blockExec := sm.NewBlockExecutor(
  599. stateStore,
  600. logger,
  601. proxyApp,
  602. mp,
  603. evpool,
  604. nil,
  605. eventBus,
  606. )
  607. pa, _ := state.Validators.GetByIndex(0)
  608. commit := makeValidCommit(ctx, t, height, types.BlockID{}, state.Validators, privVals)
  609. block, err := blockExec.CreateProposalBlock(ctx, height, state, commit, pa, nil)
  610. require.NoError(t, err)
  611. require.Len(t, block.Data.Txs.ToSliceOfBytes(), len(trs)-2)
  612. require.Equal(t, -1, block.Data.Txs.Index(types.Tx(trs[0].Tx)))
  613. require.Equal(t, -1, block.Data.Txs.Index(types.Tx(trs[1].Tx)))
  614. mp.AssertCalled(t, "RemoveTxByKey", types.Tx(trs[0].Tx).Key())
  615. mp.AssertCalled(t, "RemoveTxByKey", types.Tx(trs[1].Tx).Key())
  616. mp.AssertExpectations(t)
  617. }
  618. // TestPrepareProposalAddedTxsIncluded tests that any transactions marked as ADDED
  619. // in the prepare proposal response are included in the block. The test also
  620. // ensures that any transactions added are also checked into the mempool.
  621. func TestPrepareProposalAddedTxsIncluded(t *testing.T) {
  622. const height = 2
  623. ctx, cancel := context.WithCancel(context.Background())
  624. defer cancel()
  625. logger := log.TestingLogger()
  626. eventBus := eventbus.NewDefault(logger)
  627. require.NoError(t, eventBus.Start(ctx))
  628. state, stateDB, privVals := makeState(t, 1, height)
  629. stateStore := sm.NewStore(stateDB)
  630. evpool := &mocks.EvidencePool{}
  631. evpool.On("PendingEvidence", mock.Anything).Return([]types.Evidence{}, int64(0))
  632. txs := factory.MakeTenTxs(height)
  633. mp := &mpmocks.Mempool{}
  634. mp.On("ReapMaxBytesMaxGas", mock.Anything, mock.Anything).Return(types.Txs(txs[2:]))
  635. mp.On("CheckTx", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Twice()
  636. trs := txsToTxRecords(types.Txs(txs))
  637. trs[0].Action = abci.TxRecord_ADDED
  638. trs[1].Action = abci.TxRecord_ADDED
  639. app := abcimocks.NewBaseMock()
  640. app.On("PrepareProposal", mock.Anything).Return(abci.ResponsePrepareProposal{
  641. ModifiedTx: true,
  642. TxRecords: trs,
  643. }, nil)
  644. cc := abciclient.NewLocalClient(logger, app)
  645. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  646. err := proxyApp.Start(ctx)
  647. require.NoError(t, err)
  648. blockExec := sm.NewBlockExecutor(
  649. stateStore,
  650. logger,
  651. proxyApp,
  652. mp,
  653. evpool,
  654. nil,
  655. eventBus,
  656. )
  657. pa, _ := state.Validators.GetByIndex(0)
  658. commit := makeValidCommit(ctx, t, height, types.BlockID{}, state.Validators, privVals)
  659. block, err := blockExec.CreateProposalBlock(ctx, height, state, commit, pa, nil)
  660. require.NoError(t, err)
  661. require.Equal(t, txs[0], block.Data.Txs[0])
  662. require.Equal(t, txs[1], block.Data.Txs[1])
  663. mp.AssertExpectations(t)
  664. mp.AssertCalled(t, "CheckTx", mock.Anything, types.Tx(trs[0].Tx), mock.Anything, mock.Anything)
  665. mp.AssertCalled(t, "CheckTx", mock.Anything, types.Tx(trs[1].Tx), mock.Anything, mock.Anything)
  666. }
  667. // TestPrepareProposalReorderTxs tests that CreateBlock produces a block with transactions
  668. // in the order matching the order they are returned from PrepareProposal.
  669. func TestPrepareProposalReorderTxs(t *testing.T) {
  670. const height = 2
  671. ctx, cancel := context.WithCancel(context.Background())
  672. defer cancel()
  673. logger := log.TestingLogger()
  674. eventBus := eventbus.NewDefault(logger)
  675. require.NoError(t, eventBus.Start(ctx))
  676. state, stateDB, privVals := makeState(t, 1, height)
  677. stateStore := sm.NewStore(stateDB)
  678. evpool := &mocks.EvidencePool{}
  679. evpool.On("PendingEvidence", mock.Anything).Return([]types.Evidence{}, int64(0))
  680. txs := factory.MakeTenTxs(height)
  681. mp := &mpmocks.Mempool{}
  682. mp.On("ReapMaxBytesMaxGas", mock.Anything, mock.Anything).Return(types.Txs(txs))
  683. trs := txsToTxRecords(types.Txs(txs))
  684. trs = trs[2:]
  685. trs = append(trs[len(trs)/2:], trs[:len(trs)/2]...)
  686. app := abcimocks.NewBaseMock()
  687. app.On("PrepareProposal", mock.Anything).Return(abci.ResponsePrepareProposal{
  688. ModifiedTx: true,
  689. TxRecords: trs,
  690. }, nil)
  691. cc := abciclient.NewLocalClient(logger, app)
  692. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  693. err := proxyApp.Start(ctx)
  694. require.NoError(t, err)
  695. blockExec := sm.NewBlockExecutor(
  696. stateStore,
  697. logger,
  698. proxyApp,
  699. mp,
  700. evpool,
  701. nil,
  702. eventBus,
  703. )
  704. pa, _ := state.Validators.GetByIndex(0)
  705. commit := makeValidCommit(ctx, t, height, types.BlockID{}, state.Validators, privVals)
  706. block, err := blockExec.CreateProposalBlock(ctx, height, state, commit, pa, nil)
  707. require.NoError(t, err)
  708. for i, tx := range block.Data.Txs {
  709. require.Equal(t, types.Tx(trs[i].Tx), tx)
  710. }
  711. mp.AssertExpectations(t)
  712. }
  713. // TestPrepareProposalModifiedTxFalse tests that CreateBlock correctly ignores
  714. // the ResponsePrepareProposal TxRecords if ResponsePrepareProposal does not
  715. // set ModifiedTx to true.
  716. func TestPrepareProposalModifiedTxFalse(t *testing.T) {
  717. const height = 2
  718. ctx, cancel := context.WithCancel(context.Background())
  719. defer cancel()
  720. logger := log.TestingLogger()
  721. eventBus := eventbus.NewDefault(logger)
  722. require.NoError(t, eventBus.Start(ctx))
  723. state, stateDB, privVals := makeState(t, 1, height)
  724. stateStore := sm.NewStore(stateDB)
  725. evpool := &mocks.EvidencePool{}
  726. evpool.On("PendingEvidence", mock.Anything).Return([]types.Evidence{}, int64(0))
  727. txs := factory.MakeTenTxs(height)
  728. mp := &mpmocks.Mempool{}
  729. mp.On("ReapMaxBytesMaxGas", mock.Anything, mock.Anything).Return(types.Txs(txs))
  730. trs := txsToTxRecords(types.Txs(txs))
  731. trs = append(trs[len(trs)/2:], trs[:len(trs)/2]...)
  732. trs = trs[1:]
  733. trs[0].Action = abci.TxRecord_REMOVED
  734. trs[1] = &abci.TxRecord{
  735. Tx: []byte("new"),
  736. Action: abci.TxRecord_ADDED,
  737. }
  738. app := abcimocks.NewBaseMock()
  739. app.On("PrepareProposal", mock.Anything).Return(abci.ResponsePrepareProposal{
  740. ModifiedTx: false,
  741. TxRecords: trs,
  742. }, nil)
  743. cc := abciclient.NewLocalClient(logger, app)
  744. proxyApp := proxy.New(cc, logger, proxy.NopMetrics())
  745. err := proxyApp.Start(ctx)
  746. require.NoError(t, err)
  747. blockExec := sm.NewBlockExecutor(
  748. stateStore,
  749. logger,
  750. proxyApp,
  751. mp,
  752. evpool,
  753. nil,
  754. eventBus,
  755. )
  756. pa, _ := state.Validators.GetByIndex(0)
  757. commit := makeValidCommit(ctx, t, height, types.BlockID{}, state.Validators, privVals)
  758. block, err := blockExec.CreateProposalBlock(ctx, height, state, commit, pa, nil)
  759. require.NoError(t, err)
  760. for i, tx := range block.Data.Txs {
  761. require.Equal(t, txs[i], tx)
  762. }
  763. mp.AssertExpectations(t)
  764. }
  765. func makeBlockID(hash []byte, partSetSize uint32, partSetHash []byte) types.BlockID {
  766. var (
  767. h = make([]byte, tmhash.Size)
  768. psH = make([]byte, tmhash.Size)
  769. )
  770. copy(h, hash)
  771. copy(psH, partSetHash)
  772. return types.BlockID{
  773. Hash: h,
  774. PartSetHeader: types.PartSetHeader{
  775. Total: partSetSize,
  776. Hash: psH,
  777. },
  778. }
  779. }
  780. func txsToTxRecords(txs []types.Tx) []*abci.TxRecord {
  781. trs := make([]*abci.TxRecord, len(txs))
  782. for i, tx := range txs {
  783. trs[i] = &abci.TxRecord{
  784. Action: abci.TxRecord_UNMODIFIED,
  785. Tx: tx,
  786. }
  787. }
  788. return trs
  789. }