Skip to content

Commit 57ac3d5

Browse files
feat: add protocol and network info to minimal conditions
Resolves #2 on GitHub Co-Authored-by: RoseLabs <admin@roselabs.mx>
1 parent 3aaf4d6 commit 57ac3d5

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

configuration/config.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ def all(cls):
4242
return [cls.COSTON, cls.SONGBIRD, cls.COSTON2, cls.FLARE]
4343

4444

45+
class ProtocolId(int):
46+
pass
47+
48+
49+
class Protocol:
50+
FTSO = ProtocolId(100)
51+
FDC = ProtocolId(200)
52+
# NOTE:(janezicmatej) FSP only uses 1 byte for protocol id so we can use numbers
53+
# larger than 256 without worry
54+
FAST_UPDATES = ProtocolId(300)
55+
STAKING = ProtocolId(400)
56+
57+
@classmethod
58+
def id_to_name(cls, protocol: ProtocolId):
59+
match protocol:
60+
case cls.FTSO:
61+
return "ftso"
62+
case cls.FDC:
63+
return "fdc"
64+
case cls.FAST_UPDATES:
65+
return "fast updates"
66+
case cls.STAKING:
67+
return "staking"
68+
case _:
69+
raise ValueError(f"Unknown protocol ({protocol=})")
70+
71+
4572
class ConfigError(Exception):
4673
pass
4774

observer/message.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from attrs import define, frozen
77
from py_flare_common.fsp.epoch.epoch import VotingEpoch
88

9-
from configuration.config import ChainId
9+
from configuration.config import ChainId, Protocol, ProtocolId
1010

1111

1212
class MessageLevel(enum.Enum):
@@ -34,7 +34,7 @@ class MessageBuilder:
3434

3535
network: int | None = None
3636
round: VotingEpoch | None = None
37-
protocol: int | None = None
37+
protocol: ProtocolId | None = None
3838

3939
def copy(self) -> Self:
4040
return copy.copy(self)
@@ -53,9 +53,7 @@ def _build(self) -> Message:
5353
s.write(f"round:{self.round.id} ")
5454

5555
if self.protocol is not None:
56-
# TODO:(matej) make an enum like class like ChainId
57-
assert self.protocol in [100, 200]
58-
protocol = "ftso" if self.protocol == 100 else "fdc"
56+
protocol = Protocol.id_to_name(self.protocol)
5957
s.write(f"protocol:{protocol} ")
6058

6159
s.write(self.message)
@@ -72,7 +70,7 @@ def add(
7270
level: MessageLevel | None = None,
7371
network: int | None = None,
7472
round: VotingEpoch | None = None,
75-
protocol: int | None = None,
73+
protocol: ProtocolId | None = None,
7674
message: str | None = None,
7775
) -> Self:
7876
if level is not None:

observer/observer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,11 @@ async def observer_loop(config: Configuration) -> None:
411411
config.contracts.Submission.functions["submit2"].signature: "submit2",
412412
}
413413

414-
minimal_conditions = MinimalConditions().for_reward_epoch(reward_epoch.id)
414+
minimal_conditions = (
415+
MinimalConditions()
416+
.for_network(config.chain_id)
417+
.for_reward_epoch(reward_epoch.id)
418+
)
415419
last_minimal_conditions_check = int(time.time())
416420
last_ping = time.time()
417421

observer/validation/minimal_conditions.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from attrs import frozen
77
from py_flare_common.ftso.median import FtsoMedian
88

9+
from configuration.config import Protocol
910
from observer.fast_updates_manager import FastUpdatesManager
1011
from observer.message import Message, MessageLevel
1112
from observer.reward_epoch_manager import Entity
@@ -30,6 +31,12 @@ class MinimalConditions:
3031

3132
time_period: Interval = Interval.LAST_2_HOURS
3233

34+
network: int | None = None
35+
36+
def for_network(self, network: int) -> Self:
37+
self.network = network
38+
return self
39+
3340
def for_reward_epoch(self, rid: int) -> Self:
3441
self.reward_epoch_id = rid
3542
return self
@@ -41,7 +48,7 @@ def set_time_interval(self, interval: Interval) -> Self:
4148
def calculate_ftso_anchor_feeds(
4249
self, medians: deque[list[FtsoMedian]], votes: deque[list[int | None]]
4350
) -> Sequence[Message]:
44-
mb = Message.builder()
51+
mb = Message.builder().add(network=self.network, protocol=Protocol.FTSO)
4552
messages = []
4653

4754
total, total_hit = 0, 0
@@ -86,7 +93,7 @@ def calculate_ftso_anchor_feeds(
8693
def calculate_ftso_block_latency_feeds(
8794
self, entity: Entity, spm: SigningPolicyManager, fum: FastUpdatesManager
8895
) -> Sequence[Message]:
89-
mb = Message.builder()
96+
mb = Message.builder().add(network=self.network, protocol=Protocol.FAST_UPDATES)
9097
messages = []
9198
previous_total_active_weight = sum(
9299
[e.normalized_weight for e in spm.previous_policy.entities]
@@ -168,7 +175,7 @@ def calculate_ftso_block_latency_feeds(
168175
def calculate_staking(
169176
self, uptime_checks: int, node_connections: dict[str, deque]
170177
) -> Sequence[Message]:
171-
mb = Message.builder()
178+
mb = Message.builder().add(network=self.network, protocol=Protocol.STAKING)
172179
messages = []
173180
for node in node_connections:
174181
if (
@@ -188,7 +195,7 @@ def calculate_staking(
188195
return messages
189196

190197
def calculate_fdc_participation(self, signatures: deque[bool]) -> Sequence[Message]:
191-
mb = Message.builder()
198+
mb = Message.builder().add(network=self.network, protocol=Protocol.FDC)
192199
messages = []
193200
if (
194201
len(signatures) > 0

observer/validation/validation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
SubmitSignatures,
1111
)
1212

13+
from configuration.config import Protocol
1314
from configuration.types import Configuration
1415

1516
from ..message import Message
@@ -62,7 +63,7 @@ def validate_round(
6263
mb_ftso = Message.builder().add(
6364
network=config.chain_id,
6465
round=round.voting_epoch,
65-
protocol=100,
66+
protocol=Protocol.FTSO,
6667
)
6768
extracted_ftso = extract_round_for_entity(round.ftso, entity, round.voting_epoch)
6869

@@ -91,7 +92,7 @@ def validate_round(
9192
mb_fdc = Message.builder().add(
9293
network=config.chain_id,
9394
round=round.voting_epoch,
94-
protocol=200,
95+
protocol=Protocol.FDC,
9596
)
9697
extracted_fdc = extract_round_for_entity(round.fdc, entity, round.voting_epoch)
9798

0 commit comments

Comments
 (0)