diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go index 8199dd054a..e6d5d164ec 100644 --- a/state/txindex/kv/kv.go +++ b/state/txindex/kv/kv.go @@ -382,7 +382,11 @@ func (txi *TxIndex) setTmpHashes(tmpHeights map[string][]byte, it dbm.Iterator, eventSeq := extractEventSeqFromKey(it.Key()) tmpHeights[string(it.Value())+eventSeq] = it.Value() } else { - tmpHeights[string(it.Value())] = it.Value() + // Copy it.Value() to ensure tmpHeights stores independent values, as iterators reuse + // the same memory for it.Value(), causing overwrites on each iteration. + valueCopy := make([]byte, len(it.Value())) + copy(valueCopy, it.Value()) + tmpHeights[string(it.Value())] = valueCopy } } diff --git a/state/txindex/kv/kv_test.go b/state/txindex/kv/kv_test.go index e2e4eeecdb..d805cc3c8b 100644 --- a/state/txindex/kv/kv_test.go +++ b/state/txindex/kv/kv_test.go @@ -692,8 +692,18 @@ func TestTxSearchMultipleTxs(t *testing.T) { results, err := indexer.Search(ctx, query.MustParse("account.number >= 1")) assert.NoError(t, err) - require.Len(t, results, 3) + + // since two txs were added at height 1 and 2, we should have two unique transactions + // for both heights + results, err = indexer.Search(ctx, query.MustParse("tx.height=1")) + assert.NoError(t, err) + require.Len(t, results, 2) + + results, err = indexer.Search(ctx, query.MustParse("tx.height=2")) + assert.NoError(t, err) + require.Len(t, results, 2) + } func txResultWithEvents(events []abci.Event) *abci.TxResult {