Skip to content

Commit

Permalink
Bugfix in collection config history mgr (#1904)
Browse files Browse the repository at this point in the history
Use proper stop key to prevent iterator returning results from next namespace

Signed-off-by: manish <manish.sethi@gmail.com>
(cherry picked from commit d67feb1)

Co-authored-by: manish <manish.sethi@gmail.com>
  • Loading branch information
mergify[bot] and manish-sethi authored Sep 18, 2020
1 parent 3ee47a7 commit f32cb81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion core/ledger/confighistory/db_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ func (d *db) mostRecentEntryBelow(blockNum uint64, ns, key string) (*compositeKV
if blockNum == 0 {
return nil, errors.New("blockNum should be greater than 0")
}

startKey := encodeCompositeKey(ns, key, blockNum-1)
itr, err := d.GetIterator(startKey, nil)
stopKey := append(encodeCompositeKey(ns, key, 0), byte(0))

itr, err := d.GetIterator(startKey, stopKey)
if err != nil {
return nil, err
}
Expand Down
18 changes: 12 additions & 6 deletions core/ledger/confighistory/db_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,25 @@ func TestQueries(t *testing.T) {
{&compositeKey{ns: "ns1", key: "key1", blockNum: 20}, []byte("val1_20")},
{&compositeKey{ns: "ns1", key: "key1", blockNum: 10}, []byte("val1_10")},
{&compositeKey{ns: "ns1", key: "key1", blockNum: 0}, []byte("val1_0")},
{&compositeKey{ns: "ns2", key: "key2", blockNum: 200}, []byte("val200")},
{&compositeKey{ns: "ns3", key: "key3", blockNum: 300}, []byte("val300")},
{&compositeKey{ns: "ns3", key: "key4", blockNum: 400}, []byte("val400")},
}
populateDBWithSampleData(t, db, sampleData)
// access most recent entry below ht=[45] - expected item is the one committed at ht = 40
checkRecentEntryBelow(t, "testcase-query2", db, "ns1", "key1", 45, sampleData[0])
checkRecentEntryBelow(t, "testcase-query3", db, "ns1", "key1", 35, sampleData[1])
checkRecentEntryBelow(t, "testcase-query4", db, "ns1", "key1", 30, sampleData[2])
checkRecentEntryBelow(t, "testcase-query5", db, "ns1", "key1", 10, sampleData[4])

checkEntryAt(t, "testcase-query6", db, "ns1", "key1", 40, sampleData[0])
checkEntryAt(t, "testcase-query7", db, "ns1", "key1", 30, sampleData[1])
checkEntryAt(t, "testcase-query8", db, "ns1", "key1", 0, sampleData[4])
checkEntryAt(t, "testcase-query9", db, "ns1", "key1", 35, nil)
checkEntryAt(t, "testcase-query10", db, "ns1", "key1", 45, nil)
checkRecentEntryBelow(t, "testcase-query6", db, "ns2", "key2", 2000, sampleData[5])
checkRecentEntryBelow(t, "testcase-query7", db, "ns2", "key2", 200, nil)
checkRecentEntryBelow(t, "testcase-query8", db, "ns3", "key3", 299, nil)

checkEntryAt(t, "testcase-query9", db, "ns1", "key1", 40, sampleData[0])
checkEntryAt(t, "testcase-query10", db, "ns1", "key1", 30, sampleData[1])
checkEntryAt(t, "testcase-query11", db, "ns1", "key1", 0, sampleData[4])
checkEntryAt(t, "testcase-query12", db, "ns1", "key1", 35, nil)
checkEntryAt(t, "testcase-query13", db, "ns1", "key1", 45, nil)

t.Run("test-iter-error-path", func(t *testing.T) {
provider.Close()
Expand Down

0 comments on commit f32cb81

Please sign in to comment.