Skip to content

Commit

Permalink
Avoided adding the same chaincode package
Browse files Browse the repository at this point in the history
Signed-off-by: xiaor2 <xiaor2@illinois.edu>
  • Loading branch information
xiaor2 committed Apr 22, 2024
1 parent 090d4b3 commit 192a70a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/api-engine/api/routes/chaincode/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +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.package_id = chaincode.package_id
chaincode.language = language
chaincode.label = label
chaincode.save()
Expand Down Expand Up @@ -135,6 +135,22 @@ def package(self, request):
for chunk in file.chunks():
f.write(chunk)

with tarfile.open(temp_cc_path, "r:gz") as tar:
# Locate the metadata file
metadata_file = None
for member in tar.getmembers():
if member.name.endswith("metadata.json"):
metadata_file = member
break

if metadata_file is not None:
# Extract the metadata file
metadata_content = tar.extractfile(metadata_file).read().decode("utf-8")
metadata = json.loads(metadata_content)
label = metadata.get("label")
else:
print("Metadata file not found in the chaincode package.")

org = request.user.organization
# qs = Node.objects.filter(type="peer", organization=org)
# if not qs.exists():
Expand All @@ -158,7 +174,7 @@ def package(self, request):
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()
packageid = label + ":" + sha256_hash.hexdigest()

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

0 comments on commit 192a70a

Please sign in to comment.