mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-16 16:41:20 +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
277 lines
8.1 KiB
Go
277 lines
8.1 KiB
Go
package types
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
|
|
)
|
|
|
|
const defaultCapacity = 0
|
|
|
|
type EventBusSubscriber interface {
|
|
Subscribe(ctx context.Context, subscriber string, query tmpubsub.Query, outCapacity ...int) (Subscription, error)
|
|
Unsubscribe(ctx context.Context, subscriber string, query tmpubsub.Query) error
|
|
UnsubscribeAll(ctx context.Context, subscriber string) error
|
|
}
|
|
|
|
type Subscription interface {
|
|
Out() <-chan tmpubsub.Message
|
|
Cancelled() <-chan struct{}
|
|
Err() error
|
|
}
|
|
|
|
// EventBus is a common bus for all events going through the system. All calls
|
|
// are proxied to underlying pubsub server. All events must be published using
|
|
// EventBus to ensure correct data types.
|
|
type EventBus struct {
|
|
cmn.BaseService
|
|
pubsub *tmpubsub.Server
|
|
}
|
|
|
|
// NewEventBus returns a new event bus.
|
|
func NewEventBus() *EventBus {
|
|
return NewEventBusWithBufferCapacity(defaultCapacity)
|
|
}
|
|
|
|
// NewEventBusWithBufferCapacity returns a new event bus with the given buffer capacity.
|
|
func NewEventBusWithBufferCapacity(cap int) *EventBus {
|
|
// capacity could be exposed later if needed
|
|
pubsub := tmpubsub.NewServer(tmpubsub.BufferCapacity(cap))
|
|
b := &EventBus{pubsub: pubsub}
|
|
b.BaseService = *cmn.NewBaseService(nil, "EventBus", b)
|
|
return b
|
|
}
|
|
|
|
func (b *EventBus) SetLogger(l log.Logger) {
|
|
b.BaseService.SetLogger(l)
|
|
b.pubsub.SetLogger(l.With("module", "pubsub"))
|
|
}
|
|
|
|
func (b *EventBus) OnStart() error {
|
|
return b.pubsub.Start()
|
|
}
|
|
|
|
func (b *EventBus) OnStop() {
|
|
b.pubsub.Stop()
|
|
}
|
|
|
|
func (b *EventBus) Subscribe(ctx context.Context, subscriber string, query tmpubsub.Query, outCapacity ...int) (Subscription, error) {
|
|
return b.pubsub.Subscribe(ctx, subscriber, query, outCapacity...)
|
|
}
|
|
|
|
// This method can be used for a local consensus explorer and synchronous
|
|
// testing. Do not use for for public facing / untrusted subscriptions!
|
|
func (b *EventBus) SubscribeUnbuffered(ctx context.Context, subscriber string, query tmpubsub.Query) (Subscription, error) {
|
|
return b.pubsub.SubscribeUnbuffered(ctx, subscriber, query)
|
|
}
|
|
|
|
func (b *EventBus) Unsubscribe(ctx context.Context, subscriber string, query tmpubsub.Query) error {
|
|
return b.pubsub.Unsubscribe(ctx, subscriber, query)
|
|
}
|
|
|
|
func (b *EventBus) UnsubscribeAll(ctx context.Context, subscriber string) error {
|
|
return b.pubsub.UnsubscribeAll(ctx, subscriber)
|
|
}
|
|
|
|
func (b *EventBus) Publish(eventType string, eventData TMEventData) error {
|
|
// no explicit deadline for publishing events
|
|
ctx := context.Background()
|
|
b.pubsub.PublishWithTags(ctx, eventData, map[string]string{EventTypeKey: eventType})
|
|
return nil
|
|
}
|
|
|
|
func (b *EventBus) validateAndStringifyTags(tags []cmn.KVPair, logger log.Logger) map[string]string {
|
|
result := make(map[string]string)
|
|
for _, tag := range tags {
|
|
// basic validation
|
|
if len(tag.Key) == 0 {
|
|
logger.Debug("Got tag with an empty key (skipping)", "tag", tag)
|
|
continue
|
|
}
|
|
result[string(tag.Key)] = string(tag.Value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (b *EventBus) PublishEventNewBlock(data EventDataNewBlock) error {
|
|
// no explicit deadline for publishing events
|
|
ctx := context.Background()
|
|
|
|
resultTags := append(data.ResultBeginBlock.Tags, data.ResultEndBlock.Tags...)
|
|
tags := b.validateAndStringifyTags(resultTags, b.Logger.With("block", data.Block.StringShort()))
|
|
|
|
// add predefined tags
|
|
logIfTagExists(EventTypeKey, tags, b.Logger)
|
|
tags[EventTypeKey] = EventNewBlock
|
|
|
|
b.pubsub.PublishWithTags(ctx, data, tags)
|
|
return nil
|
|
}
|
|
|
|
func (b *EventBus) PublishEventNewBlockHeader(data EventDataNewBlockHeader) error {
|
|
// no explicit deadline for publishing events
|
|
ctx := context.Background()
|
|
|
|
resultTags := append(data.ResultBeginBlock.Tags, data.ResultEndBlock.Tags...)
|
|
// TODO: Create StringShort method for Header and use it in logger.
|
|
tags := b.validateAndStringifyTags(resultTags, b.Logger.With("header", data.Header))
|
|
|
|
// add predefined tags
|
|
logIfTagExists(EventTypeKey, tags, b.Logger)
|
|
tags[EventTypeKey] = EventNewBlockHeader
|
|
|
|
b.pubsub.PublishWithTags(ctx, data, tags)
|
|
return nil
|
|
}
|
|
|
|
func (b *EventBus) PublishEventVote(data EventDataVote) error {
|
|
return b.Publish(EventVote, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventValidBlock(data EventDataRoundState) error {
|
|
return b.Publish(EventValidBlock, data)
|
|
}
|
|
|
|
// PublishEventTx publishes tx event with tags from Result. Note it will add
|
|
// predefined tags (EventTypeKey, TxHashKey). Existing tags with the same names
|
|
// will be overwritten.
|
|
func (b *EventBus) PublishEventTx(data EventDataTx) error {
|
|
// no explicit deadline for publishing events
|
|
ctx := context.Background()
|
|
|
|
tags := b.validateAndStringifyTags(data.Result.Tags, b.Logger.With("tx", data.Tx))
|
|
|
|
// add predefined tags
|
|
logIfTagExists(EventTypeKey, tags, b.Logger)
|
|
tags[EventTypeKey] = EventTx
|
|
|
|
logIfTagExists(TxHashKey, tags, b.Logger)
|
|
tags[TxHashKey] = fmt.Sprintf("%X", data.Tx.Hash())
|
|
|
|
logIfTagExists(TxHeightKey, tags, b.Logger)
|
|
tags[TxHeightKey] = fmt.Sprintf("%d", data.Height)
|
|
|
|
b.pubsub.PublishWithTags(ctx, data, tags)
|
|
return nil
|
|
}
|
|
|
|
func (b *EventBus) PublishEventNewRoundStep(data EventDataRoundState) error {
|
|
return b.Publish(EventNewRoundStep, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventTimeoutPropose(data EventDataRoundState) error {
|
|
return b.Publish(EventTimeoutPropose, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventTimeoutWait(data EventDataRoundState) error {
|
|
return b.Publish(EventTimeoutWait, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventNewRound(data EventDataNewRound) error {
|
|
return b.Publish(EventNewRound, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventCompleteProposal(data EventDataCompleteProposal) error {
|
|
return b.Publish(EventCompleteProposal, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventPolka(data EventDataRoundState) error {
|
|
return b.Publish(EventPolka, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventUnlock(data EventDataRoundState) error {
|
|
return b.Publish(EventUnlock, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventRelock(data EventDataRoundState) error {
|
|
return b.Publish(EventRelock, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventLock(data EventDataRoundState) error {
|
|
return b.Publish(EventLock, data)
|
|
}
|
|
|
|
func (b *EventBus) PublishEventValidatorSetUpdates(data EventDataValidatorSetUpdates) error {
|
|
return b.Publish(EventValidatorSetUpdates, data)
|
|
}
|
|
|
|
func logIfTagExists(tag string, tags map[string]string, logger log.Logger) {
|
|
if value, ok := tags[tag]; ok {
|
|
logger.Error("Found predefined tag (value will be overwritten)", "tag", tag, "value", value)
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
type NopEventBus struct{}
|
|
|
|
func (NopEventBus) Subscribe(ctx context.Context, subscriber string, query tmpubsub.Query, out chan<- interface{}) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) Unsubscribe(ctx context.Context, subscriber string, query tmpubsub.Query) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) UnsubscribeAll(ctx context.Context, subscriber string) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventNewBlock(data EventDataNewBlock) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventNewBlockHeader(data EventDataNewBlockHeader) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventVote(data EventDataVote) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventTx(data EventDataTx) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventNewRoundStep(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventTimeoutPropose(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventTimeoutWait(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventNewRound(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventCompleteProposal(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventPolka(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventUnlock(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventRelock(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventLock(data EventDataRoundState) error {
|
|
return nil
|
|
}
|
|
|
|
func (NopEventBus) PublishEventValidatorSetUpdates(data EventDataValidatorSetUpdates) error {
|
|
return nil
|
|
}
|