mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-30 13:11:38 +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 0d9107a098
.
* 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
219 lines
6.0 KiB
Go
219 lines
6.0 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
// Subscribe for events via WebSocket.
|
|
//
|
|
// To tell which events you want, you need to provide a query. query is a
|
|
// string, which has a form: "condition AND condition ..." (no OR at the
|
|
// moment). condition has a form: "key operation operand". key is a string with
|
|
// a restricted set of possible symbols ( \t\n\r\\()"'=>< are not allowed).
|
|
// operation can be "=", "<", "<=", ">", ">=", "CONTAINS". operand can be a
|
|
// string (escaped with single quotes), number, date or time.
|
|
//
|
|
// Examples:
|
|
// tm.event = 'NewBlock' # new blocks
|
|
// tm.event = 'CompleteProposal' # node got a complete proposal
|
|
// tm.event = 'Tx' AND tx.hash = 'XYZ' # single transaction
|
|
// tm.event = 'Tx' AND tx.height = 5 # all txs of the fifth block
|
|
// tx.height = 5 # all txs of the fifth block
|
|
//
|
|
// Tendermint provides a few predefined keys: tm.event, tx.hash and tx.height.
|
|
// Note for transactions, you can define additional keys by providing tags with
|
|
// DeliverTx response.
|
|
//
|
|
// DeliverTx{
|
|
// Tags: []*KVPair{
|
|
// "agent.name": "K",
|
|
// }
|
|
// }
|
|
//
|
|
// tm.event = 'Tx' AND agent.name = 'K'
|
|
// tm.event = 'Tx' AND account.created_at >= TIME 2013-05-03T14:45:00Z
|
|
// tm.event = 'Tx' AND contract.sign_date = DATE 2017-01-01
|
|
// tm.event = 'Tx' AND account.owner CONTAINS 'Igor'
|
|
//
|
|
// See list of all possible events here
|
|
// https://godoc.org/github.com/tendermint/tendermint/types#pkg-constants
|
|
//
|
|
// For complete query syntax, check out
|
|
// https://godoc.org/github.com/tendermint/tendermint/libs/pubsub/query.
|
|
//
|
|
// ```go
|
|
// import "github.com/tendermint/tendermint/libs/pubsub/query"
|
|
// import "github.com/tendermint/tendermint/types"
|
|
//
|
|
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
|
|
// err := client.Start()
|
|
// if err != nil {
|
|
// // handle error
|
|
// }
|
|
// defer client.Stop()
|
|
// ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
// defer cancel()
|
|
// query := query.MustParse("tm.event = 'Tx' AND tx.height = 3")
|
|
// txs := make(chan interface{})
|
|
// err = client.Subscribe(ctx, "test-client", query, txs)
|
|
//
|
|
// go func() {
|
|
// for e := range txs {
|
|
// fmt.Println("got ", e.(types.EventDataTx))
|
|
// }
|
|
// }()
|
|
// ```
|
|
//
|
|
// > The above command returns JSON structured like this:
|
|
//
|
|
// ```json
|
|
// {
|
|
// "error": "",
|
|
// "result": {},
|
|
// "id": "",
|
|
// "jsonrpc": "2.0"
|
|
// }
|
|
// ```
|
|
//
|
|
// ### Query Parameters
|
|
//
|
|
// | Parameter | Type | Default | Required | Description |
|
|
// |-----------+--------+---------+----------+-------------|
|
|
// | query | string | "" | true | Query |
|
|
//
|
|
// <aside class="notice">WebSocket only</aside>
|
|
func Subscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultSubscribe, error) {
|
|
addr := wsCtx.GetRemoteAddr()
|
|
logger.Info("Subscribe to query", "remote", addr, "query", query)
|
|
|
|
q, err := tmquery.New(query)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to parse query")
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), subscribeTimeout)
|
|
defer cancel()
|
|
sub, err := eventBusFor(wsCtx).Subscribe(ctx, addr, q)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
select {
|
|
case msg := <-sub.Out():
|
|
resultEvent := &ctypes.ResultEvent{Query: query, Data: msg.Data(), Tags: msg.Tags()}
|
|
wsCtx.TryWriteRPCResponse(
|
|
rpctypes.NewRPCSuccessResponse(
|
|
wsCtx.Codec(),
|
|
rpctypes.JSONRPCStringID(fmt.Sprintf("%v#event", wsCtx.Request.ID)),
|
|
resultEvent,
|
|
))
|
|
case <-sub.Cancelled():
|
|
wsCtx.TryWriteRPCResponse(
|
|
rpctypes.RPCServerError(rpctypes.JSONRPCStringID(
|
|
fmt.Sprintf("%v#event", wsCtx.Request.ID)),
|
|
fmt.Errorf("subscription was cancelled (reason: %v)", sub.Err()),
|
|
))
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return &ctypes.ResultSubscribe{}, nil
|
|
}
|
|
|
|
// Unsubscribe from events via WebSocket.
|
|
//
|
|
// ```go
|
|
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
|
|
// err := client.Start()
|
|
// if err != nil {
|
|
// // handle error
|
|
// }
|
|
// defer client.Stop()
|
|
// err = client.Unsubscribe("test-client", query)
|
|
// ```
|
|
//
|
|
// > The above command returns JSON structured like this:
|
|
//
|
|
// ```json
|
|
// {
|
|
// "error": "",
|
|
// "result": {},
|
|
// "id": "",
|
|
// "jsonrpc": "2.0"
|
|
// }
|
|
// ```
|
|
//
|
|
// ### Query Parameters
|
|
//
|
|
// | Parameter | Type | Default | Required | Description |
|
|
// |-----------+--------+---------+----------+-------------|
|
|
// | query | string | "" | true | Query |
|
|
//
|
|
// <aside class="notice">WebSocket only</aside>
|
|
func Unsubscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultUnsubscribe, error) {
|
|
addr := wsCtx.GetRemoteAddr()
|
|
logger.Info("Unsubscribe from query", "remote", addr, "query", query)
|
|
q, err := tmquery.New(query)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to parse query")
|
|
}
|
|
err = eventBusFor(wsCtx).Unsubscribe(context.Background(), addr, q)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ctypes.ResultUnsubscribe{}, nil
|
|
}
|
|
|
|
// Unsubscribe from all events via WebSocket.
|
|
//
|
|
// ```go
|
|
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
|
|
// err := client.Start()
|
|
// if err != nil {
|
|
// // handle error
|
|
// }
|
|
// defer client.Stop()
|
|
// err = client.UnsubscribeAll("test-client")
|
|
// ```
|
|
//
|
|
// > The above command returns JSON structured like this:
|
|
//
|
|
// ```json
|
|
// {
|
|
// "error": "",
|
|
// "result": {},
|
|
// "id": "",
|
|
// "jsonrpc": "2.0"
|
|
// }
|
|
// ```
|
|
//
|
|
// <aside class="notice">WebSocket only</aside>
|
|
func UnsubscribeAll(wsCtx rpctypes.WSRPCContext) (*ctypes.ResultUnsubscribe, error) {
|
|
addr := wsCtx.GetRemoteAddr()
|
|
logger.Info("Unsubscribe from all", "remote", addr)
|
|
err := eventBusFor(wsCtx).UnsubscribeAll(context.Background(), addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ctypes.ResultUnsubscribe{}, nil
|
|
}
|
|
|
|
func eventBusFor(wsCtx rpctypes.WSRPCContext) tmtypes.EventBusSubscriber {
|
|
es := wsCtx.GetEventSubscriber()
|
|
if es == nil {
|
|
es = eventBus
|
|
}
|
|
return es
|
|
}
|