From b6a04a3456792826fff39095414000b1a6b59cc5 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 18 Apr 2017 20:10:02 -0400 Subject: [PATCH] more fixes from review --- node/node.go | 24 ++++++++++++++---------- rpc/core/types/responses.go | 2 +- rpc/core/types/responses_test.go | 4 ++-- state/execution.go | 5 ++--- state/execution_test.go | 4 ++-- state/txindex/indexer.go | 13 ++++--------- state/txindex/kv/kv.go | 4 ++-- state/txindex/kv/kv_test.go | 5 ++--- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/node/node.go b/node/node.go index 7b39189fe..b75898eb3 100644 --- a/node/node.go +++ b/node/node.go @@ -217,7 +217,7 @@ func (n *Node) OnStart() error { n.sw.AddListener(l) // Start the switch - n.sw.SetNodeInfo(makeNodeInfo(n.config, n.sw, n.privKey)) + n.sw.SetNodeInfo(n.makeNodeInfo()) n.sw.SetNodePrivKey(n.privKey) _, err := n.sw.Start() if err != nil { @@ -365,35 +365,39 @@ func (n *Node) ProxyApp() proxy.AppConns { return n.proxyApp } -func makeNodeInfo(config cfg.Config, sw *p2p.Switch, privKey crypto.PrivKeyEd25519) *p2p.NodeInfo { +func (n *Node) makeNodeInfo() *p2p.NodeInfo { + txIndexerStatus := "on" + if _, ok := n.txIndexer.(*null.TxIndex); ok { + txIndexerStatus = "off" + } nodeInfo := &p2p.NodeInfo{ - PubKey: privKey.PubKey().(crypto.PubKeyEd25519), - Moniker: config.GetString("moniker"), - Network: config.GetString("chain_id"), + PubKey: n.privKey.PubKey().(crypto.PubKeyEd25519), + Moniker: n.config.GetString("moniker"), + Network: n.config.GetString("chain_id"), Version: version.Version, Other: []string{ cmn.Fmt("wire_version=%v", wire.Version), cmn.Fmt("p2p_version=%v", p2p.Version), cmn.Fmt("consensus_version=%v", consensus.Version), cmn.Fmt("rpc_version=%v/%v", rpc.Version, rpccore.Version), - cmn.Fmt("tx_indexer=%v", config.GetString("tx_indexer")), + cmn.Fmt("tx_indexer=%v", txIndexerStatus), }, } // include git hash in the nodeInfo if available - if rev, err := cmn.ReadFile(config.GetString("revision_file")); err == nil { + if rev, err := cmn.ReadFile(n.config.GetString("revision_file")); err == nil { nodeInfo.Other = append(nodeInfo.Other, cmn.Fmt("revision=%v", string(rev))) } - if !sw.IsListening() { + if !n.sw.IsListening() { return nodeInfo } - p2pListener := sw.Listeners()[0] + p2pListener := n.sw.Listeners()[0] p2pHost := p2pListener.ExternalAddress().IP.String() p2pPort := p2pListener.ExternalAddress().Port - rpcListenAddr := config.GetString("rpc_laddr") + rpcListenAddr := n.config.GetString("rpc_laddr") // We assume that the rpcListener has the same ExternalAddress. // This is probably true because both P2P and RPC listeners use UPnP, diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 6979e0e6c..f73d0e010 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -47,7 +47,7 @@ func (s *ResultStatus) TxIndexEnabled() bool { for _, s := range s.NodeInfo.Other { info := strings.Split(s, "=") if len(info) == 2 && info[0] == "tx_indexer" { - return info[1] == "kv" + return info[1] == "on" } } return false diff --git a/rpc/core/types/responses_test.go b/rpc/core/types/responses_test.go index 13728e42b..d1e8aa1b7 100644 --- a/rpc/core/types/responses_test.go +++ b/rpc/core/types/responses_test.go @@ -27,8 +27,8 @@ func TestStatusIndexer(t *testing.T) { {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="}}, + {true, []string{"tx_indexer=on", "tx_indexer=other"}}, + {true, []string{"^(*^(", "tx_indexer=on", "a=n=b=d="}}, } for _, tc := range cases { diff --git a/state/execution.go b/state/execution.go index 05496aad5..2e980faa5 100644 --- a/state/execution.go +++ b/state/execution.go @@ -247,9 +247,8 @@ func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConn } batch := txindex.NewBatch() - for i, r := range txResults { - tx := block.Txs[i] - batch.Index(tx.Hash(), *r) + for _, r := range txResults { + batch.Add(*r) } s.TxIndexer.AddBatch(batch) diff --git a/state/execution_test.go b/state/execution_test.go index 2da801964..299c6baa2 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -49,7 +49,7 @@ func TestApplyBlock(t *testing.T) { //---------------------------------------------------------------------------- // make some bogus txs -func txsFunc(blockNum int) (txs []types.Tx) { +func makeTxs(blockNum int) (txs []types.Tx) { for i := 0; i < nTxsPerBlock; i++ { txs = append(txs, types.Tx([]byte{byte(blockNum), byte(i)})) } @@ -71,7 +71,7 @@ func makeBlock(num int, state *State) *types.Block { prevParts := types.PartSetHeader{} valHash := state.Validators.Hash() prevBlockID := types.BlockID{prevHash, prevParts} - block, _ := types.MakeBlock(num, chainID, txsFunc(num), new(types.Commit), + block, _ := types.MakeBlock(num, chainID, makeTxs(num), new(types.Commit), prevBlockID, valHash, state.AppHash, testPartSize) return block } diff --git a/state/txindex/indexer.go b/state/txindex/indexer.go index a962eba7d..f492dac2c 100644 --- a/state/txindex/indexer.go +++ b/state/txindex/indexer.go @@ -29,22 +29,17 @@ type TxIndexer interface { // perform operations on a batch from a single thread at a time. Once batch // execution has started, you may not modify it. type Batch struct { - Ops map[string]types.TxResult + Ops []types.TxResult } // NewBatch creates a new Batch. func NewBatch() *Batch { - return &Batch{ - Ops: make(map[string]types.TxResult), - } + return &Batch{} } // Index adds or updates entry for the given hash. -func (b *Batch) Index(hash []byte, result types.TxResult) error { - if len(hash) == 0 { - return ErrorEmptyHash - } - b.Ops[string(hash)] = result +func (b *Batch) Add(result types.TxResult) error { + b.Ops = append(b.Ops, result) return nil } diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go index b76e6a3b0..03acc8dae 100644 --- a/state/txindex/kv/kv.go +++ b/state/txindex/kv/kv.go @@ -47,9 +47,9 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) { // Batch writes a batch of transactions into the TxIndex storage. func (txi *TxIndex) AddBatch(b *txindex.Batch) error { storeBatch := txi.store.NewBatch() - for hash, result := range b.Ops { + for _, result := range b.Ops { rawBytes := wire.BinaryBytes(&result) - storeBatch.Set([]byte(hash), rawBytes) + storeBatch.Set(result.Tx.Hash(), rawBytes) } storeBatch.Write() return nil diff --git a/state/txindex/kv/kv_test.go b/state/txindex/kv/kv_test.go index 7d4a84dd1..0bd45a824 100644 --- a/state/txindex/kv/kv_test.go +++ b/state/txindex/kv/kv_test.go @@ -1,7 +1,6 @@ package kv import ( - "fmt" "io/ioutil" "os" "testing" @@ -22,7 +21,7 @@ func TestTxIndex(t *testing.T) { hash := tx.Hash() batch := txindex.NewBatch() - batch.Index(hash, *txResult) + batch.Add(*txResult) err := indexer.AddBatch(batch) require.Nil(t, err) @@ -46,7 +45,7 @@ func benchmarkTxIndex(txsCount int, b *testing.B) { batch := txindex.NewBatch() for i := 0; i < txsCount; i++ { - batch.Index([]byte(fmt.Sprintf("hash%v", i)), *txResult) + batch.Add(*txResult) } b.ResetTimer()