Skip to content

Commit

Permalink
Merge pull request #8130 from PeterChung241/nodemetadatajson_fix
Browse files Browse the repository at this point in the history
Removed some private fields from nodemetadataJSON
  • Loading branch information
shubham3121 authored Nov 1, 2023
2 parents 29e45bd + 5660c78 commit d3bb270
Show file tree
Hide file tree
Showing 23 changed files with 286 additions and 111 deletions.
2 changes: 1 addition & 1 deletion notebooks/api/0.8/07-domain-register-control-flow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
"metadata": {},
"outputs": [],
"source": [
"assert root_client.metadata.signup_enabled == False\n",
"assert root_client.settings.get().signup_enabled == False\n",
"assert isinstance(response_1, sy.SyftSuccess)\n",
"assert isinstance(response_2, sy.SyftError)\n",
"assert isinstance(response_3, sy.SyftError)"
Expand Down
26 changes: 23 additions & 3 deletions notebooks/tutorials/data-engineer/02-deployment-types.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86e66c8b-afa4-4236-a362-7ec9e07a7063",
"metadata": {},
"outputs": [],
"source": [
"assert memory_node is not None"
]
},
{
"cell_type": "markdown",
"id": "4573b485",
Expand All @@ -89,13 +99,23 @@
"outputs": [],
"source": [
"webserver_node = sy.Orchestra.launch(\n",
" name=\"Arbitrary Dev Node\",\n",
" name=\"Arbitrary Webserver Dev Node\",\n",
" dev_mode=True,\n",
" reset=True,\n",
" port=80\n",
" port=8081\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86ce3464-a51c-4870-a293-e479c08c66bc",
"metadata": {},
"outputs": [],
"source": [
"assert webserver_node is not None"
]
},
{
"cell_type": "markdown",
"id": "dd74621a",
Expand Down Expand Up @@ -353,7 +373,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions packages/syft/src/syft/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from ..serde.serialize import _serialize
from ..service.context import NodeServiceContext
from ..service.metadata.node_metadata import NodeMetadataJSON
from ..service.metadata.node_metadata import NodeMetadataV2
from ..service.metadata.node_metadata import NodeMetadataV3
from ..service.response import SyftError
from ..service.response import SyftSuccess
from ..service.user.user import UserCreate
Expand Down Expand Up @@ -597,7 +597,7 @@ def exchange_route(self, client: Self) -> Union[SyftSuccess, SyftError]:
result = self.api.services.network.exchange_credentials_with(
self_node_route=self_node_route,
remote_node_route=remote_node_route,
remote_node_verify_key=client.metadata.to(NodeMetadataV2).verify_key,
remote_node_verify_key=client.metadata.to(NodeMetadataV3).verify_key,
)

return result
Expand Down
49 changes: 22 additions & 27 deletions packages/syft/src/syft/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from ..service.dataset.dataset_service import DatasetService
from ..service.enclave.enclave_service import EnclaveService
from ..service.metadata.metadata_service import MetadataService
from ..service.metadata.node_metadata import NodeMetadataV2
from ..service.metadata.node_metadata import NodeMetadataV3
from ..service.network.network_service import NetworkService
from ..service.notification.notification_service import NotificationService
from ..service.object_search.migration_state_service import MigrateStateService
Expand All @@ -82,7 +82,7 @@
from ..service.service import AbstractService
from ..service.service import ServiceConfigRegistry
from ..service.service import UserServiceConfigRegistry
from ..service.settings.settings import NodeSettings
from ..service.settings.settings import NodeSettingsV2
from ..service.settings.settings_service import SettingsService
from ..service.settings.settings_stash import SettingsStash
from ..service.user.user import User
Expand Down Expand Up @@ -741,43 +741,35 @@ def _get_service_method_from_path(self, path: str) -> Callable:
return getattr(service_obj, method_name)

@property
def metadata(self) -> NodeMetadataV2:
def settings(self) -> NodeSettingsV2:
settings_stash = SettingsStash(store=self.document_store)
settings = settings_stash.get_all(self.signing_key.verify_key)
if settings.is_ok() and len(settings.ok()) > 0:
settings_data = settings.ok()[0]
return settings_data

@property
def metadata(self) -> NodeMetadataV3:
name = ""
deployed_on = ""
organization = ""
on_board = False
description = ""
signup_enabled = False
admin_email = ""
show_warnings = self.enable_warnings
settings_data = self.settings
name = settings_data.name
organization = settings_data.organization
description = settings_data.description
show_warnings = settings_data.show_warnings

settings_stash = SettingsStash(store=self.document_store)
settings = settings_stash.get_all(self.signing_key.verify_key)
if settings.is_ok() and len(settings.ok()) > 0:
settings_data = settings.ok()[0]
name = settings_data.name
deployed_on = settings_data.deployed_on
organization = settings_data.organization
on_board = settings_data.on_board
description = settings_data.description
signup_enabled = settings_data.signup_enabled
admin_email = settings_data.admin_email
show_warnings = settings_data.show_warnings

return NodeMetadataV2(
return NodeMetadataV3(
name=name,
id=self.id,
verify_key=self.verify_key,
highest_version=SYFT_OBJECT_VERSION_1,
lowest_version=SYFT_OBJECT_VERSION_1,
syft_version=__version__,
deployed_on=deployed_on,
description=description,
organization=organization,
on_board=on_board,
node_type=self.node_type.value,
signup_enabled=signup_enabled,
admin_email=admin_email,
node_side_type=self.node_side_type.value,
show_warnings=show_warnings,
)
Expand Down Expand Up @@ -967,7 +959,7 @@ def get_unauthed_context(
) -> NodeServiceContext:
return UnauthedServiceContext(node=self, login_credentials=login_credentials)

def create_initial_settings(self, admin_email: str) -> Optional[NodeSettings]:
def create_initial_settings(self, admin_email: str) -> Optional[NodeSettingsV2]:
if self.name is None:
self.name = random_name()
try:
Expand All @@ -981,8 +973,11 @@ def create_initial_settings(self, admin_email: str) -> Optional[NodeSettings]:
# as enclaves do not have superusers
if self.node_type == NodeType.ENCLAVE:
flags.CAN_REGISTER = True
new_settings = NodeSettings(
new_settings = NodeSettingsV2(
id=self.id,
name=self.name,
verify_key=self.verify_key,
node_type=self.node_type,
deployed_on=datetime.now().date().strftime("%m/%d/%Y"),
signup_enabled=flags.CAN_REGISTER,
admin_email=admin_email,
Expand Down
18 changes: 18 additions & 0 deletions packages/syft/src/syft/protocol/protocol_version.json
Original file line number Diff line number Diff line change
Expand Up @@ -749,5 +749,23 @@
}
}
}
},
"dev": {
"object_versions": {
"NodeMetadata": {
"3": {
"version": 3,
"hash": "3cc67abf394a805066a88aef0bea15bde609b9ecbe7ec15172eac5e7a0b7ef7c",
"action": "add"
}
},
"NodeSettings": {
"2": {
"version": 2,
"hash": "29a82afcb006a044b6ae04c6ea8a067d145d28b4210bb038ea9fa86ebde108c8",
"action": "add"
}
}
}
}
}
34 changes: 34 additions & 0 deletions packages/syft/src/syft/service/metadata/migrations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# stdlib
from typing import Callable

