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

Commit ff9b6bf

Browse files
committed
[FAB-8054] Split transactor from Channel
This change splits the transactor into its own type and interface. Change-Id: I07409a4ef58ae9e39fa38850738cb0a53000e6e0 Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 89112b8 commit ff9b6bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1077
-853
lines changed

api/apifabclient/channel.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import (
2323
* primary orderer to retrieve the configuration settings for this channel.
2424
*/
2525
type Channel interface {
26-
Sender
27-
ProposalSender
28-
2926
Name() string
3027
ChannelConfig() (*common.ConfigEnvelope, error)
3128

@@ -56,7 +53,7 @@ type Channel interface {
5653
QueryInstantiatedChaincodes() (*pb.ChaincodeQueryResponse, error)
5754
QueryByChaincode(ChaincodeInvokeRequest) ([][]byte, error)
5855
QueryBySystemChaincode(request ChaincodeInvokeRequest) ([][]byte, error)
59-
QueryConfigBlock(peers []Peer, minResponses int) (*common.ConfigEnvelope, error)
56+
QueryConfigBlock(targets []ProposalProcessor, minResponses int) (*common.ConfigEnvelope, error)
6057
}
6158

6259
// ChannelLedger provides access to the underlying ledger for a channel.

api/apifabclient/context.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ type ChannelProvider interface {
3333
type ChannelService interface {
3434
Config() (ChannelConfig, error)
3535
Ledger() (ChannelLedger, error)
36-
Channel() (Channel, error)
36+
Channel() (Channel, error) // TODO remove
37+
Transactor() (Transactor, error)
3738
EventHub() (EventHub, error) // TODO support new event delivery
3839
}
40+
41+
// Transactor supplies methods for sending transaction proposals and transactions.
42+
type Transactor interface {
43+
Sender
44+
ProposalSender
45+
}

api/apifabclient/mocks/mockfabclient.gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/apifabclient/proposer.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ import (
1212

1313
// ProposalProcessor simulates transaction proposal, so that a client can submit the result for ordering.
1414
type ProposalProcessor interface {
15-
ProcessTransactionProposal(proposal TransactionProposal) (TransactionProposalResponse, error)
15+
ProcessTransactionProposal(ProcessProposalRequest) (*TransactionProposalResponse, error)
1616
}
1717

1818
// ProposalSender provides the ability for a transaction proposal to be created and sent.
19+
//
20+
// TODO: CreateChaincodeInvokeProposal should be refactored as it is mostly a factory method.
1921
type ProposalSender interface {
20-
SendTransactionProposal(ChaincodeInvokeRequest, []ProposalProcessor) ([]*TransactionProposalResponse, TransactionID, error)
22+
CreateChaincodeInvokeProposal(ChaincodeInvokeRequest) (*TransactionProposal, error)
23+
SendTransactionProposal(*TransactionProposal, []ProposalProcessor) ([]*TransactionProposalResponse, error)
2124
}
2225

2326
// TransactionID contains the ID of a Fabric Transaction Proposal
@@ -27,27 +30,30 @@ type TransactionID struct {
2730
}
2831

2932
// ChaincodeInvokeRequest contains the parameters for sending a transaction proposal.
33+
//
34+
// Deprecated: this struct has been replaced by ChaincodeInvokeProposal.
3035
type ChaincodeInvokeRequest struct {
31-
Targets []ProposalProcessor // TODO: remove
36+
Targets []ProposalProcessor // Deprecated: this parameter is ignored in the new codes and will be removed shortly.
3237
ChaincodeID string
3338
TransientMap map[string][]byte
3439
Fcn string
3540
Args [][]byte
3641
}
3742

38-
// TransactionProposal requests simulation of a proposed transaction from transaction processors.
43+
// TransactionProposal contains a marashalled transaction proposal.
3944
type TransactionProposal struct {
40-
TxnID TransactionID
45+
TxnID TransactionID // TODO: remove?
46+
*pb.Proposal
47+
}
4148

49+
// ProcessProposalRequest requests simulation of a proposed transaction from transaction processors.
50+
type ProcessProposalRequest struct {
4251
SignedProposal *pb.SignedProposal
43-
Proposal *pb.Proposal
4452
}
4553

4654
// TransactionProposalResponse respresents the result of transaction proposal processing.
4755
type TransactionProposalResponse struct {
4856
Endorser string
4957
Status int32
50-
51-
Proposal TransactionProposal
52-
ProposalResponse *pb.ProposalResponse
58+
*pb.ProposalResponse
5359
}

api/apifabclient/sender.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ import (
1010
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
1111
)
1212

13+
// TransactionRequest holds endorsed Transaction Proposals.
14+
type TransactionRequest struct {
15+
Proposal *TransactionProposal
16+
ProposalResponses []*TransactionProposalResponse
17+
}
18+
1319
// Sender provides the ability for a transaction to be created and sent.
20+
//
21+
// TODO: CreateTransaction should be refactored as it is actually a factory method.
1422
type Sender interface {
15-
CreateTransaction(resps []*TransactionProposalResponse) (*Transaction, error)
23+
CreateTransaction(request TransactionRequest) (*Transaction, error)
1624
SendTransaction(tx *Transaction) (*TransactionResponse, error)
1725
}
1826

api/apitxn/chclient/chclient.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Response struct {
2727
Payload []byte
2828
TransactionID apifabclient.TransactionID
2929
TxValidationCode pb.TxValidationCode
30+
Proposal *apifabclient.TransactionProposal
3031
Responses []*apifabclient.TransactionProposalResponse
3132
}
3233

api/apitxn/chclient/txnhandler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ type Handler interface {
2020
//ClientContext contains context parameters for handler execution
2121
type ClientContext struct {
2222
CryptoSuite apicryptosuite.CryptoSuite
23-
Channel apifabclient.Channel
2423
Discovery apifabclient.DiscoveryService
2524
Selection apifabclient.SelectionService
25+
Channel apifabclient.Channel // TODO: this should be removed when we have MSP split out.
26+
Transactor apifabclient.Transactor
2627
EventHub apifabclient.EventHub
2728
}
2829

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright SecureKey Technologies Inc. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package channel
8+
9+
import (
10+
"github.com/golang/protobuf/proto"
11+
"github.com/pkg/errors"
12+
13+
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
14+
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
15+
protos_utils "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/utils"
16+
17+
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
18+
)
19+
20+
// ChaincodeProposalType reflects transitions in the chaincode lifecycle
21+
type ChaincodeProposalType int
22+
23+
// Define chaincode proposal types
24+
const (
25+
InstantiateChaincode ChaincodeProposalType = iota
26+
UpgradeChaincode
27+
)
28+
29+
// ChaincodeDeployRequest holds parameters for creating an instantiate or upgrade chaincode proposal.
30+
type ChaincodeDeployRequest struct {
31+
Name string
32+
Path string
33+
Version string
34+
Args [][]byte
35+
Policy *common.SignaturePolicyEnvelope
36+
CollConfig []*common.CollectionConfig
37+
}
38+
39+
// CreateChaincodeDeployProposal creates an instantiate or upgrade chaincode proposal.
40+
func CreateChaincodeDeployProposal(ctx fab.IdentityContext, deploy ChaincodeProposalType, channelID string, chaincode ChaincodeDeployRequest) (*fab.TransactionProposal, error) {
41+
42+
ccds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
43+
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincode.Name, Path: chaincode.Path, Version: chaincode.Version},
44+
Input: &pb.ChaincodeInput{Args: chaincode.Args}}}
45+
46+
creator, err := ctx.Identity()
47+
if err != nil {
48+
return nil, errors.Wrap(err, "getting user context's identity failed")
49+
}
50+
chaincodePolicyBytes, err := protos_utils.Marshal(chaincode.Policy)
51+
if err != nil {
52+
return nil, err
53+
}
54+
var collConfigBytes []byte
55+
if chaincode.CollConfig != nil {
56+
var err error
57+
collConfigBytes, err = proto.Marshal(&common.CollectionConfigPackage{Config: chaincode.CollConfig})
58+
if err != nil {
59+
return nil, err
60+
}
61+
}
62+
63+
var proposal *pb.Proposal
64+
var txID string
65+
66+
switch deploy {
67+
68+
case InstantiateChaincode:
69+
proposal, txID, err = protos_utils.CreateDeployProposalFromCDS(channelID, ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"), collConfigBytes)
70+
if err != nil {
71+
return nil, errors.Wrap(err, "create instantiate chaincode proposal failed")
72+
}
73+
case UpgradeChaincode:
74+
proposal, txID, err = protos_utils.CreateUpgradeProposalFromCDS(channelID, ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"))
75+
if err != nil {
76+
return nil, errors.Wrap(err, "create upgrade chaincode proposal failed")
77+
}
78+
default:
79+
return nil, errors.Errorf("chaincode proposal type %d not supported", deploy)
80+
}
81+
82+
txnID := fab.TransactionID{ID: txID} // Nonce is missing
83+
tp := fab.TransactionProposal{
84+
Proposal: proposal,
85+
TxnID: txnID,
86+
}
87+
88+
return &tp, err
89+
}

pkg/fabric-client/channel/txn_test.go renamed to pkg/fabric-client/channel/ccdeploy_test.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package channel
77

88
import (
99
"net"
10-
"strings"
1110
"testing"
1211

1312
"google.golang.org/grpc"
@@ -22,19 +21,6 @@ import (
2221
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
2322
)
2423

25-
func TestAddPeerDuplicateCheck(t *testing.T) {
26-
channel, _ := setupTestChannel()
27-
28-
peer := mocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil}
29-
channel.AddPeer(&peer)
30-
31-
err := channel.AddPeer(&peer)
32-
33-
if err == nil || !strings.Contains(err.Error(), "http://peer1.com already exists") {
34-
t.Fatal("Duplicate Peer check is not working as expected")
35-
}
36-
}
37-
3824
func TestSendInstantiateProposal(t *testing.T) {
3925
//Setup channel
4026
user := mocks.NewMockUserWithMSPID("test", "1234")
@@ -45,15 +31,14 @@ func TestSendInstantiateProposal(t *testing.T) {
4531
defer mockCtrl.Finish()
4632
proc := mock_fab.NewMockProposalProcessor(mockCtrl)
4733

48-
tp := fab.TransactionProposal{SignedProposal: &pb.SignedProposal{}}
49-
tpr := fab.TransactionProposalResponse{Endorser: "example.com", Status: 99, Proposal: tp, ProposalResponse: nil}
34+
tpr := fab.TransactionProposalResponse{Endorser: "example.com", Status: 99}
5035

51-
proc.EXPECT().ProcessTransactionProposal(gomock.Any()).Return(tpr, nil)
52-
proc.EXPECT().ProcessTransactionProposal(gomock.Any()).Return(tpr, nil)
36+
proc.EXPECT().ProcessTransactionProposal(gomock.Any()).Return(&tpr, nil)
37+
proc.EXPECT().ProcessTransactionProposal(gomock.Any()).Return(&tpr, nil)
5338
targets := []fab.ProposalProcessor{proc}
5439

5540
//Add a Peer
56-
peer := mocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil}
41+
peer := mocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}}
5742
channel.AddPeer(&peer)
5843

5944
tresponse, txnid, err := channel.SendInstantiateProposal("", nil, "",
@@ -121,10 +106,9 @@ func TestSendUpgradeProposal(t *testing.T) {
121106
defer mockCtrl.Finish()
122107
proc := mock_fab.NewMockProposalProcessor(mockCtrl)
123108

124-
tp := fab.TransactionProposal{SignedProposal: &pb.SignedProposal{}}
125-
tpr := fab.TransactionProposalResponse{Endorser: "example.com", Status: 99, Proposal: tp, ProposalResponse: nil}
109+
tpr := fab.TransactionProposalResponse{Endorser: "example.com", Status: 99, ProposalResponse: nil}
126110

127-
proc.EXPECT().ProcessTransactionProposal(gomock.Any()).Return(tpr, nil)
111+
proc.EXPECT().ProcessTransactionProposal(gomock.Any()).Return(&tpr, nil)
128112
targets := []fab.ProposalProcessor{proc}
129113

130114
//Add a Peer

0 commit comments

Comments
 (0)