Skip to content

Commit edec005

Browse files
committed
[FAB-11051] refactor install/instantiate/upgrade
- remove InstallChaincode and InstantiateChaincode from network - create install, instantiate, and ensure instantiated helpers to deploy - create upgrade command and helpers Change-Id: I9b1395d41be25aba2e3f2067500b0ed265c1196a Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 81fb533 commit edec005

File tree

4 files changed

+131
-77
lines changed

4 files changed

+131
-77
lines changed

integration/nwo/commands/peer.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,21 @@ func (c ChaincodeInstall) Args() []string {
108108
}
109109

110110
type ChaincodeInstantiate struct {
111-
ChannelID string
112-
Orderer string
113-
Name string
114-
Version string
115-
Ctor string
116-
Policy string
111+
ChannelID string
112+
Orderer string
113+
Name string
114+
Version string
115+
Ctor string
116+
Policy string
117+
CollectionsConfig string
117118
}
118119

119120
func (c ChaincodeInstantiate) SessionName() string {
120121
return "peer-chaincode-instantiate"
121122
}
122123

123124
func (c ChaincodeInstantiate) Args() []string {
124-
return []string{
125+
args := []string{
125126
"chaincode", "instantiate",
126127
"--channelID", c.ChannelID,
127128
"--orderer", c.Orderer,
@@ -130,6 +131,10 @@ func (c ChaincodeInstantiate) Args() []string {
130131
"--ctor", c.Ctor,
131132
"--policy", c.Policy,
132133
}
134+
if c.CollectionsConfig != "" {
135+
args = append(args, "--collections-config", c.CollectionsConfig)
136+
}
137+
return args
133138
}
134139

135140
type ChaincodeListInstalled struct{}
@@ -208,6 +213,40 @@ func (c ChaincodeInvoke) Args() []string {
208213
return args
209214
}
210215

216+
type ChaincodeUpgrade struct {
217+
Name string
218+
Version string
219+
Path string // optional
220+
ChannelID string
221+
Orderer string
222+
Ctor string
223+
Policy string
224+
CollectionsConfig string // optional
225+
}
226+
227+
func (c ChaincodeUpgrade) SessionName() string {
228+
return "peer-chaincode-upgrade"
229+
}
230+
231+
func (c ChaincodeUpgrade) Args() []string {
232+
args := []string{
233+
"chaincode", "upgrade",
234+
"--name", c.Name,
235+
"--version", c.Version,
236+
"--channelID", c.ChannelID,
237+
"--orderer", c.Orderer,
238+
"--ctor", c.Ctor,
239+
"--policy", c.Policy,
240+
}
241+
if c.Path != "" {
242+
args = append(args, "--path", c.Path)
243+
}
244+
if c.CollectionsConfig != "" {
245+
args = append(args, "--collections-config", c.CollectionsConfig)
246+
}
247+
return args
248+
}
249+
211250
type SignConfigTx struct {
212251
File string
213252
}

integration/nwo/deploy.go

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,100 @@ import (
1818
)
1919

2020
type Chaincode struct {
21-
Name string
22-
Version string
23-
Path string
24-
Ctor string
25-
Policy string
21+
Name string
22+
Version string
23+
Path string
24+
Ctor string
25+
Policy string
26+
CollectionsConfig string // optional
2627
}
2728

2829
// DeployChaincode is a helper that will install chaincode to all peers that
2930
// are connected to the specified channel, instantiate the chaincode on one of
3031
// the peers, and wait for the instantiation to complete on all of the peers.
31-
func DeployChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode) {
32-
peers := n.PeersWithChannel(channel)
32+
func DeployChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peers ...*Peer) {
33+
if len(peers) == 0 {
34+
peers = n.PeersWithChannel(channel)
35+
}
3336
if len(peers) == 0 {
3437
return
3538
}
3639

3740
// install on all peers
38-
n.InstallChaincode(peers, commands.ChaincodeInstall{
39-
Name: chaincode.Name,
40-
Version: chaincode.Version,
41-
Path: chaincode.Path,
42-
})
41+
InstallChaincode(n, chaincode, peers...)
4342

4443
// instantiate on the first peer
45-
n.InstantiateChaincode(peers[0], commands.ChaincodeInstantiate{
46-
ChannelID: channel,
47-
Orderer: n.OrdererAddress(orderer, ListenPort),
48-
Name: chaincode.Name,
49-
Version: chaincode.Version,
50-
Ctor: chaincode.Ctor,
51-
Policy: chaincode.Policy,
44+
InstantiateChaincode(n, channel, orderer, chaincode, peers[0], peers...)
45+
}
46+
47+
func InstallChaincode(n *Network, chaincode Chaincode, peers ...*Peer) {
48+
for _, p := range peers {
49+
sess, err := n.PeerAdminSession(p, commands.ChaincodeInstall{
50+
Name: chaincode.Name,
51+
Version: chaincode.Version,
52+
Path: chaincode.Path,
53+
})
54+
Expect(err).NotTo(HaveOccurred())
55+
Eventually(sess, time.Minute).Should(gexec.Exit(0))
56+
57+
sess, err = n.PeerAdminSession(p, commands.ChaincodeListInstalled{})
58+
Expect(err).NotTo(HaveOccurred())
59+
Eventually(sess, time.Minute).Should(gexec.Exit(0))
60+
Expect(sess).To(gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", chaincode.Name, chaincode.Version)))
61+
}
62+
}
63+
64+
func InstantiateChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peer *Peer, checkPeers ...*Peer) {
65+
sess, err := n.PeerAdminSession(peer, commands.ChaincodeInstantiate{
66+
ChannelID: channel,
67+
Orderer: n.OrdererAddress(orderer, ListenPort),
68+
Name: chaincode.Name,
69+
Version: chaincode.Version,
70+
Ctor: chaincode.Ctor,
71+
Policy: chaincode.Policy,
72+
CollectionsConfig: chaincode.CollectionsConfig,
5273
})
74+
Expect(err).NotTo(HaveOccurred())
75+
Eventually(sess, time.Minute).Should(gexec.Exit(0))
76+
77+
EnsureInstantiated(n, channel, chaincode.Name, chaincode.Version, checkPeers...)
78+
}
5379

54-
// make sure the instantiation of visible across the remaining peers
55-
for _, p := range peers[1:] {
80+
func EnsureInstantiated(n *Network, channel, name, version string, peers ...*Peer) {
81+
for _, p := range peers {
5682
Eventually(listInstantiated(n, p, channel), time.Minute).Should(
57-
gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", chaincode.Name, chaincode.Version)),
83+
gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", name, version)),
5884
)
5985
}
6086
}
6187

88+
func UpgradeChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peers ...*Peer) {
89+
if len(peers) == 0 {
90+
peers = n.PeersWithChannel(channel)
91+
}
92+
if len(peers) == 0 {
93+
return
94+
}
95+
96+
// install on all peers
97+
InstallChaincode(n, chaincode, peers...)
98+
99+
// upgrade from the first peer
100+
sess, err := n.PeerAdminSession(peers[0], commands.ChaincodeUpgrade{
101+
ChannelID: channel,
102+
Orderer: n.OrdererAddress(orderer, ListenPort),
103+
Name: chaincode.Name,
104+
Version: chaincode.Version,
105+
Ctor: chaincode.Ctor,
106+
Policy: chaincode.Policy,
107+
CollectionsConfig: chaincode.CollectionsConfig,
108+
})
109+
Expect(err).NotTo(HaveOccurred())
110+
Eventually(sess, time.Minute).Should(gexec.Exit(0))
111+
112+
EnsureInstantiated(n, channel, chaincode.Name, chaincode.Version, peers...)
113+
}
114+
62115
func listInstantiated(n *Network, peer *Peer, channel string) func() *gbytes.Buffer {
63116
return func() *gbytes.Buffer {
64117
sess, err := n.PeerAdminSession(peer, commands.ChaincodeListInstantiated{

integration/nwo/network.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/hyperledger/fabric/integration/runner"
2727
"github.com/onsi/ginkgo"
2828
. "github.com/onsi/gomega"
29-
"github.com/onsi/gomega/gbytes"
3029
"github.com/onsi/gomega/gexec"
3130
"github.com/tedsuo/ifrit"
3231
"github.com/tedsuo/ifrit/ginkgomon"
@@ -632,33 +631,6 @@ func (n *Network) JoinChannel(name string, o *Orderer, peers ...*Peer) {
632631
}
633632
}
634633

635-
// InstallChaincode installs chaincode to the listed peers and verifies that
636-
// the install has completed.
637-
func (n *Network) InstallChaincode(peers []*Peer, install commands.ChaincodeInstall) {
638-
for _, p := range peers {
639-
sess, err := n.PeerAdminSession(p, install)
640-
Expect(err).NotTo(HaveOccurred())
641-
Eventually(sess, time.Minute).Should(gexec.Exit(0))
642-
643-
sess, err = n.PeerAdminSession(p, commands.ChaincodeListInstalled{})
644-
Expect(err).NotTo(HaveOccurred())
645-
Eventually(sess, time.Minute).Should(gexec.Exit(0))
646-
Expect(sess).To(gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", install.Name, install.Version)))
647-
}
648-
}
649-
650-
// InstantiateChaincode instantiates chaincode at the specified peer and verifies that
651-
// the instantiation is complete before returning.
652-
func (n *Network) InstantiateChaincode(peer *Peer, instantiate commands.ChaincodeInstantiate) {
653-
sess, err := n.PeerAdminSession(peer, instantiate)
654-
Expect(err).NotTo(HaveOccurred())
655-
Eventually(sess, time.Minute).Should(gexec.Exit(0))
656-
657-
Eventually(listInstantiated(n, peer, instantiate.ChannelID), time.Minute).Should(
658-
gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", instantiate.Name, instantiate.Version)),
659-
)
660-
}
661-
662634
// Cryptogen starts a gexec.Session for the provided cryptogen command.
663635
func (n *Network) Cryptogen(command Command) (*gexec.Session, error) {
664636
cmd := NewCommand(n.Components.Cryptogen(), command)

integration/nwo/network_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,16 @@ var _ = Describe("Network", func() {
165165
network.CreateChannel("testchannel", orderer, testPeers[0])
166166
network.JoinChannel("testchannel", orderer, testPeers...)
167167

168-
network.InstallChaincode(
169-
testPeers,
170-
commands.ChaincodeInstall{
171-
Name: "mycc",
172-
Version: "0.0",
173-
Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd",
174-
},
175-
)
176-
177-
network.InstantiateChaincode(
178-
testPeers[0],
179-
commands.ChaincodeInstantiate{
180-
ChannelID: "testchannel",
181-
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
182-
Name: "mycc",
183-
Version: "0.0",
184-
Ctor: `{"Args":["init","a","100","b","200"]}`,
185-
Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`,
186-
},
187-
)
168+
chaincode := nwo.Chaincode{
169+
Name: "mycc",
170+
Version: "0.0",
171+
Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd",
172+
Ctor: `{"Args":["init","a","100","b","200"]}`,
173+
Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`,
174+
}
175+
nwo.InstallChaincode(network, chaincode, testPeers...)
176+
nwo.InstantiateChaincode(network, "testchannel", orderer, chaincode, testPeers[0])
177+
nwo.EnsureInstantiated(network, "testchannel", "mycc", "0.0", testPeers...)
188178

189179
RunQueryInvokeQuery(network, orderer, testPeers[0])
190180
})

0 commit comments

Comments
 (0)