github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/packaging/platforms.go (about)

     1  /*
     2  # Copyright IBM Corp. All Rights Reserved.
     3  #
     4  # SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package packaging
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/hyperledger/fabric/common/flogging"
    13  	"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
    14  	"github.com/hyperledger/fabric/core/chaincode/platforms/java"
    15  	"github.com/hyperledger/fabric/core/chaincode/platforms/node"
    16  )
    17  
    18  // SupportedPlatforms is the canonical list of platforms Fabric supports
    19  var SupportedPlatforms = []Platform{
    20  	&java.Platform{},
    21  	&golang.Platform{},
    22  	&node.Platform{},
    23  }
    24  
    25  // Interface for validating the specification and writing the package for
    26  // the given platform
    27  type Platform interface {
    28  	Name() string
    29  	ValidatePath(path string) error
    30  	ValidateCodePackage(code []byte) error
    31  	GetDeploymentPayload(path string) ([]byte, error)
    32  }
    33  
    34  // NormalizerPather is an optional interface that can be implemented by
    35  // platforms to modify the path stored in the chaincde ID.
    36  type NormalizePather interface {
    37  	NormalizePath(path string) (string, error)
    38  }
    39  
    40  type Registry struct {
    41  	Platforms map[string]Platform
    42  }
    43  
    44  var logger = flogging.MustGetLogger("chaincode.platform")
    45  
    46  func NewRegistry(platformTypes ...Platform) *Registry {
    47  	platforms := make(map[string]Platform)
    48  	for _, platform := range platformTypes {
    49  		if _, ok := platforms[platform.Name()]; ok {
    50  			logger.Panicf("Multiple platforms of the same name specified: %s", platform.Name())
    51  		}
    52  		platforms[platform.Name()] = platform
    53  	}
    54  	return &Registry{
    55  		Platforms: platforms,
    56  	}
    57  }
    58  
    59  func (r *Registry) ValidateSpec(ccType, path string) error {
    60  	platform, ok := r.Platforms[ccType]
    61  	if !ok {
    62  		return fmt.Errorf("Unknown chaincodeType: %s", ccType)
    63  	}
    64  	return platform.ValidatePath(path)
    65  }
    66  
    67  func (r *Registry) NormalizePath(ccType, path string) (string, error) {
    68  	platform, ok := r.Platforms[ccType]
    69  	if !ok {
    70  		return "", fmt.Errorf("unknown chaincodeType: %s", ccType)
    71  	}
    72  	if normalizer, ok := platform.(NormalizePather); ok {
    73  		return normalizer.NormalizePath(path)
    74  	}
    75  	return path, nil
    76  }
    77  
    78  func (r *Registry) ValidateDeploymentSpec(ccType string, codePackage []byte) error {
    79  	platform, ok := r.Platforms[ccType]
    80  	if !ok {
    81  		return fmt.Errorf("Unknown chaincodeType: %s", ccType)
    82  	}
    83  
    84  	// ignore empty packages
    85  	if len(codePackage) == 0 {
    86  		return nil
    87  	}
    88  
    89  	return platform.ValidateCodePackage(codePackage)
    90  }
    91  
    92  func (r *Registry) GetDeploymentPayload(ccType, path string) ([]byte, error) {
    93  	platform, ok := r.Platforms[ccType]
    94  	if !ok {
    95  		return nil, fmt.Errorf("Unknown chaincodeType: %s", ccType)
    96  	}
    97  
    98  	return platform.GetDeploymentPayload(path)
    99  }