-
Notifications
You must be signed in to change notification settings - Fork 1
/
legacy_lifecycle.go
148 lines (95 loc) · 3.69 KB
/
legacy_lifecycle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lifecycle
import (
"github.com/mcc-github/blockchain/core/common/ccprovider"
"github.com/mcc-github/blockchain/core/ledger"
"github.com/mcc-github/blockchain/core/scc"
"github.com/pkg/errors"
)
type LegacyLifecycle interface {
ChaincodeDefinition(channelID, chaincodeName string, qe ledger.SimpleQueryExecutor) (ccprovider.ChaincodeDefinition, error)
}
type ChaincodeInfoCache interface {
ChaincodeInfo(channelID, chaincodeName string) (definition *LocalChaincodeInfo, err error)
}
type LegacyDefinition struct {
Version string
EndorsementPlugin string
RequiresInitField bool
ChaincodeIDField string
}
func (ld *LegacyDefinition) CCVersion() string {
return ld.Version
}
func (ld *LegacyDefinition) Endorsement() string {
return ld.EndorsementPlugin
}
func (ld *LegacyDefinition) RequiresInit() bool {
return ld.RequiresInitField
}
func (ld *LegacyDefinition) ChaincodeID() string {
return ld.ChaincodeIDField
}
type ChaincodeEndorsementInfo struct {
Resources *Resources
Cache ChaincodeInfoCache
LegacyImpl LegacyLifecycle
BuiltinSCCs scc.BuiltinSCCs
}
func (cei *ChaincodeEndorsementInfo) CachedChaincodeInfo(channelID, chaincodeName string, qe ledger.SimpleQueryExecutor) (*LocalChaincodeInfo, bool, error) {
var qes ReadableState = &SimpleQueryExecutorShim{
Namespace: LifecycleNamespace,
SimpleQueryExecutor: qe,
}
if qe == nil {
qes = &DummyQueryExecutorShim{}
}
currentSequence, err := cei.Resources.Serializer.DeserializeFieldAsInt64(NamespacesName, chaincodeName, "Sequence", qes)
if err != nil {
return nil, false, errors.WithMessagef(err, "could not get current sequence for chaincode '%s' on channel '%s'", chaincodeName, channelID)
}
if currentSequence == 0 {
return nil, false, nil
}
chaincodeInfo, err := cei.Cache.ChaincodeInfo(channelID, chaincodeName)
if err != nil {
return nil, false, errors.WithMessage(err, "could not get approved chaincode info from cache")
}
if chaincodeInfo.Definition.Sequence != currentSequence {
return nil, false, errors.Errorf("chaincode cache at sequence %d but current sequence is %d, chaincode definition for '%s' changed during invoke", chaincodeInfo.Definition.Sequence, currentSequence, chaincodeName)
}
if !chaincodeInfo.Approved {
return nil, false, errors.Errorf("chaincode definition for '%s' at sequence %d on channel '%s' has not yet been approved by this org", chaincodeName, currentSequence, channelID)
}
if chaincodeInfo.InstallInfo == nil {
return nil, false, errors.Errorf("chaincode definition for '%s' exists, but chaincode is not installed", chaincodeName)
}
return chaincodeInfo, true, nil
}
func (cei *ChaincodeEndorsementInfo) ChaincodeDefinition(channelID, chaincodeName string, qe ledger.SimpleQueryExecutor) (ccprovider.ChaincodeDefinition, error) {
if cei.BuiltinSCCs.IsSysCC(chaincodeName) {
return &LegacyDefinition{
Version: scc.SysCCVersion,
EndorsementPlugin: "escc",
RequiresInitField: false,
ChaincodeIDField: scc.ChaincodeID(chaincodeName),
}, nil
}
chaincodeInfo, ok, err := cei.CachedChaincodeInfo(channelID, chaincodeName, qe)
if err != nil {
return nil, err
}
if !ok {
return cei.LegacyImpl.ChaincodeDefinition(channelID, chaincodeName, qe)
}
chaincodeDefinition := chaincodeInfo.Definition
return &LegacyDefinition{
Version: chaincodeDefinition.EndorsementInfo.Version,
EndorsementPlugin: chaincodeDefinition.EndorsementInfo.EndorsementPlugin,
RequiresInitField: chaincodeDefinition.EndorsementInfo.InitRequired,
ChaincodeIDField: string(chaincodeInfo.InstallInfo.PackageID),
}, nil
}