From 99392f8505832667128f346b83ee7b48c76d6c36 Mon Sep 17 00:00:00 2001 From: Matthew B White Date: Fri, 2 Jul 2021 21:37:35 +0100 Subject: [PATCH] [FAB-18509] Stop panic of collection index path is wrong (#2726) Signed-off-by: Matthew B White (cherry picked from commit 1249da2598549b3a7b03bceb0c29c19664c24c25) --- .../kvledger/txmgmt/privacyenabledstate/db.go | 21 +++++++++++-------- .../txmgmt/privacyenabledstate/db_test.go | 14 ++++++++----- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/core/ledger/kvledger/txmgmt/privacyenabledstate/db.go b/core/ledger/kvledger/txmgmt/privacyenabledstate/db.go index 41aa4c246c0..d3944e11896 100644 --- a/core/ledger/kvledger/txmgmt/privacyenabledstate/db.go +++ b/core/ledger/kvledger/txmgmt/privacyenabledstate/db.go @@ -420,27 +420,30 @@ type indexInfo struct { const ( // Example for chaincode indexes: - // "META-INF/statedb/couchdb/indexes/indexColorSortName.json" + // "META-INF/statedb/couchdb/indexes" chaincodeIndexDirDepth = 3 + // Example for collection scoped indexes: - // "META-INF/statedb/couchdb/collections/collectionMarbles/indexes/indexCollMarbles.json" + // "META-INF/statedb/couchdb/collections/collectionMarbles/indexes" collectionDirDepth = 3 collectionNameDepth = 4 collectionIndexDirDepth = 5 ) +// Note previous functions will have ensured that the path starts +// with 'META-INF/statedb' and does not have leading or trailing +// path deliminators. func getIndexInfo(indexPath string) *indexInfo { indexInfo := &indexInfo{} - dirsDepth := strings.Split(indexPath, "/") + pathParts := strings.Split(indexPath, "/") + pathDepth := len(pathParts) + switch { - case len(dirsDepth) > chaincodeIndexDirDepth && - dirsDepth[chaincodeIndexDirDepth] == "indexes": + case pathDepth > chaincodeIndexDirDepth && pathParts[chaincodeIndexDirDepth] == "indexes": indexInfo.hasIndexForChaincode = true - case len(dirsDepth) > collectionDirDepth && - dirsDepth[collectionDirDepth] == "collections" && - dirsDepth[collectionIndexDirDepth] == "indexes": + case pathDepth > collectionIndexDirDepth && pathParts[collectionDirDepth] == "collections" && pathParts[collectionIndexDirDepth] == "indexes": indexInfo.hasIndexForCollection = true - indexInfo.collectionName = dirsDepth[collectionNameDepth] + indexInfo.collectionName = pathParts[collectionNameDepth] } return indexInfo } diff --git a/core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go b/core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go index a30b7247320..866d1058324 100644 --- a/core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go +++ b/core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go @@ -49,7 +49,7 @@ func TestHealthCheckRegister(t *testing.T) { } func TestGetIndexInfo(t *testing.T) { - chaincodeIndexPath := "META-INF/statedb/couchdb/indexes/indexColorSortName.json" + chaincodeIndexPath := "META-INF/statedb/couchdb/indexes" actualIndexInfo := getIndexInfo(chaincodeIndexPath) expectedIndexInfo := &indexInfo{ hasIndexForChaincode: true, @@ -58,7 +58,7 @@ func TestGetIndexInfo(t *testing.T) { } require.Equal(t, expectedIndexInfo, actualIndexInfo) - collectionIndexPath := "META-INF/statedb/couchdb/collections/collectionMarbles/indexes/indexCollMarbles.json" + collectionIndexPath := "META-INF/statedb/couchdb/collections/collectionMarbles/indexes" actualIndexInfo = getIndexInfo(collectionIndexPath) expectedIndexInfo = &indexInfo{ hasIndexForChaincode: false, @@ -67,7 +67,7 @@ func TestGetIndexInfo(t *testing.T) { } require.Equal(t, expectedIndexInfo, actualIndexInfo) - incorrectChaincodeIndexPath := "META-INF/statedb/couchdb/indexColorSortName.json" + incorrectChaincodeIndexPath := "META-INF/statedb/couchdb" actualIndexInfo = getIndexInfo(incorrectChaincodeIndexPath) expectedIndexInfo = &indexInfo{ hasIndexForChaincode: false, @@ -76,11 +76,15 @@ func TestGetIndexInfo(t *testing.T) { } require.Equal(t, expectedIndexInfo, actualIndexInfo) - incorrectCollectionIndexPath := "META-INF/statedb/couchdb/collections/indexes/indexCollMarbles.json" + incorrectCollectionIndexPath := "META-INF/statedb/couchdb/collections/indexes" actualIndexInfo = getIndexInfo(incorrectCollectionIndexPath) require.Equal(t, expectedIndexInfo, actualIndexInfo) - incorrectIndexPath := "META-INF/statedb/" + incorrectCollectionIndexPath = "META-INF/statedb/couchdb/collections" + actualIndexInfo = getIndexInfo(incorrectCollectionIndexPath) + require.Equal(t, expectedIndexInfo, actualIndexInfo) + + incorrectIndexPath := "META-INF/statedb" actualIndexInfo = getIndexInfo(incorrectIndexPath) require.Equal(t, expectedIndexInfo, actualIndexInfo) }