From a4ee7d25d10e4cf876cbd367aaaaa10c98545a15 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 13 Apr 2017 20:20:41 +0200 Subject: [PATCH] Add TxIndexEnabled method to ResultStatus --- rpc/core/types/responses.go | 15 +++++++++++++ rpc/core/types/responses_test.go | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 rpc/core/types/responses_test.go diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 50b36099..1449164f 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -1,6 +1,8 @@ package core_types import ( + "strings" + abci "github.com/tendermint/abci/types" "github.com/tendermint/go-crypto" "github.com/tendermint/go-p2p" @@ -38,6 +40,19 @@ type ResultStatus struct { LatestBlockTime int64 `json:"latest_block_time"` // nano } +func (s *ResultStatus) TxIndexEnabled() bool { + if s == nil || s.NodeInfo == nil { + return false + } + for _, s := range s.NodeInfo.Other { + info := strings.Split(s, "=") + if len(info) == 2 && info[0] == "tx_indexer" { + return info[1] == "kv" + } + } + return false +} + type ResultNetInfo struct { Listening bool `json:"listening"` Listeners []string `json:"listeners"` diff --git a/rpc/core/types/responses_test.go b/rpc/core/types/responses_test.go new file mode 100644 index 00000000..13728e42 --- /dev/null +++ b/rpc/core/types/responses_test.go @@ -0,0 +1,38 @@ +package core_types + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tendermint/go-p2p" +) + +func TestStatusIndexer(t *testing.T) { + assert := assert.New(t) + + var status *ResultStatus + assert.False(status.TxIndexEnabled()) + + status = &ResultStatus{} + assert.False(status.TxIndexEnabled()) + + status.NodeInfo = &p2p.NodeInfo{} + assert.False(status.TxIndexEnabled()) + + cases := []struct { + expected bool + other []string + }{ + {false, nil}, + {false, []string{}}, + {false, []string{"a=b"}}, + {false, []string{"tx_indexeriskv", "some=dood"}}, + {true, []string{"tx_indexer=kv", "tx_indexer=other"}}, + {true, []string{"^(*^(", "tx_indexer=kv", "a=n=b=d="}}, + } + + for _, tc := range cases { + status.NodeInfo.Other = tc.other + assert.Equal(tc.expected, status.TxIndexEnabled()) + } +}