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

Commit d3c36d4

Browse files
committed
[FAB-7281] Resource Mgmt - Instantiate/Upgrade CC
Change-Id: I9ae04f62d10dc61c4124a40f25a575cccc32dc5f Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
1 parent 015582c commit d3c36d4

File tree

16 files changed

+1228
-554
lines changed

16 files changed

+1228
-554
lines changed

api/apitxn/resmgmtclient/resmgmt.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ SPDX-License-Identifier: Apache-2.0
66

77
package resmgmt
88

9-
import fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
9+
import (
10+
"time"
11+
12+
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
13+
common "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
14+
)
1015

1116
// TargetFilter allows for filtering target peers
1217
type TargetFilter interface {
@@ -42,6 +47,38 @@ type InstallCCOpts struct {
4247
TargetFilter TargetFilter // target filter
4348
}
4449

50+
// InstantiateCCRequest contains instantiate chaincode request parameters
51+
type InstantiateCCRequest struct {
52+
Name string
53+
Path string
54+
Version string
55+
Args [][]byte
56+
Policy *common.SignaturePolicyEnvelope
57+
}
58+
59+
// InstantiateCCOpts contains options for instantiating chaincode
60+
type InstantiateCCOpts struct {
61+
Targets []fab.Peer // target peers
62+
TargetFilter TargetFilter // target filter
63+
Timeout time.Duration
64+
}
65+
66+
// UpgradeCCRequest contains upgrade chaincode request parameters
67+
type UpgradeCCRequest struct {
68+
Name string
69+
Path string
70+
Version string
71+
Args [][]byte
72+
Policy *common.SignaturePolicyEnvelope
73+
}
74+
75+
// UpgradeCCOpts contains options for upgrading chaincode
76+
type UpgradeCCOpts struct {
77+
Targets []fab.Peer // target peers
78+
TargetFilter TargetFilter // target filter
79+
Timeout time.Duration
80+
}
81+
4582
// ResourceMgmtClient is responsible for managing resources: peers joining channels, and installing and instantiating chaincodes(TODO).
4683
type ResourceMgmtClient interface {
4784

@@ -51,6 +88,18 @@ type ResourceMgmtClient interface {
5188
// InstallCCWithOpts installs chaincode with custom options (specific peers, filtered peers)
5289
InstallCCWithOpts(req InstallCCRequest, opts InstallCCOpts) ([]InstallCCResponse, error)
5390

91+
// InstantiateCC instantiates chaincode using default settings
92+
InstantiateCC(channelID string, req InstantiateCCRequest) error
93+
94+
// InstantiateCCWithOpts instantiates chaincode with custom options (target peers, filtered peers)
95+
InstantiateCCWithOpts(channelID string, req InstantiateCCRequest, opts InstantiateCCOpts) error
96+
97+
// UpgradeCC upgrades chaincode using default settings
98+
UpgradeCC(channelID string, req UpgradeCCRequest) error
99+
100+
// UpgradeCCWithOpts upgrades chaincode with custom options (target peers, filtered peers)
101+
UpgradeCCWithOpts(channelID string, req UpgradeCCRequest, opts UpgradeCCOpts) error
102+
54103
// JoinChannel allows for peers to join existing channel
55104
JoinChannel(channelID string) error
56105

def/fabapi/context/defprovider/session.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func (f *SessionClientFactory) NewResourceMgmtClient(sdk context.SDK, session co
6464
return nil, err
6565
}
6666

67-
discovery, err := sdk.DiscoveryProvider().NewDiscoveryService("")
67+
provider := sdk.DiscoveryProvider()
6868
if err != nil {
69-
return nil, errors.WithMessage(err, "create discovery service failed")
69+
return nil, errors.WithMessage(err, "create discovery provider failed")
7070
}
7171

72-
return resmgmtImpl.NewResourceMgmtClient(client, discovery, filter, config)
72+
return resmgmtImpl.NewResourceMgmtClient(client, provider, filter, config)
7373
}
7474

7575
// NewChannelClient returns a client that can execute transactions on specified channel
@@ -146,16 +146,10 @@ func getEventHub(client fab.FabricClient, channelID string, session context.Sess
146146
return nil, errors.WithMessage(err, "read configuration for channel peers failed")
147147
}
148148

149-
serverHostOverride := ""
150149
var eventSource *apiconfig.PeerConfig
151150

152151
for _, p := range peerConfig {
153-
154152
if p.EventSource && p.MspID == session.Identity().MspID() {
155-
serverHostOverride = ""
156-
if str, ok := p.GRPCOptions["ssl-target-name-override"].(string); ok {
157-
serverHostOverride = str
158-
}
159153
eventSource = &p.PeerConfig
160154
break
161155
}
@@ -165,13 +159,6 @@ func getEventHub(client fab.FabricClient, channelID string, session context.Sess
165159
return nil, errors.New("unable to find peer event source for channel")
166160
}
167161

168-
// Event source found create event hub
169-
eventHub, err := events.NewEventHub(client)
170-
if err != nil {
171-
return nil, err
172-
}
173-
174-
eventHub.SetPeerAddr(eventSource.EventURL, eventSource.TLSCACerts.Path, serverHostOverride)
162+
return events.NewEventHubFromConfig(client, eventSource)
175163

176-
return eventHub, nil
177164
}

pkg/fabric-client/channel/txnsender.go

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ func init() {
2828
rand.Seed(time.Now().UnixNano())
2929
}
3030

31+
// CCProposalType reflects transitions in the chaincode lifecycle
32+
type CCProposalType int
33+
34+
// Define chaincode proposal types
35+
const (
36+
Instantiate CCProposalType = iota
37+
Upgrade
38+
)
39+
3140
// CreateTransaction create a transaction with proposal response, following the endorsement policy.
3241
func (c *Channel) CreateTransaction(resps []*apitxn.TransactionProposalResponse) (*apitxn.Transaction, error) {
3342
if len(resps) == 0 {
@@ -163,59 +172,8 @@ func (c *Channel) SendInstantiateProposal(chaincodeName string,
163172
args [][]byte, chaincodePath string, chaincodeVersion string,
164173
chaincodePolicy *common.SignaturePolicyEnvelope, targets []apitxn.ProposalProcessor) ([]*apitxn.TransactionProposalResponse, apitxn.TransactionID, error) {
165174

166-
if chaincodeName == "" {
167-
return nil, apitxn.TransactionID{}, errors.New("chaincodeName is required")
168-
}
169-
if chaincodePath == "" {
170-
return nil, apitxn.TransactionID{}, errors.New("chaincodePath is required")
171-
}
172-
if chaincodeVersion == "" {
173-
return nil, apitxn.TransactionID{}, errors.New("chaincodeVersion is required")
174-
}
175-
if chaincodePolicy == nil {
176-
return nil, apitxn.TransactionID{}, errors.New("chaincodePolicy is required")
177-
}
178-
179-
// TODO: We should validate that targets are added to the channel.
180-
if targets == nil || len(targets) < 1 {
181-
return nil, apitxn.TransactionID{}, errors.New("missing peer objects for instantiate chaincode proposal")
182-
}
183-
184-
ccds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
185-
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincodeName, Path: chaincodePath, Version: chaincodeVersion},
186-
Input: &pb.ChaincodeInput{Args: args}}}
187-
188-
if c.clientContext.UserContext() == nil {
189-
return nil, apitxn.TransactionID{}, errors.New("user context is nil")
190-
}
191-
creator, err := c.clientContext.UserContext().Identity()
192-
if err != nil {
193-
return nil, apitxn.TransactionID{}, errors.Wrap(err, "getting user context's identity failed")
194-
}
195-
chaincodePolicyBytes, err := protos_utils.Marshal(chaincodePolicy)
196-
if err != nil {
197-
return nil, apitxn.TransactionID{}, err
198-
}
199-
// create a proposal from a chaincodeDeploymentSpec
200-
proposal, txID, err := protos_utils.CreateDeployProposalFromCDS(c.Name(), ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"))
201-
if err != nil {
202-
return nil, apitxn.TransactionID{}, errors.Wrap(err, "create chaincode deploy proposal failed")
203-
}
204-
205-
signedProposal, err := c.signProposal(proposal)
206-
if err != nil {
207-
return nil, apitxn.TransactionID{}, err
208-
}
209-
210-
txnID := apitxn.TransactionID{ID: txID} // Nonce is missing
211-
212-
transactionProposalResponse, err := txnproc.SendTransactionProposalToProcessors(&apitxn.TransactionProposal{
213-
SignedProposal: signedProposal,
214-
Proposal: proposal,
215-
TxnID: txnID,
216-
}, targets)
175+
return c.sendCCProposal(Instantiate, chaincodeName, args, chaincodePath, chaincodeVersion, chaincodePolicy, targets)
217176

218-
return transactionProposalResponse, txnID, err
219177
}
220178

221179
// SendUpgradeProposal sends an upgrade proposal to one or more endorsing peers.
@@ -227,6 +185,15 @@ func (c *Channel) SendUpgradeProposal(chaincodeName string,
227185
args [][]byte, chaincodePath string, chaincodeVersion string,
228186
chaincodePolicy *common.SignaturePolicyEnvelope, targets []apitxn.ProposalProcessor) ([]*apitxn.TransactionProposalResponse, apitxn.TransactionID, error) {
229187

188+
return c.sendCCProposal(Upgrade, chaincodeName, args, chaincodePath, chaincodeVersion, chaincodePolicy, targets)
189+
190+
}
191+
192+
// helper function that sends an instantiate or upgrade chaincode proposal to one or more endorsing peers
193+
func (c *Channel) sendCCProposal(ccProposalType CCProposalType, chaincodeName string,
194+
args [][]byte, chaincodePath string, chaincodeVersion string,
195+
chaincodePolicy *common.SignaturePolicyEnvelope, targets []apitxn.ProposalProcessor) ([]*apitxn.TransactionProposalResponse, apitxn.TransactionID, error) {
196+
230197
if chaincodeName == "" {
231198
return nil, apitxn.TransactionID{}, errors.New("chaincodeName is required")
232199
}
@@ -240,9 +207,8 @@ func (c *Channel) SendUpgradeProposal(chaincodeName string,
240207
return nil, apitxn.TransactionID{}, errors.New("chaincodePolicy is required")
241208
}
242209

243-
// TODO: We should validate that targets are added to the channel.
244210
if targets == nil || len(targets) < 1 {
245-
return nil, apitxn.TransactionID{}, errors.New("missing peer objects for upgrade chaincode proposal")
211+
return nil, apitxn.TransactionID{}, errors.New("missing peer objects for chaincode proposal")
246212
}
247213

248214
ccds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
@@ -260,10 +226,24 @@ func (c *Channel) SendUpgradeProposal(chaincodeName string,
260226
if err != nil {
261227
return nil, apitxn.TransactionID{}, err
262228
}
263-
// create a proposal from a chaincodeDeploymentSpec
264-
proposal, txID, err := protos_utils.CreateUpgradeProposalFromCDS(c.Name(), ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"))
265-
if err != nil {
266-
return nil, apitxn.TransactionID{}, errors.Wrap(err, "create chaincode upgrade proposal failed")
229+
230+
var proposal *pb.Proposal
231+
var txID string
232+
233+
switch ccProposalType {
234+
235+
case Instantiate:
236+
proposal, txID, err = protos_utils.CreateDeployProposalFromCDS(c.Name(), ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"))
237+
if err != nil {
238+
return nil, apitxn.TransactionID{}, errors.Wrap(err, "create instantiate chaincode proposal failed")
239+
}
240+
case Upgrade:
241+
proposal, txID, err = protos_utils.CreateUpgradeProposalFromCDS(c.Name(), ccds, creator, chaincodePolicyBytes, []byte("escc"), []byte("vscc"))
242+
if err != nil {
243+
return nil, apitxn.TransactionID{}, errors.Wrap(err, "create upgrade chaincode proposal failed")
244+
}
245+
default:
246+
return nil, apitxn.TransactionID{}, errors.Errorf("chaincode proposal type %d not supported", ccProposalType)
267247
}
268248

269249
signedProposal, err := c.signProposal(proposal)
@@ -282,7 +262,7 @@ func (c *Channel) SendUpgradeProposal(chaincodeName string,
282262
return transactionProposalResponse, txnID, err
283263
}
284264

285-
// SignPayload ... TODO.
265+
// SignPayload signs payload
286266
func (c *Channel) SignPayload(payload []byte) (*fab.SignedEnvelope, error) {
287267
//Get user info
288268
user := c.clientContext.UserContext()

pkg/fabric-client/channel/txnsender_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ func TestSendInstantiateProposal(t *testing.T) {
191191

192192
tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "test",
193193
"1", cauthdsl.SignedByMspMember("Org1MSP"), nil)
194-
if err == nil || err.Error() != "missing peer objects for instantiate chaincode proposal" {
194+
195+
if err == nil || err.Error() != "missing peer objects for chaincode proposal" {
195196
t.Fatal("Missing peer objects validation is not working as expected")
196197
}
197198
}
@@ -259,7 +260,7 @@ func TestSendUpgradeProposal(t *testing.T) {
259260

260261
tresponse, txnid, err = channel.SendUpgradeProposal("qscc", nil, "test",
261262
"2", cauthdsl.SignedByMspMember("Org1MSP"), nil)
262-
if err == nil || err.Error() != "missing peer objects for upgrade chaincode proposal" {
263+
if err == nil || err.Error() != "missing peer objects for chaincode proposal" {
263264
t.Fatal("Missing peer objects validation is not working as expected")
264265
}
265266
}

pkg/fabric-client/events/eventhub.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ func NewEventHub(client fab.FabricClient) (*EventHub, error) {
8888
return &eventHub, nil
8989
}
9090

91+
// NewEventHubFromConfig creates new event hub from client and peer config
92+
func NewEventHubFromConfig(client fab.FabricClient, peerCfg *apiconfig.PeerConfig) (*EventHub, error) {
93+
94+
eventHub, err := NewEventHub(client)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
serverHostOverride := ""
100+
if str, ok := peerCfg.GRPCOptions["ssl-target-name-override"].(string); ok {
101+
serverHostOverride = str
102+
}
103+
104+
eventHub.peerAddr = peerCfg.EventURL
105+
eventHub.peerTLSCertificate = peerCfg.TLSCACerts.Path
106+
eventHub.peerTLSServerHostOverride = serverHostOverride
107+
108+
return eventHub, nil
109+
}
110+
91111
// SetInterests clears all interests and sets the interests for BLOCK type of events.
92112
func (eventHub *EventHub) SetInterests(block bool) {
93113
eventHub.mtx.Lock()

pkg/fabric-client/mocks/mockeventserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func StartMockEventServer(testAddress string) (*MockEventServer, error) {
3232
}
3333
eventServer := &MockEventServer{grpcServer: grpcServer}
3434
pb.RegisterEventsServer(grpcServer, eventServer)
35-
fmt.Printf("Starting test server\n")
35+
fmt.Printf("Starting mock event server\n")
3636
go grpcServer.Serve(lis)
3737

3838
return eventServer, nil

0 commit comments

Comments
 (0)