From 6b55a95c1575ea8dba402f522e3b3eb54f9213f6 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 09651faa297..bc9910fcac1 100644 --- a/core/ledger/kvledger/txmgmt/privacyenabledstate/db.go +++ b/core/ledger/kvledger/txmgmt/privacyenabledstate/db.go @@ -408,27 +408,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 84da4bbb1f8..f8a7028fe67 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) }