Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[indexer] order results by index if height is the same (#2900)
Browse files Browse the repository at this point in the history
Fixes #2775
  • Loading branch information
melekes authored and ebuchman committed Nov 27, 2018
1 parent 9570ac4 commit 94e63be
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion state/txindex/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
17 changes: 15 additions & 2 deletions state/txindex/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down

0 comments on commit 94e63be

Please sign in to comment.