# relative
from ...types.syft_migration import migrate
from ...types.transforms import TransformContext
from ...types.transforms import drop
from ...types.transforms import rename
from .node_metadata import NodeMetadata
from .node_metadata import NodeMetadataV2
from .node_metadata import NodeMetadataV3


@migrate(NodeMetadata, NodeMetadataV2)
Expand All @@ -19,3 +25,31 @@ def downgrade_metadata_v2_to_v1():
rename("highest_version", "highest_object_version"),
rename("lowest_version", "lowest_object_version"),
]


@migrate(NodeMetadataV2, NodeMetadataV3)
def upgrade_metadata_v2_to_v3():
return [drop(["deployed_on", "on_board", "signup_enabled", "admin_email"])]


def _downgrade_metadata_v3_to_v2() -> Callable:
def set_defaults_from_settings(context: TransformContext) -> TransformContext:
# Extract from settings if node is attached to context
if context.node is not None:
context.output["deployed_on"] = context.node.settings.deployed_on
context.output["on_board"] = context.node.settings.on_board
context.output["signup_enabled"] = context.node.settings.signup_enabled
context.output["admin_email"] = context.node.settings.admin_email
else:
# Else set default value
context.output["signup_enabled"] = False
context.output["admin_email"] = ""

return context

return set_defaults_from_settings


@migrate(NodeMetadataV3, NodeMetadataV2)
def downgrade_metadata_v3_to_v2():
return [_downgrade_metadata_v3_to_v2()]
34 changes: 28 additions & 6 deletions packages/syft/src/syft/service/metadata/node_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ...serde.serializable import serializable
from ...types.syft_object import SYFT_OBJECT_VERSION_1
from ...types.syft_object import SYFT_OBJECT_VERSION_2
from ...types.syft_object import SYFT_OBJECT_VERSION_3
from ...types.syft_object import StorableObjectType
from ...types.syft_object import SyftObject
from ...types.transforms import convert_types
Expand Down Expand Up @@ -120,6 +121,31 @@ def check_version(self, client_version: str) -> bool:
)


