@@ -66,19 +66,61 @@ type CDSPackage struct {
6666 buf []byte
6767 depSpec * pb.ChaincodeDeploymentSpec
6868 data * CDSData
69+ datab []byte
70+ id []byte
71+ }
72+
73+ // resets data
74+ func (ccpack * CDSPackage ) reset () {
75+ * ccpack = CDSPackage {}
76+ }
77+
78+ // GetId gets the fingerprint of the chaincode based on package computation
79+ func (ccpack * CDSPackage ) GetId () []byte {
80+ //this has to be after creating a package and initializing it
81+ //If those steps fail, GetId() should never be called
82+ if ccpack .id == nil {
83+ panic ("GetId called on uninitialized package" )
84+ }
85+ return ccpack .id
6986}
7087
7188// GetDepSpec gets the ChaincodeDeploymentSpec from the package
7289func (ccpack * CDSPackage ) GetDepSpec () * pb.ChaincodeDeploymentSpec {
90+ //this has to be after creating a package and initializing it
91+ //If those steps fail, GetDepSpec() should never be called
92+ if ccpack .depSpec == nil {
93+ panic ("GetDepSpec called on uninitialized package" )
94+ }
7395 return ccpack .depSpec
7496}
7597
98+ // GetDepSpecBytes gets the serialized ChaincodeDeploymentSpec from the package
99+ func (ccpack * CDSPackage ) GetDepSpecBytes () []byte {
100+ //this has to be after creating a package and initializing it
101+ //If those steps fail, GetDepSpecBytes() should never be called
102+ if ccpack .buf == nil {
103+ panic ("GetDepSpecBytes called on uninitialized package" )
104+ }
105+ return ccpack .buf
106+ }
107+
76108// GetPackageObject gets the ChaincodeDeploymentSpec as proto.Message
77109func (ccpack * CDSPackage ) GetPackageObject () proto.Message {
78110 return ccpack .depSpec
79111}
80112
81- func (ccpack * CDSPackage ) getCDSData (cds * pb.ChaincodeDeploymentSpec ) ([]byte , * CDSData , error ) {
113+ // GetChaincodeData gets the ChaincodeData
114+ func (ccpack * CDSPackage ) GetChaincodeData () * ChaincodeData {
115+ //this has to be after creating a package and initializing it
116+ //If those steps fail, GetChaincodeData() should never be called
117+ if ccpack .depSpec == nil || ccpack .datab == nil || ccpack .id == nil {
118+ panic ("GetChaincodeData called on uninitialized package" )
119+ }
120+ return & ChaincodeData {Name : ccpack .depSpec .ChaincodeSpec .ChaincodeId .Name , Version : ccpack .depSpec .ChaincodeSpec .ChaincodeId .Version , Data : ccpack .datab , Id : ccpack .id }
121+ }
122+
123+ func (ccpack * CDSPackage ) getCDSData (cds * pb.ChaincodeDeploymentSpec ) ([]byte , []byte , * CDSData , error ) {
82124 // check for nil argument. It is an assertion that getCDSData
83125 // is never called on a package that did not go through/succeed
84126 // package initialization.
@@ -88,17 +130,17 @@ func (ccpack *CDSPackage) getCDSData(cds *pb.ChaincodeDeploymentSpec) ([]byte, *
88130
89131 b , err := proto .Marshal (cds )
90132 if err != nil {
91- return nil , nil , err
133+ return nil , nil , nil , err
92134 }
93135
94136 if err = factory .InitFactories (nil ); err != nil {
95- return nil , nil , fmt .Errorf ("Internal error, BCCSP could not be initialized : %s" , err )
137+ return nil , nil , nil , fmt .Errorf ("Internal error, BCCSP could not be initialized : %s" , err )
96138 }
97139
98140 //compute hashes now
99141 hash , err := factory .GetDefault ().GetHash (& bccsp.SHAOpts {})
100142 if err != nil {
101- return nil , nil , err
143+ return nil , nil , nil , err
102144 }
103145
104146 cdsdata := & CDSData {}
@@ -116,93 +158,88 @@ func (ccpack *CDSPackage) getCDSData(cds *pb.ChaincodeDeploymentSpec) ([]byte, *
116158
117159 b , err = proto .Marshal (cdsdata )
118160 if err != nil {
119- return nil , nil , err
161+ return nil , nil , nil , err
120162 }
121163
122- return b , cdsdata , nil
164+ hash .Reset ()
165+
166+ //compute the id
167+ hash .Write (cdsdata .CodeHash )
168+ hash .Write (cdsdata .MetaDataHash )
169+
170+ id := hash .Sum (nil )
171+
172+ return b , id , cdsdata , nil
123173}
124174
125175// ValidateCC returns error if the chaincode is not found or if its not a
126176// ChaincodeDeploymentSpec
127- func (ccpack * CDSPackage ) ValidateCC (ccdata * ChaincodeData ) ( * pb. ChaincodeDeploymentSpec , error ) {
177+ func (ccpack * CDSPackage ) ValidateCC (ccdata * ChaincodeData ) error {
128178 if ccpack .depSpec == nil {
129- return nil , fmt .Errorf ("uninitialized package" )
179+ return fmt .Errorf ("uninitialized package" )
130180 }
131181
132182 if ccpack .data == nil {
133- return nil , fmt .Errorf ("nil data" )
183+ return fmt .Errorf ("nil data" )
134184 }
135185
136186 if ccdata .Name != ccpack .depSpec .ChaincodeSpec .ChaincodeId .Name || ccdata .Version != ccpack .depSpec .ChaincodeSpec .ChaincodeId .Version {
137- return nil , fmt .Errorf ("invalid chaincode data %v (%v)" , ccdata , ccpack .depSpec .ChaincodeSpec .ChaincodeId )
187+ return fmt .Errorf ("invalid chaincode data %v (%v)" , ccdata , ccpack .depSpec .ChaincodeSpec .ChaincodeId )
138188 }
139189
140190 otherdata := & CDSData {}
141191 err := proto .Unmarshal (ccdata .Data , otherdata )
142192 if err != nil {
143- return nil , err
193+ return err
144194 }
145195
146196 if ! ccpack .data .Equals (otherdata ) {
147- return nil , fmt .Errorf ("data mismatch" )
197+ return fmt .Errorf ("data mismatch" )
148198 }
149199
150- return ccpack . depSpec , nil
200+ return nil
151201}
152202
153203//InitFromBuffer sets the buffer if valid and returns ChaincodeData
154204func (ccpack * CDSPackage ) InitFromBuffer (buf []byte ) (* ChaincodeData , error ) {
155205 //incase ccpack is reused
156- ccpack .buf = nil
157- ccpack .depSpec = nil
158- ccpack .data = nil
206+ ccpack .reset ()
159207
160208 depSpec := & pb.ChaincodeDeploymentSpec {}
161209 err := proto .Unmarshal (buf , depSpec )
162210 if err != nil {
163211 return nil , fmt .Errorf ("failed to unmarshal deployment spec from bytes" )
164212 }
165213
166- databytes , data , err := ccpack .getCDSData (depSpec )
214+ databytes , id , data , err := ccpack .getCDSData (depSpec )
167215 if err != nil {
168216 return nil , err
169217 }
170218
171219 ccpack .buf = buf
172220 ccpack .depSpec = depSpec
173221 ccpack .data = data
222+ ccpack .datab = databytes
223+ ccpack .id = id
174224
175- return & ChaincodeData { Name : depSpec . ChaincodeSpec . ChaincodeId . Name , Version : depSpec . ChaincodeSpec . ChaincodeId . Version , Data : databytes } , nil
225+ return ccpack . GetChaincodeData () , nil
176226}
177227
178228//InitFromFS returns the chaincode and its package from the file system
179229func (ccpack * CDSPackage ) InitFromFS (ccname string , ccversion string ) ([]byte , * pb.ChaincodeDeploymentSpec , error ) {
180230 //incase ccpack is reused
181- ccpack .buf = nil
182- ccpack .depSpec = nil
183- ccpack .data = nil
231+ ccpack .reset ()
184232
185233 buf , err := GetChaincodePackage (ccname , ccversion )
186234 if err != nil {
187235 return nil , nil , err
188236 }
189237
190- depSpec := & pb.ChaincodeDeploymentSpec {}
191- err = proto .Unmarshal (buf , depSpec )
192- if err != nil {
193- return nil , nil , fmt .Errorf ("failed to unmarshal fs deployment spec for %s, %s" , ccname , ccversion )
194- }
195-
196- _ , data , err := ccpack .getCDSData (depSpec )
197- if err != nil {
238+ if _ , err = ccpack .InitFromBuffer (buf ); err != nil {
198239 return nil , nil , err
199240 }
200241
201- ccpack .buf = buf
202- ccpack .depSpec = depSpec
203- ccpack .data = data
204-
205- return buf , depSpec , nil
242+ return ccpack .buf , ccpack .depSpec , nil
206243}
207244
208245//PutChaincodeToFS - serializes chaincode to a package on the file system
@@ -211,6 +248,10 @@ func (ccpack *CDSPackage) PutChaincodeToFS() error {
211248 return fmt .Errorf ("uninitialized package" )
212249 }
213250
251+ if ccpack .id == nil {
252+ return fmt .Errorf ("id cannot be nil if buf is not nil" )
253+ }
254+
214255 if ccpack .depSpec == nil {
215256 return fmt .Errorf ("depspec cannot be nil if buf is not nil" )
216257 }
@@ -219,6 +260,10 @@ func (ccpack *CDSPackage) PutChaincodeToFS() error {
219260 return fmt .Errorf ("nil data" )
220261 }
221262
263+ if ccpack .datab == nil {
264+ return fmt .Errorf ("nil data bytes" )
265+ }
266+
222267 ccname := ccpack .depSpec .ChaincodeSpec .ChaincodeId .Name
223268 ccversion := ccpack .depSpec .ChaincodeSpec .ChaincodeId .Version
224269
0 commit comments