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.

86 lines
2.2 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
  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. "github.com/tendermint/tendermint/proxy"
  9. "github.com/tendermint/tendermint/state/txindex"
  10. "github.com/tendermint/tendermint/types"
  11. dbm "github.com/tendermint/tmlibs/db"
  12. )
  13. var (
  14. privKey = crypto.GenPrivKeyEd25519FromSecret([]byte("execution_test"))
  15. chainID = "execution_chain"
  16. testPartSize = 65536
  17. nTxsPerBlock = 10
  18. )
  19. func TestApplyBlock(t *testing.T) {
  20. cc := proxy.NewLocalClientCreator(dummy.NewDummyApplication())
  21. proxyApp := proxy.NewAppConns(cc, nil)
  22. _, err := proxyApp.Start()
  23. require.Nil(t, err)
  24. defer proxyApp.Stop()
  25. state := state()
  26. indexer := &dummyIndexer{0}
  27. state.TxIndexer = indexer
  28. // make block
  29. block := makeBlock(1, state)
  30. err = state.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), types.MockMempool{})
  31. require.Nil(t, err)
  32. assert.Equal(t, nTxsPerBlock, indexer.Indexed) // test indexing works
  33. // TODO check state and mempool
  34. }
  35. //----------------------------------------------------------------------------
  36. // make some bogus txs
  37. func makeTxs(blockNum int) (txs []types.Tx) {
  38. for i := 0; i < nTxsPerBlock; i++ {
  39. txs = append(txs, types.Tx([]byte{byte(blockNum), byte(i)}))
  40. }
  41. return txs
  42. }
  43. func state() *State {
  44. return MakeGenesisState(dbm.NewMemDB(), &types.GenesisDoc{
  45. ChainID: chainID,
  46. Validators: []types.GenesisValidator{
  47. types.GenesisValidator{privKey.PubKey(), 10000, "test"},
  48. },
  49. AppHash: nil,
  50. })
  51. }
  52. func makeBlock(num int, state *State) *types.Block {
  53. prevHash := state.LastBlockID.Hash
  54. prevParts := types.PartSetHeader{}
  55. valHash := state.Validators.Hash()
  56. prevBlockID := types.BlockID{prevHash, prevParts}
  57. block, _ := types.MakeBlock(num, chainID, makeTxs(num), new(types.Commit),
  58. prevBlockID, valHash, state.AppHash, testPartSize)
  59. return block
  60. }
  61. // dummyIndexer increments counter every time we index transaction.
  62. type dummyIndexer struct {
  63. Indexed int
  64. }
  65. func (indexer *dummyIndexer) Get(hash []byte) (*types.TxResult, error) {
  66. return nil, nil
  67. }
  68. func (indexer *dummyIndexer) AddBatch(batch *txindex.Batch) error {
  69. indexer.Indexed += batch.Size()
  70. return nil
  71. }