mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-12 14:57:12 +00:00
* green pubsub tests :OK: * get rid of clientToQueryMap * Subscribe and SubscribeUnbuffered * start adapting other pkgs to new pubsub * nope * rename MsgAndTags to Message * remove TagMap it does not bring any additional benefits * bring back EventSubscriber * fix test * fix data race in TestStartNextHeightCorrectly ``` Write at 0x00c0001c7418 by goroutine 796: github.com/tendermint/tendermint/consensus.TestStartNextHeightCorrectly() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:1296 +0xad testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Previous read at 0x00c0001c7418 by goroutine 858: github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1631 +0x1366 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1476 +0x8f github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /go/src/github.com/tendermint/tendermint/consensus/state.go:667 +0xa1e github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /go/src/github.com/tendermint/tendermint/consensus/state.go:628 +0x794 Goroutine 796 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1119 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 testing.runTests() /usr/local/go/src/testing/testing.go:1117 +0x4ee testing.(*M).Run() /usr/local/go/src/testing/testing.go:1034 +0x2ee main.main() _testmain.go:214 +0x332 Goroutine 858 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).startRoutines() /go/src/github.com/tendermint/tendermint/consensus/state.go:334 +0x221 github.com/tendermint/tendermint/consensus.startTestRound() /go/src/github.com/tendermint/tendermint/consensus/common_test.go:122 +0x63 github.com/tendermint/tendermint/consensus.TestStateFullRound1() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:255 +0x397 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ``` * fixes after my own review * fix formatting * wait 100ms before kicking a subscriber out + a test for indexer_service * fixes after my second review * no timeout * add changelog entries * fix merge conflicts * fix typos after Thane's review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * reformat code * rewrite indexer service in the attempt to fix failing test https://github.com/tendermint/tendermint/pull/3227/#issuecomment-462316527 * Revert "rewrite indexer service in the attempt to fix failing test" This reverts commit 0d9107a098230de7138abb1c201877c246e89ed1. * another attempt to fix indexer * fixes after Ethan's review * use unbuffered channel when indexing transactions Refs https://github.com/tendermint/tendermint/pull/3227#discussion_r258786716 * add a comment for EventBus#SubscribeUnbuffered * format code
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package txindex
|
|
|
|
import (
|
|
"context"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
const (
|
|
subscriber = "IndexerService"
|
|
)
|
|
|
|
// IndexerService connects event bus and transaction indexer together in order
|
|
// to index transactions coming from event bus.
|
|
type IndexerService struct {
|
|
cmn.BaseService
|
|
|
|
idr TxIndexer
|
|
eventBus *types.EventBus
|
|
}
|
|
|
|
// NewIndexerService returns a new service instance.
|
|
func NewIndexerService(idr TxIndexer, eventBus *types.EventBus) *IndexerService {
|
|
is := &IndexerService{idr: idr, eventBus: eventBus}
|
|
is.BaseService = *cmn.NewBaseService(nil, "IndexerService", is)
|
|
return is
|
|
}
|
|
|
|
// OnStart implements cmn.Service by subscribing for all transactions
|
|
// and indexing them by tags.
|
|
func (is *IndexerService) OnStart() error {
|
|
// Use SubscribeUnbuffered here to ensure both subscriptions does not get
|
|
// cancelled due to not pulling messages fast enough. Cause this might
|
|
// sometimes happen when there are no other subscribers.
|
|
|
|
blockHeadersSub, err := is.eventBus.SubscribeUnbuffered(context.Background(), subscriber, types.EventQueryNewBlockHeader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
txsSub, err := is.eventBus.SubscribeUnbuffered(context.Background(), subscriber, types.EventQueryTx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
msg := <-blockHeadersSub.Out()
|
|
header := msg.Data().(types.EventDataNewBlockHeader).Header
|
|
batch := NewBatch(header.NumTxs)
|
|
for i := int64(0); i < header.NumTxs; i++ {
|
|
msg2 := <-txsSub.Out()
|
|
txResult := msg2.Data().(types.EventDataTx).TxResult
|
|
if err = batch.Add(&txResult); err != nil {
|
|
is.Logger.Error("Can't add tx to batch",
|
|
"height", header.Height,
|
|
"index", txResult.Index,
|
|
"err", err)
|
|
}
|
|
}
|
|
if err = is.idr.AddBatch(batch); err != nil {
|
|
is.Logger.Error("Failed to index block", "height", header.Height, "err", err)
|
|
} else {
|
|
is.Logger.Info("Indexed block", "height", header.Height)
|
|
}
|
|
}
|
|
}()
|
|
return nil
|
|
}
|
|
|
|
// OnStop implements cmn.Service by unsubscribing from all transactions.
|
|
func (is *IndexerService) OnStop() {
|
|
if is.eventBus.IsRunning() {
|
|
_ = is.eventBus.UnsubscribeAll(context.Background(), subscriber)
|
|
}
|
|
}
|