From 94e63be922d48869dd670059bcf619865ac8079e Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Tue, 27 Nov 2018 16:53:06 +0400 Subject: [PATCH] [indexer] order results by index if height is the same (#2900) Fixes #2775 --- CHANGELOG_PENDING.md | 1 + state/txindex/kv/kv.go | 5 ++++- state/txindex/kv/kv_test.go | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index e1215f79e..1bc1910a8 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -37,5 +37,6 @@ program](https://hackerone.com/tendermint). - [blockchain] \#2731 Retry both blocks if either is bad to avoid getting stuck during fast sync (@goolAdapter) - [log] \#2868 fix module=main setting overriding all others - [rpc] \#2808 RPC validators calls IncrementAccum if necessary +- [kv indexer] \#2775 order results by index if height is the same - [rpc] \#2759 fix tx.height range queries - [rpc] \#2811 Allow integer IDs in JSON-RPC requests \ No newline at end of file diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go index 1137853c2..6082316bf 100644 --- a/state/txindex/kv/kv.go +++ b/state/txindex/kv/kv.go @@ -207,8 +207,11 @@ func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) { i++ } - // sort by height by default + // sort by height & index by default sort.Slice(results, func(i, j int) bool { + if results[i].Height == results[j].Height { + return results[i].Index < results[j].Index + } return results[i].Height < results[j].Height }) diff --git a/state/txindex/kv/kv_test.go b/state/txindex/kv/kv_test.go index 78a76168d..6657e5429 100644 --- a/state/txindex/kv/kv_test.go +++ b/state/txindex/kv/kv_test.go @@ -133,6 +133,7 @@ func TestTxSearchMultipleTxs(t *testing.T) { }) txResult.Tx = types.Tx("Bob's account") txResult.Height = 2 + txResult.Index = 1 err := indexer.Index(txResult) require.NoError(t, err) @@ -142,14 +143,26 @@ func TestTxSearchMultipleTxs(t *testing.T) { }) txResult2.Tx = types.Tx("Alice's account") txResult2.Height = 1 + txResult2.Index = 2 + err = indexer.Index(txResult2) require.NoError(t, err) + // indexed third (to test the order of transactions) + txResult3 := txResultWithTags([]cmn.KVPair{ + {Key: []byte("account.number"), Value: []byte("3")}, + }) + txResult3.Tx = types.Tx("Jack's account") + txResult3.Height = 1 + txResult3.Index = 1 + err = indexer.Index(txResult3) + require.NoError(t, err) + results, err := indexer.Search(query.MustParse("account.number >= 1")) assert.NoError(t, err) - require.Len(t, results, 2) - assert.Equal(t, []*types.TxResult{txResult2, txResult}, results) + require.Len(t, results, 3) + assert.Equal(t, []*types.TxResult{txResult3, txResult2, txResult}, results) } func TestIndexAllTags(t *testing.T) {