|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +/* |
| 17 | +Notice: This file has been modified for Hyperledger Fabric SDK Go usage. |
| 18 | +Please review third_party pinning scripts and patches for more details. |
| 19 | +*/ |
| 20 | + |
| 21 | +package ccprovider |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + "github.com/golang/protobuf/proto" |
| 27 | + "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/core/ledger" |
| 28 | + flogging "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge" |
| 29 | + pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer" |
| 30 | +) |
| 31 | + |
| 32 | +var ccproviderLogger = flogging.MustGetLogger("ccprovider") |
| 33 | + |
| 34 | +var chaincodeInstallPath string |
| 35 | + |
| 36 | +//CCPackage encapsulates a chaincode package which can be |
| 37 | +// raw ChaincodeDeploymentSpec |
| 38 | +// SignedChaincodeDeploymentSpec |
| 39 | +// Attempt to keep the interface at a level with minimal |
| 40 | +// interface for possible generalization. |
| 41 | +type CCPackage interface { |
| 42 | + //InitFromBuffer initialize the package from bytes |
| 43 | + InitFromBuffer(buf []byte) (*ChaincodeData, error) |
| 44 | + |
| 45 | + // InitFromFS gets the chaincode from the filesystem (includes the raw bytes too) |
| 46 | + InitFromFS(ccname string, ccversion string) ([]byte, *pb.ChaincodeDeploymentSpec, error) |
| 47 | + |
| 48 | + // PutChaincodeToFS writes the chaincode to the filesystem |
| 49 | + PutChaincodeToFS() error |
| 50 | + |
| 51 | + // GetDepSpec gets the ChaincodeDeploymentSpec from the package |
| 52 | + GetDepSpec() *pb.ChaincodeDeploymentSpec |
| 53 | + |
| 54 | + // GetDepSpecBytes gets the serialized ChaincodeDeploymentSpec from the package |
| 55 | + GetDepSpecBytes() []byte |
| 56 | + |
| 57 | + // ValidateCC validates and returns the chaincode deployment spec corresponding to |
| 58 | + // ChaincodeData. The validation is based on the metadata from ChaincodeData |
| 59 | + // One use of this method is to validate the chaincode before launching |
| 60 | + ValidateCC(ccdata *ChaincodeData) error |
| 61 | + |
| 62 | + // GetPackageObject gets the object as a proto.Message |
| 63 | + GetPackageObject() proto.Message |
| 64 | + |
| 65 | + // GetChaincodeData gets the ChaincodeData |
| 66 | + GetChaincodeData() *ChaincodeData |
| 67 | + |
| 68 | + // GetId gets the fingerprint of the chaincode based on package computation |
| 69 | + GetId() []byte |
| 70 | +} |
| 71 | + |
| 72 | +type CCCacheSupport interface { |
| 73 | + //GetChaincode is needed by the cache to get chaincode data |
| 74 | + GetChaincode(ccname string, ccversion string) (CCPackage, error) |
| 75 | +} |
| 76 | + |
| 77 | +// CCInfoFSImpl provides the implementation for CC on the FS and the access to it |
| 78 | +// It implements CCCacheSupport |
| 79 | +type CCInfoFSImpl struct{} |
| 80 | + |
| 81 | +// The following lines create the cache of CCPackage data that sits |
| 82 | +// on top of the file system and avoids a trip to the file system |
| 83 | +// every time. The cache is disabled by default and only enabled |
| 84 | +// if EnableCCInfoCache is called. This is an unfortunate hack |
| 85 | +// required by some legacy tests that remove chaincode packages |
| 86 | +// from the file system as a means of simulating particular test |
| 87 | +// conditions. This way of testing is incompatible with the |
| 88 | +// immutable nature of chaincode packages that is assumed by hlf v1 |
| 89 | +// and implemented by this cache. For this reason, tests are for now |
| 90 | +// allowed to run with the cache disabled (unless they enable it) |
| 91 | +// until a later time in which they are fixed. The peer process on |
| 92 | +// the other hand requires the benefits of this cache and therefore |
| 93 | +// enables it. |
| 94 | +// TODO: (post v1) enable cache by default as soon as https://jira.hyperledger.org/browse/FAB-3785 is completed |
| 95 | + |
| 96 | +// ccInfoFSStorageMgr is the storage manager used either by the cache or if the |
| 97 | +// cache is bypassed |
| 98 | +var ccInfoFSProvider = &CCInfoFSImpl{} |
| 99 | + |
| 100 | +// ccInfoCache is the cache instance itself |
| 101 | + |
| 102 | +// ccInfoCacheEnabled keeps track of whether the cache is enable |
| 103 | +// (it is disabled by default) |
| 104 | +var ccInfoCacheEnabled bool |
| 105 | + |
| 106 | +//CCContext pass this around instead of string of args |
| 107 | +type CCContext struct { |
| 108 | + //ChainID chain id |
| 109 | + ChainID string |
| 110 | + |
| 111 | + //Name chaincode name |
| 112 | + Name string |
| 113 | + |
| 114 | + //Version used to construct the chaincode image and register |
| 115 | + Version string |
| 116 | + |
| 117 | + //TxID is the transaction id for the proposal (if any) |
| 118 | + TxID string |
| 119 | + |
| 120 | + //Syscc is this a system chaincode |
| 121 | + Syscc bool |
| 122 | + |
| 123 | + //SignedProposal for this invoke (if any) |
| 124 | + //this is kept here for access control and in case we need to pass something |
| 125 | + //from this to the chaincode |
| 126 | + SignedProposal *pb.SignedProposal |
| 127 | + |
| 128 | + //Proposal for this invoke (if any) |
| 129 | + //this is kept here just in case we need to pass something |
| 130 | + //from this to the chaincode |
| 131 | + Proposal *pb.Proposal |
| 132 | + |
| 133 | + //this is not set but computed (note that this is not exported. use GetCanonicalName) |
| 134 | + canonicalName string |
| 135 | + |
| 136 | + // this is additional data passed to the chaincode |
| 137 | + ProposalDecorations map[string][]byte |
| 138 | +} |
| 139 | + |
| 140 | +//-------- ChaincodeData is stored on the LSCC ------- |
| 141 | + |
| 142 | +//ChaincodeData defines the datastructure for chaincodes to be serialized by proto |
| 143 | +//Type provides an additional check by directing to use a specific package after instantiation |
| 144 | +//Data is Type specifc (see CDSPackage and SignedCDSPackage) |
| 145 | +type ChaincodeData struct { |
| 146 | + //Name of the chaincode |
| 147 | + Name string `protobuf:"bytes,1,opt,name=name"` |
| 148 | + |
| 149 | + //Version of the chaincode |
| 150 | + Version string `protobuf:"bytes,2,opt,name=version"` |
| 151 | + |
| 152 | + //Escc for the chaincode instance |
| 153 | + Escc string `protobuf:"bytes,3,opt,name=escc"` |
| 154 | + |
| 155 | + //Vscc for the chaincode instance |
| 156 | + Vscc string `protobuf:"bytes,4,opt,name=vscc"` |
| 157 | + |
| 158 | + //Policy endorsement policy for the chaincode instance |
| 159 | + Policy []byte `protobuf:"bytes,5,opt,name=policy,proto3"` |
| 160 | + |
| 161 | + //Data data specific to the package |
| 162 | + Data []byte `protobuf:"bytes,6,opt,name=data,proto3"` |
| 163 | + |
| 164 | + //Id of the chaincode that's the unique fingerprint for the CC |
| 165 | + //This is not currently used anywhere but serves as a good |
| 166 | + //eyecatcher |
| 167 | + Id []byte `protobuf:"bytes,7,opt,name=id,proto3"` |
| 168 | + |
| 169 | + //InstantiationPolicy for the chaincode |
| 170 | + InstantiationPolicy []byte `protobuf:"bytes,8,opt,name=instantiation_policy,proto3"` |
| 171 | +} |
| 172 | + |
| 173 | +//implement functions needed from proto.Message for proto's mar/unmarshal functions |
| 174 | + |
| 175 | +//Reset resets |
| 176 | +func (cd *ChaincodeData) Reset() { *cd = ChaincodeData{} } |
| 177 | + |
| 178 | +//String converts to string |
| 179 | +func (cd *ChaincodeData) String() string { return proto.CompactTextString(cd) } |
| 180 | + |
| 181 | +//ProtoMessage just exists to make proto happy |
| 182 | +func (*ChaincodeData) ProtoMessage() {} |
| 183 | + |
| 184 | +// ChaincodeProvider provides an abstraction layer that is |
| 185 | +// used for different packages to interact with code in the |
| 186 | +// chaincode package without importing it; more methods |
| 187 | +// should be added below if necessary |
| 188 | +type ChaincodeProvider interface { |
| 189 | + // GetContext returns a ledger context and a tx simulator; it's the |
| 190 | + // caller's responsability to release the simulator by calling its |
| 191 | + // done method once it is no longer useful |
| 192 | + GetContext(ledger ledger.PeerLedger, txid string) (context.Context, ledger.TxSimulator, error) |
| 193 | + // GetCCContext returns an opaque chaincode context |
| 194 | + GetCCContext(cid, name, version, txid string, syscc bool, signedProp *pb.SignedProposal, prop *pb.Proposal) interface{} |
| 195 | + // GetCCValidationInfoFromLSCC returns the VSCC and the policy listed by LSCC for the supplied chaincode |
| 196 | + GetCCValidationInfoFromLSCC(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (string, []byte, error) |
| 197 | + // ExecuteChaincode executes the chaincode given context and args |
| 198 | + ExecuteChaincode(ctxt context.Context, cccid interface{}, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) |
| 199 | + // Execute executes the chaincode given context and spec (invocation or deploy) |
| 200 | + Execute(ctxt context.Context, cccid interface{}, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) |
| 201 | + // ExecuteWithErrorFilter executes the chaincode given context and spec and returns payload |
| 202 | + ExecuteWithErrorFilter(ctxt context.Context, cccid interface{}, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) |
| 203 | + // Stop stops the chaincode given context and deployment spec |
| 204 | + Stop(ctxt context.Context, cccid interface{}, spec *pb.ChaincodeDeploymentSpec) error |
| 205 | +} |
| 206 | + |
| 207 | +var ccFactory ChaincodeProviderFactory |
| 208 | + |
| 209 | +// ChaincodeProviderFactory defines a factory interface so |
| 210 | +// that the actual implementation can be injected |
| 211 | +type ChaincodeProviderFactory interface { |
| 212 | + NewChaincodeProvider() ChaincodeProvider |
| 213 | +} |
0 commit comments