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.

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