Skip to content

Commit d6bd74b

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

3 files changed

Lines changed: 51 additions & 31 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=None, Tsize=len(chunk) + 14)

tests/ethpm/utils/test_ipfs_utils.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,49 @@ 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 = (
123-
(b'QmZ5RgT3jJhRNMEgLSEsez9uz1oDnNeAysLLxRco8jz5Be', 262158),
124-
(b'QmUZvm5TertyZagJfoaw5E5DRvH6Ssu4Wsdfw69NHaNRTc', 262158),
125-
(b'QmTA3tDxTZn5DGaDshGTu9XHon3kcRt17dgyoomwbJkxvJ', 262158),
126-
(b'QmXRkS2AtimY2gujGJDXjSSkpt2Xmgog6FjtmEzt2PwcsA', 262158),
127-
(b'QmVuqvYLEo76hJVE9c5h9KP2MbQuTxSFyntV22qdz6F1Dr', 262158),
128-
(b'QmbsEhRqFwKAUoc6ivZyPa1vGUxFKBT4ciH79gVszPcFEG', 262158),
129-
(b'QmegS44oDgNU2hnD3j8r1WH8xZ2RWfe3Z5eb6aJRHXwJsw', 262158),
130-
(b'QmbC1ZyGUoxZrmTTjgmiB3KSRRXJFkhpnyKYkiVC6PUMzf', 262158),
131-
(b'QmZvpEyzP7C8BABesRvpYWPec2HGuzgnTg4VSPiTpQWGpy', 262158),
132-
(b'QmZhzU2QJF4rUpRSWZxjutWz22CpFELmcNXkGAB1GVb26H', 262158),
133-
(b'QmZeXvgS1NTxtVv9AeHMpA9oGCRrnVTa9bSCSDgAt52iyT', 262158),
134-
(b'QmPy1wpe1mACVrXRBtyxriT2T5AffZ1SUkE7xxnAHo4Dvs', 262158),
135-
(b'QmcHbhgwAVddCyFVigt2DLSg8FGaQ1GLqkyy5M3U5DvTc6', 262158),
136-
(b'QmNsx32qEiEcHRL1TFcy2bPvwqjHZGp62mbcVa9FUpY9Z5', 262158),
137-
(b'QmVx2NfXEvHaS8uaRTYaF4ExeLaCSGpTSDhhYBEAembdbk', 69716),
122+
pic_expected_links = (
123+
(b'QmZ5RgT3jJhRNMEgLSEsez9uz1oDnNeAysLLxRco8jz5Be', 262144),
124+
(b'QmUZvm5TertyZagJfoaw5E5DRvH6Ssu4Wsdfw69NHaNRTc', 262144),
125+
(b'QmTA3tDxTZn5DGaDshGTu9XHon3kcRt17dgyoomwbJkxvJ', 262144),
126+
(b'QmXRkS2AtimY2gujGJDXjSSkpt2Xmgog6FjtmEzt2PwcsA', 262144),
127+
(b'QmVuqvYLEo76hJVE9c5h9KP2MbQuTxSFyntV22qdz6F1Dr', 262144),
128+
(b'QmbsEhRqFwKAUoc6ivZyPa1vGUxFKBT4ciH79gVszPcFEG', 262144),
129+
(b'QmegS44oDgNU2hnD3j8r1WH8xZ2RWfe3Z5eb6aJRHXwJsw', 262144),
130+
(b'QmbC1ZyGUoxZrmTTjgmiB3KSRRXJFkhpnyKYkiVC6PUMzf', 262144),
131+
(b'QmZvpEyzP7C8BABesRvpYWPec2HGuzgnTg4VSPiTpQWGpy', 262144),
132+
(b'QmZhzU2QJF4rUpRSWZxjutWz22CpFELmcNXkGAB1GVb26H', 262144),
133+
(b'QmZeXvgS1NTxtVv9AeHMpA9oGCRrnVTa9bSCSDgAt52iyT', 262144),
134+
(b'QmPy1wpe1mACVrXRBtyxriT2T5AffZ1SUkE7xxnAHo4Dvs', 262144),
135+
(b'QmcHbhgwAVddCyFVigt2DLSg8FGaQ1GLqkyy5M3U5DvTc6', 262144),
136+
(b'QmNsx32qEiEcHRL1TFcy2bPvwqjHZGp62mbcVa9FUpY9Z5', 262144),
137+
(b'QmVx2NfXEvHaS8uaRTYaF4ExeLaCSGpTSDhhYBEAembdbk', 69702),
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', 262144),
141+
(b'QmZw2JEKQu8n5zisuU7JDAeWSUm2XvqjqV72bizCdDQUDM', 19264),
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)