libs/common: refactor libs/common 01 (#4230)

* libs/common: Refactor libs/common 01

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* regenerate proto files, move intslice to where its used

* update kv.KVPair(s) to kv.Pair(s)

* add changelog entry

* make intInSlice private
This commit is contained in:
Marko
2019-12-10 12:40:01 +01:00
committed by GitHub
parent 6b1fef7f4d
commit dfebac86f7
26 changed files with 428 additions and 451 deletions

View File

@@ -13,6 +13,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/kv"
sm "github.com/tendermint/tendermint/state"
dbm "github.com/tendermint/tm-db"
@@ -132,8 +133,8 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
{
Data: []byte("Gotcha!"),
Events: []abci.Event{
{Type: "type1", Attributes: []cmn.KVPair{{Key: []byte("a"), Value: []byte("1")}}},
{Type: "type2", Attributes: []cmn.KVPair{{Key: []byte("build"), Value: []byte("stuff")}}},
{Type: "type1", Attributes: []kv.Pair{{Key: []byte("a"), Value: []byte("1")}}},
{Type: "type2", Attributes: []kv.Pair{{Key: []byte("build"), Value: []byte("stuff")}}},
},
},
},

View File

@@ -220,7 +220,7 @@ func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {
// for all other conditions
for i, c := range conditions {
if cmn.IntInSlice(i, skipIndexes) {
if intInSlice(i, skipIndexes) {
continue
}

View File

@@ -7,7 +7,7 @@ import (
"testing"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/kv"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
@@ -31,7 +31,7 @@ func BenchmarkTxSearch(b *testing.B) {
events := []abci.Event{
{
Type: "transfer",
Attributes: []cmn.KVPair{
Attributes: []kv.Pair{
{Key: []byte("address"), Value: []byte(fmt.Sprintf("address_%d", i%100))},
{Key: []byte("amount"), Value: []byte("50")},
},

View File

@@ -13,6 +13,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/kv"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
@@ -69,9 +70,9 @@ func TestTxSearch(t *testing.T) {
indexer := NewTxIndex(db.NewMemDB(), IndexEvents(allowedKeys))
txResult := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("owner"), Value: []byte("Ivan")}}},
{Type: "", Attributes: []cmn.KVPair{{Key: []byte("not_allowed"), Value: []byte("Vlad")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("owner"), Value: []byte("Ivan")}}},
{Type: "", Attributes: []kv.Pair{{Key: []byte("not_allowed"), Value: []byte("Vlad")}}},
})
hash := txResult.Tx.Hash()
@@ -137,7 +138,7 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
// index tx using events indexing (composite key)
txResult1 := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("1")}}},
})
hash1 := txResult1.Tx.Hash()
@@ -206,8 +207,8 @@ func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
indexer := NewTxIndex(db.NewMemDB(), IndexEvents(allowedKeys))
txResult := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("2")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("2")}}},
})
err := indexer.Index(txResult)
@@ -226,7 +227,7 @@ func TestTxSearchMultipleTxs(t *testing.T) {
// indexed first, but bigger height (to test the order of transactions)
txResult := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("1")}}},
})
txResult.Tx = types.Tx("Bob's account")
@@ -237,7 +238,7 @@ func TestTxSearchMultipleTxs(t *testing.T) {
// indexed second, but smaller height (to test the order of transactions)
txResult2 := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("2")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("2")}}},
})
txResult2.Tx = types.Tx("Alice's account")
txResult2.Height = 1
@@ -248,7 +249,7 @@ func TestTxSearchMultipleTxs(t *testing.T) {
// indexed third (to test the order of transactions)
txResult3 := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("3")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("3")}}},
})
txResult3.Tx = types.Tx("Jack's account")
txResult3.Height = 1
@@ -259,7 +260,7 @@ func TestTxSearchMultipleTxs(t *testing.T) {
// indexed fourth (to test we don't include txs with similar events)
// https://github.com/tendermint/tendermint/issues/2908
txResult4 := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number.id"), Value: []byte("1")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number.id"), Value: []byte("1")}}},
})
txResult4.Tx = types.Tx("Mike's account")
txResult4.Height = 2
@@ -278,8 +279,8 @@ func TestIndexAllTags(t *testing.T) {
indexer := NewTxIndex(db.NewMemDB(), IndexAllEvents())
txResult := txResultWithEvents([]abci.Event{
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("owner"), Value: []byte("Ivan")}}},
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("owner"), Value: []byte("Ivan")}}},
{Type: "account", Attributes: []kv.Pair{{Key: []byte("number"), Value: []byte("1")}}},
})
err := indexer.Index(txResult)

11
state/txindex/kv/utils.go Normal file
View File

@@ -0,0 +1,11 @@
package kv
// IntInSlice returns true if a is found in the list.
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

View File

@@ -0,0 +1,14 @@
package kv
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIntInSlice(t *testing.T) {
assert.True(t, intInSlice(1, []int{1, 2, 3}))
assert.False(t, intInSlice(4, []int{1, 2, 3}))
assert.True(t, intInSlice(0, []int{0}))
assert.False(t, intInSlice(0, []int{}))
}