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.

527 lines
17 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. abci "github.com/tendermint/tendermint/abci/types"
  9. "github.com/tendermint/tendermint/crypto"
  10. "github.com/tendermint/tendermint/crypto/ed25519"
  11. cmn "github.com/tendermint/tendermint/libs/common"
  12. dbm "github.com/tendermint/tendermint/libs/db"
  13. cfg "github.com/tendermint/tendermint/config"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. // setupTestCase does setup common to all test cases.
  17. func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, State) {
  18. config := cfg.ResetTestRoot("state_")
  19. dbType := dbm.DBBackendType(config.DBBackend)
  20. stateDB := dbm.NewDB("state", dbType, config.DBDir())
  21. state, err := LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
  22. assert.NoError(t, err, "expected no error on LoadStateFromDBOrGenesisFile")
  23. tearDown := func(t *testing.T) {}
  24. return tearDown, stateDB, state
  25. }
  26. // TestStateCopy tests the correct copying behaviour of State.
  27. func TestStateCopy(t *testing.T) {
  28. tearDown, _, state := setupTestCase(t)
  29. defer tearDown(t)
  30. // nolint: vetshadow
  31. assert := assert.New(t)
  32. stateCopy := state.Copy()
  33. assert.True(state.Equals(stateCopy),
  34. fmt.Sprintf("expected state and its copy to be identical.\ngot: %v\nexpected: %v\n",
  35. stateCopy, state))
  36. stateCopy.LastBlockHeight++
  37. assert.False(state.Equals(stateCopy), fmt.Sprintf(`expected states to be different. got same
  38. %v`, state))
  39. }
  40. //TestMakeGenesisStateNilValidators tests state's consistency when genesis file's validators field is nil.
  41. func TestMakeGenesisStateNilValidators(t *testing.T) {
  42. doc := types.GenesisDoc{
  43. ChainID: "dummy",
  44. Validators: nil,
  45. }
  46. require.Nil(t, doc.ValidateAndComplete())
  47. state, err := MakeGenesisState(&doc)
  48. require.Nil(t, err)
  49. require.Equal(t, 0, len(state.Validators.Validators))
  50. require.Equal(t, 0, len(state.NextValidators.Validators))
  51. }
  52. // TestStateSaveLoad tests saving and loading State from a db.
  53. func TestStateSaveLoad(t *testing.T) {
  54. tearDown, stateDB, state := setupTestCase(t)
  55. defer tearDown(t)
  56. // nolint: vetshadow
  57. assert := assert.New(t)
  58. state.LastBlockHeight++
  59. SaveState(stateDB, state)
  60. loadedState := LoadState(stateDB)
  61. assert.True(state.Equals(loadedState),
  62. fmt.Sprintf("expected state and its copy to be identical.\ngot: %v\nexpected: %v\n",
  63. loadedState, state))
  64. }
  65. // TestABCIResponsesSaveLoad tests saving and loading ABCIResponses.
  66. func TestABCIResponsesSaveLoad1(t *testing.T) {
  67. tearDown, stateDB, state := setupTestCase(t)
  68. defer tearDown(t)
  69. // nolint: vetshadow
  70. assert := assert.New(t)
  71. state.LastBlockHeight++
  72. // Build mock responses.
  73. block := makeBlock(state, 2)
  74. abciResponses := NewABCIResponses(block)
  75. abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: nil}
  76. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: nil}
  77. abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{
  78. types.TM2PB.NewValidatorUpdate(ed25519.GenPrivKey().PubKey(), 10),
  79. }}
  80. saveABCIResponses(stateDB, block.Height, abciResponses)
  81. loadedABCIResponses, err := LoadABCIResponses(stateDB, block.Height)
  82. assert.Nil(err)
  83. assert.Equal(abciResponses, loadedABCIResponses,
  84. fmt.Sprintf("ABCIResponses don't match:\ngot: %v\nexpected: %v\n",
  85. loadedABCIResponses, abciResponses))
  86. }
  87. // TestResultsSaveLoad tests saving and loading ABCI results.
  88. func TestABCIResponsesSaveLoad2(t *testing.T) {
  89. tearDown, stateDB, _ := setupTestCase(t)
  90. defer tearDown(t)
  91. // nolint: vetshadow
  92. assert := assert.New(t)
  93. cases := [...]struct {
  94. // Height is implied to equal index+2,
  95. // as block 1 is created from genesis.
  96. added []*abci.ResponseDeliverTx
  97. expected types.ABCIResults
  98. }{
  99. 0: {
  100. nil,
  101. nil,
  102. },
  103. 1: {
  104. []*abci.ResponseDeliverTx{
  105. {Code: 32, Data: []byte("Hello"), Log: "Huh?"},
  106. },
  107. types.ABCIResults{
  108. {32, []byte("Hello")},
  109. }},
  110. 2: {
  111. []*abci.ResponseDeliverTx{
  112. {Code: 383},
  113. {Data: []byte("Gotcha!"),
  114. Tags: []cmn.KVPair{
  115. {Key: []byte("a"), Value: []byte("1")},
  116. {Key: []byte("build"), Value: []byte("stuff")},
  117. }},
  118. },
  119. types.ABCIResults{
  120. {383, nil},
  121. {0, []byte("Gotcha!")},
  122. }},
  123. 3: {
  124. nil,
  125. nil,
  126. },
  127. }
  128. // Query all before, this should return error.
  129. for i := range cases {
  130. h := int64(i + 1)
  131. res, err := LoadABCIResponses(stateDB, h)
  132. assert.Error(err, "%d: %#v", i, res)
  133. }
  134. // Add all cases.
  135. for i, tc := range cases {
  136. h := int64(i + 1) // last block height, one below what we save
  137. responses := &ABCIResponses{
  138. DeliverTx: tc.added,
  139. EndBlock: &abci.ResponseEndBlock{},
  140. }
  141. saveABCIResponses(stateDB, h, responses)
  142. }
  143. // Query all before, should return expected value.
  144. for i, tc := range cases {
  145. h := int64(i + 1)
  146. res, err := LoadABCIResponses(stateDB, h)
  147. assert.NoError(err, "%d", i)
  148. assert.Equal(tc.expected.Hash(), res.ResultsHash(), "%d", i)
  149. }
  150. }
  151. // TestValidatorSimpleSaveLoad tests saving and loading validators.
  152. func TestValidatorSimpleSaveLoad(t *testing.T) {
  153. tearDown, stateDB, state := setupTestCase(t)
  154. defer tearDown(t)
  155. // nolint: vetshadow
  156. assert := assert.New(t)
  157. // Can't load anything for height 0.
  158. v, err := LoadValidators(stateDB, 0)
  159. assert.IsType(ErrNoValSetForHeight{}, err, "expected err at height 0")
  160. // Should be able to load for height 1.
  161. v, err = LoadValidators(stateDB, 1)
  162. assert.Nil(err, "expected no err at height 1")
  163. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  164. // Should be able to load for height 2.
  165. v, err = LoadValidators(stateDB, 2)
  166. assert.Nil(err, "expected no err at height 2")
  167. assert.Equal(v.Hash(), state.NextValidators.Hash(), "expected validator hashes to match")
  168. // Increment height, save; should be able to load for next & next next height.
  169. state.LastBlockHeight++
  170. nextHeight := state.LastBlockHeight + 1
  171. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  172. vp0, err := LoadValidators(stateDB, nextHeight+0)
  173. assert.Nil(err, "expected no err")
  174. vp1, err := LoadValidators(stateDB, nextHeight+1)
  175. assert.Nil(err, "expected no err")
  176. assert.Equal(vp0.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  177. assert.Equal(vp1.Hash(), state.NextValidators.Hash(), "expected next validator hashes to match")
  178. }
  179. // TestValidatorChangesSaveLoad tests saving and loading a validator set with changes.
  180. func TestOneValidatorChangesSaveLoad(t *testing.T) {
  181. tearDown, stateDB, state := setupTestCase(t)
  182. defer tearDown(t)
  183. // Change vals at these heights.
  184. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  185. N := len(changeHeights)
  186. // Build the validator history by running updateState
  187. // with the right validator set for each height.
  188. highestHeight := changeHeights[N-1] + 5
  189. changeIndex := 0
  190. _, val := state.Validators.GetByIndex(0)
  191. power := val.VotingPower
  192. var err error
  193. var validatorUpdates []*types.Validator
  194. for i := int64(1); i < highestHeight; i++ {
  195. // When we get to a change height, use the next pubkey.
  196. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  197. changeIndex++
  198. power++
  199. }
  200. header, blockID, responses := makeHeaderPartsResponsesValPowerChange(state, i, power)
  201. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  202. require.NoError(t, err)
  203. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  204. require.NoError(t, err)
  205. nextHeight := state.LastBlockHeight + 1
  206. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  207. }
  208. // On each height change, increment the power by one.
  209. testCases := make([]int64, highestHeight)
  210. changeIndex = 0
  211. power = val.VotingPower
  212. for i := int64(1); i < highestHeight+1; i++ {
  213. // We get to the height after a change height use the next pubkey (note
  214. // our counter starts at 0 this time).
  215. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  216. changeIndex++
  217. power++
  218. }
  219. testCases[i-1] = power
  220. }
  221. for i, power := range testCases {
  222. v, err := LoadValidators(stateDB, int64(i+1+1)) // +1 because vset changes delayed by 1 block.
  223. assert.Nil(t, err, fmt.Sprintf("expected no err at height %d", i))
  224. assert.Equal(t, v.Size(), 1, "validator set size is greater than 1: %d", v.Size())
  225. _, val := v.GetByIndex(0)
  226. assert.Equal(t, val.VotingPower, power, fmt.Sprintf(`unexpected powerat
  227. height %d`, i))
  228. }
  229. }
  230. func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) {
  231. const valSetSize = 2
  232. tearDown, stateDB, state := setupTestCase(t)
  233. state.Validators = genValSet(valSetSize)
  234. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  235. SaveState(stateDB, state)
  236. defer tearDown(t)
  237. nextHeight := state.LastBlockHeight + 1
  238. v0, err := LoadValidators(stateDB, nextHeight)
  239. assert.Nil(t, err)
  240. acc0 := v0.Validators[0].ProposerPriority
  241. v1, err := LoadValidators(stateDB, nextHeight+1)
  242. assert.Nil(t, err)
  243. acc1 := v1.Validators[0].ProposerPriority
  244. assert.NotEqual(t, acc1, acc0, "expected ProposerPriority value to change between heights")
  245. }
  246. // TestValidatorChangesSaveLoad tests saving and loading a validator set with
  247. // changes.
  248. func TestManyValidatorChangesSaveLoad(t *testing.T) {
  249. const valSetSize = 7
  250. tearDown, stateDB, state := setupTestCase(t)
  251. require.Equal(t, int64(0), state.LastBlockHeight)
  252. state.Validators = genValSet(valSetSize)
  253. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  254. SaveState(stateDB, state)
  255. defer tearDown(t)
  256. _, valOld := state.Validators.GetByIndex(0)
  257. var pubkeyOld = valOld.PubKey
  258. pubkey := ed25519.GenPrivKey().PubKey()
  259. const height = 1
  260. // Swap the first validator with a new one (validator set size stays the same).
  261. header, blockID, responses := makeHeaderPartsResponsesValPubKeyChange(state, height, pubkey)
  262. // Save state etc.
  263. var err error
  264. var validatorUpdates []*types.Validator
  265. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  266. require.NoError(t, err)
  267. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  268. require.Nil(t, err)
  269. nextHeight := state.LastBlockHeight + 1
  270. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  271. // Load nextheight, it should be the oldpubkey.
  272. v0, err := LoadValidators(stateDB, nextHeight)
  273. assert.Nil(t, err)
  274. assert.Equal(t, valSetSize, v0.Size())
  275. index, val := v0.GetByAddress(pubkeyOld.Address())
  276. assert.NotNil(t, val)
  277. if index < 0 {
  278. t.Fatal("expected to find old validator")
  279. }
  280. // Load nextheight+1, it should be the new pubkey.
  281. v1, err := LoadValidators(stateDB, nextHeight+1)
  282. assert.Nil(t, err)
  283. assert.Equal(t, valSetSize, v1.Size())
  284. index, val = v1.GetByAddress(pubkey.Address())
  285. assert.NotNil(t, val)
  286. if index < 0 {
  287. t.Fatal("expected to find newly added validator")
  288. }
  289. }
  290. func genValSet(size int) *types.ValidatorSet {
  291. vals := make([]*types.Validator, size)
  292. for i := 0; i < size; i++ {
  293. vals[i] = types.NewValidator(ed25519.GenPrivKey().PubKey(), 10)
  294. }
  295. return types.NewValidatorSet(vals)
  296. }
  297. func TestStateMakeBlock(t *testing.T) {
  298. tearDown, _, state := setupTestCase(t)
  299. defer tearDown(t)
  300. proposerAddress := state.Validators.GetProposer().Address
  301. stateVersion := state.Version.Consensus
  302. block := makeBlock(state, 2)
  303. // test we set some fields
  304. assert.Equal(t, stateVersion, block.Version)
  305. assert.Equal(t, proposerAddress, block.ProposerAddress)
  306. }
  307. // TestConsensusParamsChangesSaveLoad tests saving and loading consensus params
  308. // with changes.
  309. func TestConsensusParamsChangesSaveLoad(t *testing.T) {
  310. tearDown, stateDB, state := setupTestCase(t)
  311. defer tearDown(t)
  312. // Change vals at these heights.
  313. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  314. N := len(changeHeights)
  315. // Each valset is just one validator.
  316. // create list of them.
  317. params := make([]types.ConsensusParams, N+1)
  318. params[0] = state.ConsensusParams
  319. for i := 1; i < N+1; i++ {
  320. params[i] = *types.DefaultConsensusParams()
  321. params[i].BlockSize.MaxBytes += int64(i)
  322. }
  323. // Build the params history by running updateState
  324. // with the right params set for each height.
  325. highestHeight := changeHeights[N-1] + 5
  326. changeIndex := 0
  327. cp := params[changeIndex]
  328. var err error
  329. var validatorUpdates []*types.Validator
  330. for i := int64(1); i < highestHeight; i++ {
  331. // When we get to a change height, use the next params.
  332. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  333. changeIndex++
  334. cp = params[changeIndex]
  335. }
  336. header, blockID, responses := makeHeaderPartsResponsesParams(state, i, cp)
  337. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  338. require.NoError(t, err)
  339. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  340. require.Nil(t, err)
  341. nextHeight := state.LastBlockHeight + 1
  342. saveConsensusParamsInfo(stateDB, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  343. }
  344. // Make all the test cases by using the same params until after the change.
  345. testCases := make([]paramsChangeTestCase, highestHeight)
  346. changeIndex = 0
  347. cp = params[changeIndex]
  348. for i := int64(1); i < highestHeight+1; i++ {
  349. // We get to the height after a change height use the next pubkey (note
  350. // our counter starts at 0 this time).
  351. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  352. changeIndex++
  353. cp = params[changeIndex]
  354. }
  355. testCases[i-1] = paramsChangeTestCase{i, cp}
  356. }
  357. for _, testCase := range testCases {
  358. p, err := LoadConsensusParams(stateDB, testCase.height)
  359. assert.Nil(t, err, fmt.Sprintf("expected no err at height %d", testCase.height))
  360. assert.Equal(t, testCase.params, p, fmt.Sprintf(`unexpected consensus params at
  361. height %d`, testCase.height))
  362. }
  363. }
  364. func makeParams(blockBytes, blockGas, evidenceAge int64) types.ConsensusParams {
  365. return types.ConsensusParams{
  366. BlockSize: types.BlockSizeParams{
  367. MaxBytes: blockBytes,
  368. MaxGas: blockGas,
  369. },
  370. Evidence: types.EvidenceParams{
  371. MaxAge: evidenceAge,
  372. },
  373. }
  374. }
  375. func pk() []byte {
  376. return ed25519.GenPrivKey().PubKey().Bytes()
  377. }
  378. func TestApplyUpdates(t *testing.T) {
  379. initParams := makeParams(1, 2, 3)
  380. cases := [...]struct {
  381. init types.ConsensusParams
  382. updates abci.ConsensusParams
  383. expected types.ConsensusParams
  384. }{
  385. 0: {initParams, abci.ConsensusParams{}, initParams},
  386. 1: {initParams, abci.ConsensusParams{}, initParams},
  387. 2: {initParams,
  388. abci.ConsensusParams{
  389. BlockSize: &abci.BlockSizeParams{
  390. MaxBytes: 44,
  391. MaxGas: 55,
  392. },
  393. },
  394. makeParams(44, 55, 3)},
  395. 3: {initParams,
  396. abci.ConsensusParams{
  397. Evidence: &abci.EvidenceParams{
  398. MaxAge: 66,
  399. },
  400. },
  401. makeParams(1, 2, 66)},
  402. }
  403. for i, tc := range cases {
  404. res := tc.init.Update(&(tc.updates))
  405. assert.Equal(t, tc.expected, res, "case %d", i)
  406. }
  407. }
  408. func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
  409. pubkey crypto.PubKey) (types.Header, types.BlockID, *ABCIResponses) {
  410. block := makeBlock(state, state.LastBlockHeight+1)
  411. abciResponses := &ABCIResponses{
  412. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  413. }
  414. // If the pubkey is new, remove the old and add the new.
  415. _, val := state.NextValidators.GetByIndex(0)
  416. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  417. abciResponses.EndBlock = &abci.ResponseEndBlock{
  418. ValidatorUpdates: []abci.ValidatorUpdate{
  419. types.TM2PB.NewValidatorUpdate(val.PubKey, 0),
  420. types.TM2PB.NewValidatorUpdate(pubkey, 10),
  421. },
  422. }
  423. }
  424. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  425. }
  426. func makeHeaderPartsResponsesValPowerChange(state State, height int64,
  427. power int64) (types.Header, types.BlockID, *ABCIResponses) {
  428. block := makeBlock(state, state.LastBlockHeight+1)
  429. abciResponses := &ABCIResponses{
  430. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  431. }
  432. // If the pubkey is new, remove the old and add the new.
  433. _, val := state.NextValidators.GetByIndex(0)
  434. if val.VotingPower != power {
  435. abciResponses.EndBlock = &abci.ResponseEndBlock{
  436. ValidatorUpdates: []abci.ValidatorUpdate{
  437. types.TM2PB.NewValidatorUpdate(val.PubKey, power),
  438. },
  439. }
  440. }
  441. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  442. }
  443. func makeHeaderPartsResponsesParams(state State, height int64,
  444. params types.ConsensusParams) (types.Header, types.BlockID, *ABCIResponses) {
  445. block := makeBlock(state, state.LastBlockHeight+1)
  446. abciResponses := &ABCIResponses{
  447. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  448. }
  449. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  450. }
  451. type paramsChangeTestCase struct {
  452. height int64
  453. params types.ConsensusParams
  454. }