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.

490 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. cmn.Fmt("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), cmn.Fmt(`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. cmn.Fmt("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.Validator{
  66. types.TM2PB.ValidatorFromPubKeyAndPower(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. cmn.Fmt("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{[]byte("a"), []byte("1")},
  104. cmn.KVPair{[]byte("build"), []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. // TestConsensusParamsChangesSaveLoad tests saving and loading consensus params
  264. // with changes.
  265. func TestConsensusParamsChangesSaveLoad(t *testing.T) {
  266. tearDown, stateDB, state := setupTestCase(t)
  267. defer tearDown(t)
  268. // Change vals at these heights.
  269. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  270. N := len(changeHeights)
  271. // Each valset is just one validator.
  272. // create list of them.
  273. params := make([]types.ConsensusParams, N+1)
  274. params[0] = state.ConsensusParams
  275. for i := 1; i < N+1; i++ {
  276. params[i] = *types.DefaultConsensusParams()
  277. params[i].BlockSize.MaxBytes += i
  278. }
  279. // Build the params history by running updateState
  280. // with the right params set for each height.
  281. highestHeight := changeHeights[N-1] + 5
  282. changeIndex := 0
  283. cp := params[changeIndex]
  284. var err error
  285. for i := int64(1); i < highestHeight; i++ {
  286. // When we get to a change height, use the next params.
  287. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  288. changeIndex++
  289. cp = params[changeIndex]
  290. }
  291. header, blockID, responses := makeHeaderPartsResponsesParams(state, i, cp)
  292. state, err = updateState(state, blockID, &header, responses)
  293. require.Nil(t, err)
  294. nextHeight := state.LastBlockHeight + 1
  295. saveConsensusParamsInfo(stateDB, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  296. }
  297. // Make all the test cases by using the same params until after the change.
  298. testCases := make([]paramsChangeTestCase, highestHeight)
  299. changeIndex = 0
  300. cp = params[changeIndex]
  301. for i := int64(1); i < highestHeight+1; i++ {
  302. // We get to the height after a change height use the next pubkey (note
  303. // our counter starts at 0 this time).
  304. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  305. changeIndex++
  306. cp = params[changeIndex]
  307. }
  308. testCases[i-1] = paramsChangeTestCase{i, cp}
  309. }
  310. for _, testCase := range testCases {
  311. p, err := LoadConsensusParams(stateDB, testCase.height)
  312. assert.Nil(t, err, fmt.Sprintf("expected no err at height %d", testCase.height))
  313. assert.Equal(t, testCase.params, p, fmt.Sprintf(`unexpected consensus params at
  314. height %d`, testCase.height))
  315. }
  316. }
  317. func makeParams(blockBytes, blockTx, blockGas, txBytes,
  318. txGas, partSize int) types.ConsensusParams {
  319. return types.ConsensusParams{
  320. BlockSize: types.BlockSize{
  321. MaxBytes: blockBytes,
  322. MaxTxs: blockTx,
  323. MaxGas: int64(blockGas),
  324. },
  325. TxSize: types.TxSize{
  326. MaxBytes: txBytes,
  327. MaxGas: int64(txGas),
  328. },
  329. BlockGossip: types.BlockGossip{
  330. BlockPartSizeBytes: partSize,
  331. },
  332. }
  333. }
  334. func pk() []byte {
  335. return ed25519.GenPrivKey().PubKey().Bytes()
  336. }
  337. func TestApplyUpdates(t *testing.T) {
  338. initParams := makeParams(1, 2, 3, 4, 5, 6)
  339. cases := [...]struct {
  340. init types.ConsensusParams
  341. updates abci.ConsensusParams
  342. expected types.ConsensusParams
  343. }{
  344. 0: {initParams, abci.ConsensusParams{}, initParams},
  345. 1: {initParams, abci.ConsensusParams{}, initParams},
  346. 2: {initParams,
  347. abci.ConsensusParams{
  348. TxSize: &abci.TxSize{
  349. MaxBytes: 123,
  350. },
  351. },
  352. makeParams(1, 2, 3, 123, 5, 6)},
  353. 3: {initParams,
  354. abci.ConsensusParams{
  355. BlockSize: &abci.BlockSize{
  356. MaxTxs: 44,
  357. MaxGas: 55,
  358. },
  359. },
  360. makeParams(1, 44, 55, 4, 5, 6)},
  361. 4: {initParams,
  362. abci.ConsensusParams{
  363. BlockSize: &abci.BlockSize{
  364. MaxTxs: 789,
  365. },
  366. TxSize: &abci.TxSize{
  367. MaxGas: 888,
  368. },
  369. BlockGossip: &abci.BlockGossip{
  370. BlockPartSizeBytes: 2002,
  371. },
  372. },
  373. makeParams(1, 789, 3, 4, 888, 2002)},
  374. }
  375. for i, tc := range cases {
  376. res := tc.init.Update(&(tc.updates))
  377. assert.Equal(t, tc.expected, res, "case %d", i)
  378. }
  379. }
  380. func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
  381. pubkey crypto.PubKey) (types.Header, types.BlockID, *ABCIResponses) {
  382. block := makeBlock(state, state.LastBlockHeight+1)
  383. abciResponses := &ABCIResponses{
  384. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  385. }
  386. // If the pubkey is new, remove the old and add the new.
  387. _, val := state.NextValidators.GetByIndex(0)
  388. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  389. abciResponses.EndBlock = &abci.ResponseEndBlock{
  390. ValidatorUpdates: []abci.Validator{
  391. types.TM2PB.ValidatorFromPubKeyAndPower(val.PubKey, 0),
  392. types.TM2PB.ValidatorFromPubKeyAndPower(pubkey, 10),
  393. },
  394. }
  395. }
  396. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  397. }
  398. func makeHeaderPartsResponsesValPowerChange(state State, height int64,
  399. power int64) (types.Header, types.BlockID, *ABCIResponses) {
  400. block := makeBlock(state, state.LastBlockHeight+1)
  401. abciResponses := &ABCIResponses{
  402. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  403. }
  404. // If the pubkey is new, remove the old and add the new.
  405. _, val := state.NextValidators.GetByIndex(0)
  406. if val.VotingPower != power {
  407. abciResponses.EndBlock = &abci.ResponseEndBlock{
  408. ValidatorUpdates: []abci.Validator{
  409. types.TM2PB.ValidatorFromPubKeyAndPower(val.PubKey, power),
  410. },
  411. }
  412. }
  413. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  414. }
  415. func makeHeaderPartsResponsesParams(state State, height int64,
  416. params types.ConsensusParams) (types.Header, types.BlockID, *ABCIResponses) {
  417. block := makeBlock(state, state.LastBlockHeight+1)
  418. abciResponses := &ABCIResponses{
  419. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  420. }
  421. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  422. }
  423. type paramsChangeTestCase struct {
  424. height int64
  425. params types.ConsensusParams
  426. }