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.

148 lines
4.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/abci/example/dummy"
  8. abci "github.com/tendermint/abci/types"
  9. crypto "github.com/tendermint/go-crypto"
  10. "github.com/tendermint/tendermint/proxy"
  11. "github.com/tendermint/tendermint/types"
  12. dbm "github.com/tendermint/tmlibs/db"
  13. "github.com/tendermint/tmlibs/log"
  14. )
  15. var (
  16. privKey = crypto.GenPrivKeyEd25519FromSecret([]byte("execution_test"))
  17. chainID = "execution_chain"
  18. testPartSize = 65536
  19. nTxsPerBlock = 10
  20. )
  21. func TestApplyBlock(t *testing.T) {
  22. cc := proxy.NewLocalClientCreator(dummy.NewDummyApplication())
  23. proxyApp := proxy.NewAppConns(cc, nil)
  24. err := proxyApp.Start()
  25. require.Nil(t, err)
  26. defer proxyApp.Stop()
  27. state, stateDB := state(), dbm.NewMemDB()
  28. blockExec := NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(),
  29. types.MockMempool{}, types.MockEvidencePool{})
  30. block := makeBlock(state, 1)
  31. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  32. state, err = blockExec.ApplyBlock(state, blockID, block)
  33. require.Nil(t, err)
  34. // TODO check state and mempool
  35. }
  36. // TestBeginBlockAbsentValidators ensures we send absent validators list.
  37. func TestBeginBlockAbsentValidators(t *testing.T) {
  38. app := &testApp{}
  39. cc := proxy.NewLocalClientCreator(app)
  40. proxyApp := proxy.NewAppConns(cc, nil)
  41. err := proxyApp.Start()
  42. require.Nil(t, err)
  43. defer proxyApp.Stop()
  44. state := state()
  45. prevHash := state.LastBlockID.Hash
  46. prevParts := types.PartSetHeader{}
  47. prevBlockID := types.BlockID{prevHash, prevParts}
  48. now := time.Now().UTC()
  49. testCases := []struct {
  50. desc string
  51. lastCommitPrecommits []*types.Vote
  52. expectedAbsentValidators []int32
  53. }{
  54. {"none absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, {ValidatorIndex: 1, Timestamp: now}}, []int32{}},
  55. {"one absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, nil}, []int32{1}},
  56. {"multiple absent", []*types.Vote{nil, nil}, []int32{0, 1}},
  57. }
  58. for _, tc := range testCases {
  59. lastCommit := &types.Commit{BlockID: prevBlockID, Precommits: tc.lastCommitPrecommits}
  60. block, _ := state.MakeBlock(2, makeTxs(2), lastCommit)
  61. _, err = ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger())
  62. require.Nil(t, err, tc.desc)
  63. // -> app must receive an index of the absent validator
  64. assert.Equal(t, tc.expectedAbsentValidators, app.AbsentValidators, tc.desc)
  65. }
  66. }
  67. //----------------------------------------------------------------------------
  68. // make some bogus txs
  69. func makeTxs(height int64) (txs []types.Tx) {
  70. for i := 0; i < nTxsPerBlock; i++ {
  71. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  72. }
  73. return txs
  74. }
  75. func state() State {
  76. s, _ := MakeGenesisState(&types.GenesisDoc{
  77. ChainID: chainID,
  78. Validators: []types.GenesisValidator{
  79. {privKey.PubKey(), 10000, "test"},
  80. },
  81. AppHash: nil,
  82. })
  83. return s
  84. }
  85. func makeBlock(state State, height int64) *types.Block {
  86. block, _ := state.MakeBlock(height, makeTxs(state.LastBlockHeight), new(types.Commit))
  87. return block
  88. }
  89. //----------------------------------------------------------------------------
  90. var _ abci.Application = (*testApp)(nil)
  91. type testApp struct {
  92. abci.BaseApplication
  93. AbsentValidators []int32
  94. }
  95. func NewDummyApplication() *testApp {
  96. return &testApp{}
  97. }
  98. func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
  99. return abci.ResponseInfo{}
  100. }
  101. func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
  102. app.AbsentValidators = req.AbsentValidators
  103. return abci.ResponseBeginBlock{}
  104. }
  105. func (app *testApp) DeliverTx(tx []byte) abci.ResponseDeliverTx {
  106. return abci.ResponseDeliverTx{Tags: []*abci.KVPair{}}
  107. }
  108. func (app *testApp) CheckTx(tx []byte) abci.ResponseCheckTx {
  109. return abci.ResponseCheckTx{}
  110. }
  111. func (app *testApp) Commit() abci.ResponseCommit {
  112. return abci.ResponseCommit{}
  113. }
  114. func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
  115. return
  116. }