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.

90 lines
2.4 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
  1. package state
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/abci/example/dummy"
  7. crypto "github.com/tendermint/go-crypto"
  8. dbm "github.com/tendermint/tmlibs/db"
  9. cfg "github.com/tendermint/tendermint/config/tendermint_test"
  10. "github.com/tendermint/tendermint/mempool"
  11. "github.com/tendermint/tendermint/proxy"
  12. "github.com/tendermint/tendermint/state/txindex"
  13. "github.com/tendermint/tendermint/types"
  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. config := cfg.ResetConfig("execution_test_")
  24. proxyApp := proxy.NewAppConns(config, cc, nil)
  25. _, err := proxyApp.Start()
  26. require.Nil(t, err)
  27. defer proxyApp.Stop()
  28. mempool := mempool.NewMempool(config, proxyApp.Mempool())
  29. state := state()
  30. indexer := &dummyIndexer{0}
  31. state.TxIndexer = indexer
  32. // make block
  33. block := makeBlock(1, state)
  34. err = state.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), mempool)
  35. require.Nil(t, err)
  36. assert.Equal(t, nTxsPerBlock, indexer.Indexed) // test indexing works
  37. // TODO check state and mempool
  38. }
  39. //----------------------------------------------------------------------------
  40. // make some bogus txs
  41. func makeTxs(blockNum int) (txs []types.Tx) {
  42. for i := 0; i < nTxsPerBlock; i++ {
  43. txs = append(txs, types.Tx([]byte{byte(blockNum), byte(i)}))
  44. }
  45. return txs
  46. }
  47. func state() *State {
  48. return MakeGenesisState(dbm.NewMemDB(), &types.GenesisDoc{
  49. ChainID: chainID,
  50. Validators: []types.GenesisValidator{
  51. types.GenesisValidator{privKey.PubKey(), 10000, "test"},
  52. },
  53. AppHash: nil,
  54. })
  55. }
  56. func makeBlock(num int, state *State) *types.Block {
  57. prevHash := state.LastBlockID.Hash
  58. prevParts := types.PartSetHeader{}
  59. valHash := state.Validators.Hash()
  60. prevBlockID := types.BlockID{prevHash, prevParts}
  61. block, _ := types.MakeBlock(num, chainID, makeTxs(num), new(types.Commit),
  62. prevBlockID, valHash, state.AppHash, testPartSize)
  63. return block
  64. }
  65. // dummyIndexer increments counter every time we index transaction.
  66. type dummyIndexer struct {
  67. Indexed int
  68. }
  69. func (indexer *dummyIndexer) Get(hash []byte) (*types.TxResult, error) {
  70. return nil, nil
  71. }
  72. func (indexer *dummyIndexer) AddBatch(batch *txindex.Batch) error {
  73. indexer.Indexed += batch.Size()
  74. return nil
  75. }