flwr 1.18.0__py3-none-any.whl → 1.19.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flwr/app/__init__.py +15 -0
- flwr/app/error.py +68 -0
- flwr/app/metadata.py +223 -0
- flwr/cli/build.py +82 -57
- flwr/cli/log.py +3 -3
- flwr/cli/login/login.py +3 -7
- flwr/cli/ls.py +15 -36
- flwr/cli/new/templates/app/code/client.baseline.py.tpl +1 -1
- flwr/cli/new/templates/app/code/model.baseline.py.tpl +1 -1
- flwr/cli/new/templates/app/code/server.baseline.py.tpl +2 -3
- flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +14 -17
- flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.jax.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +1 -1
- flwr/cli/run/run.py +10 -18
- flwr/cli/stop.py +2 -2
- flwr/cli/utils.py +31 -5
- flwr/client/__init__.py +2 -2
- flwr/client/client_app.py +1 -1
- flwr/client/clientapp/__init__.py +0 -7
- flwr/client/grpc_adapter_client/connection.py +4 -4
- flwr/client/grpc_rere_client/connection.py +130 -60
- flwr/client/grpc_rere_client/grpc_adapter.py +34 -6
- flwr/client/message_handler/message_handler.py +1 -1
- flwr/client/mod/comms_mods.py +36 -17
- flwr/client/rest_client/connection.py +173 -67
- flwr/clientapp/__init__.py +15 -0
- flwr/common/__init__.py +2 -2
- flwr/common/auth_plugin/__init__.py +2 -0
- flwr/common/auth_plugin/auth_plugin.py +29 -3
- flwr/common/constant.py +36 -7
- flwr/common/event_log_plugin/event_log_plugin.py +3 -3
- flwr/common/exit_handlers.py +30 -0
- flwr/common/heartbeat.py +165 -0
- flwr/common/inflatable.py +290 -0
- flwr/common/inflatable_grpc_utils.py +99 -0
- flwr/common/inflatable_rest_utils.py +99 -0
- flwr/common/inflatable_utils.py +341 -0
- flwr/common/message.py +110 -242
- flwr/common/record/__init__.py +2 -1
- flwr/common/record/array.py +323 -0
- flwr/common/record/arrayrecord.py +103 -225
- flwr/common/record/configrecord.py +59 -4
- flwr/common/record/conversion_utils.py +1 -1
- flwr/common/record/metricrecord.py +55 -4
- flwr/common/record/recorddict.py +69 -1
- flwr/common/recorddict_compat.py +2 -2
- flwr/common/retry_invoker.py +5 -1
- flwr/common/serde.py +59 -183
- flwr/common/serde_utils.py +175 -0
- flwr/common/typing.py +5 -3
- flwr/compat/__init__.py +15 -0
- flwr/compat/client/__init__.py +15 -0
- flwr/{client → compat/client}/app.py +19 -159
- flwr/compat/common/__init__.py +15 -0
- flwr/compat/server/__init__.py +15 -0
- flwr/compat/server/app.py +174 -0
- flwr/compat/simulation/__init__.py +15 -0
- flwr/proto/fleet_pb2.py +32 -27
- flwr/proto/fleet_pb2.pyi +49 -35
- flwr/proto/fleet_pb2_grpc.py +117 -13
- flwr/proto/fleet_pb2_grpc.pyi +47 -6
- flwr/proto/heartbeat_pb2.py +33 -0
- flwr/proto/heartbeat_pb2.pyi +66 -0
- flwr/proto/heartbeat_pb2_grpc.py +4 -0
- flwr/proto/heartbeat_pb2_grpc.pyi +4 -0
- flwr/proto/message_pb2.py +28 -11
- flwr/proto/message_pb2.pyi +125 -0
- flwr/proto/recorddict_pb2.py +16 -28
- flwr/proto/recorddict_pb2.pyi +46 -64
- flwr/proto/run_pb2.py +24 -32
- flwr/proto/run_pb2.pyi +4 -52
- flwr/proto/serverappio_pb2.py +32 -23
- flwr/proto/serverappio_pb2.pyi +45 -3
- flwr/proto/serverappio_pb2_grpc.py +138 -34
- flwr/proto/serverappio_pb2_grpc.pyi +54 -13
- flwr/proto/simulationio_pb2.py +12 -11
- flwr/proto/simulationio_pb2_grpc.py +35 -0
- flwr/proto/simulationio_pb2_grpc.pyi +14 -0
- flwr/server/__init__.py +1 -1
- flwr/server/app.py +68 -186
- flwr/server/compat/app_utils.py +50 -28
- flwr/server/fleet_event_log_interceptor.py +2 -2
- flwr/server/grid/grpc_grid.py +104 -34
- flwr/server/grid/inmemory_grid.py +5 -4
- flwr/server/serverapp/app.py +18 -0
- flwr/server/superlink/ffs/__init__.py +2 -0
- flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +13 -3
- flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +101 -7
- flwr/server/superlink/fleet/message_handler/message_handler.py +135 -18
- flwr/server/superlink/fleet/rest_rere/rest_api.py +72 -11
- flwr/server/superlink/fleet/vce/vce_api.py +6 -3
- flwr/server/superlink/linkstate/in_memory_linkstate.py +138 -43
- flwr/server/superlink/linkstate/linkstate.py +53 -20
- flwr/server/superlink/linkstate/sqlite_linkstate.py +149 -55
- flwr/server/superlink/linkstate/utils.py +33 -29
- flwr/server/superlink/serverappio/serverappio_grpc.py +3 -0
- flwr/server/superlink/serverappio/serverappio_servicer.py +211 -57
- flwr/server/superlink/simulation/simulationio_servicer.py +25 -1
- flwr/server/superlink/utils.py +44 -2
- flwr/server/utils/validator.py +2 -2
- flwr/serverapp/__init__.py +15 -0
- flwr/simulation/app.py +17 -0
- flwr/supercore/__init__.py +15 -0
- flwr/supercore/object_store/__init__.py +24 -0
- flwr/supercore/object_store/in_memory_object_store.py +229 -0
- flwr/supercore/object_store/object_store.py +192 -0
- flwr/supercore/object_store/object_store_factory.py +44 -0
- flwr/superexec/deployment.py +6 -2
- flwr/superexec/exec_event_log_interceptor.py +4 -4
- flwr/superexec/exec_grpc.py +7 -3
- flwr/superexec/exec_servicer.py +125 -23
- flwr/superexec/exec_user_auth_interceptor.py +37 -8
- flwr/superexec/executor.py +4 -0
- flwr/superexec/simulation.py +7 -1
- flwr/superlink/__init__.py +15 -0
- flwr/{client/supernode → supernode}/__init__.py +0 -7
- flwr/{client/nodestate/nodestate.py → supernode/cli/__init__.py} +7 -14
- flwr/{client/supernode/app.py → supernode/cli/flower_supernode.py} +3 -12
- flwr/supernode/cli/flwr_clientapp.py +81 -0
- flwr/supernode/nodestate/in_memory_nodestate.py +190 -0
- flwr/supernode/nodestate/nodestate.py +212 -0
- flwr/supernode/runtime/__init__.py +15 -0
- flwr/{client/clientapp/app.py → supernode/runtime/run_clientapp.py} +25 -56
- flwr/supernode/servicer/__init__.py +15 -0
- flwr/supernode/servicer/clientappio/__init__.py +24 -0
- flwr/supernode/start_client_internal.py +491 -0
- {flwr-1.18.0.dist-info → flwr-1.19.0.dist-info}/METADATA +5 -4
- {flwr-1.18.0.dist-info → flwr-1.19.0.dist-info}/RECORD +141 -108
- {flwr-1.18.0.dist-info → flwr-1.19.0.dist-info}/WHEEL +1 -1
- {flwr-1.18.0.dist-info → flwr-1.19.0.dist-info}/entry_points.txt +2 -2
- flwr/client/heartbeat.py +0 -74
- flwr/client/nodestate/in_memory_nodestate.py +0 -38
- /flwr/{client → compat/client}/grpc_client/__init__.py +0 -0
- /flwr/{client → compat/client}/grpc_client/connection.py +0 -0
- /flwr/{client → supernode}/nodestate/__init__.py +0 -0
- /flwr/{client → supernode}/nodestate/nodestate_factory.py +0 -0
- /flwr/{client/clientapp → supernode/servicer/clientappio}/clientappio_servicer.py +0 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
# Copyright 2025 Flower Labs GmbH. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
"""InflatableObject utilities."""
|
|
16
|
+
|
|
17
|
+
import concurrent.futures
|
|
18
|
+
import random
|
|
19
|
+
import threading
|
|
20
|
+
import time
|
|
21
|
+
from typing import Callable, Optional
|
|
22
|
+
|
|
23
|
+
from .constant import (
|
|
24
|
+
HEAD_BODY_DIVIDER,
|
|
25
|
+
HEAD_VALUE_DIVIDER,
|
|
26
|
+
MAX_CONCURRENT_PULLS,
|
|
27
|
+
MAX_CONCURRENT_PUSHES,
|
|
28
|
+
PULL_BACKOFF_CAP,
|
|
29
|
+
PULL_INITIAL_BACKOFF,
|
|
30
|
+
PULL_MAX_TIME,
|
|
31
|
+
PULL_MAX_TRIES_PER_OBJECT,
|
|
32
|
+
)
|
|
33
|
+
from .inflatable import (
|
|
34
|
+
InflatableObject,
|
|
35
|
+
UnexpectedObjectContentError,
|
|
36
|
+
_get_object_head,
|
|
37
|
+
get_object_head_values_from_object_content,
|
|
38
|
+
get_object_id,
|
|
39
|
+
is_valid_sha256_hash,
|
|
40
|
+
)
|
|
41
|
+
from .message import Message
|
|
42
|
+
from .record import Array, ArrayRecord, ConfigRecord, MetricRecord, RecordDict
|
|
43
|
+
|
|
44
|
+
# Helper registry that maps names of classes to their type
|
|
45
|
+
inflatable_class_registry: dict[str, type[InflatableObject]] = {
|
|
46
|
+
Array.__qualname__: Array,
|
|
47
|
+
ArrayRecord.__qualname__: ArrayRecord,
|
|
48
|
+
ConfigRecord.__qualname__: ConfigRecord,
|
|
49
|
+
Message.__qualname__: Message,
|
|
50
|
+
MetricRecord.__qualname__: MetricRecord,
|
|
51
|
+
RecordDict.__qualname__: RecordDict,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ObjectUnavailableError(Exception):
|
|
56
|
+
"""Exception raised when an object has been pre-registered but is not yet
|
|
57
|
+
available."""
|
|
58
|
+
|
|
59
|
+
def __init__(self, object_id: str):
|
|
60
|
+
super().__init__(f"Object with ID '{object_id}' is not yet available.")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class ObjectIdNotPreregisteredError(Exception):
|
|
64
|
+
"""Exception raised when an object ID is not pre-registered."""
|
|
65
|
+
|
|
66
|
+
def __init__(self, object_id: str):
|
|
67
|
+
super().__init__(f"Object with ID '{object_id}' could not be found.")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def push_objects(
|
|
71
|
+
objects: dict[str, InflatableObject],
|
|
72
|
+
push_object_fn: Callable[[str, bytes], None],
|
|
73
|
+
*,
|
|
74
|
+
object_ids_to_push: Optional[set[str]] = None,
|
|
75
|
+
keep_objects: bool = False,
|
|
76
|
+
max_concurrent_pushes: int = MAX_CONCURRENT_PUSHES,
|
|
77
|
+
) -> None:
|
|
78
|
+
"""Push multiple objects to the servicer.
|
|
79
|
+
|
|
80
|
+
Parameters
|
|
81
|
+
----------
|
|
82
|
+
objects : dict[str, InflatableObject]
|
|
83
|
+
A dictionary of objects to push, where keys are object IDs and values are
|
|
84
|
+
`InflatableObject` instances.
|
|
85
|
+
push_object_fn : Callable[[str, bytes], None]
|
|
86
|
+
A function that takes an object ID and its content as bytes, and pushes
|
|
87
|
+
it to the servicer. This function should raise `ObjectIdNotPreregisteredError`
|
|
88
|
+
if the object ID is not pre-registered.
|
|
89
|
+
object_ids_to_push : Optional[set[str]] (default: None)
|
|
90
|
+
A set of object IDs to push. If not provided, all objects will be pushed.
|
|
91
|
+
keep_objects : bool (default: False)
|
|
92
|
+
If `True`, the original objects will be kept in the `objects` dictionary
|
|
93
|
+
after pushing. If `False`, they will be removed from the dictionary to avoid
|
|
94
|
+
high memory usage.
|
|
95
|
+
max_concurrent_pushes : int (default: MAX_CONCURRENT_PUSHES)
|
|
96
|
+
The maximum number of concurrent pushes to perform.
|
|
97
|
+
"""
|
|
98
|
+
if object_ids_to_push is not None:
|
|
99
|
+
# Filter objects to push only those with IDs in the set
|
|
100
|
+
objects = {k: v for k, v in objects.items() if k in object_ids_to_push}
|
|
101
|
+
|
|
102
|
+
lock = threading.Lock()
|
|
103
|
+
|
|
104
|
+
def push(obj_id: str) -> None:
|
|
105
|
+
"""Push a single object."""
|
|
106
|
+
object_content = objects[obj_id].deflate()
|
|
107
|
+
if not keep_objects:
|
|
108
|
+
with lock:
|
|
109
|
+
del objects[obj_id]
|
|
110
|
+
push_object_fn(obj_id, object_content)
|
|
111
|
+
|
|
112
|
+
with concurrent.futures.ThreadPoolExecutor(
|
|
113
|
+
max_workers=max_concurrent_pushes
|
|
114
|
+
) as executor:
|
|
115
|
+
list(executor.map(push, list(objects.keys())))
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def pull_objects( # pylint: disable=too-many-arguments
|
|
119
|
+
object_ids: list[str],
|
|
120
|
+
pull_object_fn: Callable[[str], bytes],
|
|
121
|
+
*,
|
|
122
|
+
max_concurrent_pulls: int = MAX_CONCURRENT_PULLS,
|
|
123
|
+
max_time: Optional[float] = PULL_MAX_TIME,
|
|
124
|
+
max_tries_per_object: Optional[int] = PULL_MAX_TRIES_PER_OBJECT,
|
|
125
|
+
initial_backoff: float = PULL_INITIAL_BACKOFF,
|
|
126
|
+
backoff_cap: float = PULL_BACKOFF_CAP,
|
|
127
|
+
) -> dict[str, bytes]:
|
|
128
|
+
"""Pull multiple objects from the servicer.
|
|
129
|
+
|
|
130
|
+
Parameters
|
|
131
|
+
----------
|
|
132
|
+
object_ids : list[str]
|
|
133
|
+
A list of object IDs to pull.
|
|
134
|
+
pull_object_fn : Callable[[str], bytes]
|
|
135
|
+
A function that takes an object ID and returns the object content as bytes.
|
|
136
|
+
The function should raise `ObjectUnavailableError` if the object is not yet
|
|
137
|
+
available, or `ObjectIdNotPreregisteredError` if the object ID is not
|
|
138
|
+
pre-registered.
|
|
139
|
+
max_concurrent_pulls : int (default: MAX_CONCURRENT_PULLS)
|
|
140
|
+
The maximum number of concurrent pulls to perform.
|
|
141
|
+
max_time : Optional[float] (default: PULL_MAX_TIME)
|
|
142
|
+
The maximum time to wait for all pulls to complete. If `None`, waits
|
|
143
|
+
indefinitely.
|
|
144
|
+
max_tries_per_object : Optional[int] (default: PULL_MAX_TRIES_PER_OBJECT)
|
|
145
|
+
The maximum number of attempts to pull each object. If `None`, pulls
|
|
146
|
+
indefinitely until the object is available.
|
|
147
|
+
initial_backoff : float (default: PULL_INITIAL_BACKOFF)
|
|
148
|
+
The initial backoff time in seconds for retrying pulls after an
|
|
149
|
+
`ObjectUnavailableError`.
|
|
150
|
+
backoff_cap : float (default: PULL_BACKOFF_CAP)
|
|
151
|
+
The maximum backoff time in seconds. Backoff times will not exceed this value.
|
|
152
|
+
|
|
153
|
+
Returns
|
|
154
|
+
-------
|
|
155
|
+
dict[str, bytes]
|
|
156
|
+
A dictionary where keys are object IDs and values are the pulled
|
|
157
|
+
object contents.
|
|
158
|
+
"""
|
|
159
|
+
if max_tries_per_object is None:
|
|
160
|
+
max_tries_per_object = int(1e9)
|
|
161
|
+
if max_time is None:
|
|
162
|
+
max_time = float("inf")
|
|
163
|
+
|
|
164
|
+
results: dict[str, bytes] = {}
|
|
165
|
+
results_lock = threading.Lock()
|
|
166
|
+
err_to_raise: Optional[Exception] = None
|
|
167
|
+
early_stop = threading.Event()
|
|
168
|
+
start = time.monotonic()
|
|
169
|
+
|
|
170
|
+
def pull_with_retries(object_id: str) -> None:
|
|
171
|
+
"""Attempt to pull a single object with retry and backoff."""
|
|
172
|
+
nonlocal err_to_raise
|
|
173
|
+
tries = 0
|
|
174
|
+
delay = initial_backoff
|
|
175
|
+
|
|
176
|
+
while not early_stop.is_set():
|
|
177
|
+
try:
|
|
178
|
+
object_content = pull_object_fn(object_id)
|
|
179
|
+
with results_lock:
|
|
180
|
+
results[object_id] = object_content
|
|
181
|
+
return
|
|
182
|
+
|
|
183
|
+
except ObjectUnavailableError as err:
|
|
184
|
+
tries += 1
|
|
185
|
+
if (
|
|
186
|
+
tries >= max_tries_per_object
|
|
187
|
+
or time.monotonic() - start >= max_time
|
|
188
|
+
):
|
|
189
|
+
# Stop all work if one object exhausts retries
|
|
190
|
+
early_stop.set()
|
|
191
|
+
with results_lock:
|
|
192
|
+
if err_to_raise is None:
|
|
193
|
+
err_to_raise = err
|
|
194
|
+
return
|
|
195
|
+
|
|
196
|
+
# Apply exponential backoff with ±20% jitter
|
|
197
|
+
sleep_time = delay * (1 + random.uniform(-0.2, 0.2))
|
|
198
|
+
early_stop.wait(sleep_time)
|
|
199
|
+
delay = min(delay * 2, backoff_cap)
|
|
200
|
+
|
|
201
|
+
except ObjectIdNotPreregisteredError as err:
|
|
202
|
+
# Permanent failure: object ID is invalid
|
|
203
|
+
early_stop.set()
|
|
204
|
+
with results_lock:
|
|
205
|
+
if err_to_raise is None:
|
|
206
|
+
err_to_raise = err
|
|
207
|
+
return
|
|
208
|
+
|
|
209
|
+
# Submit all pull tasks concurrently
|
|
210
|
+
with concurrent.futures.ThreadPoolExecutor(
|
|
211
|
+
max_workers=max_concurrent_pulls
|
|
212
|
+
) as executor:
|
|
213
|
+
futures = {
|
|
214
|
+
executor.submit(pull_with_retries, obj_id): obj_id for obj_id in object_ids
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
# Wait for completion
|
|
218
|
+
concurrent.futures.wait(futures)
|
|
219
|
+
|
|
220
|
+
if err_to_raise is not None:
|
|
221
|
+
raise err_to_raise
|
|
222
|
+
|
|
223
|
+
return results
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def inflate_object_from_contents(
|
|
227
|
+
object_id: str,
|
|
228
|
+
object_contents: dict[str, bytes],
|
|
229
|
+
*,
|
|
230
|
+
keep_object_contents: bool = False,
|
|
231
|
+
objects: Optional[dict[str, InflatableObject]] = None,
|
|
232
|
+
) -> InflatableObject:
|
|
233
|
+
"""Inflate an object from object contents.
|
|
234
|
+
|
|
235
|
+
Parameters
|
|
236
|
+
----------
|
|
237
|
+
object_id : str
|
|
238
|
+
The ID of the object to inflate.
|
|
239
|
+
object_contents : dict[str, bytes]
|
|
240
|
+
A dictionary mapping object IDs to their contents as bytes.
|
|
241
|
+
All descendant objects must be present in this dictionary.
|
|
242
|
+
keep_object_contents : bool (default: False)
|
|
243
|
+
If `True`, the object content will be kept in the `object_contents`
|
|
244
|
+
dictionary after inflation. If `False`, the object content will be
|
|
245
|
+
removed from the dictionary to save memory.
|
|
246
|
+
objects : Optional[dict[str, InflatableObject]] (default: None)
|
|
247
|
+
No need to provide this parameter. A dictionary to store already
|
|
248
|
+
inflated objects, mapping object IDs to their corresponding
|
|
249
|
+
`InflatableObject` instances.
|
|
250
|
+
|
|
251
|
+
Returns
|
|
252
|
+
-------
|
|
253
|
+
InflatableObject
|
|
254
|
+
The inflated object.
|
|
255
|
+
"""
|
|
256
|
+
if objects is None:
|
|
257
|
+
# Initialize objects dictionary
|
|
258
|
+
objects = {}
|
|
259
|
+
|
|
260
|
+
if object_id in objects:
|
|
261
|
+
# If the object is already in the objects dictionary, return it
|
|
262
|
+
return objects[object_id]
|
|
263
|
+
|
|
264
|
+
# Extract object class and object_ids of children
|
|
265
|
+
object_content = object_contents[object_id]
|
|
266
|
+
obj_type, children_obj_ids, _ = get_object_head_values_from_object_content(
|
|
267
|
+
object_content=object_contents[object_id]
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
# Remove the object content from the dictionary to save memory
|
|
271
|
+
if not keep_object_contents:
|
|
272
|
+
del object_contents[object_id]
|
|
273
|
+
|
|
274
|
+
# Resolve object class
|
|
275
|
+
cls_type = inflatable_class_registry[obj_type]
|
|
276
|
+
|
|
277
|
+
# Inflate all children objects
|
|
278
|
+
children: dict[str, InflatableObject] = {}
|
|
279
|
+
for child_obj_id in children_obj_ids:
|
|
280
|
+
children[child_obj_id] = inflate_object_from_contents(
|
|
281
|
+
child_obj_id,
|
|
282
|
+
object_contents,
|
|
283
|
+
keep_object_contents=keep_object_contents,
|
|
284
|
+
objects=objects,
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
# Inflate object passing its children
|
|
288
|
+
obj = cls_type.inflate(object_content, children=children)
|
|
289
|
+
del object_content # Free memory after inflation
|
|
290
|
+
objects[object_id] = obj
|
|
291
|
+
return obj
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def validate_object_content(content: bytes) -> None:
|
|
295
|
+
"""Validate the deflated content of an InflatableObject."""
|
|
296
|
+
try:
|
|
297
|
+
# Check if there is a head-body divider
|
|
298
|
+
index = content.find(HEAD_BODY_DIVIDER)
|
|
299
|
+
if index == -1:
|
|
300
|
+
raise ValueError(
|
|
301
|
+
"Unexpected format for object content. Head and body "
|
|
302
|
+
"could not be split."
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
head = _get_object_head(content)
|
|
306
|
+
|
|
307
|
+
# check if the head has three parts:
|
|
308
|
+
# <object_type> <children_ids> <object_body_len>
|
|
309
|
+
head_decoded = head.decode(encoding="utf-8")
|
|
310
|
+
head_parts = head_decoded.split(HEAD_VALUE_DIVIDER)
|
|
311
|
+
|
|
312
|
+
if len(head_parts) != 3:
|
|
313
|
+
raise ValueError("Unexpected format for object head.")
|
|
314
|
+
|
|
315
|
+
obj_type, children_str, body_len = head_parts
|
|
316
|
+
|
|
317
|
+
# Check that children IDs are valid IDs
|
|
318
|
+
children = children_str.split(",")
|
|
319
|
+
for children_id in children:
|
|
320
|
+
if children_id and not is_valid_sha256_hash(children_id):
|
|
321
|
+
raise ValueError(
|
|
322
|
+
f"Detected invalid object ID ({children_id}) in children."
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
# Check that object type is recognized
|
|
326
|
+
if obj_type not in inflatable_class_registry:
|
|
327
|
+
if obj_type != "CustomDataClass": # to allow for the class in tests
|
|
328
|
+
raise ValueError(f"Object of type {obj_type} is not supported.")
|
|
329
|
+
|
|
330
|
+
# Check if the body length in the head matches that of the body
|
|
331
|
+
actual_body_len = len(content) - len(head) - len(HEAD_BODY_DIVIDER)
|
|
332
|
+
if actual_body_len != int(body_len):
|
|
333
|
+
raise ValueError(
|
|
334
|
+
f"Object content length expected {body_len} bytes but got "
|
|
335
|
+
f"{actual_body_len} bytes."
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
except ValueError as err:
|
|
339
|
+
raise UnexpectedObjectContentError(
|
|
340
|
+
object_id=get_object_id(content), reason=str(err)
|
|
341
|
+
) from err
|