github.com/evdatsion/aphelion-dpos-bft@v0.32.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/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	abci "github.com/evdatsion/aphelion-dpos-bft/abci/types"
    11  	"github.com/evdatsion/aphelion-dpos-bft/libs/db"
    12  	"github.com/evdatsion/aphelion-dpos-bft/libs/log"
    13  	"github.com/evdatsion/aphelion-dpos-bft/state/txindex"
    14  	"github.com/evdatsion/aphelion-dpos-bft/state/txindex/kv"
    15  	"github.com/evdatsion/aphelion-dpos-bft/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  	defer eventBus.Stop()
    25  
    26  	// tx indexer
    27  	store := db.NewMemDB()
    28  	txIndexer := kv.NewTxIndex(store, kv.IndexAllTags())
    29  
    30  	service := txindex.NewIndexerService(txIndexer, eventBus)
    31  	service.SetLogger(log.TestingLogger())
    32  	err = service.Start()
    33  	require.NoError(t, err)
    34  	defer service.Stop()
    35  
    36  	// publish block with txs
    37  	eventBus.PublishEventNewBlockHeader(types.EventDataNewBlockHeader{
    38  		Header: types.Header{Height: 1, NumTxs: 2},
    39  	})
    40  	txResult1 := &types.TxResult{
    41  		Height: 1,
    42  		Index:  uint32(0),
    43  		Tx:     types.Tx("foo"),
    44  		Result: abci.ResponseDeliverTx{Code: 0},
    45  	}
    46  	eventBus.PublishEventTx(types.EventDataTx{TxResult: *txResult1})
    47  	txResult2 := &types.TxResult{
    48  		Height: 1,
    49  		Index:  uint32(1),
    50  		Tx:     types.Tx("bar"),
    51  		Result: abci.ResponseDeliverTx{Code: 0},
    52  	}
    53  	eventBus.PublishEventTx(types.EventDataTx{TxResult: *txResult2})
    54  
    55  	time.Sleep(100 * time.Millisecond)
    56  
    57  	// check the result
    58  	res, err := txIndexer.Get(types.Tx("foo").Hash())
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, txResult1, res)
    61  	res, err = txIndexer.Get(types.Tx("bar").Hash())
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, txResult2, res)
    64  }