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.

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