Skip to content

Commit

Permalink
[FAB-18509] Stop panic of collection index path is wrong (#2726)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
(cherry picked from commit 1249da2)
  • Loading branch information
mbwhite authored and mergify-bot committed Jul 7, 2021
1 parent 6727a63 commit 99392f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
21 changes: 12 additions & 9 deletions core/ledger/kvledger/txmgmt/privacyenabledstate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 9 additions & 5 deletions core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
}
Expand Down

0 comments on commit 99392f8

Please sign in to comment.