Skip to content

Commit

Permalink
[FAB-8376] Refactor deployment spec creation
Browse files Browse the repository at this point in the history
This change remove usage of proto util's deployment spec creation.

Change-Id: I26b227af7e519ef4fe8172fa0310ceda848b76b4
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Feb 19, 2018
1 parent e7a2d89 commit 42a1d82
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func ImportBCCSPKeyFromPEM(keyFile string, myCSP apicryptosuite.CryptoSuite, tem
// ImportBCCSPKeyFromPEMBytes attempts to create a private BCCSP key from a pem byte slice
func ImportBCCSPKeyFromPEMBytes(keyBuff []byte, myCSP apicryptosuite.CryptoSuite, temporary bool) (apicryptosuite.Key, error) {
keyFile := "pem bytes"

key, err := factory.PEMtoPrivateKey(keyBuff, nil)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed parsing private key from %s", keyFile))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ import (
"io/ioutil"
"math/big"
mrand "math/rand"

"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"
factory "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkpatch/cryptosuitebridge"

"net/http"
"path/filepath"
"reflect"
"regexp"
"strings"
"time"

"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"
factory "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkpatch/cryptosuitebridge"

"github.com/pkg/errors"

"golang.org/x/crypto/ocsp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ package signer

import (
"crypto"
"io"

"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"

"io"

"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp/utils"
"github.com/pkg/errors"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import (
"crypto/rand"
"crypto/x509"
"encoding/hex"

"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"

"encoding/pem"
"time"

"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"

"github.com/golang/protobuf/proto"
bccsp "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/cryptosuitebridge"
flogging "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package channel

import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/txn"
"github.com/pkg/errors"

"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -39,51 +40,52 @@ type ChaincodeDeployRequest struct {
// CreateChaincodeDeployProposal creates an instantiate or upgrade chaincode proposal.
func CreateChaincodeDeployProposal(ctx fab.IdentityContext, deploy ChaincodeProposalType, channelID string, chaincode ChaincodeDeployRequest) (*fab.TransactionProposal, error) {

// Generate arguments for deploy (channel, marshaled CCDS, marshaled chaincode policy, marshaled collection policy)
args := [][]byte{}
args = append(args, []byte(channelID))

ccds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincode.Name, Path: chaincode.Path, Version: chaincode.Version},
Input: &pb.ChaincodeInput{Args: chaincode.Args}}}

creator, err := ctx.Identity()
ccdsBytes, err := protos_utils.Marshal(ccds)
if err != nil {
return nil, errors.Wrap(err, "getting user context's identity failed")
return nil, errors.WithMessage(err, "marshal of chaincode deployment spec failed")
}
args = append(args, ccdsBytes)

chaincodePolicyBytes, err := protos_utils.Marshal(chaincode.Policy)
if err != nil {
return nil, err
return nil, errors.WithMessage(err, "marshal of chaincode policy failed")
}
var collConfigBytes []byte
args = append(args, chaincodePolicyBytes)

args = append(args, []byte("escc"))
args = append(args, []byte("vscc"))

if chaincode.CollConfig != nil {
var err error
collConfigBytes, err = proto.Marshal(&common.CollectionConfigPackage{Config: chaincode.CollConfig})
collConfigBytes, err := proto.Marshal(&common.CollectionConfigPackage{Config: chaincode.CollConfig})
if err != nil {
return nil, err
return nil, errors.WithMessage(err, "marshal of collection policy failed")
}
args = append(args, collConfigBytes)
}

var proposal *pb.Proposal
var txID string

// Fcn is deploy or upgrade
fcn := ""
switch deploy {

case InstantiateChaincode:
proposal, txID, err = protos_utils.CreateDeployProposalFromCDS(channelID, ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"), collConfigBytes)
if err != nil {
return nil, errors.Wrap(err, "create instantiate chaincode proposal failed")
}
fcn = "deploy"
case UpgradeChaincode:
proposal, txID, err = protos_utils.CreateUpgradeProposalFromCDS(channelID, ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"))
if err != nil {
return nil, errors.Wrap(err, "create upgrade chaincode proposal failed")
}
fcn = "upgrade"
default:
return nil, errors.Errorf("chaincode proposal type %d not supported", deploy)
return nil, errors.WithMessage(err, "chaincode deployment type unknown")
}

txnID := fab.TransactionID{ID: txID} // Nonce is missing
tp := fab.TransactionProposal{
Proposal: proposal,
TxnID: txnID,
cir := fab.ChaincodeInvokeRequest{
ChaincodeID: "lscc",
Fcn: fcn,
Args: args,
}

return &tp, err
return txn.CreateChaincodeInvokeProposal(ctx, channelID, cir)
}
File renamed without changes.
65 changes: 65 additions & 0 deletions pkg/fabric-client/resource/proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package resource

import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/txn"
"github.com/pkg/errors"

pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
protos_utils "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/utils"

fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
)

// ChaincodeInstallRequest requests chaincode installation on the network
type ChaincodeInstallRequest struct {
Name string
Path string
Version string
Package *ChaincodePackage
}

// ChaincodePackage contains package type and bytes required to create CDS
type ChaincodePackage struct {
Type pb.ChaincodeSpec_Type
Code []byte
}

// CreateChaincodeInstallProposal creates an install chaincode proposal.
func CreateChaincodeInstallProposal(ctx fab.IdentityContext, request ChaincodeInstallRequest) (*fab.TransactionProposal, error) {

// Generate arguments for install
args := [][]byte{}
timestamp := time.Now()
ts, err := ptypes.TimestampProto(timestamp)
if err != nil {
return nil, errors.Wrap(err, "failed to create timestamp in install proposal")
}

ccds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
Type: request.Package.Type, ChaincodeId: &pb.ChaincodeID{Name: request.Name, Path: request.Path, Version: request.Version}},
CodePackage: request.Package.Code, EffectiveDate: ts}
ccdsBytes, err := protos_utils.Marshal(ccds)
if err != nil {
return nil, errors.WithMessage(err, "marshal of chaincode deployment spec failed")
}
args = append(args, ccdsBytes)

args = append(args, []byte("escc"))
args = append(args, []byte("vscc"))

cir := fab.ChaincodeInvokeRequest{
ChaincodeID: "lscc",
Fcn: "install",
Args: args,
}
return txn.CreateChaincodeInvokeProposal(ctx, "", cir)
}
35 changes: 35 additions & 0 deletions pkg/fabric-client/resource/proposal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package resource

import (
"testing"

fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/txn"

"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
"github.com/stretchr/testify/assert"
)

func TestCreateChaincodeInstallProposal(t *testing.T) {
c := setupTestClient()
peer := mocks.MockPeer{MockName: "Peer1", MockURL: "peer1.example.com", MockRoles: []string{}, MockCert: nil, Payload: []byte("A"), Status: 200}

request := ChaincodeInstallRequest{
Name: "examplecc",
Path: "github.com/examplecc",
Version: "1",
Package: &ChaincodePackage{},
}

prop, err := CreateChaincodeInstallProposal(c.clientContext, request)
assert.Nil(t, err, "CreateChaincodeInstallProposal failed")

_, err = txn.SendProposal(c.clientContext, prop, []fab.ProposalProcessor{&peer})
assert.Nil(t, err, "sending mock proposal failed")
}
32 changes: 12 additions & 20 deletions pkg/fabric-client/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ package resource

import (
"net/http"
"time"

"github.com/golang/protobuf/proto"
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
"github.com/pkg/errors"

fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
Expand Down Expand Up @@ -386,30 +384,24 @@ func (c *Resource) InstallChaincode(req fab.InstallChaincodeRequest) ([]*fab.Tra
return nil, "", errors.New("chaincode package is required")
}

now := time.Now()
cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
Type: req.Package.Type, ChaincodeId: &pb.ChaincodeID{Name: req.Name, Path: req.Path, Version: req.Version}},
CodePackage: req.Package.Code, EffectiveDate: &google_protobuf.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())}}

