github.com/noirx94/tendermintmp@v0.0.1/state/txindex/indexer_service_test.go (about)

     1  package txindex_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	db "github.com/tendermint/tm-db"
     9  
    10  	abci "github.com/tendermint/tendermint/abci/types"
    11  	"github.com/tendermint/tendermint/libs/log"
    12  	blockidxkv "github.com/tendermint/tendermint/state/indexer/block/kv"
    13  	"github.com/tendermint/tendermint/state/txindex"
    14  	"github.com/tendermint/tendermint/state/txindex/kv"
    15  	"github.com/tendermint/tendermint/types"
    16  )
    17  
    18  func TestIndexerServiceIndexesBlocks(t *testing.T) {
    19  	// event bus
    20  	eventBus := types.NewEventBus()
    21  	eventBus.SetLogger(log.TestingLogger())
    22  	err := eventBus.Start()
    23  	require.NoError(t, err)
    24  	t.Cleanup(func() {
    25  		if err := eventBus.Stop(); err != nil {
    26  			t.Error(err)
    27  		}
    28  	})
    29  
    30  	// tx indexer
    31  	store := db.NewMemDB()
    32  	txIndexer := kv.NewTxIndex(store)
    33  	blockIndexer := blockidxkv.New(db.NewPrefixDB(store, []byte("block_events")))
    34  
    35  	service := txindex.NewIndexerService(txIndexer, blockIndexer, eventBus)
    36  	service.SetLogger(log.TestingLogger())
    37  	err = service.Start()
    38  	require.NoError(t, err)
    39  	t.Cleanup(func() {
    40  		if err := service.Stop(); err != nil {
    41  			t.Error(err)
    42  		}
    43  	})
    44  
    45  	// publish block with txs
    46  	err = eventBus.PublishEventNewBlockHeader(types.EventDataNewBlockHeader{
    47  		Header: types.Header{Height: 1},
    48  		NumTxs: int64(2),
    49  	})
    50  	require.NoError(t, err)
    51  	txResult1 := &abci.TxResult{
    52  		Height: 1,
    53  		Index:  uint32(0),
    54  		Tx:     types.Tx("foo"),
    55  		Result: abci.ResponseDeliverTx{Code: 0},
    56  	}
    57  	err = eventBus.PublishEventTx(types.EventDataTx{TxResult: *txResult1})
    58  	require.NoError(t, err)
    59  	txResult2 := &abci.TxResult{
    60  		Height: 1,
    61  		Index:  uint32(1),
    62  		Tx:     types.Tx("bar"),
    63  		Result: abci.ResponseDeliverTx{Code: 0},
    64  	}
    65  	err = eventBus.PublishEventTx(types.EventDataTx{TxResult: *txResult2})
    66  	require.NoError(t, err)
    67  
    68  	time.Sleep(100 * time.Millisecond)
    69  
    70  	res, err := txIndexer.Get(types.Tx("foo").Hash())
    71  	require.NoError(t, err)
    72  	require.Equal(t, txResult1, res)
    73  
    74  	ok, err := blockIndexer.Has(1)
    75  	require.NoError(t, err)
    76  	require.True(t, ok)
    77  
    78  	res, err = txIndexer.Get(types.Tx("bar").Hash())
    79  	require.NoError(t, err)
    80  	require.Equal(t, txResult2, res)
    81  }