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.

285 lines
8.1 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
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
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
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
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
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
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
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
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
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
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
  1. package state_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. dbm "github.com/tendermint/tm-db"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. "github.com/tendermint/tendermint/proxy"
  11. sm "github.com/tendermint/tendermint/state"
  12. "github.com/tendermint/tendermint/types"
  13. tmtime "github.com/tendermint/tendermint/types/time"
  14. )
  15. type paramsChangeTestCase struct {
  16. height int64
  17. params types.ConsensusParams
  18. }
  19. // always returns true if asked if any evidence was already committed.
  20. type mockEvPoolAlwaysCommitted struct{}
  21. func (m mockEvPoolAlwaysCommitted) PendingEvidence(int64) []types.Evidence { return nil }
  22. func (m mockEvPoolAlwaysCommitted) AddEvidence(types.Evidence) error { return nil }
  23. func (m mockEvPoolAlwaysCommitted) Update(*types.Block, sm.State) {}
  24. func (m mockEvPoolAlwaysCommitted) IsCommitted(types.Evidence) bool { return true }
  25. func newTestApp() proxy.AppConns {
  26. app := &testApp{}
  27. cc := proxy.NewLocalClientCreator(app)
  28. return proxy.NewAppConns(cc)
  29. }
  30. func makeAndCommitGoodBlock(
  31. state sm.State,
  32. height int64,
  33. lastCommit *types.Commit,
  34. proposerAddr []byte,
  35. blockExec *sm.BlockExecutor,
  36. privVals map[string]types.PrivValidator,
  37. evidence []types.Evidence) (sm.State, types.BlockID, *types.Commit, error) {
  38. // A good block passes
  39. state, blockID, err := makeAndApplyGoodBlock(state, height, lastCommit, proposerAddr, blockExec, evidence)
  40. if err != nil {
  41. return state, types.BlockID{}, nil, err
  42. }
  43. // Simulate a lastCommit for this block from all validators for the next height
  44. commit, err := makeValidCommit(height, blockID, state.Validators, privVals)
  45. if err != nil {
  46. return state, types.BlockID{}, nil, err
  47. }
  48. return state, blockID, commit, nil
  49. }
  50. func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commit, proposerAddr []byte,
  51. blockExec *sm.BlockExecutor, evidence []types.Evidence) (sm.State, types.BlockID, error) {
  52. block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, evidence, proposerAddr)
  53. if err := blockExec.ValidateBlock(state, block); err != nil {
  54. return state, types.BlockID{}, err
  55. }
  56. blockID := types.BlockID{Hash: block.Hash(),
  57. PartsHeader: types.PartSetHeader{Total: 3, Hash: tmrand.Bytes(32)}}
  58. state, err := blockExec.ApplyBlock(state, blockID, block)
  59. if err != nil {
  60. return state, types.BlockID{}, err
  61. }
  62. return state, blockID, nil
  63. }
  64. func makeValidCommit(
  65. height int64,
  66. blockID types.BlockID,
  67. vals *types.ValidatorSet,
  68. privVals map[string]types.PrivValidator,
  69. ) (*types.Commit, error) {
  70. sigs := make([]types.CommitSig, 0)
  71. for i := 0; i < vals.Size(); i++ {
  72. _, val := vals.GetByIndex(i)
  73. vote, err := types.MakeVote(height, blockID, vals, privVals[val.Address.String()], chainID)
  74. if err != nil {
  75. return nil, err
  76. }
  77. sigs = append(sigs, vote.CommitSig())
  78. }
  79. return types.NewCommit(height, 0, blockID, sigs), nil
  80. }
  81. // make some bogus txs
  82. func makeTxs(height int64) (txs []types.Tx) {
  83. for i := 0; i < nTxsPerBlock; i++ {
  84. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  85. }
  86. return txs
  87. }
  88. func makeState(nVals, height int) (sm.State, dbm.DB, map[string]types.PrivValidator) {
  89. vals := make([]types.GenesisValidator, nVals)
  90. privVals := make(map[string]types.PrivValidator, nVals)
  91. for i := 0; i < nVals; i++ {
  92. secret := []byte(fmt.Sprintf("test%d", i))
  93. pk := ed25519.GenPrivKeyFromSecret(secret)
  94. valAddr := pk.PubKey().Address()
  95. vals[i] = types.GenesisValidator{
  96. Address: valAddr,
  97. PubKey: pk.PubKey(),
  98. Power: 1000,
  99. Name: fmt.Sprintf("test%d", i),
  100. }
  101. privVals[valAddr.String()] = types.NewMockPVWithParams(pk, false, false)
  102. }
  103. s, _ := sm.MakeGenesisState(&types.GenesisDoc{
  104. ChainID: chainID,
  105. Validators: vals,
  106. AppHash: nil,
  107. })
  108. stateDB := dbm.NewMemDB()
  109. sm.SaveState(stateDB, s)
  110. for i := 1; i < height; i++ {
  111. s.LastBlockHeight++
  112. s.LastValidators = s.Validators.Copy()
  113. sm.SaveState(stateDB, s)
  114. }
  115. return s, stateDB, privVals
  116. }
  117. func makeBlock(state sm.State, height int64) *types.Block {
  118. block, _ := state.MakeBlock(
  119. height,
  120. makeTxs(state.LastBlockHeight),
  121. new(types.Commit),
  122. nil,
  123. state.Validators.GetProposer().Address,
  124. )
  125. return block
  126. }
  127. func genValSet(size int) *types.ValidatorSet {
  128. vals := make([]*types.Validator, size)
  129. for i := 0; i < size; i++ {
  130. vals[i] = types.NewValidator(ed25519.GenPrivKey().PubKey(), 10)
  131. }
  132. return types.NewValidatorSet(vals)
  133. }
  134. func makeConsensusParams(
  135. blockBytes, blockGas int64,
  136. blockTimeIotaMs int64,
  137. evidenceAge int64,
  138. ) types.ConsensusParams {
  139. return types.ConsensusParams{
  140. Block: types.BlockParams{
  141. MaxBytes: blockBytes,
  142. MaxGas: blockGas,
  143. TimeIotaMs: blockTimeIotaMs,
  144. },
  145. Evidence: types.EvidenceParams{
  146. MaxAge: evidenceAge,
  147. },
  148. }
  149. }
  150. func makeHeaderPartsResponsesValPubKeyChange(
  151. state sm.State,
  152. pubkey crypto.PubKey,
  153. ) (types.Header, types.BlockID, *sm.ABCIResponses) {
  154. block := makeBlock(state, state.LastBlockHeight+1)
  155. abciResponses := &sm.ABCIResponses{
  156. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  157. }
  158. // If the pubkey is new, remove the old and add the new.
  159. _, val := state.NextValidators.GetByIndex(0)
  160. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  161. abciResponses.EndBlock = &abci.ResponseEndBlock{
  162. ValidatorUpdates: []abci.ValidatorUpdate{
  163. types.TM2PB.NewValidatorUpdate(val.PubKey, 0),
  164. types.TM2PB.NewValidatorUpdate(pubkey, 10),
  165. },
  166. }
  167. }
  168. return block.Header, types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}, abciResponses
  169. }
  170. func makeHeaderPartsResponsesValPowerChange(
  171. state sm.State,
  172. power int64,
  173. ) (types.Header, types.BlockID, *sm.ABCIResponses) {
  174. block := makeBlock(state, state.LastBlockHeight+1)
  175. abciResponses := &sm.ABCIResponses{
  176. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  177. }
  178. // If the pubkey is new, remove the old and add the new.
  179. _, val := state.NextValidators.GetByIndex(0)
  180. if val.VotingPower != power {
  181. abciResponses.EndBlock = &abci.ResponseEndBlock{
  182. ValidatorUpdates: []abci.ValidatorUpdate{
  183. types.TM2PB.NewValidatorUpdate(val.PubKey, power),
  184. },
  185. }
  186. }
  187. return block.Header, types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}, abciResponses
  188. }
  189. func makeHeaderPartsResponsesParams(
  190. state sm.State,
  191. params types.ConsensusParams,
  192. ) (types.Header, types.BlockID, *sm.ABCIResponses) {
  193. block := makeBlock(state, state.LastBlockHeight+1)
  194. abciResponses := &sm.ABCIResponses{
  195. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  196. }
  197. return block.Header, types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}, abciResponses
  198. }
  199. func randomGenesisDoc() *types.GenesisDoc {
  200. pubkey := ed25519.GenPrivKey().PubKey()
  201. return &types.GenesisDoc{
  202. GenesisTime: tmtime.Now(),
  203. ChainID: "abc",
  204. Validators: []types.GenesisValidator{
  205. {
  206. Address: pubkey.Address(),
  207. PubKey: pubkey,
  208. Power: 10,
  209. Name: "myval",
  210. },
  211. },
  212. ConsensusParams: types.DefaultConsensusParams(),
  213. }
  214. }
  215. //----------------------------------------------------------------------------
  216. type testApp struct {
  217. abci.BaseApplication
  218. CommitVotes []abci.VoteInfo
  219. ByzantineValidators []abci.Evidence
  220. ValidatorUpdates []abci.ValidatorUpdate
  221. }
  222. var _ abci.Application = (*testApp)(nil)
  223. func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
  224. return abci.ResponseInfo{}
  225. }
  226. func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
  227. app.CommitVotes = req.LastCommitInfo.Votes
  228. app.ByzantineValidators = req.ByzantineValidators
  229. return abci.ResponseBeginBlock{}
  230. }
  231. func (app *testApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
  232. return abci.ResponseEndBlock{ValidatorUpdates: app.ValidatorUpdates}
  233. }
  234. func (app *testApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
  235. return abci.ResponseDeliverTx{Events: []abci.Event{}}
  236. }
  237. func (app *testApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
  238. return abci.ResponseCheckTx{}
  239. }
  240. func (app *testApp) Commit() abci.ResponseCommit {
  241. return abci.ResponseCommit{}
  242. }
  243. func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
  244. return
  245. }