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.

562 lines
17 KiB

8 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
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. crypto "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. 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. SaveState(stateDB, state)
  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 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: []cmn.KVPair{}}
  63. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: []cmn.KVPair{}}
  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: Got %v, Expected %v`, loadedAbciResponses,
  75. 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. []*abci.ResponseDeliverTx{},
  91. types.ABCIResults{},
  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, []byte{}},
  111. {0, []byte("Gotcha!")},
  112. }},
  113. 3: {
  114. nil,
  115. types.ABCIResults{},
  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 += 1
  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 += 1
  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 TestLessThanOneThirdOfVotingPowerPerBlockEnforced(t *testing.T) {
  327. testCases := []struct {
  328. initialValSetSize int
  329. shouldErr bool
  330. valUpdatesFn func(vals *types.ValidatorSet) []abci.Validator
  331. }{
  332. ///////////// 1 val (vp: 10) => less than 3 is ok ////////////////////////
  333. // adding 1 validator => 10
  334. 0: {1, false, func(vals *types.ValidatorSet) []abci.Validator {
  335. return []abci.Validator{
  336. {PubKey: pk(), Power: 2},
  337. }
  338. }},
  339. 1: {1, true, func(vals *types.ValidatorSet) []abci.Validator {
  340. return []abci.Validator{
  341. {PubKey: pk(), Power: 3},
  342. }
  343. }},
  344. 2: {1, true, func(vals *types.ValidatorSet) []abci.Validator {
  345. return []abci.Validator{
  346. {PubKey: pk(), Power: 100},
  347. }
  348. }},
  349. ///////////// 3 val (vp: 30) => less than 10 is ok ////////////////////////
  350. // adding and removing validator => 20
  351. 3: {3, true, func(vals *types.ValidatorSet) []abci.Validator {
  352. _, firstVal := vals.GetByIndex(0)
  353. return []abci.Validator{
  354. {PubKey: firstVal.PubKey.Bytes(), Power: 0},
  355. {PubKey: pk(), Power: 10},
  356. }
  357. }},
  358. // adding 1 validator => 10
  359. 4: {3, true, func(vals *types.ValidatorSet) []abci.Validator {
  360. return []abci.Validator{
  361. {PubKey: pk(), Power: 10},
  362. }
  363. }},
  364. // adding 2 validators => 8
  365. 5: {3, false, func(vals *types.ValidatorSet) []abci.Validator {
  366. return []abci.Validator{
  367. {PubKey: pk(), Power: 4},
  368. {PubKey: pk(), Power: 4},
  369. }
  370. }},
  371. }
  372. for i, tc := range testCases {
  373. tearDown, stateDB, state := setupTestCase(t)
  374. state.Validators = genValSet(tc.initialValSetSize)
  375. SaveState(stateDB, state)
  376. height := state.LastBlockHeight + 1
  377. block := makeBlock(state, height)
  378. abciResponses := &ABCIResponses{
  379. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: tc.valUpdatesFn(state.Validators)},
  380. }
  381. state, err := updateState(state, types.BlockID{block.Hash(), types.PartSetHeader{}}, block.Header, abciResponses)
  382. if tc.shouldErr {
  383. assert.Error(t, err, "#%d", i)
  384. } else {
  385. assert.NoError(t, err, "#%d", i)
  386. }
  387. tearDown(t)
  388. }
  389. }
  390. func pk() []byte {
  391. return crypto.GenPrivKeyEd25519().PubKey().Bytes()
  392. }
  393. func TestApplyUpdates(t *testing.T) {
  394. initParams := makeParams(1, 2, 3, 4, 5, 6)
  395. cases := [...]struct {
  396. init types.ConsensusParams
  397. updates abci.ConsensusParams
  398. expected types.ConsensusParams
  399. }{
  400. 0: {initParams, abci.ConsensusParams{}, initParams},
  401. 1: {initParams, abci.ConsensusParams{}, initParams},
  402. 2: {initParams,
  403. abci.ConsensusParams{
  404. TxSize: &abci.TxSize{
  405. MaxBytes: 123,
  406. },
  407. },
  408. makeParams(1, 2, 3, 123, 5, 6)},
  409. 3: {initParams,
  410. abci.ConsensusParams{
  411. BlockSize: &abci.BlockSize{
  412. MaxTxs: 44,
  413. MaxGas: 55,
  414. },
  415. },
  416. makeParams(1, 44, 55, 4, 5, 6)},
  417. 4: {initParams,
  418. abci.ConsensusParams{
  419. BlockSize: &abci.BlockSize{
  420. MaxTxs: 789,
  421. },
  422. TxSize: &abci.TxSize{
  423. MaxGas: 888,
  424. },
  425. BlockGossip: &abci.BlockGossip{
  426. BlockPartSizeBytes: 2002,
  427. },
  428. },
  429. makeParams(1, 789, 3, 4, 888, 2002)},
  430. }
  431. for i, tc := range cases {
  432. res := tc.init.Update(&(tc.updates))
  433. assert.Equal(t, tc.expected, res, "case %d", i)
  434. }
  435. }
  436. func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
  437. pubkey crypto.PubKey) (*types.Header, types.BlockID, *ABCIResponses) {
  438. block := makeBlock(state, height)
  439. abciResponses := &ABCIResponses{
  440. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.Validator{}},
  441. }
  442. // if the pubkey is new, remove the old and add the new
  443. _, val := state.Validators.GetByIndex(0)
  444. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  445. abciResponses.EndBlock = &abci.ResponseEndBlock{
  446. ValidatorUpdates: []abci.Validator{
  447. {val.PubKey.Bytes(), 0},
  448. {pubkey.Bytes(), 10},
  449. },
  450. }
  451. }
  452. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  453. }
  454. func makeHeaderPartsResponsesValPowerChange(state State, height int64,
  455. power int64) (*types.Header, types.BlockID, *ABCIResponses) {
  456. block := makeBlock(state, height)
  457. abciResponses := &ABCIResponses{
  458. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.Validator{}},
  459. }
  460. // if the pubkey is new, remove the old and add the new
  461. _, val := state.Validators.GetByIndex(0)
  462. if val.VotingPower != power {
  463. abciResponses.EndBlock = &abci.ResponseEndBlock{
  464. ValidatorUpdates: []abci.Validator{
  465. {val.PubKey.Bytes(), power},
  466. },
  467. }
  468. }
  469. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  470. }
  471. func makeHeaderPartsResponsesParams(state State, height int64,
  472. params types.ConsensusParams) (*types.Header, types.BlockID, *ABCIResponses) {
  473. block := makeBlock(state, height)
  474. abciResponses := &ABCIResponses{
  475. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  476. }
  477. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  478. }
  479. type paramsChangeTestCase struct {
  480. height int64
  481. params types.ConsensusParams
  482. }
  483. func makeHeaderPartsResults(state State, height int64,
  484. results []*abci.ResponseDeliverTx) (*types.Header, types.BlockID, *ABCIResponses) {
  485. block := makeBlock(state, height)
  486. abciResponses := &ABCIResponses{
  487. DeliverTx: results,
  488. EndBlock: &abci.ResponseEndBlock{},
  489. }
  490. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  491. }