Skip to content

Commit c13400c

Browse files
committed
Add blocksizes to data buffer
1 parent f4b722a commit c13400c

3 files changed

Lines changed: 36 additions & 16 deletions

File tree

ethpm/utils/ipfs.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from eth_utils import to_text
1010
from google.protobuf.descriptor import Descriptor
1111

12-
from ethpm._utils.protobuf.ipfs_file_pb2 import Data, PBNode
12+
from ethpm._utils.protobuf.ipfs_file_pb2 import Data, PBNode, PBLink
1313
from ethpm._utils.protobuf.unixfs_pb2 import Data as uData
1414
from ethpm._utils.protobuf.unixfs_pb2 import Metadata as uMetadata
1515
from ethpm._utils.protobuf.merkledag_pb2 import PBNode as uPBNode
@@ -110,11 +110,10 @@ def generate_file_hash(content_bytes: bytes) -> str:
110110

111111
def serialize_chunks(file_bytes: bytes) -> Descriptor:
112112
file_size = len(file_bytes)
113-
links = generate_links(file_bytes)
113+
links, block_sizes = generate_links(file_bytes)
114114
data_protobuf = uData(
115115
Type=uData.DataType.Value("File"),
116-
Data=b'',
117-
filesize=file_size,
116+
blocksizes=block_sizes,
118117
)
119118
data_protobuf_bytes = data_protobuf.SerializeToString()
120119
file_protobuf = uPBNode(Links=links, Data=data_protobuf_bytes)
@@ -131,11 +130,11 @@ def generate_links(content_bytes):
131130
gen_single_link(content_bytes, i)
132131
for i in range(0, len(content_bytes), IPFS_CHUNK_SIZE)
133132
]
134-
return links
133+
block_sizes = [link.Tsize - 14 for link in links]
134+
return links, block_sizes
135135

136136

137137
def gen_single_link(content_bytes, i):
138138
chunk = content_bytes[i : i + IPFS_CHUNK_SIZE]
139139
chunk_hash = generate_file_hash(chunk)
140-
# not sure why adding 14 is necessary, but is required to make links match ls output size
141-
return uPBLink(Hash=to_bytes(text=chunk_hash), Name="", Tsize=len(chunk) + 14)
140+
return uPBLink(Hash=to_bytes(text=chunk_hash), Name=b'', Tsize=len(chunk) + 14)

tests/ethpm/utils/test_ipfs_utils.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_generate_file_hash(tmpdir, file_name, file_contents, expected):
119119

120120

121121
def test_generate_file_hash_for_large_files(tmp_path):
122-
expected_links = (
122+
pic_expected_links = (
123123
(b'QmZ5RgT3jJhRNMEgLSEsez9uz1oDnNeAysLLxRco8jz5Be', 262158),
124124
(b'QmUZvm5TertyZagJfoaw5E5DRvH6Ssu4Wsdfw69NHaNRTc', 262158),
125125
(b'QmTA3tDxTZn5DGaDshGTu9XHon3kcRt17dgyoomwbJkxvJ', 262158),
@@ -136,12 +136,32 @@ def test_generate_file_hash_for_large_files(tmp_path):
136136
(b'QmNsx32qEiEcHRL1TFcy2bPvwqjHZGp62mbcVa9FUpY9Z5', 262158),
137137
(b'QmVx2NfXEvHaS8uaRTYaF4ExeLaCSGpTSDhhYBEAembdbk', 69716),
138138
)
139-
expected_root_hash = 'QmQgQUbBeMTnH1j3QWwNw9LkXjpWDJrjyGYfZpnPp8x5Lu'
140-
test = Path(__file__).parent / 'pic.jpg'
141-
ipfs_multihash = generate_links(test.read_bytes())
142-
hashes = [(x.Hash, x.Tsize) for x in ipfs_multihash]
143-
for x in expected_links:
144-
assert x in hashes
139+
txt_expected_links = (
140+
(b'QmbYfXF7A7hgXUzEZSifji3D6Nf856kdK8Edu8n7knQSHr', 262158),
141+
(b'QmZw2JEKQu8n5zisuU7JDAeWSUm2XvqjqV72bizCdDQUDM', 19278),
142+
)
143+
pic_expected_root_hash = 'QmQgQUbBeMTnH1j3QWwNw9LkXjpWDJrjyGYfZpnPp8x5Lu'
144+
txt_exp_hash = 'QmegXsU7EopJ9EVj9ELwgHGE75Xke6FYBP84PaygiAsqB5'
145+
pic_path = Path(__file__).parent / 'pic.jpg'
146+
txt_path = Path(__file__).parent / 'txt.txt'
147+
txt_multihash, txt_blocksizes = generate_links(txt_path.read_bytes())
148+
pic_multihash, pic_blocksizes = generate_links(pic_path.read_bytes())
149+
txt_hashes = [(x.Hash, x.Tsize) for x in txt_multihash]
150+
pic_hashes = [(x.Hash, x.Tsize) for x in pic_multihash]
151+
for x in txt_expected_links:
152+
assert x in txt_hashes
153+
for y in pic_expected_links:
154+
assert y in pic_hashes
145155
# test works until here, where it has to calculate the root hash
146-
root_hash = generate_file_hash(test.read_bytes())
147-
assert root_hash == expected_root_hash
156+
txt_root_hash = generate_file_hash(txt_path.read_bytes())
157+
pic_root_hash = generate_file_hash(pic_path.read_bytes())
158+
print("txt: actual / expected")
159+
print(txt_root_hash)
160+
print(txt_exp_hash)
161+
print(f'txt blocksizes: count: {len(txt_blocksizes)}, set: {set(txt_blocksizes)}')
162+
print("pic: actual / expected")
163+
print(pic_root_hash)
164+
print(pic_expected_root_hash)
165+
print(f'pic blocksizes: count: {len(pic_blocksizes)}, set: {set(pic_blocksizes)}')
166+
assert txt_root_hash == txt_exp_hash
167+
assert pic_root_hash == pic_expected_root_hash

tests/ethpm/utils/txt.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)