Skip to content

Commit

Permalink
Manually calculate the package id
Browse files Browse the repository at this point in the history
Signed-off-by: xiaor2 <xiaor2@illinois.edu>
  • Loading branch information
xiaor2 committed Apr 17, 2024
1 parent 4fd0fe9 commit be8b873
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/api-engine/api/routes/chaincode/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from api.common import ok, err
import threading
import hashlib


class ChainCodeViewSet(viewsets.ViewSet):
Expand Down Expand Up @@ -61,6 +62,7 @@ def _read_cc_pkg(self, pk, filename, ccpackage_path):
os.remove(meta_path)

chaincode = ChainCode.objects.get(id=pk)
chaincode.package_id = label + ":" + chaincode.package_id
chaincode.language = language
chaincode.label = label
chaincode.save()
Expand Down Expand Up @@ -134,22 +136,29 @@ def package(self, request):
f.write(chunk)

org = request.user.organization
qs = Node.objects.filter(type="peer", organization=org)
if not qs.exists():
return Response(
err("at least 1 peer node is required for the chaincode package upload."),
status=status.HTTP_400_BAD_REQUEST
)
peer_node = qs.first()
envs = init_env_vars(peer_node, org)
peer_channel_cli = PeerChainCode("v2.2.0", **envs)
return_code, content = peer_channel_cli.lifecycle_calculatepackageid(temp_cc_path)
if (return_code != 0):
return Response(
err("calculate packageid failed for {}.".format(content)),
status=status.HTTP_400_BAD_REQUEST
)
packageid = content.strip()
# qs = Node.objects.filter(type="peer", organization=org)
# if not qs.exists():
# return Response(
# err("at least 1 peer node is required for the chaincode package upload."),
# status=status.HTTP_400_BAD_REQUEST
# )
# peer_node = qs.first()
# envs = init_env_vars(peer_node, org)
# peer_channel_cli = PeerChainCode("v2.2.0", **envs)
# return_code, content = peer_channel_cli.lifecycle_calculatepackageid(temp_cc_path)
# if (return_code != 0):
# return Response(
# err("calculate packageid failed for {}.".format(content)),
# status=status.HTTP_400_BAD_REQUEST
# )
# packageid = content.strip()

# manually calculate the package id
sha256_hash = hashlib.sha256()
with open(temp_cc_path, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
packageid = sha256_hash.hexdigest()

# check if packageid exists
cc = ChainCode.objects.filter(package_id=packageid)
Expand Down

0 comments on commit be8b873

Please sign in to comment.