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.

157 lines
4.3 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
  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(),
  29. types.NopEventBus{}, proxyApp.Consensus(),
  30. types.MockMempool{}, types.MockEvidencePool{})
  31. block := makeBlock(state, 1)
  32. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  33. state, err = blockExec.ApplyBlock(state, blockID, block)
  34. require.Nil(t, err)
  35. // TODO check state and mempool
  36. }
  37. // TestBeginBlockAbsentValidators ensures we send absent validators list.
  38. func TestBeginBlockAbsentValidators(t *testing.T) {
  39. app := &testApp{}
  40. cc := proxy.NewLocalClientCreator(app)
  41. proxyApp := proxy.NewAppConns(cc, nil)
  42. err := proxyApp.Start()
  43. require.Nil(t, err)
  44. defer proxyApp.Stop()
  45. state := state()
  46. // there were 2 validators
  47. /*val1PrivKey := crypto.GenPrivKeyEd25519()
  48. val2PrivKey := crypto.GenPrivKeyEd25519()
  49. lastValidators := types.NewValidatorSet([]*types.Validator{
  50. types.NewValidator(val1PrivKey.PubKey(), 10),
  51. types.NewValidator(val2PrivKey.PubKey(), 5),
  52. })*/
  53. prevHash := state.LastBlockID.Hash
  54. prevParts := types.PartSetHeader{}
  55. prevBlockID := types.BlockID{prevHash, prevParts}
  56. now := time.Now().UTC()
  57. testCases := []struct {
  58. desc string
  59. lastCommitPrecommits []*types.Vote
  60. expectedAbsentValidators []int32
  61. }{
  62. {"none absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, {ValidatorIndex: 1, Timestamp: now}}, []int32{}},
  63. {"one absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, nil}, []int32{1}},
  64. {"multiple absent", []*types.Vote{nil, nil}, []int32{0, 1}},
  65. }
  66. for _, tc := range testCases {
  67. lastCommit := &types.Commit{BlockID: prevBlockID, Precommits: tc.lastCommitPrecommits}
  68. block, _ := state.MakeBlock(2, makeTxs(2), lastCommit)
  69. _, err = ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger())
  70. require.Nil(t, err, tc.desc)
  71. // -> app must receive an index of the absent validator
  72. assert.Equal(t, tc.expectedAbsentValidators, app.AbsentValidators, tc.desc)
  73. }
  74. }
  75. //----------------------------------------------------------------------------
  76. // make some bogus txs
  77. func makeTxs(height int64) (txs []types.Tx) {
  78. for i := 0; i < nTxsPerBlock; i++ {
  79. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  80. }
  81. return txs
  82. }
  83. func state() State {
  84. s, _ := MakeGenesisState(&types.GenesisDoc{
  85. ChainID: chainID,
  86. Validators: []types.GenesisValidator{
  87. {privKey.PubKey(), 10000, "test"},
  88. },
  89. AppHash: nil,
  90. })
  91. return s
  92. }
  93. func makeBlock(state State, height int64) *types.Block {
  94. block, _ := state.MakeBlock(height, makeTxs(state.LastBlockHeight), new(types.Commit))
  95. return block
  96. }
  97. //----------------------------------------------------------------------------
  98. var _ abci.Application = (*testApp)(nil)
  99. type testApp struct {
  100. abci.BaseApplication
  101. AbsentValidators []int32
  102. }
  103. func NewDummyApplication() *testApp {
  104. return &testApp{}
  105. }
  106. func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
  107. return abci.ResponseInfo{}
  108. }
  109. func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
  110. app.AbsentValidators = req.AbsentValidators
  111. return abci.ResponseBeginBlock{}
  112. }
  113. func (app *testApp) DeliverTx(tx []byte) abci.ResponseDeliverTx {
  114. return abci.ResponseDeliverTx{Tags: []*abci.KVPair{}}
  115. }
  116. func (app *testApp) CheckTx(tx []byte) abci.ResponseCheckTx {
  117. return abci.ResponseCheckTx{}
  118. }
  119. func (app *testApp) Commit() abci.ResponseCommit {
  120. return abci.ResponseCommit{}
  121. }
  122. func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
  123. return
  124. }