github.com/fiagdao/tendermint@v0.32.11-0.20220824195748-2087fcc480c1/state/txindex/indexer_service_test.go (about)

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