Skip to content

Commit

Permalink
Merge pull request #292 from HathorNetwork/feat/support-sync-v2-status
Browse files Browse the repository at this point in the history
feat: support sync-v2 peers on node status
  • Loading branch information
jansegre authored Jan 4, 2024
2 parents a87eb45 + 72b8977 commit 1f8f8f1
Show file tree
Hide file tree
Showing 6 changed files with 11,145 additions and 501 deletions.
31 changes: 23 additions & 8 deletions domain/network/network.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from dataclasses import asdict, dataclass
from typing import List, Set
from typing import List, Optional, Set

from dacite import Config, from_dict

from domain.network.node import Node, NodeState, Peer
from domain.network.node import BlockInfo, Node, NodeState, Peer


@dataclass
Expand All @@ -28,11 +28,20 @@ class AggregatedPeer:
:param last_message: timestamp of last message
:type last_message: int
:param latest_timestamp: latest timestamp
:type latest_timestamp: int
:param protocol_version: version of the protocol (typically sync-v1.1 or sync-v2)
:type protocol_version: str
:param latest_timestamp: latest timestamp (sync v1)
:type latest_timestamp: Optional[int]
:param sync_timestamp: timestamp of last sychronized block
:type sync_timestamp: int
:param sync_timestamp: timestamp of last sychronized block (sync v1)
:type sync_timestamp: Optional[int]
:param peer_best_block: best block reported by the remote connected peer (sync v2)
:type peer_best_block: Optional[BlockInfo]
:param synced_block: current synchronized block on this connection (sync v2)
:type synced_block: Optional[BlockInfo]
:param warning_flags: list of warning flags, if any
:type warning_flags: List[str]
Expand All @@ -51,8 +60,11 @@ class AggregatedPeer:
address: str
state: NodeState
last_message: int
latest_timestamp: int
sync_timestamp: int
protocol_version: str
latest_timestamp: Optional[int]
sync_timestamp: Optional[int]
peer_best_block: Optional[BlockInfo]
synced_block: Optional[BlockInfo]
warning_flags: List[str]
entrypoints: List[str]
connected_to: Set[str]
Expand All @@ -74,8 +86,11 @@ def from_peer(cls, peer: Peer) -> "AggregatedPeer":
address=peer.address,
state=peer.state,
last_message=int(peer.last_message),
protocol_version=peer.protocol_version,
latest_timestamp=peer.latest_timestamp,
sync_timestamp=peer.sync_timestamp,
peer_best_block=peer.peer_best_block,
synced_block=peer.synced_block,
warning_flags=peer.warning_flags,
entrypoints=peer.entrypoints,
connected_to=set(),
Expand Down
77 changes: 68 additions & 9 deletions domain/network/node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import asdict, dataclass
from enum import Enum
from typing import List
from typing import List, Optional


class NodeState(str, Enum):
Expand All @@ -11,6 +11,40 @@ class NodeState(str, Enum):
READY = "READY"


@dataclass(frozen=True)
class BlockInfo:
"""Class to hold block info as returned by the status of a sync-v2 connection
Example relevant portion of the status:
```
"node-block-sync": {
"is_enabled": true,
"peer_best_block": {
"height": 4125002,
"id": "000000000000000107483b7ac4c7eaf4d31069c05257047093532d6f148ba5a0"
},
"synced_block": {
"height": 4125002,
"id": "000000000000000107483b7ac4c7eaf4d31069c05257047093532d6f148ba5a0"
},
"synced": true,
"state": "syncing-mempool"
}
```
BlockInfo models "peer_best_block" and "synced_block".
"""

height: int
id: str

@classmethod
def from_status_dict(cls, status: dict) -> "BlockInfo":
"""Create class instance from the inner dict `{"height": ..., "id": ...}`"""
return cls(status["height"], status["id"])


@dataclass
class Peer:
"""Information about a Peer connected to a node
Expand Down Expand Up @@ -52,8 +86,11 @@ class Peer:
address: str
state: NodeState
last_message: float
latest_timestamp: int
sync_timestamp: int
protocol_version: str
latest_timestamp: Optional[int]
sync_timestamp: Optional[int]
peer_best_block: Optional[BlockInfo]
synced_block: Optional[BlockInfo]
warning_flags: List[str]
entrypoints: List[str]

Expand Down Expand Up @@ -152,6 +189,29 @@ def from_status_dict(cls, status: dict) -> "Node":
connected_peers = []

for peer in status["connections"]["connected_peers"]:
latest_timestamp: Optional[int] = None
sync_timestamp: Optional[int] = None
peer_best_block: Optional[BlockInfo] = None
synced_block: Optional[BlockInfo] = None
protocol_version = peer["protocol_version"]
if protocol_version in {"sync-v1", "sync-v1.1"}:
latest_timestamp = peer["plugins"]["node-sync-timestamp"][
"latest_timestamp"
]
sync_timestamp = peer["plugins"]["node-sync-timestamp"][
"synced_timestamp"
]
elif protocol_version == "sync-v2":
peer_best_block = BlockInfo.from_status_dict(
peer["plugins"]["node-block-sync"]["peer_best_block"]
)
synced_block = BlockInfo.from_status_dict(
peer["plugins"]["node-block-sync"]["synced_block"]
)
else:
# still try to support, but don't attempt to parse the fields above
# mark version as unsupported
protocol_version += " (unsupported)"
connected_peers.append(
Peer(
id=peer["id"],
Expand All @@ -160,12 +220,11 @@ def from_status_dict(cls, status: dict) -> "Node":
address=peer["address"],
state=NodeState(peer["state"]),
last_message=peer["last_message"],
latest_timestamp=peer["plugins"]["node-sync-timestamp"][
"latest_timestamp"
],
sync_timestamp=peer["plugins"]["node-sync-timestamp"][
"synced_timestamp"
],
protocol_version=protocol_version,
latest_timestamp=latest_timestamp,
sync_timestamp=sync_timestamp,
peer_best_block=peer_best_block,
synced_block=synced_block,
warning_flags=peer["warning_flags"],
entrypoints=peer_entrypoints[peer["id"]],
)
Expand Down
Loading

0 comments on commit 1f8f8f1

Please sign in to comment.