Skip to content

Commit bedc9c0

Browse files
committed
22 Nov Update. integration/modules/test_chunk.py all tests added & passed.structlog added for logging.
1 parent 73191d3 commit bedc9c0

File tree

18 files changed

+258
-103
lines changed

18 files changed

+258
-103
lines changed

Diff for: pdm.lock

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ description = "Python client library for connecting to Bee decentralised storage
66
authors = [{ name = "SAIKAT KARMAKAR", email = "[email protected]" }]
77
dependencies = [
88
"eth-ape>=0.6.25",
9+
"structlog>=23.2.0",
910
]
1011
requires-python = ">=3.9"
1112
readme = "README.md"

Diff for: setup_integration_test.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
bee-factory start --detach 1.9.0
4+
5+
# BEE_POSTAGE
6+
bee_postage=$(echo "Y" | swarm-cli stamp buy --bee-api-url http://localhost:1633/ --depth 23 --amount 100000000000 | grep "Stamp ID" | cut -d ' ' -f3)
7+
8+
# BEE_PEER_POSTAGE
9+
bee_peer_postage=$(echo "Y" | swarm-cli stamp buy --bee-api-url http://localhost:11633/ --depth 23 --amount 100000000000 | grep "Stamp ID" | cut -d ' ' -f3)
10+
11+
echo "BEE_POSTAGE: $bee_posage"
12+
echo "BEE_PEER_POSTAGE: $bee_peer_posage"

Diff for: src/bee_py/chunk/cac.py

-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ def __init__(self, data: bytes, span: bytes):
2727
self.data = data
2828
self.span = span
2929

30-
def data(self):
31-
return self.data
32-
33-
def span(self):
34-
return self.span
35-
3630
def payload(self) -> bytes:
3731
return flex_bytes_at_offset(self.data, CAC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE)
3832

Diff for: src/bee_py/chunk/soc.py

+79-35
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from ape.managers.accounts import AccountAPI
44
from eth_typing import ChecksumAddress as AddressType
5-
from pydantic import BaseModel
5+
from hexbytes import HexBytes
6+
from pydantic.dataclasses import dataclass
67

78
from bee_py.chunk.bmt import bmt_hash
89
from bee_py.chunk.cac import (
@@ -21,7 +22,7 @@
2122
from bee_py.utils.bytes import bytes_at_offset, bytes_equal, flex_bytes_at_offset
2223
from bee_py.utils.error import BeeError
2324
from bee_py.utils.hash import keccak256_hash
24-
from bee_py.utils.hex import bytes_to_hex
25+
from bee_py.utils.hex import bytes_to_hex, hex_to_bytes
2526

2627
# * Global variables
2728
IDENTIFIER_SIZE = 32
@@ -36,7 +37,8 @@
3637
Identifier = NewType("Identifier", bytes)
3738

3839

39-
class SingleOwnerChunkBase(BaseModel):
40+
@dataclass
41+
class SingleOwnerChunkBase:
4042
"""Abstract base class for Single Owner Chunks (SOCs).
4143
4244
Represents a SOC, a type of chunk that allows a user to assign arbitrary data to an address
@@ -61,12 +63,32 @@ class SingleOwnerChunk(SingleOwnerChunkBase, Chunk):
6163
with all its properties and behaviors defined.
6264
6365
Attributes:
66+
data: The data contained in the SOC.
6467
identifier: The identifier of the SOC.
6568
signature: The signature of the SOC.
69+
span: The span of the SOC.
70+
payload: The payload of the SOC.
71+
address: The address of the SOC.
6672
owner: The owner of the SOC.
6773
"""
6874

69-
pass
75+
def __init__(
76+
self,
77+
data: bytes,
78+
identifier: Identifier,
79+
signature: bytes,
80+
span: bytes,
81+
payload: bytes,
82+
address: HexBytes,
83+
owner: AddressType,
84+
):
85+
SingleOwnerChunkBase.__init__(self, identifier, signature, owner)
86+
self.data = data
87+
self.span = span
88+
self.payload = payload
89+
self.address = address
90+
91+
# address: AddressType
7092

7193

7294
def recover_chunk_owner(data: bytes) -> AddressType:
@@ -88,7 +110,7 @@ def recover_chunk_owner(data: bytes) -> AddressType:
88110
return owner_address
89111

90112

91-
def make_single_owner_chunk_from_data(data: bytes, address: AddressType) -> dict:
113+
def make_single_owner_chunk_from_data(data: bytes, address: AddressType) -> SingleOwnerChunk:
92114
"""
93115
Verifies if the data is a valid single owner chunk.
94116
@@ -116,26 +138,36 @@ def span():
116138
def payload():
117139
return flex_bytes_at_offset(data, SOC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE)
118140

119-
return {
120-
"data": data,
121-
"identifier": identifier,
122-
"signature": signature(),
123-
"span": span(),
124-
"payload": payload(),
125-
"address": soc_address,
126-
"owner": owner_address,
127-
}
141+
# return {
142+
# "data": data,
143+
# "identifier": identifier,
144+
# "signature": signature(),
145+
# "span": span(),
146+
# "payload": payload(),
147+
# "address": soc_address,
148+
# "owner": owner_address,
149+
# }
150+
return SingleOwnerChunk(
151+
data=data,
152+
identifier=identifier,
153+
signature=signature(),
154+
span=span(),
155+
payload=payload,
156+
address=soc_address,
157+
owner=owner_address,
158+
)
128159

129160

130161
def make_soc_address(identifier: Identifier, address: AddressType) -> bytes:
131-
return keccak256_hash(identifier, address)
162+
address_bytes = hex_to_bytes(address)
163+
return keccak256_hash(identifier, address_bytes)
132164

133165

134166
def make_single_owner_chunk(
135167
chunk: Chunk,
136168
identifier: Identifier,
137169
signer: AccountAPI,
138-
) -> dict:
170+
) -> SingleOwnerChunk:
139171
"""
140172
Creates a single owner chunk object.
141173
@@ -145,33 +177,43 @@ def make_single_owner_chunk(
145177
signer: The singer interface for signing the chunk.
146178
147179
Returns:
148-
dict: A dictionary representing a single owner chunk.
180+
SingleOwnerChunk: SingleOwnerChunk object.
149181
"""
150182
chunk_address = chunk.address()
151-
assert_valid_chunk_data(chunk.data(), chunk_address)
183+
assert_valid_chunk_data(chunk.data, chunk_address)
152184

153185
digest = keccak256_hash(identifier, chunk_address)
154186
signature = sign(account=signer, data=digest)
155-
data = serialize_bytes(identifier, signature.encode_vrs(), chunk.span(), chunk.payload())
156-
address = make_soc_address(identifier, signer.get("address", ""))
157-
158-
return {
159-
"data": data,
160-
"identifier": identifier,
161-
"signature": signature,
162-
"span": chunk.span(),
163-
"payload": chunk.payload(),
164-
"address": address,
165-
"owner": signer.get("address", ""),
166-
}
187+
data = serialize_bytes(identifier, signature.encode_vrs(), chunk.span, chunk.payload())
188+
address = make_soc_address(identifier, signer.address)
189+
190+
# return {
191+
# "data": data,
192+
# "identifier": identifier,
193+
# "signature": signature,
194+
# "span": chunk.span,
195+
# "payload": chunk.payload(),
196+
# "address": address,
197+
# "owner": signer.address,
198+
# }
199+
200+
return SingleOwnerChunk(
201+
data=data,
202+
identifier=identifier,
203+
signature=signature.encode_vrs(),
204+
span=chunk.span,
205+
payload=chunk.payload(),
206+
address=address,
207+
owner=signer.address,
208+
)
167209

168210

169211
def upload_single_owner_chunk(
170212
request_options: BeeRequestOptions,
171213
chunk: SingleOwnerChunk,
172214
postage_batch_id: BatchId,
173215
options: Optional[UploadOptions] = None,
174-
) -> str:
216+
) -> Reference:
175217
"""Uploads a Single Owner Chunk (SOC) to the Bee network.
176218
177219
Args:
@@ -183,14 +225,16 @@ def upload_single_owner_chunk(
183225
Returns:
184226
A Reference object representing the uploaded chunk.
185227
"""
186-
187-
# Convert the owner, identifier, and signature to hexadecimal strings
188-
owner = bytes_to_hex(chunk.owner)
228+
# * Convert the owner, identifier, and signature to hexadecimal strings
229+
if isinstance(chunk.owner, bytes):
230+
owner = bytes_to_hex(chunk.owner)
231+
else:
232+
owner = chunk.owner
189233
identifier = bytes_to_hex(chunk.identifier)
190234
signature = bytes_to_hex(chunk.signature)
191235

192236
# Serialize the chunk data, including the span and payload
193-
data = serialize_bytes(chunk.span(), chunk.payload())
237+
data = serialize_bytes(chunk.span, chunk.payload)
194238

195239
# Upload the SOC data using the SOC API's upload method
196240
return upload(request_options, owner, identifier, signature, data, postage_batch_id, options)

Diff for: src/bee_py/feed/json.py

Whitespace-only changes.

Diff for: src/bee_py/modules/chunk.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from bee_py.utils.headers import extract_upload_headers
66
from bee_py.utils.http import http
77

8-
ENDPOINT = "chunk"
8+
ENDPOINT = "chunks"
99

1010

1111
def upload(
@@ -22,14 +22,12 @@ def upload(
2222
Returns:
2323
The reference of the uploaded data.
2424
"""
25-
headers = extract_upload_headers(postage_batch_id, options)
2625

2726
config = {
28-
"url": request_options["url"] + f"/{ENDPOINT}",
27+
"url": f"/{ENDPOINT}",
2928
"method": "post",
3029
"data": data,
31-
"headers": headers,
32-
"responseType": "json",
30+
"headers": {**extract_upload_headers(postage_batch_id, options)},
3331
}
3432

3533
response = http(request_options, config)
@@ -40,11 +38,10 @@ def upload(
4038
return response.json()["reference"]
4139

4240

43-
def downalod(request_options: BeeRequestOptions, _hash: ReferenceOrENS) -> Data:
41+
def download(request_options: BeeRequestOptions, _hash: ReferenceOrENS) -> Data:
4442
config = {
45-
"url": f"{request_options['url']}/{ENDPOINT}/{_hash}",
43+
"url": f"/{ENDPOINT}/{_hash}",
4644
"method": "get",
47-
"responseType": "arraybuffer",
4845
}
4946
response = http(request_options, config)
5047

Diff for: src/bee_py/modules/debug/balance.py

Whitespace-only changes.

Diff for: src/bee_py/modules/debug/chunk.py

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def check_if_chunk_exists_locally(
2424
request_options,
2525
{
2626
"url": f"{ENDPOINT}/{address}",
27-
"responseType": "json",
2827
},
2928
)
3029
status_code = response.status_code
@@ -54,7 +53,6 @@ def delete_chunk_from_local_storage(
5453
{
5554
"method": "delete",
5655
"url": f"{ENDPOINT}/{address}",
57-
"responseType": "json",
5856
},
5957
)
6058
status_code = response.status_code

Diff for: src/bee_py/modules/feed.py

-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def create_feed_manifest(
9595
"""
9696
config = {
9797
"method": "post",
98-
"responseType": "json",
9998
"url": f"{FEED_ENDPOINT}/{owner}/{topic}",
10099
"params": options,
101100
"headers": extract_upload_headers(postage_batch_id),
@@ -156,7 +155,6 @@ def fetch_latest_feed_update(
156155
response = http(
157156
request_options,
158157
{
159-
"responseType": "json",
160158
"url": f"{FEED_ENDPOINT}/{owner}/{topic}",
161159
"params": options,
162160
},

Diff for: src/bee_py/modules/soc.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from typing import Optional
22

3+
from structlog import get_logger
4+
35
from bee_py.types.type import BatchId, BeeRequestOptions, Reference, UploadOptions
46
from bee_py.utils.headers import extract_upload_headers
7+
from bee_py.utils.hex import remove_0x_prefix
58
from bee_py.utils.http import http
69

710
SOC_ENDPOINT = "soc"
811

12+
logger = get_logger()
13+
914

1015
def upload(
1116
request_options: BeeRequestOptions,
@@ -31,6 +36,11 @@ def upload(
3136
Returns:
3237
Reference: The reference of the uploaded chunk.
3338
"""
39+
# https://docs.ethswarm.org/api/#tag/Single-owner-chunk
40+
owner = remove_0x_prefix(owner)
41+
identifier = remove_0x_prefix(identifier)
42+
signature = remove_0x_prefix(signature)
43+
3444
config = {
3545
"method": "post",
3646
"url": f"{SOC_ENDPOINT}/{owner}/{identifier}",
@@ -39,10 +49,13 @@ def upload(
3949
"content-type": "application/octet-stream",
4050
**extract_upload_headers(postage_batch_id, options),
4151
},
42-
"responseType": "json",
4352
"params": {"sig": signature},
4453
}
54+
logger.debug(f"\n***********Config***********\n{config}")
55+
4556
response = http(request_options, config)
46-
response.raise_for_status()
57+
if response.status_code != 200: # noqa: PLR2004
58+
print(response.json()) # noqa: T201
59+
logger.error(response.raise_for_status())
4760

4861
return response.json()["reference"]

0 commit comments

Comments
 (0)