creator, err := c.clientContext.Identity()
if err != nil {
return nil, "", errors.Wrap(err, "failed to get creator identity")
propReq := ChaincodeInstallRequest{
Name: req.Name,
Path: req.Path,
Version: req.Version,
Package: &ChaincodePackage{
Type: req.Package.Type,
Code: req.Package.Code,
},
}

// create an install from a chaincodeDeploymentSpec
proposal, txID, err := protos_utils.CreateInstallProposalFromCDS(cds, creator)
prop, err := CreateChaincodeInstallProposal(c.clientContext, propReq)
if err != nil {
return nil, "", errors.Wrap(err, "failed to create chaincode deploy proposal")
return nil, "", errors.Wrap(err, "creation of install chaincode proposal failed")
}

txnID := fab.TransactionID{ID: txID} // Nonce is missing

transactionProposalResponse, err := txn.SendProposal(c.clientContext, &fab.TransactionProposal{
Proposal: proposal,
TxnID: txnID,
}, req.Targets)
transactionProposalResponse, err := txn.SendProposal(c.clientContext, prop, req.Targets)

return transactionProposalResponse, txID, err
return transactionProposalResponse, prop.TxnID.ID, err
}

func (c *Resource) queryChaincode(request fab.ChaincodeInvokeRequest, targets []fab.ProposalProcessor) ([][]byte, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ gofilter

FILTER_FILENAME="protos/utils/proputils.go"
FILTER_FN="GetHeader,GetChaincodeProposalPayload,GetSignatureHeader,GetChaincodeHeaderExtension,GetBytesChaincodeActionPayload"
FILTER_FN+=",GetBytesTransaction,GetBytesPayload,GetHeader,GetBytesProposalResponsePayload,GetBytesProposal,CreateChaincodeProposal"
FILTER_FN+=",GetBytesChaincodeProposalPayload,CreateChaincodeProposalWithTransient,ComputeProposalTxID"
FILTER_FN+=",CreateChaincodeProposalWithTxIDNonceAndTransient,CreateDeployProposalFromCDS,CreateUpgradeProposalFromCDS"
FILTER_FN+=",createProposalFromCDS,CreateProposalFromCIS,CreateInstallProposalFromCDS,GetTransaction,GetPayload"
FILTER_FN+=",GetBytesTransaction,GetBytesPayload,GetHeader,GetBytesProposalResponsePayload,GetBytesProposal"
FILTER_FN+=",GetBytesChaincodeProposalPayload,ComputeProposalTxID"
FILTER_FN+=",CreateChaincodeProposalWithTxIDNonceAndTransient"
FILTER_FN+=",GetTransaction,GetPayload"
FILTER_FN+=",GetChaincodeActionPayload,GetProposalResponsePayload,GetChaincodeAction,GetChaincodeEvents,GetBytesChaincodeEvent,GetBytesEnvelope"
gofilter
sed -i'' -e 's/"github.com\/hyperledger\/fabric\/bccsp\/factory"/factory "github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\/sdkpatch\/cryptosuitebridge"/g' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
Expand Down
Loading

0 comments on commit 42a1d82

Please sign in to comment.