Skip to content

Commit

Permalink
fix peer update service method
Browse files Browse the repository at this point in the history
- add serializable decorator to NodePeerUpdate
  • Loading branch information
shubham3121 committed Jun 3, 2024
1 parent 60b2284 commit ba0445b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
7 changes: 7 additions & 0 deletions packages/syft/src/syft/protocol/protocol_version.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@
"hash": "c1796e7b01c9eae0dbf59cfd5c2c2f0e7eba593e0cea615717246572b27aae4b",
"action": "remove"
}
},
"NodePeerUpdate": {
"1": {
"version": 1,
"hash": "9e7cd39f6a9f90e8c595452865525e0989df1688236acfd1a665ed047ba47de9",
"action": "add"
}
}
}
}
Expand Down
29 changes: 9 additions & 20 deletions packages/syft/src/syft/service/network/network_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ def exchange_credentials_with(
f"as a peer for {remote_node_peer.node_type} '{remote_node_peer.name}'."
)
if remote_self_node_peer != self_node_peer:
updated_peer = NodePeerUpdate(
id=self_node_peer.id, node_routes=self_node_peer.node_routes
)
result = remote_client.api.services.network.update_peer(
peer=self_node_peer,
peer_update=updated_peer
)
logger.info(
f"{self_node_peer.node_type} peer '{self_node_peer.name}' information change detected."
Expand Down Expand Up @@ -462,38 +465,24 @@ def get_peers_by_type(
return result.ok() or []

@service_method(
path="network.update_peer", name="update_peer", roles=GUEST_ROLE_LEVEL
path="network.update_peer",
name="update_peer",
roles=GUEST_ROLE_LEVEL,
)
def update_peer(
self,
context: AuthedServiceContext,
peer: NodePeer,
peer_update: NodePeerUpdate,
) -> SyftSuccess | SyftError:
# try setting all fields of NodePeerUpdate according to NodePeer

get_peer_result = self.stash.get_by_uid(context.credentials, peer.id)
if get_peer_result.is_err():
return SyftError(
message=f"Failed to get peer '{peer.name}'. Error: {get_peer_result.err()}"
)
existing_peer = get_peer_result.ok()
peer_update = NodePeerUpdate()

# Only update the fields that have changed
for field_name, value in existing_peer.to_dict().items():
try:
if getattr(peer, field_name) != value:
setattr(peer_update, field_name, value)
except Exception as e:
logger.debug(f"Failed to set {field_name} to {value}. Exception: {e}")
pass
result = self.stash.update(
credentials=context.node.verify_key,
peer_update=peer_update,
)
if result.is_err():
return SyftError(
message=f"Failed to update peer '{peer.name}'. Error: {result.err()}"
message=f"Failed to update peer '{peer_update.name}'. Error: {result.err()}"
)
return SyftSuccess(
message=f"Peer '{result.ok().name}' information successfully updated."
Expand Down
1 change: 1 addition & 0 deletions packages/syft/src/syft/service/network/node_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ def delete_route(
return None


@serializable()
class NodePeerUpdate(PartialSyftObject):
__canonical_name__ = "NodePeerUpdate"
__version__ = SYFT_OBJECT_VERSION_1
Expand Down

0 comments on commit ba0445b

Please sign in to comment.