Skip to content

Commit

Permalink
OCT-1326: Use blocks range only for mainnet (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarbacinski authored Feb 27, 2024
1 parent cf96dda commit 461b492
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
35 changes: 23 additions & 12 deletions backend/app/context/epoch_details.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from datetime import datetime
from typing import List, Tuple

from app.settings import config
from app.context.epoch_state import EpochState
from app.context.helpers import check_if_future
from app.exceptions import InvalidBlocksRange
from app.extensions import epochs
from app.infrastructure import graphql
from app.infrastructure.external_api.etherscan.blocks import get_block_num_from_ts
from app.legacy.utils.time import from_timestamp_s, sec_to_days
from app.shared.blockchain_types import compare_blockchain_types, ChainTypes


class EpochDetails:
Expand All @@ -17,8 +19,6 @@ def __init__(
start,
duration,
decision_window,
start_block: int | None = None,
end_block: int | None = None,
remaining_sec=None,
):
self.epoch_num = int(epoch_num)
Expand All @@ -28,8 +28,7 @@ def __init__(
self.decision_window_days = sec_to_days(self.decision_window_sec)
self.start_sec = int(start)
self.end_sec = self.start_sec + self.duration_sec
self.start_block = start_block
self.end_block = end_block
self.start_block, self.end_block = self._calc_blocks_range()
self.finalized_sec = self.end_sec + self.decision_window_sec
self.finalized_timestamp = from_timestamp_s(self.finalized_sec)

Expand Down Expand Up @@ -60,6 +59,26 @@ def no_blocks(self):
raise InvalidBlocksRange
return self.end_block - self.start_block

def _calc_blocks_range(self) -> tuple:
can_blocks_be_calced = (
compare_blockchain_types(config.CHAIN_ID, ChainTypes.MAINNET)
and self.start_sec
and self.end_sec
)
start_block, end_block = None, None
if can_blocks_be_calced:
is_start_future = check_if_future(self.start_sec)
is_end_future = check_if_future(self.end_sec)

start_block = (
get_block_num_from_ts(self.start_sec) if not is_start_future else None
)
end_block = (
get_block_num_from_ts(self.end_sec) if not is_end_future else None
)

return start_block, end_block


def get_epoch_details(epoch_num: int, epoch_state: EpochState) -> EpochDetails:
if epoch_state == EpochState.FUTURE:
Expand Down Expand Up @@ -93,17 +112,9 @@ def get_future_epoch_details(epoch_num: int) -> EpochDetails:


def _epoch_details_from_graphql_result(epoch_details: dict) -> EpochDetails:
from_ts = int(epoch_details["fromTs"])
end_ts = from_ts + int(epoch_details["duration"])

is_end_future = check_if_future(end_ts)
is_from_future = check_if_future(from_ts)

return EpochDetails(
epoch_num=epoch_details["epoch"],
start=epoch_details["fromTs"],
start_block=get_block_num_from_ts(from_ts) if not is_from_future else None,
end_block=get_block_num_from_ts(end_ts) if not is_end_future else None,
duration=epoch_details["duration"],
decision_window=epoch_details["decisionWindow"],
)
2 changes: 1 addition & 1 deletion backend/app/modules/modules_factory/pre_pending.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import app.modules.staking.proceeds.service.aggregated as aggregated
import app.modules.staking.proceeds.service.contract_balance as contract_balance
from app.modules.common.blockchain_types import ChainTypes, compare_blockchain_types
from app.shared.blockchain_types import ChainTypes, compare_blockchain_types
from app.modules.modules_factory.protocols import (
AllUserEffectiveDeposits,
OctantRewards,
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ def patch_compare_blockchain_types_for_mainnet(monkeypatch):
"app.modules.modules_factory.pre_pending.compare_blockchain_types",
lambda *args: True,
)
monkeypatch.setattr(
"app.context.epoch_details.compare_blockchain_types",
lambda *args: True,
)


@pytest.fixture(scope="function")
Expand Down
18 changes: 18 additions & 0 deletions backend/tests/context/test_epoch_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from app.context.epoch_details import EpochDetails


def test_check_if_epoch_details_gets_blocks_when_mainnet(
patch_compare_blockchain_types_for_mainnet, patch_etherscan_get_block_api
):
mocked_etherscan_block = 12712551
epoch_details = EpochDetails(1, 1, 1, 1)

assert epoch_details.start_block == mocked_etherscan_block
assert epoch_details.end_block == mocked_etherscan_block


def test_check_if_epoch_details_gets_blocks_when_not_mainnet():
epoch_details = EpochDetails(1, 1, 1, 1)

assert epoch_details.start_block is None
assert epoch_details.end_block is None

0 comments on commit 461b492

Please sign in to comment.