Skip to content

Commit

Permalink
Merge pull request #327 from tianxuanhong/issue-#326
Browse files Browse the repository at this point in the history
[#issue-326] cmdlinelib feature of approveformyorg && queryapproved f…
  • Loading branch information
XuHugo authored Sep 9, 2021
2 parents 4cd4a9f + 047ffb8 commit 224121c
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions src/api-engine/api/lib/peer/chaincode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ def __init__(self, version="2.2.0", peer=FABRIC_TOOL, **kwargs):
self.peer = peer + "/peer"
super(ChainCode, self).__init__(version, **kwargs)

def lifecycle_package(self, cc_name, cc_path, language, label):
def lifecycle_package(self, cc_name, cc_version, cc_path, language):
"""
package the chaincode to a tar.gz file.
:param cc_name: chaincode name
:param cc_version: chaincode version
:param cc_path: where the chaincode is
:param language: Chain code development language, default: golang
:param label: Label of the generated chain code package
:return 0 means success.
"""
try:
label = cc_name+"_"+cc_version
res = os.system("{} lifecycle chaincode package {}.tar.gz --path {} --lang {} --label {}"
.format(self.peer, cc_name, cc_path, language, label))
res = res >> 8
Expand Down Expand Up @@ -94,3 +95,69 @@ def lifecycle_get_installed_package(self, timeout):
raise Exception(err_msg)
return res_return

def lifecycle_approve_for_my_org(self, orderer_url, orderer_tls_rootcert, channel_name, cc_name,
chaincode_version, policy):
"""
The administrator can use the peer lifecycle chaincode approveformyorg subcommand to approve the chain code on
behalf of the organization.
:param orderer_url: orderer accessable url
:param orderer_tls_rootcert: orderer tls certificate
:param channel_name: channel name
:param cc_name: chaincode name
:param chaincode_version: chaincode version
:param policy: chaincode policy
:return:
"""
res, installed = self.lifecycle_query_installed("3s")
cc_label = cc_name+"_"+chaincode_version
package_id = ""
for each in installed['installed_chaincodes']:
if each['label'] == cc_label:
package_id = each['package_id']
break
if package_id == "":
return 1, "not exist the chaincode, please check chaincode_name and chaincode_version"

if os.getenv("CORE_PEER_TLS_ENABLED") == "false" or os.getenv("CORE_PEER_TLS_ENABLED") is None:
res = subprocess.Popen("{} lifecycle chaincode approveformyorg -o {} - --channelID {} --name {} "
"--version {} --init-required --package-id {} --sequence 1 --signature-policy {}"
.format(self.peer, orderer_url, channel_name, cc_name, chaincode_version, package_id,
policy), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
res = subprocess.Popen("{} lifecycle chaincode approveformyorg -o {} --tls --cafile {} --channelID {} "
"--name {} --version {} --init-required --package-id {} --sequence 1 "
"--signature-policy {}"
.format(self.peer, orderer_url, orderer_tls_rootcert, channel_name,
cc_name, chaincode_version, package_id, policy), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = res.communicate()
return_code = res.returncode

if return_code == 0:
content = str(stdout, encoding="utf-8")
else:
stderr = str(stderr, encoding="utf-8")
return return_code, stderr
return return_code, content

def lifecycle_query_approved(self, channel_name, cc_name):
"""
query_approved chaincode information.
:param channel_name: channel name
:param cc_name: chaincode name
:return:
"""

res = subprocess.Popen("{} lifecycle chaincode queryapproved --output json --channelID {}"
" --name {}".format(self.peer, channel_name, cc_name),
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = res.communicate()
return_code = res.returncode
if return_code == 0:
content = str(stdout, encoding="utf-8")
chaincodes_info = json.loads(content)
else:
stderr = str(stderr, encoding="utf-8")
return return_code, stderr

return return_code, chaincodes_info

0 comments on commit 224121c

Please sign in to comment.