@@ -21,7 +21,7 @@ const (
2121// ExtractStatedbArtifactsAsTarbytes extracts the statedb artifacts from the code package tar and create a statedb artifact tar.
2222// The state db artifacts are expected to contain state db specific artifacts such as index specification in the case of couchdb.
2323// This function is intented to be used during chaincode instantiate/upgrade so that statedb artifacts can be created.
24- func ExtractStatedbArtifactsAsTarbytes (ccname , ccversion string ) (installed bool , statedbArtifactsTar []byte , err error ) {
24+ func ExtractStatedbArtifactsForChaincode (ccname , ccversion string ) (installed bool , statedbArtifactsTar []byte , err error ) {
2525 ccpackage , err := GetChaincodeFromFS (ccname , ccversion )
2626 if err != nil {
2727 // TODO for now, we assume that an error indicates that the chaincode is not installed on the peer.
@@ -31,12 +31,21 @@ func ExtractStatedbArtifactsAsTarbytes(ccname, ccversion string) (installed bool
3131 return false , nil , nil
3232 }
3333
34+ statedbArtifactsTar , err = ExtractStatedbArtifactsFromCCPackage (ccpackage )
35+ return true , statedbArtifactsTar , err
36+ }
37+
38+ // ExtractStatedbArtifactsFromCCPackage extracts the statedb artifacts from the code package tar and create a statedb artifact tar.
39+ // The state db artifacts are expected to contain state db specific artifacts such as index specification in the case of couchdb.
40+ // This function is called during chaincode instantiate/upgrade (from above), and from install, so that statedb artifacts can be created.
41+ func ExtractStatedbArtifactsFromCCPackage (ccpackage CCPackage ) (statedbArtifactsTar []byte , err error ) {
42+
3443 cds := ccpackage .GetDepSpec ()
3544 is := bytes .NewReader (cds .CodePackage )
3645 gr , err := gzip .NewReader (is )
3746 if err != nil {
3847 ccproviderLogger .Errorf ("Failure opening codepackage gzip stream: %s" , err )
39- return true , nil , err
48+ return nil , err
4049 }
4150 tr := tar .NewReader (gr )
4251 statedbTarBuffer := bytes .NewBuffer (nil )
@@ -52,25 +61,25 @@ func ExtractStatedbArtifactsAsTarbytes(ccname, ccversion string) (installed bool
5261 }
5362
5463 if err != nil {
55- return true , nil , err
64+ return nil , err
5665 }
5766 ccproviderLogger .Debugf ("header.Name = %s" , header .Name )
5867 if ! strings .HasPrefix (header .Name , ccPackageStatedbDir ) {
5968 continue
6069 }
6170 if err = tw .WriteHeader (header ); err != nil {
6271 ccproviderLogger .Error ("Error adding header to statedb tar:" , err , header .Name )
63- return true , nil , err
72+ return nil , err
6473 }
6574 if _ , err := io .Copy (tw , tr ); err != nil {
6675 ccproviderLogger .Error ("Error copying file to statedb tar:" , err , header .Name )
67- return true , nil , err
76+ return nil , err
6877 }
6978 ccproviderLogger .Debug ("Wrote file to statedb tar:" , header .Name )
7079 }
7180 if err = tw .Close (); err != nil {
72- return true , nil , err
81+ return nil , err
7382 }
7483 ccproviderLogger .Debug ("Created statedb artifact tar" )
75- return true , statedbTarBuffer .Bytes (), nil
84+ return statedbTarBuffer .Bytes (), nil
7685}
0 commit comments