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.

361 lines
11 KiB

8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. abci "github.com/tendermint/abci/types"
  8. crypto "github.com/tendermint/go-crypto"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. dbm "github.com/tendermint/tmlibs/db"
  11. "github.com/tendermint/tmlibs/log"
  12. cfg "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. // setupTestCase does setup common to all test cases
  16. func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, *State) {
  17. config := cfg.ResetTestRoot("state_")
  18. stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
  19. state, err := GetState(stateDB, config.GenesisFile())
  20. assert.NoError(t, err, "expected no error on GetState")
  21. state.SetLogger(log.TestingLogger())
  22. tearDown := func(t *testing.T) {}
  23. return tearDown, stateDB, state
  24. }
  25. // TestStateCopy tests the correct copying behaviour of State.
  26. func TestStateCopy(t *testing.T) {
  27. tearDown, _, state := setupTestCase(t)
  28. defer tearDown(t)
  29. // nolint: vetshadow
  30. assert := assert.New(t)
  31. stateCopy := state.Copy()
  32. assert.True(state.Equals(stateCopy),
  33. cmn.Fmt(`expected state and its copy to be identical. got %v\n expected %v\n`,
  34. stateCopy, state))
  35. stateCopy.LastBlockHeight++
  36. assert.False(state.Equals(stateCopy), cmn.Fmt(`expected states to be different. got same
  37. %v`, state))
  38. }
  39. // TestStateSaveLoad tests saving and loading State from a db.
  40. func TestStateSaveLoad(t *testing.T) {
  41. tearDown, stateDB, state := setupTestCase(t)
  42. defer tearDown(t)
  43. // nolint: vetshadow
  44. assert := assert.New(t)
  45. state.LastBlockHeight++
  46. state.Save()
  47. loadedState := LoadState(stateDB)
  48. assert.True(state.Equals(loadedState),
  49. cmn.Fmt(`expected state and its copy to be identical. got %v\n expected %v\n`,
  50. loadedState, state))
  51. }
  52. // TestABCIResponsesSaveLoad tests saving and loading ABCIResponses.
  53. func TestABCIResponsesSaveLoad(t *testing.T) {
  54. tearDown, _, state := setupTestCase(t)
  55. defer tearDown(t)
  56. // nolint: vetshadow
  57. assert := assert.New(t)
  58. state.LastBlockHeight++
  59. // build mock responses
  60. block := makeBlock(state, 2)
  61. abciResponses := NewABCIResponses(block)
  62. abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: []*abci.KVPair{}}
  63. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: []*abci.KVPair{}}
  64. abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []*abci.Validator{
  65. {
  66. PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
  67. Power: 10,
  68. },
  69. }}
  70. abciResponses.txs = nil
  71. state.SaveABCIResponses(abciResponses)
  72. loadedAbciResponses := state.LoadABCIResponses()
  73. assert.Equal(abciResponses, loadedAbciResponses,
  74. cmn.Fmt(`ABCIResponses don't match: Got %v, Expected %v`, loadedAbciResponses,
  75. abciResponses))
  76. }
  77. // TestValidatorSimpleSaveLoad tests saving and loading validators.
  78. func TestValidatorSimpleSaveLoad(t *testing.T) {
  79. tearDown, _, state := setupTestCase(t)
  80. defer tearDown(t)
  81. // nolint: vetshadow
  82. assert := assert.New(t)
  83. // can't load anything for height 0
  84. v, err := state.LoadValidators(0)
  85. assert.IsType(ErrNoValSetForHeight{}, err, "expected err at height 0")
  86. // should be able to load for height 1
  87. v, err = state.LoadValidators(1)
  88. assert.Nil(err, "expected no err at height 1")
  89. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  90. // increment height, save; should be able to load for next height
  91. state.LastBlockHeight++
  92. state.saveValidatorsInfo()
  93. v, err = state.LoadValidators(state.LastBlockHeight + 1)
  94. assert.Nil(err, "expected no err")
  95. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  96. // increment height, save; should be able to load for next height
  97. state.LastBlockHeight += 10
  98. state.saveValidatorsInfo()
  99. v, err = state.LoadValidators(state.LastBlockHeight + 1)
  100. assert.Nil(err, "expected no err")
  101. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  102. // should be able to load for next next height
  103. _, err = state.LoadValidators(state.LastBlockHeight + 2)
  104. assert.IsType(ErrNoValSetForHeight{}, err, "expected err at unknown height")
  105. }
  106. // TestValidatorChangesSaveLoad tests saving and loading a validator set with changes.
  107. func TestValidatorChangesSaveLoad(t *testing.T) {
  108. tearDown, _, state := setupTestCase(t)
  109. defer tearDown(t)
  110. // nolint: vetshadow
  111. assert := assert.New(t)
  112. // change vals at these heights
  113. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  114. N := len(changeHeights)
  115. // each valset is just one validator.
  116. // create list of them
  117. pubkeys := make([]crypto.PubKey, N+1)
  118. _, val := state.Validators.GetByIndex(0)
  119. pubkeys[0] = val.PubKey
  120. for i := 1; i < N+1; i++ {
  121. pubkeys[i] = crypto.GenPrivKeyEd25519().PubKey()
  122. }
  123. // build the validator history by running SetBlockAndValidators
  124. // with the right validator set for each height
  125. highestHeight := changeHeights[N-1] + 5
  126. changeIndex := 0
  127. pubkey := pubkeys[changeIndex]
  128. for i := int64(1); i < highestHeight; i++ {
  129. // when we get to a change height,
  130. // use the next pubkey
  131. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  132. changeIndex++
  133. pubkey = pubkeys[changeIndex]
  134. }
  135. header, parts, responses := makeHeaderPartsResponses(state, i, pubkey)
  136. state.SetBlockAndValidators(header, parts, responses)
  137. state.saveValidatorsInfo()
  138. }
  139. // make all the test cases by using the same validator until after the change
  140. testCases := make([]valChangeTestCase, highestHeight)
  141. changeIndex = 0
  142. pubkey = pubkeys[changeIndex]
  143. for i := int64(1); i < highestHeight+1; i++ {
  144. // we we get to the height after a change height
  145. // use the next pubkey (note our counter starts at 0 this time)
  146. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  147. changeIndex++
  148. pubkey = pubkeys[changeIndex]
  149. }
  150. testCases[i-1] = valChangeTestCase{i, pubkey}
  151. }
  152. for _, testCase := range testCases {
  153. v, err := state.LoadValidators(testCase.height)
  154. assert.Nil(err, fmt.Sprintf("expected no err at height %d", testCase.height))
  155. assert.Equal(v.Size(), 1, "validator set size is greater than 1: %d", v.Size())
  156. addr, _ := v.GetByIndex(0)
  157. assert.Equal(addr, testCase.vals.Address(), fmt.Sprintf(`unexpected pubkey at
  158. height %d`, testCase.height))
  159. }
  160. }
  161. // TestConsensusParamsChangesSaveLoad tests saving and loading consensus params with changes.
  162. func TestConsensusParamsChangesSaveLoad(t *testing.T) {
  163. tearDown, _, state := setupTestCase(t)
  164. defer tearDown(t)
  165. // nolint: vetshadow
  166. assert := assert.New(t)
  167. // change vals at these heights
  168. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  169. N := len(changeHeights)
  170. // each valset is just one validator.
  171. // create list of them
  172. params := make([]types.ConsensusParams, N+1)
  173. params[0] = state.ConsensusParams
  174. for i := 1; i < N+1; i++ {
  175. params[i] = *types.DefaultConsensusParams()
  176. params[i].BlockSize.MaxBytes += i
  177. }
  178. // build the params history by running SetBlockAndValidators
  179. // with the right params set for each height
  180. highestHeight := changeHeights[N-1] + 5
  181. changeIndex := 0
  182. cp := params[changeIndex]
  183. for i := int64(1); i < highestHeight; i++ {
  184. // when we get to a change height,
  185. // use the next params
  186. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  187. changeIndex++
  188. cp = params[changeIndex]
  189. }
  190. header, parts, responses := makeHeaderPartsResponsesParams(state, i, cp)
  191. state.SetBlockAndValidators(header, parts, responses)
  192. state.saveConsensusParamsInfo()
  193. }
  194. // make all the test cases by using the same params until after the change
  195. testCases := make([]paramsChangeTestCase, highestHeight)
  196. changeIndex = 0
  197. cp = params[changeIndex]
  198. for i := int64(1); i < highestHeight+1; i++ {
  199. // we we get to the height after a change height
  200. // use the next pubkey (note our counter starts at 0 this time)
  201. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  202. changeIndex++
  203. cp = params[changeIndex]
  204. }
  205. testCases[i-1] = paramsChangeTestCase{i, cp}
  206. }
  207. for _, testCase := range testCases {
  208. p, err := state.LoadConsensusParams(testCase.height)
  209. assert.Nil(err, fmt.Sprintf("expected no err at height %d", testCase.height))
  210. assert.Equal(testCase.params, p, fmt.Sprintf(`unexpected consensus params at
  211. height %d`, testCase.height))
  212. }
  213. }
  214. func makeParams(blockBytes, blockTx, blockGas, txBytes,
  215. txGas, partSize int) types.ConsensusParams {
  216. return types.ConsensusParams{
  217. BlockSize: types.BlockSize{
  218. MaxBytes: blockBytes,
  219. MaxTxs: blockTx,
  220. MaxGas: int64(blockGas),
  221. },
  222. TxSize: types.TxSize{
  223. MaxBytes: txBytes,
  224. MaxGas: int64(txGas),
  225. },
  226. BlockGossip: types.BlockGossip{
  227. BlockPartSizeBytes: partSize,
  228. },
  229. }
  230. }
  231. func TestApplyUpdates(t *testing.T) {
  232. initParams := makeParams(1, 2, 3, 4, 5, 6)
  233. cases := [...]struct {
  234. init types.ConsensusParams
  235. updates *abci.ConsensusParams
  236. expected types.ConsensusParams
  237. }{
  238. 0: {initParams, nil, initParams},
  239. 1: {initParams, &abci.ConsensusParams{}, initParams},
  240. 2: {initParams,
  241. &abci.ConsensusParams{
  242. TxSize: &abci.TxSize{
  243. MaxBytes: 123,
  244. },
  245. },
  246. makeParams(1, 2, 3, 123, 5, 6)},
  247. 3: {initParams,
  248. &abci.ConsensusParams{
  249. BlockSize: &abci.BlockSize{
  250. MaxTxs: 44,
  251. MaxGas: 55,
  252. },
  253. },
  254. makeParams(1, 44, 55, 4, 5, 6)},
  255. 4: {initParams,
  256. &abci.ConsensusParams{
  257. BlockSize: &abci.BlockSize{
  258. MaxTxs: 789,
  259. },
  260. TxSize: &abci.TxSize{
  261. MaxGas: 888,
  262. },
  263. BlockGossip: &abci.BlockGossip{
  264. BlockPartSizeBytes: 2002,
  265. },
  266. },
  267. makeParams(1, 789, 3, 4, 888, 2002)},
  268. }
  269. for i, tc := range cases {
  270. res := tc.init.Update(tc.updates)
  271. assert.Equal(t, tc.expected, res, "case %d", i)
  272. }
  273. }
  274. func makeHeaderPartsResponses(state *State, height int64,
  275. pubkey crypto.PubKey) (*types.Header, types.PartSetHeader, *ABCIResponses) {
  276. block := makeBlock(state, height)
  277. _, val := state.Validators.GetByIndex(0)
  278. abciResponses := &ABCIResponses{
  279. Height: height,
  280. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []*abci.Validator{}},
  281. }
  282. // if the pubkey is new, remove the old and add the new
  283. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  284. abciResponses.EndBlock = &abci.ResponseEndBlock{
  285. ValidatorUpdates: []*abci.Validator{
  286. {val.PubKey.Bytes(), 0},
  287. {pubkey.Bytes(), 10},
  288. },
  289. }
  290. }
  291. return block.Header, types.PartSetHeader{}, abciResponses
  292. }
  293. type valChangeTestCase struct {
  294. height int64
  295. vals crypto.PubKey
  296. }
  297. func makeHeaderPartsResponsesParams(state *State, height int64,
  298. params types.ConsensusParams) (*types.Header, types.PartSetHeader, *ABCIResponses) {
  299. block := makeBlock(state, height)
  300. abciResponses := &ABCIResponses{
  301. Height: height,
  302. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  303. }
  304. return block.Header, types.PartSetHeader{}, abciResponses
  305. }
  306. type paramsChangeTestCase struct {
  307. height int64
  308. params types.ConsensusParams
  309. }