@@ -21,7 +21,7 @@ const (
21
21
// ExtractStatedbArtifactsAsTarbytes extracts the statedb artifacts from the code package tar and create a statedb artifact tar.
22
22
// The state db artifacts are expected to contain state db specific artifacts such as index specification in the case of couchdb.
23
23
// 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 ) {
25
25
ccpackage , err := GetChaincodeFromFS (ccname , ccversion )
26
26
if err != nil {
27
27
// 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
31
31
return false , nil , nil
32
32
}
33
33
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
+
34
43
cds := ccpackage .GetDepSpec ()
35
44
is := bytes .NewReader (cds .CodePackage )
36
45
gr , err := gzip .NewReader (is )
37
46
if err != nil {
38
47
ccproviderLogger .Errorf ("Failure opening codepackage gzip stream: %s" , err )
39
- return true , nil , err
48
+ return nil , err
40
49
}
41
50
tr := tar .NewReader (gr )
42
51
statedbTarBuffer := bytes .NewBuffer (nil )
@@ -52,25 +61,25 @@ func ExtractStatedbArtifactsAsTarbytes(ccname, ccversion string) (installed bool
52
61
}
53
62
54
63
if err != nil {
55
- return true , nil , err
64
+ return nil , err
56
65
}
57
66
ccproviderLogger .Debugf ("header.Name = %s" , header .Name )
58
67
if ! strings .HasPrefix (header .Name , ccPackageStatedbDir ) {
59
68
continue
60
69
}
61
70
if err = tw .WriteHeader (header ); err != nil {
62
71
ccproviderLogger .Error ("Error adding header to statedb tar:" , err , header .Name )
63
- return true , nil , err
72
+ return nil , err
64
73
}
65
74
if _ , err := io .Copy (tw , tr ); err != nil {
66
75
ccproviderLogger .Error ("Error copying file to statedb tar:" , err , header .Name )
67
- return true , nil , err
76
+ return nil , err
68
77
}
69
78
ccproviderLogger .Debug ("Wrote file to statedb tar:" , header .Name )
70
79
}
71
80
if err = tw .Close (); err != nil {
72
- return true , nil , err
81
+ return nil , err
73
82
}
74
83
ccproviderLogger .Debug ("Created statedb artifact tar" )
75
- return true , statedbTarBuffer .Bytes (), nil
84
+ return statedbTarBuffer .Bytes (), nil
76
85
}
0 commit comments