github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/integration/sbe/sbe_test.go (about) 1 /* 2 Copyright hechain All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package sbe 8 9 import ( 10 "encoding/json" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "strings" 15 "syscall" 16 17 docker "github.com/fsouza/go-dockerclient" 18 "github.com/hechain20/hechain/integration/nwo" 19 "github.com/hechain20/hechain/integration/nwo/commands" 20 "github.com/hyperledger/fabric-protos-go/common" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 "github.com/onsi/gomega/gbytes" 24 "github.com/onsi/gomega/gexec" 25 "github.com/tedsuo/ifrit" 26 ) 27 28 var _ = Describe("SBE_E2E", func() { 29 var ( 30 testDir string 31 client *docker.Client 32 network *nwo.Network 33 chaincode nwo.Chaincode 34 process ifrit.Process 35 tempDir string 36 ) 37 38 BeforeEach(func() { 39 var err error 40 testDir, err = ioutil.TempDir("", "e2e_sbe") 41 Expect(err).NotTo(HaveOccurred()) 42 43 client, err = docker.NewClientFromEnv() 44 Expect(err).NotTo(HaveOccurred()) 45 46 chaincode = nwo.Chaincode{ 47 Name: "mycc", 48 Version: "0.0", 49 Path: "github.com/hechain20/hechain/integration/chaincode/keylevelep/cmd", 50 Ctor: `{"Args":["init"]}`, 51 CollectionsConfig: "testdata/collection_config.json", 52 } 53 54 tempDir, err = ioutil.TempDir("", "sbe") 55 Expect(err).NotTo(HaveOccurred()) 56 }) 57 58 AfterEach(func() { 59 if process != nil { 60 process.Signal(syscall.SIGTERM) 61 Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive()) 62 } 63 if network != nil { 64 network.Cleanup() 65 } 66 os.RemoveAll(tempDir) 67 os.RemoveAll(testDir) 68 }) 69 70 Describe("basic solo network with 2 orgs", func() { 71 BeforeEach(func() { 72 network = nwo.New(nwo.BasicSolo(), testDir, client, StartPort(), components) 73 network.GenerateConfigTree() 74 network.Bootstrap() 75 76 networkRunner := network.NetworkGroupRunner() 77 process = ifrit.Invoke(networkRunner) 78 Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed()) 79 }) 80 81 It("executes a basic solo network with 2 orgs and SBE checks", func() { 82 By("getting the orderer by name") 83 orderer := network.Orderer("orderer") 84 85 By("setting up the channel") 86 network.CreateAndJoinChannel(orderer, "testchannel") 87 88 By("updating the anchor peers") 89 network.UpdateChannelAnchors(orderer, "testchannel") 90 91 By("deploying the chaincode") 92 nwo.DeployChaincodeLegacy(network, "testchannel", orderer, chaincode) 93 94 By("deploying a second instance of the chaincode") 95 chaincode.Name = "mycc2" 96 nwo.DeployChaincodeLegacy(network, "testchannel", orderer, chaincode) 97 98 RunSBE(network, orderer, "pub") 99 RunSBE(network, orderer, "priv") 100 }) 101 102 It("executes a basic solo network with 2 orgs and SBE checks with _lifecycle", func() { 103 chaincode = nwo.Chaincode{ 104 Name: "mycc", 105 Version: "0.0", 106 Path: "github.com/hechain20/hechain/integration/chaincode/keylevelep/cmd", 107 Lang: "golang", 108 PackageFile: filepath.Join(tempDir, "simplecc.tar.gz"), 109 Ctor: `{"Args":["init"]}`, 110 SignaturePolicy: `OR('Org1MSP.member','Org2MSP.member')`, 111 Sequence: "1", 112 InitRequired: true, 113 Label: "my_simple_chaincode", 114 CollectionsConfig: "testdata/collection_config.json", 115 } 116 117 By("getting the orderer by name") 118 orderer := network.Orderer("orderer") 119 120 By("setting up the channel") 121 network.CreateAndJoinChannel(orderer, "testchannel") 122 123 By("updating the anchor peers") 124 network.UpdateChannelAnchors(orderer, "testchannel") 125 network.VerifyMembership(network.PeersWithChannel("testchannel"), "testchannel") 126 127 By("enabling 2.0 application capabilities") 128 nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0")) 129 130 By("deploying the chaincode") 131 nwo.DeployChaincode(network, "testchannel", orderer, chaincode) 132 133 By("deploying a second instance of the chaincode") 134 chaincode.Name = "mycc2" 135 chaincode.PackageFile = filepath.Join(tempDir, "simplecc2.tar.gz") 136 chaincode.Label = "my_other_simple_chaincode" 137 nwo.DeployChaincode(network, "testchannel", orderer, chaincode) 138 139 RunSBE(network, orderer, "pub") 140 RunSBE(network, orderer, "priv") 141 }) 142 }) 143 }) 144 145 func RunSBE(n *nwo.Network, orderer *nwo.Orderer, mode string) { 146 peerOrg1 := n.Peer("Org1", "peer0") 147 peerOrg2 := n.Peer("Org2", "peer0") 148 149 By("org1 initializes the key") 150 sess, err := n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 151 ChannelID: "testchannel", 152 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 153 Name: "mycc", 154 Ctor: `{"Args":["setval", "` + mode + `", "foo"]}`, 155 PeerAddresses: []string{ 156 n.PeerAddress(peerOrg1, nwo.ListenPort), 157 }, 158 WaitForEvent: true, 159 }) 160 Expect(err).NotTo(HaveOccurred()) 161 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 162 163 syncLedgerHeights(n, peerOrg1, peerOrg2) 164 165 By("org2 checks that setting the value was successful by reading it") 166 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 167 ChannelID: "testchannel", 168 Name: "mycc", 169 Ctor: `{"Args":["getval", "` + mode + `"]}`, 170 }) 171 Expect(err).NotTo(HaveOccurred()) 172 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 173 Expect(sess).To(gbytes.Say("foo")) 174 175 By("org1 adds org1 to the state-based ep of a key") 176 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 177 ChannelID: "testchannel", 178 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 179 Name: "mycc", 180 Ctor: `{"Args":["addorgs", "` + mode + `", "Org1MSP"]}`, 181 PeerAddresses: []string{ 182 n.PeerAddress(peerOrg1, nwo.ListenPort), 183 }, 184 WaitForEvent: true, 185 }) 186 Expect(err).NotTo(HaveOccurred()) 187 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 188 189 By("checking that the modification succeeded through listing the orgs in the ep") 190 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 191 ChannelID: "testchannel", 192 Name: "mycc", 193 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 194 }) 195 Expect(err).NotTo(HaveOccurred()) 196 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 197 Expect(sess).To(gbytes.Say("Org1MSP")) 198 199 By("org1 sets the value of the key") 200 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 201 ChannelID: "testchannel", 202 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 203 Name: "mycc", 204 Ctor: `{"Args":["setval", "` + mode + `", "val1"]}`, 205 PeerAddresses: []string{ 206 n.PeerAddress(peerOrg1, nwo.ListenPort), 207 }, 208 WaitForEvent: true, 209 }) 210 Expect(err).NotTo(HaveOccurred()) 211 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 212 213 syncLedgerHeights(n, peerOrg1, peerOrg2) 214 215 By("org2 checks that setting the value was successful by reading it") 216 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 217 ChannelID: "testchannel", 218 Name: "mycc", 219 Ctor: `{"Args":["getval", "` + mode + `"]}`, 220 }) 221 Expect(err).NotTo(HaveOccurred()) 222 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 223 Expect(sess).To(gbytes.Say("val1")) 224 225 By("org2 sets the value of the key") 226 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 227 ChannelID: "testchannel", 228 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 229 Name: "mycc", 230 Ctor: `{"Args":["setval", "` + mode + `", "val2"]}`, 231 PeerAddresses: []string{ 232 n.PeerAddress(peerOrg2, nwo.ListenPort), 233 }, 234 WaitForEvent: true, 235 }) 236 Expect(err).NotTo(HaveOccurred()) 237 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 238 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 239 240 By("org2 checks that setting the value was not successful by reading it") 241 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 242 ChannelID: "testchannel", 243 Name: "mycc", 244 Ctor: `{"Args":["getval", "` + mode + `"]}`, 245 }) 246 Expect(err).NotTo(HaveOccurred()) 247 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 248 Expect(sess).To(gbytes.Say("val1")) 249 250 syncLedgerHeights(n, peerOrg2, peerOrg1) 251 252 By("org1 adds org2 to the ep of the key") 253 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 254 ChannelID: "testchannel", 255 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 256 Name: "mycc", 257 Ctor: `{"Args":["addorgs", "` + mode + `", "Org2MSP"]}`, 258 PeerAddresses: []string{ 259 n.PeerAddress(peerOrg1, nwo.ListenPort), 260 }, 261 WaitForEvent: true, 262 }) 263 Expect(err).NotTo(HaveOccurred()) 264 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 265 266 By("org1 lists the orgs of the ep to check that both org1 and org2 are there") 267 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 268 ChannelID: "testchannel", 269 Name: "mycc", 270 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 271 }) 272 Expect(err).NotTo(HaveOccurred()) 273 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 274 orgs := [2]string{"Org1MSP", "Org2MSP"} 275 orgsList, err := json.Marshal(orgs) 276 Expect(err).NotTo(HaveOccurred()) 277 Expect(sess).To(gbytes.Say(string(orgsList))) 278 279 syncLedgerHeights(n, peerOrg1, peerOrg2) 280 281 By("org2 sets the value of the key") 282 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 283 ChannelID: "testchannel", 284 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 285 Name: "mycc", 286 Ctor: `{"Args":["setval", "` + mode + `", "val3"]}`, 287 PeerAddresses: []string{ 288 n.PeerAddress(peerOrg2, nwo.ListenPort), 289 }, 290 WaitForEvent: true, 291 }) 292 Expect(err).NotTo(HaveOccurred()) 293 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 294 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 295 296 By("org2 checks that seting the value was not successful by reading it") 297 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 298 ChannelID: "testchannel", 299 Name: "mycc", 300 Ctor: `{"Args":["getval", "` + mode + `"]}`, 301 }) 302 Expect(err).NotTo(HaveOccurred()) 303 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 304 Expect(sess).To(gbytes.Say("val1")) 305 306 syncLedgerHeights(n, peerOrg2, peerOrg1) 307 308 By("org1 and org2 set the value of the key") 309 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 310 ChannelID: "testchannel", 311 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 312 Name: "mycc", 313 Ctor: `{"Args":["setval", "` + mode + `", "val4"]}`, 314 PeerAddresses: []string{ 315 n.PeerAddress(peerOrg1, nwo.ListenPort), 316 n.PeerAddress(peerOrg2, nwo.ListenPort), 317 }, 318 WaitForEvent: true, 319 }) 320 Expect(err).NotTo(HaveOccurred()) 321 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 322 323 By("org1 checks that setting the value was successful by reading it") 324 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 325 ChannelID: "testchannel", 326 Name: "mycc", 327 Ctor: `{"Args":["getval", "` + mode + `"]}`, 328 }) 329 Expect(err).NotTo(HaveOccurred()) 330 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 331 Expect(sess).To(gbytes.Say("val4")) 332 333 syncLedgerHeights(n, peerOrg1, peerOrg2) 334 335 By("org2 deletes org1 from the ep of the key") 336 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 337 ChannelID: "testchannel", 338 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 339 Name: "mycc", 340 Ctor: `{"Args":["delorgs", "` + mode + `", "Org1MSP"]}`, 341 PeerAddresses: []string{ 342 n.PeerAddress(peerOrg2, nwo.ListenPort), 343 }, 344 WaitForEvent: true, 345 }) 346 Expect(err).NotTo(HaveOccurred()) 347 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 348 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 349 350 By("org2 lists the orgs of the key to check that deleting org1 did not succeed") 351 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 352 ChannelID: "testchannel", 353 Name: "mycc", 354 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 355 }) 356 Expect(err).NotTo(HaveOccurred()) 357 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 358 Expect(sess).To(gbytes.Say(string(orgsList))) 359 360 syncLedgerHeights(n, peerOrg2, peerOrg1) 361 362 By("org1 and org2 delete org1 from the ep of the key") 363 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 364 ChannelID: "testchannel", 365 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 366 Name: "mycc", 367 Ctor: `{"Args":["delorgs", "` + mode + `", "Org1MSP"]}`, 368 PeerAddresses: []string{ 369 n.PeerAddress(peerOrg1, nwo.ListenPort), 370 n.PeerAddress(peerOrg2, nwo.ListenPort), 371 }, 372 WaitForEvent: true, 373 }) 374 Expect(err).NotTo(HaveOccurred()) 375 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 376 377 By("org2 lists the orgs of the key's ep to check that removing org1 from the ep was successful") 378 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 379 ChannelID: "testchannel", 380 Name: "mycc", 381 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 382 }) 383 Expect(err).NotTo(HaveOccurred()) 384 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 385 Expect(sess).To(gbytes.Say("Org2MSP")) 386 387 By("org2 uses cc2cc invocation to set the value of the key") 388 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 389 ChannelID: "testchannel", 390 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 391 Name: "mycc2", 392 Ctor: `{"Args":["cc2cc", "testchannel", "mycc", "setval", "` + mode + `", "cc2cc_org2"]}`, 393 PeerAddresses: []string{ 394 n.PeerAddress(peerOrg2, nwo.ListenPort), 395 }, 396 WaitForEvent: true, 397 }) 398 Expect(err).NotTo(HaveOccurred()) 399 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 400 401 By("org2 reads the value of the key to check that setting it was successful") 402 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 403 ChannelID: "testchannel", 404 Name: "mycc", 405 Ctor: `{"Args":["getval", "` + mode + `"]}`, 406 }) 407 Expect(err).NotTo(HaveOccurred()) 408 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 409 Expect(sess).To(gbytes.Say("cc2cc_org2")) 410 411 syncLedgerHeights(n, peerOrg2, peerOrg1) 412 413 By("org1 uses cc2cc to set the value of the key") 414 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 415 ChannelID: "testchannel", 416 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 417 Name: "mycc2", 418 Ctor: `{"Args":["cc2cc", "testchannel", "mycc", "setval", "` + mode + `", "cc2cc_org1"]}`, 419 PeerAddresses: []string{ 420 n.PeerAddress(peerOrg1, nwo.ListenPort), 421 }, 422 WaitForEvent: true, 423 }) 424 Expect(err).NotTo(HaveOccurred()) 425 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 426 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 427 428 By("org1 reads the value of the key to check that setting it was not successful") 429 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 430 ChannelID: "testchannel", 431 Name: "mycc", 432 Ctor: `{"Args":["getval", "` + mode + `"]}`, 433 }) 434 Expect(err).NotTo(HaveOccurred()) 435 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 436 Expect(sess).To(gbytes.Say("cc2cc_org2")) 437 } 438 439 func getLedgerHeight(n *nwo.Network, peer *nwo.Peer, channelName string) int { 440 sess, err := n.PeerUserSession(peer, "User1", commands.ChannelInfo{ 441 ChannelID: channelName, 442 }) 443 Expect(err).NotTo(HaveOccurred()) 444 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 445 446 channelInfoStr := strings.TrimPrefix(string(sess.Buffer().Contents()[:]), "Blockchain info:") 447 channelInfo := common.BlockchainInfo{} 448 err = json.Unmarshal([]byte(channelInfoStr), &channelInfo) 449 Expect(err).NotTo(HaveOccurred()) 450 return int(channelInfo.Height) 451 } 452 453 func syncLedgerHeights(n *nwo.Network, peer1 *nwo.Peer, peer2 *nwo.Peer) { 454 // get height from peer1 455 height := getLedgerHeight(n, peer1, "testchannel") 456 // wait for same height on peer2 457 Eventually(func() int { 458 return getLedgerHeight(n, peer2, "testchannel") 459 }, n.EventuallyTimeout).Should(Equal(height)) 460 }