Skip to content

Commit

Permalink
[FAB-11051] refactor install/instantiate/upgrade
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
sykesm committed Jul 10, 2018
1 parent 81fb533 commit edec005
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 77 deletions.
53 changes: 46 additions & 7 deletions integration/nwo/commands/peer.go
Expand Up @@ -108,20 +108,21 @@ 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 {
return "peer-chaincode-instantiate"
}

func (c ChaincodeInstantiate) Args() []string {
return []string{
args := []string{
"chaincode", "instantiate",
"--channelID", c.ChannelID,
"--orderer", c.Orderer,
Expand All @@ -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{}
Expand Down Expand Up @@ -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
}
Expand Down
97 changes: 75 additions & 22 deletions integration/nwo/deploy.go
Expand Up @@ -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{
Expand Down
28 changes: 0 additions & 28 deletions integration/nwo/network.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 10 additions & 20 deletions integration/nwo/network_test.go
Expand Up @@ -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])
})
Expand Down

0 comments on commit edec005

Please sign in to comment.