如何在一个链码中部署多个合同(Hyperledger架构)

时间:2020-05-28 03:33:28

标签: hyperledger-fabric hyperledger-chaincode

我用fabric-contractapi-go编写了我的链码,该链码包含两个合同。我已经在fabric-chaincodedev-mode中对其进行了测试。但是,当我使用这样的命令将其部署在fabric-firstnetwork中时,无论是在命令行中还是在fabric-gateway-sdk-java中,我都无法获得第二份合同。

#package
docker exec cli peer lifecycle chaincode package helloworld.tar.gz \
--path github.com/hyperledger/fabric-samples/chaincode/helloworld/go/ \
--label helloworld_1

#install in two orgs
docker exec cli peer lifecycle chaincode install helloworld.tar.gz
docker exec \
  -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp \
  -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 \
  -e CORE_PEER_LOCALMSPID="Org2MSP" \
  -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
  cli peer lifecycle chaincode install helloworld.tar.gz 

#approve in tow orgs
docker exec cli peer lifecycle chaincode approveformyorg \
  --tls \
  --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
  --channelID mychannel --name mycc --version 1 \
  --init-required --sequence 1 --waitForEvent --package-id ${PACKAGE_ID}

docker exec \
  -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp \
  -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 \
  -e CORE_PEER_LOCALMSPID="Org2MSP" \
  -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
  cli peer lifecycle chaincode approveformyorg \
  --tls \
  --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
  --channelID mychannel --name mycc --version 1 --init-required \
  --sequence 1 --waitForEvent --package-id ${PACKAGE_ID}

#commit
docker exec cli peer lifecycle chaincode commit -o orderer.example.com:7050 \
  --tls \
  --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
  --peerAddresses peer0.org1.example.com:7051 \
  --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
  --peerAddresses peer0.org2.example.com:9051 \
  --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
  --channelID mychannel --name mycc --version 1 --sequence 1 --init-required



1 个答案:

答案 0 :(得分:0)

在你的链码中

func main () {
  contracts := []contractapi.ContractInterface{&contract.A{}, &contract.B{}}
  yourChaincode, _ := contractapi.NewChaincode(contracts...)
  if err := yourChaincode.Start();err!=nil{
    log.Panicf("Error starting chaincode: %v",err) 
  }
}

在您的应用程序中,您可以将其用作

contractA := network.GetContractWithName("yourChaincodeName","contractA")
contractB := network.GetContractWithName("yourChaincodeName","contractB")

“yourChaincodeName”是您在频道中部署链码时使用的名称