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

Commit 4bb3690

Browse files
committed
[FAB-4890] Fabric Txn API and removed utils
Change-Id: I07d55a38e59107cef0ab30925327113b15e3d686 Signed-off-by: Emir Heidinger <emir.heidinger@securekey.com>
1 parent 85fa310 commit 4bb3690

21 files changed

+904
-676
lines changed

api/channel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
type Channel interface {
2626
GetName() string
2727
Initialize(data []byte) error
28+
IsInitialized() bool
2829
IsSecurityEnabled() bool
2930
GetTCertBatchSize() int
3031
SetTCertBatchSize(batchSize int)
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
Copyright SecureKey Technologies Inc. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package admin
8+
9+
import (
10+
"fmt"
11+
"io/ioutil"
12+
"os"
13+
"time"
14+
15+
api "github.com/hyperledger/fabric-sdk-go/api"
16+
internal "github.com/hyperledger/fabric-sdk-go/fabric-txn/internal"
17+
"github.com/hyperledger/fabric/protos/common"
18+
"github.com/op/go-logging"
19+
)
20+
21+
var logger = logging.MustGetLogger("fabric_sdk_go")
22+
var origGoPath = os.Getenv("GOPATH")
23+
24+
// SendInstallCC Sends an install proposal to one or more endorsing peers.
25+
func SendInstallCC(client api.FabricClient, chainCodeID string, chainCodePath string,
26+
chainCodeVersion string, chaincodePackage []byte, targets []api.Peer, deployPath string) error {
27+
28+
changeGOPATHToDeploy(deployPath)
29+
transactionProposalResponse, _, err := client.InstallChaincode(chainCodeID, chainCodePath, chainCodeVersion, chaincodePackage, targets)
30+
resetGOPATH()
31+
if err != nil {
32+
return fmt.Errorf("InstallChaincode returned error: %v", err)
33+
}
34+
for _, v := range transactionProposalResponse {
35+
if v.Err != nil {
36+
return fmt.Errorf("InstallChaincode Endorser %s returned error: %v", v.Endorser, v.Err)
37+
}
38+
logger.Debugf("InstallChaincode Endorser '%s' returned ProposalResponse status:%v\n", v.Endorser, v.Status)
39+
}
40+
41+
return nil
42+
}
43+
44+
// SendInstantiateCC Sends instantiate CC proposal to one or more endorsing peers
45+
func SendInstantiateCC(channel api.Channel, chainCodeID string, channelID string, args []string,
46+
chaincodePath string, chaincodeVersion string, targets []api.Peer, eventHub api.EventHub) error {
47+
48+
transactionProposalResponse, txID, err := channel.SendInstantiateProposal(chainCodeID,
49+
channelID, args, chaincodePath, chaincodeVersion, targets)
50+
if err != nil {
51+
return fmt.Errorf("SendInstantiateProposal returned error: %v", err)
52+
}
53+
54+
for _, v := range transactionProposalResponse {
55+
if v.Err != nil {
56+
return fmt.Errorf("SendInstantiateProposal Endorser %s returned error: %v", v.Endorser, v.Err)
57+
}
58+
logger.Debug("SendInstantiateProposal Endorser '%s' returned ProposalResponse status:%v\n", v.Endorser, v.Status)
59+
}
60+
61+
// Register for commit event
62+
done, fail := internal.RegisterTxEvent(txID, eventHub)
63+
64+
if _, err = internal.CreateAndSendTransaction(channel, transactionProposalResponse); err != nil {
65+
return fmt.Errorf("CreateTransaction returned error: %v", err)
66+
}
67+
68+
select {
69+
case <-done:
70+
case <-fail:
71+
return fmt.Errorf("instantiateCC Error received from eventhub for txid(%s) error(%v)", txID, fail)
72+
case <-time.After(time.Second * 30):
73+
return fmt.Errorf("instantiateCC Didn't receive block event for txid(%s)", txID)
74+
}
75+
return nil
76+
}
77+
78+
// CreateChannel ...
79+
func CreateChannel(client api.FabricClient, ordererUser api.User, orgUser api.User, channel api.Channel, channelConfig string) error {
80+
// Check if primary peer has joined this channel
81+
var foundChannel bool
82+
primaryPeer := channel.GetPrimaryPeer()
83+
client.SetUserContext(orgUser)
84+
response, err := client.QueryChannels(primaryPeer)
85+
if err != nil {
86+
return fmt.Errorf("Error querying channels for primary peer: %s", err)
87+
}
88+
for _, responseChannel := range response.Channels {
89+
if responseChannel.ChannelId == channel.GetName() {
90+
foundChannel = true
91+
}
92+
}
93+
94+
if foundChannel {
95+
// There's no need to create a channel, initialize the channel from the orderer and return
96+
if err = channel.Initialize(nil); err != nil {
97+
return fmt.Errorf("Error initializing channel: %v", err)
98+
}
99+
return nil
100+
}
101+
102+
logger.Debugf("***** Creating channel: %s *****\n", channel.GetName())
103+
104+
configTx, err := ioutil.ReadFile(channelConfig)
105+
if err != nil {
106+
return fmt.Errorf("Error reading config file: %v", err)
107+
}
108+
109+
config, err := client.ExtractChannelConfig(configTx)
110+
if err != nil {
111+
return fmt.Errorf("Error extracting channel config: %v", err)
112+
}
113+
114+
configSignature, err := client.SignChannelConfig(config)
115+
if err != nil {
116+
return fmt.Errorf("Error signing configuration: %v", err)
117+
}
118+
119+
var configSignatures []*common.ConfigSignature
120+
configSignatures = append(configSignatures, configSignature)
121+
122+
creator, err := client.GetIdentity()
123+
if err != nil {
124+
return fmt.Errorf("Error getting creator: %v", err)
125+
}
126+
nonce, err := internal.GenerateRandomNonce()
127+
if err != nil {
128+
return fmt.Errorf("Could not compute nonce: %s", err)
129+
}
130+
txID, err := internal.ComputeTxID(nonce, creator)
131+
if err != nil {
132+
return fmt.Errorf("Could not compute TxID: %s", err)
133+
}
134+
135+
request := api.CreateChannelRequest{
136+
Name: channel.GetName(),
137+
Orderer: channel.GetOrderers()[0],
138+
Config: config,
139+
Signatures: configSignatures,
140+
TxID: txID,
141+
Nonce: nonce,
142+
}
143+
144+
client.SetUserContext(ordererUser)
145+
err = client.CreateChannel(&request)
146+
if err != nil {
147+
return fmt.Errorf("CreateChannel returned error")
148+
}
149+
150+
return nil
151+
}
152+
153+
// JoinChannel ...
154+
func JoinChannel(client api.FabricClient, orgUser api.User, channel api.Channel) error {
155+
// Check if primary peer has joined this channel
156+
var foundChannel bool
157+
primaryPeer := channel.GetPrimaryPeer()
158+
client.SetUserContext(orgUser)
159+
response, err := client.QueryChannels(primaryPeer)
160+
if err != nil {
161+
return fmt.Errorf("Error querying channels for primary peer: %s", err)
162+
}
163+
for _, responseChannel := range response.Channels {
164+
if responseChannel.ChannelId == channel.GetName() {
165+
foundChannel = true
166+
}
167+
}
168+
169+
if foundChannel {
170+
// no need to join channel
171+
return nil
172+
}
173+
174+
creator, err := client.GetIdentity()
175+
if err != nil {
176+
return fmt.Errorf("Error getting creator: %v", err)
177+
}
178+
179+
nonce, err := internal.GenerateRandomNonce()
180+
if err != nil {
181+
return fmt.Errorf("Could not compute nonce: %s", err)
182+
}
183+
txID, err := internal.ComputeTxID(nonce, creator)
184+
if err != nil {
185+
return fmt.Errorf("Could not compute TxID: %s", err)
186+
}
187+
188+
genesisBlockRequest := &api.GenesisBlockRequest{
189+
TxID: txID,
190+
Nonce: nonce,
191+
}
192+
genesisBlock, err := channel.GetGenesisBlock(genesisBlockRequest)
193+
if err != nil {
194+
return fmt.Errorf("Error getting genesis block: %v", err)
195+
}
196+
197+
nonce, err = internal.GenerateRandomNonce()
198+
if err != nil {
199+
return fmt.Errorf("Could not compute nonce: %s", err)
200+
}
201+
txID, err = internal.ComputeTxID(nonce, creator)
202+
if err != nil {
203+
return fmt.Errorf("Could not compute TxID: %s", err)
204+
}
205+
joinChannelRequest := &api.JoinChannelRequest{
206+
Targets: channel.GetPeers(),
207+
GenesisBlock: genesisBlock,
208+
TxID: txID,
209+
Nonce: nonce,
210+
}
211+
212+
err = channel.JoinChannel(joinChannelRequest)
213+
if err != nil {
214+
return fmt.Errorf("Error joining channel: %s", err)
215+
}
216+
217+
return nil
218+
}
219+
220+
// ChangeGOPATHToDeploy changes go path to fixtures folder
221+
func changeGOPATHToDeploy(deployPath string) {
222+
os.Setenv("GOPATH", deployPath)
223+
}
224+
225+
// ResetGOPATH resets go path to original
226+
func resetGOPATH() {
227+
os.Setenv("GOPATH", origGoPath)
228+
}

0 commit comments

Comments
 (0)