Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAB-18509] Stop panic if collection index path is wrong (backport #2726) #2744

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
}
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