Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit ec9d2ef

Browse files
[FAB-3313] Move APIs to appropriate class
Change-Id: I75b8656055a8e2ee935d083e279d2a6ee8923639 Signed-off-by: biljana lukovic <biljana.lukovic@securekey.com>
1 parent 0aacd1f commit ec9d2ef

File tree

13 files changed

+272
-236
lines changed

13 files changed

+272
-236
lines changed

fabric-client/chain.go

Lines changed: 40 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/hyperledger/fabric/protos/common"
3434
pb "github.com/hyperledger/fabric/protos/peer"
3535

36-
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
3736
protos_utils "github.com/hyperledger/fabric/protos/utils"
3837
"github.com/op/go-logging"
3938

@@ -78,15 +77,12 @@ type Chain interface {
7877
QueryBlock(blockNumber int) (*common.Block, error)
7978
QueryBlockByHash(blockHash []byte) (*common.Block, error)
8079
QueryTransaction(transactionID string) (*pb.ProcessedTransaction, error)
81-
QueryInstalledChaincodes(peer Peer) (*pb.ChaincodeQueryResponse, error)
8280
QueryInstantiatedChaincodes() (*pb.ChaincodeQueryResponse, error)
83-
QueryChannels(peer Peer) (*pb.ChannelQueryResponse, error)
8481
QueryByChaincode(chaincodeName string, args []string, targets []Peer) ([][]byte, error)
8582
CreateTransactionProposal(chaincodeName string, chainID string, args []string, sign bool, transientData map[string][]byte) (*TransactionProposal, error)
8683
SendTransactionProposal(proposal *TransactionProposal, retry int, targets []Peer) ([]*TransactionProposalResponse, error)
8784
CreateTransaction(resps []*TransactionProposalResponse) (*Transaction, error)
8885
SendTransaction(tx *Transaction) ([]*TransactionResponse, error)
89-
SendInstallProposal(chaincodeName string, chaincodePath string, chaincodeVersion string, chaincodePackage []byte, targets []Peer) ([]*TransactionProposalResponse, string, error)
9086
SendInstantiateProposal(chaincodeName string, chainID string, args []string, chaincodePath string, chaincodeVersion string, targets []Peer) ([]*TransactionProposalResponse, string, error)
9187
GetOrganizationUnits() ([]string, error)
9288
QueryExtensionInterface() ChainExtension
@@ -523,8 +519,8 @@ func (c *chain) JoinChannel(request *JoinChannelRequest) error {
523519
return err
524520
}
525521
// Sign join proposal
526-
signature, err := c.signObjectWithKey(proposalBytes, user.GetPrivateKey(),
527-
&bccsp.SHAOpts{}, nil)
522+
signature, err := signObjectWithKey(proposalBytes, user.GetPrivateKey(),
523+
&bccsp.SHAOpts{}, nil, c.clientContext.GetCryptoSuite())
528524
if err != nil {
529525
return err
530526
}
@@ -701,30 +697,6 @@ func (c *chain) QueryTransaction(transactionID string) (*pb.ProcessedTransaction
701697
return transaction, nil
702698
}
703699

704-
//QueryInstalledChaincodes
705-
/**
706-
* Queries the installed chaincodes on a peer
707-
* Returning the details of all chaincodes installed on a peer.
708-
* @param {Peer} peer
709-
* @returns {object} ChaincodeQueryResponse proto
710-
*/
711-
712-
func (c *chain) QueryInstalledChaincodes(peer Peer) (*pb.ChaincodeQueryResponse, error) {
713-
714-
payload, err := c.queryByChaincodeByTarget("lccc", []string{"getinstalledchaincodes"}, peer)
715-
if err != nil {
716-
return nil, fmt.Errorf("Invoke lccc getinstalledchaincodes return error: %v", err)
717-
}
718-
719-
response := new(pb.ChaincodeQueryResponse)
720-
err = proto.Unmarshal(payload, response)
721-
if err != nil {
722-
return nil, fmt.Errorf("Unmarshal ChaincodeQueryResponse return error: %v", err)
723-
}
724-
725-
return response, nil
726-
}
727-
728700
//QueryInstantiatedChaincodes
729701
/**
730702
* Queries the instantiated chaincodes on this channel.
@@ -747,30 +719,6 @@ func (c *chain) QueryInstantiatedChaincodes() (*pb.ChaincodeQueryResponse, error
747719
return response, nil
748720
}
749721

750-
//QueryChannels
751-
/**
752-
* Queries the names of all the channels that a
753-
* peer has joined.
754-
* @param {Peer} peer
755-
* @returns {object} ChannelQueryResponse proto
756-
*/
757-
758-
func (c *chain) QueryChannels(peer Peer) (*pb.ChannelQueryResponse, error) {
759-
760-
payload, err := c.queryByChaincodeByTarget("cscc", []string{"GetChannels"}, peer)
761-
if err != nil {
762-
return nil, fmt.Errorf("Invoke cscc GetChannels return error: %v", err)
763-
}
764-
765-
response := new(pb.ChannelQueryResponse)
766-
err = proto.Unmarshal(payload, response)
767-
if err != nil {
768-
return nil, fmt.Errorf("Unmarshal ChannelQueryResponse return error: %v", err)
769-
}
770-
771-
return response, nil
772-
}
773-
774722
/**
775723
* Generic helper for query functionality for chain
776724
* This query will be made to one target peer and will return one result only.
@@ -795,7 +743,7 @@ func (c *chain) queryByChaincodeByTarget(chaincodeName string, args []string, ta
795743

796744
}
797745

798-
//QueryByChaincode
746+
//QueryByChaincode ..
799747
/**
800748
* Sends a proposal to one or more endorsing peers that will be handled by the chaincode.
801749
* This request will be presented to the chaincode 'invoke' and must understand
@@ -807,8 +755,7 @@ func (c *chain) queryByChaincodeByTarget(chaincodeName string, args []string, ta
807755
* @param {[]Peer} target peers
808756
* @returns {[][]byte} an array of payloads
809757
*/
810-
func (c *chain) QueryByChaincode(chaincodeName string, args []string, targets []Peer) ([][]byte, error) {
811-
758+
func QueryByChaincode(chaincodeName string, args []string, targets []Peer, clientContext Client) ([][]byte, error) {
812759
if chaincodeName == "" {
813760
return nil, fmt.Errorf("Missing chaincode name")
814761
}
@@ -823,12 +770,12 @@ func (c *chain) QueryByChaincode(chaincodeName string, args []string, targets []
823770

824771
logger.Debugf("Calling %s function %v on targets: %s\n", chaincodeName, args[0], targets)
825772

826-
signedProposal, err := c.CreateTransactionProposal(chaincodeName, "", args, true, nil)
773+
signedProposal, err := CreateTransactionProposal(chaincodeName, "", args, true, nil, clientContext)
827774
if err != nil {
828775
return nil, fmt.Errorf("CreateTransactionProposal return error: %v", err)
829776
}
830777

831-
transactionProposalResponses, err := c.SendTransactionProposal(signedProposal, 0, targets)
778+
transactionProposalResponses, err := SendTransactionProposal(signedProposal, 0, targets)
832779
if err != nil {
833780
return nil, fmt.Errorf("SendTransactionProposal return error: %v", err)
834781
}
@@ -850,6 +797,10 @@ func (c *chain) QueryByChaincode(chaincodeName string, args []string, targets []
850797
return responses, nil
851798
}
852799

800+
func (c *chain) QueryByChaincode(chaincodeName string, args []string, targets []Peer) ([][]byte, error) {
801+
return QueryByChaincode(chaincodeName, args, targets, c.clientContext)
802+
}
803+
853804
// CreateTransactionProposal ...
854805
/**
855806
* Create a proposal for transaction. This involves assembling the proposal
@@ -858,6 +809,12 @@ func (c *chain) QueryByChaincode(chaincodeName string, args []string, targets []
858809
*/
859810
func (c *chain) CreateTransactionProposal(chaincodeName string, chainID string,
860811
args []string, sign bool, transientData map[string][]byte) (*TransactionProposal, error) {
812+
return CreateTransactionProposal(chaincodeName, chainID, args, sign, transientData, c.clientContext)
813+
}
814+
815+
//CreateTransactionProposal ...
816+
func CreateTransactionProposal(chaincodeName string, chainID string,
817+
args []string, sign bool, transientData map[string][]byte, clientContext Client) (*TransactionProposal, error) {
861818

862819
argsArray := make([][]byte, len(args))
863820
for i, arg := range args {
@@ -867,7 +824,7 @@ func (c *chain) CreateTransactionProposal(chaincodeName string, chainID string,
867824
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincodeName},
868825
Input: &pb.ChaincodeInput{Args: argsArray}}}
869826

870-
user, err := c.clientContext.LoadUserFromStateStore("")
827+
user, err := clientContext.LoadUserFromStateStore("")
871828
if err != nil {
872829
return nil, fmt.Errorf("LoadUserFromStateStore return error: %s", err)
873830
}
@@ -887,8 +844,8 @@ func (c *chain) CreateTransactionProposal(chaincodeName string, chainID string,
887844
return nil, err
888845
}
889846

890-
signature, err := c.signObjectWithKey(proposalBytes, user.GetPrivateKey(),
891-
&bccsp.SHAOpts{}, nil)
847+
signature, err := signObjectWithKey(proposalBytes, user.GetPrivateKey(),
848+
&bccsp.SHAOpts{}, nil, clientContext.GetCryptoSuite())
892849
if err != nil {
893850
return nil, err
894851
}
@@ -898,6 +855,7 @@ func (c *chain) CreateTransactionProposal(chaincodeName string, chainID string,
898855
signedProposal: signedProposal,
899856
proposal: proposal,
900857
}, nil
858+
901859
}
902860

903861
// SendTransactionProposal ...
@@ -914,6 +872,20 @@ func (c *chain) SendTransactionProposal(proposal *TransactionProposal, retry int
914872
if err != nil {
915873
return nil, fmt.Errorf("GetTargetPeers return error: %s", err)
916874
}
875+
if len(targetPeers) < 1 {
876+
return nil, fmt.Errorf("Missing peer objects for sending transaction proposal")
877+
}
878+
879+
return SendTransactionProposal(proposal, retry, targetPeers)
880+
881+
}
882+
883+
//SendTransactionProposal ...
884+
func SendTransactionProposal(proposal *TransactionProposal, retry int, targetPeers []Peer) ([]*TransactionProposalResponse, error) {
885+
886+
if proposal == nil || proposal.signedProposal == nil {
887+
return nil, fmt.Errorf("signedProposal is nil")
888+
}
917889

918890
if len(targetPeers) < 1 {
919891
return nil, fmt.Errorf("Missing peer objects for sending transaction proposal")
@@ -1094,77 +1066,6 @@ func (c *chain) SendTransaction(tx *Transaction) ([]*TransactionResponse, error)
10941066
return transactionResponses, nil
10951067
}
10961068

1097-
// SendInstallProposal ...
1098-
/**
1099-
* Sends an install proposal to one or more endorsing peers.
1100-
* @param {string} chaincodeName: required - The name of the chaincode.
1101-
* @param {[]string} chaincodePath: required - string of the path to the location of the source code of the chaincode
1102-
* @param {[]string} chaincodeVersion: required - string of the version of the chaincode
1103-
* @param {[]string} chaincodeVersion: optional - Array of byte the chaincodePackage
1104-
*/
1105-
func (c *chain) SendInstallProposal(chaincodeName string, chaincodePath string, chaincodeVersion string, chaincodePackage []byte, targets []Peer) ([]*TransactionProposalResponse, string, error) {
1106-
1107-
if chaincodeName == "" {
1108-
return nil, "", fmt.Errorf("Missing 'chaincodeName' parameter")
1109-
}
1110-
if chaincodePath == "" {
1111-
return nil, "", fmt.Errorf("Missing 'chaincodePath' parameter")
1112-
}
1113-
if chaincodeVersion == "" {
1114-
return nil, "", fmt.Errorf("Missing 'chaincodeVersion' parameter")
1115-
}
1116-
1117-
if chaincodePackage == nil {
1118-
var err error
1119-
chaincodePackage, err = PackageCC(chaincodePath, "")
1120-
if err != nil {
1121-
return nil, "", fmt.Errorf("PackageCC return error: %s", err)
1122-
}
1123-
}
1124-
1125-
targetPeers, err := c.getTargetPeers(targets)
1126-
if err != nil {
1127-
return nil, "", fmt.Errorf("Invalid target peers return error: %s", err)
1128-
}
1129-
1130-
if len(targetPeers) < 1 {
1131-
return nil, "", fmt.Errorf("Missing peer objects for install CC proposal")
1132-
}
1133-
1134-
now := time.Now()
1135-
cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
1136-
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincodeName, Path: chaincodePath, Version: chaincodeVersion}},
1137-
CodePackage: chaincodePackage, EffectiveDate: &google_protobuf.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())}}
1138-
1139-
user, err := c.clientContext.LoadUserFromStateStore("")
1140-
if err != nil {
1141-
return nil, "", fmt.Errorf("LoadUserFromStateStore return error: %s", err)
1142-
}
1143-
1144-
creatorID, err := getSerializedIdentity(user.GetEnrollmentCertificate())
1145-
if err != nil {
1146-
return nil, "", err
1147-
}
1148-
1149-
// create an install from a chaincodeDeploymentSpec
1150-
proposal, txID, err := protos_utils.CreateInstallProposalFromCDS(cds, creatorID)
1151-
if err != nil {
1152-
return nil, "", fmt.Errorf("Could not create chaincode Deploy proposal, err %s", err)
1153-
}
1154-
1155-
signedProposal, err := c.signProposal(proposal)
1156-
if err != nil {
1157-
return nil, "", err
1158-
}
1159-
1160-
transactionProposalResponse, err := c.SendTransactionProposal(&TransactionProposal{
1161-
signedProposal: signedProposal,
1162-
proposal: proposal,
1163-
TransactionID: txID,
1164-
}, 0, targetPeers)
1165-
return transactionProposalResponse, txID, err
1166-
}
1167-
11681069
// SendInstantiateProposal ...
11691070
/**
11701071
* Sends an instantiate proposal to one or more endorsing peers.
@@ -1187,6 +1088,7 @@ func (c *chain) SendInstantiateProposal(chaincodeName string, chainID string,
11871088
return nil, "", fmt.Errorf("Missing 'chaincodePath' parameter")
11881089
}
11891090
if chaincodeVersion == "" {
1091+
11901092
return nil, "", fmt.Errorf("Missing 'chaincodeVersion' parameter")
11911093
}
11921094

@@ -1252,8 +1154,8 @@ func (c *chain) SignPayload(payload []byte) (*SignedEnvelope, error) {
12521154
return nil, fmt.Errorf("LoadUserFromStateStore returned error: %s", err)
12531155
}
12541156

1255-
signature, err := c.signObjectWithKey(payload, user.GetPrivateKey(),
1256-
&bccsp.SHAOpts{}, nil)
1157+
signature, err := signObjectWithKey(payload, user.GetPrivateKey(),
1158+
&bccsp.SHAOpts{}, nil, c.clientContext.GetCryptoSuite())
12571159
if err != nil {
12581160
return nil, err
12591161
}
@@ -1365,9 +1267,8 @@ func (c *chain) SendEnvelope(envelope *SignedEnvelope) (*common.Block, error) {
13651267

13661268
// signObjectWithKey will sign the given object with the given key,
13671269
// hashOpts and signerOpts
1368-
func (c *chain) signObjectWithKey(object []byte, key bccsp.Key,
1369-
hashOpts bccsp.HashOpts, signerOpts bccsp.SignerOpts) ([]byte, error) {
1370-
cryptoSuite := c.clientContext.GetCryptoSuite()
1270+
func signObjectWithKey(object []byte, key bccsp.Key,
1271+
hashOpts bccsp.HashOpts, signerOpts bccsp.SignerOpts, cryptoSuite bccsp.BCCSP) ([]byte, error) {
13711272
digest, err := cryptoSuite.Hash(object, hashOpts)
13721273
if err != nil {
13731274
return nil, err
@@ -1391,7 +1292,7 @@ func (c *chain) signProposal(proposal *pb.Proposal) (*pb.SignedProposal, error)
13911292
return nil, err
13921293
}
13931294

1394-
signature, err := c.signObjectWithKey(proposalBytes, user.GetPrivateKey(), &bccsp.SHAOpts{}, nil)
1295+
signature, err := signObjectWithKey(proposalBytes, user.GetPrivateKey(), &bccsp.SHAOpts{}, nil, c.clientContext.GetCryptoSuite())
13951296
if err != nil {
13961297
return nil, err
13971298
}

fabric-client/chain_test.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ func TestQueryMethods(t *testing.T) {
7373
if err == nil {
7474
t.Fatalf("Query hash cannot be nil")
7575
}
76-
77-
_, err = chain.QueryChannels(nil)
78-
if err == nil {
79-
t.Fatalf("QueryChanels: peer cannot be nil")
80-
}
81-
82-
_, err = chain.QueryInstalledChaincodes(nil)
83-
if err == nil {
84-
t.Fatalf("QueryInstalledChaincodes: peer cannot be nil")
85-
}
86-
8776
_, err = chain.QueryByChaincode("", []string{"method"}, nil)
8877
if err == nil {
8978
t.Fatalf("QueryByChaincode: name cannot be empty")
@@ -221,7 +210,7 @@ func TestJoinChannel(t *testing.T) {
221210
var peers []Peer
222211
endorserServer := startEndorserServer(t)
223212
chain, _ := setupTestChain()
224-
peer, _ := CreateNewPeer(testAddress, "", "")
213+
peer, _ := NewPeer(testAddress, "", "")
225214
peers = append(peers, peer)
226215
orderer := &mockOrderer{DeliverResponse: NewMockDeliverResponse(mocks.NewSimpleMockBlock())}
227216
nonce, _ := util.GenerateRandomNonce()

0 commit comments

Comments
 (0)