@serializable()
class NodeMetadataV3(SyftObject):
__canonical_name__ = "NodeMetadata"
__version__ = SYFT_OBJECT_VERSION_3

name: str
id: UID
verify_key: SyftVerifyKey
highest_version: int
lowest_version: int
syft_version: str
node_type: NodeType = NodeType.DOMAIN
organization: str = "OpenMined"
description: str = "Text"
node_side_type: str
show_warnings: bool

def check_version(self, client_version: str) -> bool:
return check_version(
client_version=client_version,
server_version=self.syft_version,
server_name=self.name,
)


@serializable()
class NodeMetadataJSON(BaseModel, StorableObjectType):
metadata_version: int
Expand All @@ -130,12 +156,8 @@ class NodeMetadataJSON(BaseModel, StorableObjectType):
lowest_object_version: Optional[int]
syft_version: str
node_type: str = NodeType.DOMAIN.value
deployed_on: str = "Date"
organization: str = "OpenMined"
on_board: bool = False
description: str = "My cool domain"
signup_enabled: bool
admin_email: str
node_side_type: str
show_warnings: bool
supported_protocols: List = []
Expand All @@ -155,7 +177,7 @@ def check_version(self, client_version: str) -> bool:
)


@transform(NodeMetadataV2, NodeMetadataJSON)
@transform(NodeMetadataV3, NodeMetadataJSON)
def metadata_to_json() -> List[Callable]:
return [
drop(["__canonical_name__"]),
Expand All @@ -166,7 +188,7 @@ def metadata_to_json() -> List[Callable]:
]


@transform(NodeMetadataJSON, NodeMetadataV2)
@transform(NodeMetadataJSON, NodeMetadataV3)
def json_to_metadata() -> List[Callable]:
return [
drop(["metadata_version", "supported_protocols"]),
Expand Down
12 changes: 10 additions & 2 deletions packages/syft/src/syft/service/network/network_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ...node.credentials import SyftVerifyKey
from ...node.worker_settings import WorkerSettings
from ...serde.serializable import serializable
from ...service.settings.settings import NodeSettingsV2
from ...store.document_store import BaseUIDStoreStash
from ...store.document_store import DocumentStore
from ...store.document_store import PartitionKey
Expand All @@ -30,7 +31,7 @@
from ...util.telemetry import instrument
from ..context import AuthedServiceContext
from ..data_subject.data_subject import NamePartitionKey
from ..metadata.node_metadata import NodeMetadataV2
from ..metadata.node_metadata import NodeMetadataV3
from ..response import SyftError
from ..response import SyftSuccess
from ..service import AbstractService
Expand Down Expand Up @@ -401,8 +402,15 @@ def node_route_to_http_connection(
return HTTPConnection(url=url, proxy_target_uid=obj.proxy_target_uid)


@transform(NodeMetadataV2, NodePeer)
@transform(NodeMetadataV3, NodePeer)
def metadata_to_peer() -> List[Callable]:
return [
keep(["id", "name", "verify_key", "node_type", "admin_email"]),
]


@transform(NodeSettingsV2, NodePeer)
def settings_to_peer() -> List[Callable]:
return [
keep(["id", "name", "verify_key", "node_type", "admin_email"]),
]
6 changes: 3 additions & 3 deletions packages/syft/src/syft/service/network/node_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ...types.syft_object import SyftObject
from ...types.uid import UID
from ..context import NodeServiceContext
from ..metadata.node_metadata import NodeMetadataV2
from ..metadata.node_metadata import NodeMetadataV3
from .routes import HTTPNodeRoute
from .routes import NodeRoute
from .routes import NodeRouteType
Expand Down Expand Up @@ -108,9 +108,9 @@ def existed_route(self, route: NodeRoute) -> Tuple[bool, Optional[int]]:
@staticmethod
def from_client(client: SyftClient) -> Self:
if not client.metadata:
raise Exception("Client has have metadata first")
raise Exception("Client has to have metadata first")

peer = client.metadata.to(NodeMetadataV2).to(NodePeer)
peer = client.metadata.to(NodeMetadataV3).to(NodePeer)
route = connection_to_route(client.connection)
peer.node_routes.append(route)
return peer
Expand Down
2 changes: 1 addition & 1 deletion packages/syft/src/syft/service/network/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def validate_with_context(self, context: AuthedServiceContext) -> NodePeer:
return SyftError(message="Signature Verification Failed in ping")

# Step 2: Create a Node Peer with the given route
self_node_peer = context.node.metadata.to(NodePeer)
self_node_peer = context.node.settings.to(NodePeer)
self_node_peer.node_routes.append(self)

return self_node_peer
Expand Down
Loading

0 comments on commit d3bb270

Please sign in to comment.