Commit edec005f authored by Matthew Sykes's avatar Matthew Sykes
Browse files

[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: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent 81fb533d
......@@ -108,12 +108,13 @@ func (c ChaincodeInstall) Args() []string {
}
type ChaincodeInstantiate struct {
ChannelID string
Orderer string
Name string
Version string
Ctor string
Policy string
ChannelID string
Orderer string
Name string
Version string
Ctor string
Policy string
CollectionsConfig string
}
func (c ChaincodeInstantiate) SessionName() string {
......@@ -121,7 +122,7 @@ func (c ChaincodeInstantiate) SessionName() string {
}
func (c ChaincodeInstantiate) Args() []string {
return []string{
args := []string{
"chaincode", "instantiate",
"--channelID", c.ChannelID,
"--orderer", c.Orderer,
......@@ -130,6 +131,10 @@ func (c ChaincodeInstantiate) Args() []string {
"--ctor", c.Ctor,
"--policy", c.Policy,
}
if c.CollectionsConfig != "" {
args = append(args, "--collections-config", c.CollectionsConfig)
}
return args
}
type ChaincodeListInstalled struct{}
......@@ -208,6 +213,40 @@ func (c ChaincodeInvoke) Args() []string {
return args
}
type ChaincodeUpgrade struct {
Name string
Version string
Path string // optional
ChannelID string
Orderer string
Ctor string
Policy string
CollectionsConfig string // optional
}
func (c ChaincodeUpgrade) SessionName() string {
return "peer-chaincode-upgrade"
}
func (c ChaincodeUpgrade) Args() []string {
args := []string{
"chaincode", "upgrade",
"--name", c.Name,
"--version", c.Version,
"--channelID", c.ChannelID,
"--orderer", c.Orderer,
"--ctor", c.Ctor,
"--policy", c.Policy,
}
if c.Path != "" {
args = append(args, "--path", c.Path)
}
if c.CollectionsConfig != "" {
args = append(args, "--collections-config", c.CollectionsConfig)
}
return args
}
type SignConfigTx struct {
File string
}
......
......@@ -18,47 +18,100 @@ import (
)
type Chaincode struct {
Name string
Version string
Path string
Ctor string
Policy string
Name string
Version string
Path string
Ctor string
Policy string
CollectionsConfig string // optional
}
// DeployChaincode is a helper that will install chaincode to all peers that
// are connected to the specified channel, instantiate the chaincode on one of
// the peers, and wait for the instantiation to complete on all of the peers.
func DeployChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode) {
peers := n.PeersWithChannel(channel)
func DeployChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peers ...*Peer) {
if len(peers) == 0 {
peers = n.PeersWithChannel(channel)
}
if len(peers) == 0 {
return
}
// install on all peers
n.InstallChaincode(peers, commands.ChaincodeInstall{
Name: chaincode.Name,
Version: chaincode.Version,
Path: chaincode.Path,
})
InstallChaincode(n, chaincode, peers...)
// instantiate on the first peer
n.InstantiateChaincode(peers[0], commands.ChaincodeInstantiate{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, ListenPort),
Name: chaincode.Name,
Version: chaincode.Version,
Ctor: chaincode.Ctor,
Policy: chaincode.Policy,
InstantiateChaincode(n, channel, orderer, chaincode, peers[0], peers...)
}
func InstallChaincode(n *Network, chaincode Chaincode, peers ...*Peer) {
for _, p := range peers {
sess, err := n.PeerAdminSession(p, commands.ChaincodeInstall{
Name: chaincode.Name,
Version: chaincode.Version,
Path: chaincode.Path,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
sess, err = n.PeerAdminSession(p, commands.ChaincodeListInstalled{})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
Expect(sess).To(gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", chaincode.Name, chaincode.Version)))
}
}
func InstantiateChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peer *Peer, checkPeers ...*Peer) {
sess, err := n.PeerAdminSession(peer, commands.ChaincodeInstantiate{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, ListenPort),
Name: chaincode.Name,
Version: chaincode.Version,
Ctor: chaincode.Ctor,
Policy: chaincode.Policy,
CollectionsConfig: chaincode.CollectionsConfig,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
EnsureInstantiated(n, channel, chaincode.Name, chaincode.Version, checkPeers...)
}
// make sure the instantiation of visible across the remaining peers
for _, p := range peers[1:] {
func EnsureInstantiated(n *Network, channel, name, version string, peers ...*Peer) {
for _, p := range peers {
Eventually(listInstantiated(n, p, channel), time.Minute).Should(
gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", chaincode.Name, chaincode.Version)),
gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", name, version)),
)
}
}
func UpgradeChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peers ...*Peer) {
if len(peers) == 0 {
peers = n.PeersWithChannel(channel)
}
if len(peers) == 0 {
return
}
// install on all peers
InstallChaincode(n, chaincode, peers...)
// upgrade from the first peer
sess, err := n.PeerAdminSession(peers[0], commands.ChaincodeUpgrade{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, ListenPort),
Name: chaincode.Name,
Version: chaincode.Version,
Ctor: chaincode.Ctor,
Policy: chaincode.Policy,
CollectionsConfig: chaincode.CollectionsConfig,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
EnsureInstantiated(n, channel, chaincode.Name, chaincode.Version, peers...)
}
func listInstantiated(n *Network, peer *Peer, channel string) func() *gbytes.Buffer {
return func() *gbytes.Buffer {
sess, err := n.PeerAdminSession(peer, commands.ChaincodeListInstantiated{
......
......@@ -26,7 +26,6 @@ import (
"github.com/hyperledger/fabric/integration/runner"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/ginkgomon"
......@@ -632,33 +631,6 @@ func (n *Network) JoinChannel(name string, o *Orderer, peers ...*Peer) {
}
}
// InstallChaincode installs chaincode to the listed peers and verifies that
// the install has completed.
func (n *Network) InstallChaincode(peers []*Peer, install commands.ChaincodeInstall) {
for _, p := range peers {
sess, err := n.PeerAdminSession(p, install)
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
sess, err = n.PeerAdminSession(p, commands.ChaincodeListInstalled{})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
Expect(sess).To(gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", install.Name, install.Version)))
}
}
// InstantiateChaincode instantiates chaincode at the specified peer and verifies that
// the instantiation is complete before returning.
func (n *Network) InstantiateChaincode(peer *Peer, instantiate commands.ChaincodeInstantiate) {
sess, err := n.PeerAdminSession(peer, instantiate)
Expect(err).NotTo(HaveOccurred())
Eventually(sess, time.Minute).Should(gexec.Exit(0))
Eventually(listInstantiated(n, peer, instantiate.ChannelID), time.Minute).Should(
gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", instantiate.Name, instantiate.Version)),
)
}
// Cryptogen starts a gexec.Session for the provided cryptogen command.
func (n *Network) Cryptogen(command Command) (*gexec.Session, error) {
cmd := NewCommand(n.Components.Cryptogen(), command)
......
......@@ -165,26 +165,16 @@ var _ = Describe("Network", func() {
network.CreateChannel("testchannel", orderer, testPeers[0])
network.JoinChannel("testchannel", orderer, testPeers...)
network.InstallChaincode(
testPeers,
commands.ChaincodeInstall{
Name: "mycc",
Version: "0.0",
Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd",
},
)
network.InstantiateChaincode(
testPeers[0],
commands.ChaincodeInstantiate{
ChannelID: "testchannel",
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
Name: "mycc",
Version: "0.0",
Ctor: `{"Args":["init","a","100","b","200"]}`,
Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`,
},
)
chaincode := nwo.Chaincode{
Name: "mycc",
Version: "0.0",
Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd",
Ctor: `{"Args":["init","a","100","b","200"]}`,
Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`,
}
nwo.InstallChaincode(network, chaincode, testPeers...)
nwo.InstantiateChaincode(network, "testchannel", orderer, chaincode, testPeers[0])
nwo.EnsureInstantiated(network, "testchannel", "mycc", "0.0", testPeers...)
RunQueryInvokeQuery(network, orderer, testPeers[0])
})
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment