Skip to content

Commit

Permalink
[FAB-9059] move current metadata code to use platfrom
Browse files Browse the repository at this point in the history
Follow through moving of metadata to platform with moving
the code that uses metadata to use the platform abstraction.

Change-Id: Ibdd3b8e194ff508f7eeb420c50bc548074072478
Signed-off-by: Srinivasan Muralidharan <srinivasan.muralidharan99@gmail.com>
  • Loading branch information
muralisrini committed Apr 6, 2018
1 parent f791a37 commit 73c6759
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package metadata
package ccmetadata

import (
"encoding/json"
"fmt"
"path/filepath"
"reflect"
"strings"

"github.com/hyperledger/fabric/common/flogging"
)

var logger = flogging.MustGetLogger("metadata")

// fileValidators are used as handlers to validate specific metadata directories
type fileValidator func(fileName string, fileBytes []byte) error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package metadata
package ccmetadata

import (
"io/ioutil"
Expand Down
7 changes: 1 addition & 6 deletions core/chaincode/platforms/golang/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/hyperledger/fabric/common/metadata"
"github.com/hyperledger/fabric/core/chaincode/platforms/ccmetadata"
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
ccprovmetadata "github.com/hyperledger/fabric/core/common/ccprovider/metadata"
cutil "github.com/hyperledger/fabric/core/container/util"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
Expand Down Expand Up @@ -461,11 +460,7 @@ func (goPlatform *Platform) GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte
// Validate metadata file for inclusion in tar
// Validation is based on the passed metadata directory, e.g. META-INF/statedb/couchdb/indexes
// Clean metadata directory to remove trailing slash
//
// NOTE: given we now have a platform specific metadata, it would likely make sense to move
// core/common/ccprovider/metadata to this package (core/common/chaincode/platforms/metadata)
// in future.
err = ccprovmetadata.ValidateMetadataFile(filename, fileBytes, filepath.Clean(packageDir))
err = ccmetadata.ValidateMetadataFile(filename, fileBytes, filepath.Clean(packageDir))
if err != nil {
return nil, err
}
Expand Down
50 changes: 8 additions & 42 deletions core/common/ccprovider/cc_statedb_artifacts_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ package ccprovider
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"

"github.com/hyperledger/fabric/core/chaincode/platforms"
)

const (
Expand Down Expand Up @@ -47,49 +48,14 @@ func ExtractStatedbArtifactsForChaincode(ccname, ccversion string) (installed bo
// The state db artifacts are expected to contain state db specific artifacts such as index specification in the case of couchdb.
// This function is called during chaincode instantiate/upgrade (from above), and from install, so that statedb artifacts can be created.
func ExtractStatedbArtifactsFromCCPackage(ccpackage CCPackage) (statedbArtifactsTar []byte, err error) {

cds := ccpackage.GetDepSpec()
is := bytes.NewReader(cds.CodePackage)
gr, err := gzip.NewReader(is)
pform, err := platforms.Find(cds.ChaincodeSpec.Type)
if err != nil {
ccproviderLogger.Errorf("Failure opening codepackage gzip stream: %s", err)
return nil, err
}
tr := tar.NewReader(gr)
statedbTarBuffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(statedbTarBuffer)

// For each file in the code package tar,
// add it to the statedb artifact tar if it has "statedb" in the path
for {
header, err := tr.Next()
if err == io.EOF {
// We only get here if there are no more entries to scan
break
}

if err != nil {
return nil, err
}
ccproviderLogger.Debugf("header.Name = %s", header.Name)
if !strings.HasPrefix(header.Name, ccPackageStatedbDir) {
continue
}
if err = tw.WriteHeader(header); err != nil {
ccproviderLogger.Error("Error adding header to statedb tar:", err, header.Name)
return nil, err
}
if _, err := io.Copy(tw, tr); err != nil {
ccproviderLogger.Error("Error copying file to statedb tar:", err, header.Name)
return nil, err
}
ccproviderLogger.Debug("Wrote file to statedb tar:", header.Name)
}
if err = tw.Close(); err != nil {
return nil, err
ccproviderLogger.Infof("invalid deployment spec (bad platform type:%s)", cds.ChaincodeSpec.Type)
return nil, fmt.Errorf("invalid deployment spec")
}
ccproviderLogger.Debug("Created statedb artifact tar")
return statedbTarBuffer.Bytes(), nil
metaprov := pform.GetMetadataProvider(cds)
return metaprov.GetMetadataAsTarEntries()
}

// ExtractFileEntries extract file entries from the given `tarBytes`. A file entry is included in the
Expand Down
4 changes: 2 additions & 2 deletions core/container/util/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/common/ccprovider/metadata"
"github.com/hyperledger/fabric/core/chaincode/platforms/ccmetadata"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -111,7 +111,7 @@ func WriteFolderToTarPackage(tw *tar.Writer, srcPath string, excludeDir string,
// Validate metadata file for inclusion in tar
// Validation is based on the passed metadata directory, e.g. META-INF/statedb/couchdb/indexes
// Clean metadata directory to remove trailing slash
err = metadata.ValidateMetadataFile(filename, fileBytes, filepath.Clean(packageDir))
err = ccmetadata.ValidateMetadataFile(filename, fileBytes, filepath.Clean(packageDir))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/scc/lscc/lscc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/aclmgmt"
"github.com/hyperledger/fabric/core/aclmgmt/resources"
"github.com/hyperledger/fabric/core/chaincode/platforms/ccmetadata"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
ccmetadata "github.com/hyperledger/fabric/core/common/ccprovider/metadata"
"github.com/hyperledger/fabric/core/common/privdata"
"github.com/hyperledger/fabric/core/common/sysccprovider"
"github.com/hyperledger/fabric/core/ledger/cceventmgmt"
Expand Down

0 comments on commit 73c6759

Please sign in to comment.