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.

492 lines
15 KiB

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