Skip to content

Commit 0742ff3

Browse files
author
Jason Yellick
committed
FAB-13838 DeployedCCInfoProvider.UpdatedChaincodes
This implements another piece of the DeployedCCInfoProvider. Change-Id: I0b320476724b2d4bcb31c8a7b2441229509f22b7 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent f56656d commit 0742ff3

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

core/chaincode/lifecycle/deployedcc_infoprovider.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,44 @@ SPDX-License-Identifier: Apache-2.0
77
package lifecycle
88

99
import (
10+
"regexp"
11+
1012
"github.com/hyperledger/fabric/core/ledger"
1113
cb "github.com/hyperledger/fabric/protos/common"
1214
"github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
15+
16+
"github.com/pkg/errors"
1317
)
1418

1519
// Namespaces returns the list of namespaces which are relevant to chaincode lifecycle
1620
func (l *Lifecycle) Namespaces() []string {
1721
return append([]string{LifecycleNamespace}, l.LegacyDeployedCCInfoProvider.Namespaces()...)
1822
}
1923

24+
var SequenceMatcher = regexp.MustCompile("^" + NamespacesName + "/fields/([^/]+)/Sequence$")
25+
2026
// UpdatedChaincodes returns the chaincodes that are getting updated by the supplied 'stateUpdates'
2127
func (l *Lifecycle) UpdatedChaincodes(stateUpdates map[string][]*kvrwset.KVWrite) ([]*ledger.ChaincodeLifecycleInfo, error) {
22-
return l.LegacyDeployedCCInfoProvider.UpdatedChaincodes(stateUpdates)
28+
lifecycleInfo := []*ledger.ChaincodeLifecycleInfo{}
29+
30+
// If the lifecycle table was updated, report only modified chaincodes
31+
lifecycleUpdates := stateUpdates[LifecycleNamespace]
32+
33+
for _, kvWrite := range lifecycleUpdates {
34+
matches := SequenceMatcher.FindStringSubmatch(kvWrite.Key)
35+
if len(matches) != 2 {
36+
continue
37+
}
38+
// XXX Note, this may not be a chaincode namespace, handle this later
39+
lifecycleInfo = append(lifecycleInfo, &ledger.ChaincodeLifecycleInfo{Name: matches[1]})
40+
}
41+
42+
legacyUpdates, err := l.LegacyDeployedCCInfoProvider.UpdatedChaincodes(stateUpdates)
43+
if err != nil {
44+
return nil, errors.WithMessage(err, "error invoking legacy deployed cc info provider")
45+
}
46+
47+
return append(lifecycleInfo, legacyUpdates...), nil
2348
}
2449

2550
// ChaincodeInfo implements function in interface ledger.DeployedChaincodeInfoProvider and returns info about a deployed chaincode

core/chaincode/lifecycle/deployedcc_infoprovider_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,43 @@ var _ = Describe("Lifecycle", func() {
5656

5757
BeforeEach(func() {
5858
updates = map[string][]*kvrwset.KVWrite{
59-
"key1": nil,
60-
"key2": nil,
59+
"+lifecycle": {
60+
{Key: "some/random/value"},
61+
{Key: "namespaces/fields/cc-name/Sequence"},
62+
{Key: "prefix/namespaces/fields/cc-name/Sequence"},
63+
{Key: "namespaces/fields/Sequence/infix"},
64+
{Key: "namespaces/fields/cc-name/Sequence/Postfix"},
65+
},
66+
"other-namespace": nil,
6167
}
6268
fakeLegacyProvider.UpdatedChaincodesReturns([]*ledger.ChaincodeLifecycleInfo{
6369
{Name: "foo"},
6470
{Name: "bar"},
65-
}, fmt.Errorf("updated-chaincodes-error"))
71+
}, nil)
6672
})
6773

68-
It("passes through to the legacy impl", func() {
74+
It("checks its own namespace, then passes through to the legacy impl", func() {
6975
res, err := l.UpdatedChaincodes(updates)
7076
Expect(res).To(Equal([]*ledger.ChaincodeLifecycleInfo{
77+
{Name: "cc-name"},
7178
{Name: "foo"},
7279
{Name: "bar"},
7380
}))
74-
Expect(err).To(MatchError("updated-chaincodes-error"))
81+
Expect(err).NotTo(HaveOccurred())
7582
Expect(fakeLegacyProvider.UpdatedChaincodesCallCount()).To(Equal(1))
7683
Expect(fakeLegacyProvider.UpdatedChaincodesArgsForCall(0)).To(Equal(updates))
7784
})
85+
86+
Context("when the legacy provider returns an error", func() {
87+
BeforeEach(func() {
88+
fakeLegacyProvider.UpdatedChaincodesReturns(nil, fmt.Errorf("legacy-error"))
89+
})
90+
91+
It("wraps and returns the error", func() {
92+
_, err := l.UpdatedChaincodes(updates)
93+
Expect(err).To(MatchError("error invoking legacy deployed cc info provider: legacy-error"))
94+
})
95+
})
7896
})
7997

8098
Describe("ChaincodeInfo", func() {

0 commit comments

Comments
 (0)