forked from chainHero/heroes-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.go
129 lines (109 loc) · 5.44 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package blockchain
import (
"fmt"
"time"
"github.com/hyperledger/fabric-sdk-go/api/apitxn/chclient"
chmgmt "github.com/hyperledger/fabric-sdk-go/api/apitxn/chmgmtclient"
resmgmt "github.com/hyperledger/fabric-sdk-go/api/apitxn/resmgmtclient"
"github.com/hyperledger/fabric-sdk-go/pkg/config"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
)
// FabricSetup implementation
type FabricSetup struct {
ConfigFile string
OrgID string
ChannelID string
ChainCodeID string
initialized bool
ChannelConfig string
ChaincodeGoPath string
ChaincodePath string
OrgAdmin string
OrgName string
UserName string
client chclient.ChannelClient
admin resmgmt.ResourceMgmtClient
sdk *fabsdk.FabricSDK
}
// Initialize reads the configuration file and sets up the client, chain and event hub
func (setup *FabricSetup) Initialize() error {
// Add parameters for the initialization
if setup.initialized {
return fmt.Errorf("sdk already initialized")
}
// Initialize the SDK with the configuration file
sdk, err := fabsdk.New(config.FromFile(setup.ConfigFile))
if err != nil {
return fmt.Errorf("failed to create sdk: %v", err)
}
setup.sdk = sdk
// Channel management client is responsible for managing channels (create/update channel)
// Supply user that has privileges to create channel (in this case orderer admin)
chMgmtClient, err := setup.sdk.NewClient(fabsdk.WithUser(setup.OrgAdmin), fabsdk.WithOrg(setup.OrgName)).ChannelMgmt()
if err != nil {
return fmt.Errorf("failed to add Admin user to sdk: %v", err)
}
// Org admin user is signing user for creating channel.
// The session method is the only way for now to get the user identity.
session, err := setup.sdk.NewClient(fabsdk.WithUser(setup.OrgAdmin), fabsdk.WithOrg(setup.OrgName)).Session()
if err != nil {
return fmt.Errorf("failed to get session for %s, %s: %s", setup.OrgName, setup.OrgAdmin, err)
}
orgAdminUser := session
// Creation of the channel chainhero. A channel can be understood as a private network inside the main network between two or more specific network Organizations
// The channel is defined by its : Organizations, anchor peer (A peer node that all other peers can discover and communicate with. Every Organizations have one), the shared ledger, chaincode application(s) and the ordering service node(s)
// Each transaction on the network is executed on a channel.
req := chmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentity: orgAdminUser}
if err = chMgmtClient.SaveChannel(req); err != nil {
return fmt.Errorf("failed to create channel: %v", err)
}
// Allow orderer to process channel creation
time.Sleep(time.Second * 5)
// The resource management client is a client API for managing system resources
// It will allow us to directly interact with the blockchain. It can be associated with the admin status
setup.admin, err = setup.sdk.NewClient(fabsdk.WithUser(setup.OrgAdmin)).ResourceMgmt()
if err != nil {
return fmt.Errorf("failed to create new resource management client: %v", err)
}
// Org peers join channel
if err = setup.admin.JoinChannel(setup.ChannelID); err != nil {
return fmt.Errorf("org peers failed to join the channel: %v", err)
}
fmt.Println("Initialization Successful")
setup.initialized = true
return nil
}
func (setup *FabricSetup) InstallAndInstantiateCC() error {
// Create a new go lang chaincode package and initializing it with our chaincode
ccPkg, err := packager.NewCCPackage(setup.ChaincodePath, setup.ChaincodeGoPath)
if err != nil {
return fmt.Errorf("failed to create chaincode package: %v", err)
}
// Install our chaincode on org peers
// The resource management client send the chaincode to all peers in its channel in order for them to store it and interact with it later
installCCReq := resmgmt.InstallCCRequest{Name: setup.ChainCodeID, Path: setup.ChaincodePath, Version: "1.0", Package: ccPkg}
_, err = setup.admin.InstallCC(installCCReq)
if err != nil {
return fmt.Errorf("failed to install cc to org peers %v", err)
}
// Set up chaincode policy
// The chaincode policy is required if your transactions must follow some specific rules
// If you don't provide any policy every transaction will be endorsed, and it's probably not what you want
// In this case, we set the rule to : Endorse the transaction if the transaction have been signed by a member from the org "org1.hf.leadiq.com"
ccPolicy := cauthdsl.SignedByAnyMember([]string{"org1.hf.leadiq.com"})
// Instantiate our chaincode on org peers
// The resource management client tells to all peers in its channel to instantiate the chaincode previously installed
err = setup.admin.InstantiateCC(setup.ChannelID, resmgmt.InstantiateCCRequest{Name: setup.ChainCodeID, Path: setup.ChaincodePath, Version: "1.0", Args: [][]byte{[]byte("init")}, Policy: ccPolicy})
if err != nil {
return fmt.Errorf("failed to instantiate the chaincode: %v", err)
}
// Channel client is used to query and execute transactions
setup.client, err = setup.sdk.NewClient(fabsdk.WithUser(setup.UserName)).Channel(setup.ChannelID)
if err != nil {
return fmt.Errorf("failed to create new channel client: %v", err)
}
fmt.Println("Chaincode Installation & Instantiation Successful")
return nil
}