Skip to content

Commit

Permalink
turn BlockchainInterface into a proper Protocol, to allow mypy to che…
Browse files Browse the repository at this point in the history
…ck it
  • Loading branch information
arvidn committed Aug 18, 2024
1 parent f0eacf5 commit 5196436
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 63 deletions.
8 changes: 4 additions & 4 deletions chia/consensus/block_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from chia.consensus.block_record import BlockRecord
from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.constants import ConsensusConstants
from chia.consensus.cost_calculator import NPCResult
Expand Down Expand Up @@ -59,7 +59,7 @@ def create_foliage(
additions: List[Coin],
removals: List[Coin],
prev_block: Optional[BlockRecord],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
total_iters_sp: uint128,
timestamp: uint64,
farmer_reward_puzzlehash: bytes32,
Expand Down Expand Up @@ -291,7 +291,7 @@ def create_unfinished_block(
get_pool_signature: Callable[[PoolTarget, Optional[G1Element]], Optional[G2Element]],
signage_point: SignagePoint,
timestamp: uint64,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
seed: bytes = b"",
block_generator: Optional[BlockGenerator] = None,
aggregate_sig: G2Element = G2Element(),
Expand Down Expand Up @@ -430,7 +430,7 @@ def unfinished_block_to_full_block(
icc_ip_proof: Optional[VDFProof],
finished_sub_slots: List[EndOfSubSlotBundle],
prev_block: Optional[BlockRecord],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
total_iters_sp: uint128,
difficulty: uint64,
) -> FullBlock:
Expand Down
6 changes: 3 additions & 3 deletions chia/consensus/block_header_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from chia_rs import AugSchemeMPL

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.consensus.deficit import calculate_deficit
from chia.consensus.difficulty_adjustment import can_finish_sub_and_full_epoch
Expand Down Expand Up @@ -39,7 +39,7 @@
# noinspection PyCallByClass
def validate_unfinished_header_block(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
header_block: UnfinishedHeaderBlock,
check_filter: bool,
expected_difficulty: uint64,
Expand Down Expand Up @@ -828,7 +828,7 @@ def validate_unfinished_header_block(

def validate_finished_header_block(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
header_block: HeaderBlock,
check_filter: bool,
expected_difficulty: uint64,
Expand Down
17 changes: 10 additions & 7 deletions chia/consensus/blockchain_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
from chia.util.ints import uint32


class BlockchainInterface(Protocol):
def get_peak(self) -> Optional[BlockRecord]: ...
def get_peak_height(self) -> Optional[uint32]: ...
class BlockRecordsInterface(Protocol):
def try_block_record(self, header_hash: bytes32) -> Optional[BlockRecord]: ...
def block_record(self, header_hash: bytes32) -> BlockRecord: ...
def contains_height(self, height: uint32) -> bool: ...
def contains_block(self, header_hash: bytes32) -> bool: ...
def height_to_hash(self, height: uint32) -> Optional[bytes32]: ...
def height_to_block_record(self, height: uint32) -> BlockRecord: ...


class BlockchainInterface(BlockRecordsInterface, Protocol):
def get_peak(self) -> Optional[BlockRecord]: ...
def get_peak_height(self) -> Optional[uint32]: ...
def get_ses_heights(self) -> List[uint32]: ...
def get_ses(self, height: uint32) -> SubEpochSummary: ...
def height_to_hash(self, height: uint32) -> Optional[bytes32]: ...
def contains_block(self, header_hash: bytes32) -> bool: ...
async def contains_block_from_db(self, header_hash: bytes32) -> bool: ...
def remove_block_record(self, header_hash: bytes32) -> None: ...
def add_block_record(self, block_record: BlockRecord) -> None: ...
def contains_height(self, height: uint32) -> bool: ...
async def warmup(self, fork_point: uint32) -> None: ...
async def get_block_record_from_db(self, header_hash: bytes32) -> Optional[BlockRecord]: ...
async def get_block_records_in_range(self, start: int, stop: int) -> Dict[bytes32, BlockRecord]: ...
Expand All @@ -37,7 +41,6 @@ async def get_header_block_by_height(
self, height: int, header_hash: bytes32, tx_filter: bool = True
) -> Optional[HeaderBlock]: ...
async def get_block_records_at(self, heights: List[uint32]) -> List[BlockRecord]: ...
def try_block_record(self, header_hash: bytes32) -> Optional[BlockRecord]: ...

async def persist_sub_epoch_challenge_segments(
self, sub_epoch_summary_hash: bytes32, segments: List[SubEpochChallengeSegment]
Expand Down
14 changes: 7 additions & 7 deletions chia/consensus/difficulty_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from typing import List, Optional, Tuple

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.ints import uint8, uint32, uint64, uint128
from chia.util.significant_bits import count_significant_bits, truncate_to_significant_bits


def _get_blocks_at_height(
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
prev_b: BlockRecord,
target_height: uint32,
max_num_blocks: uint32 = uint32(1),
Expand Down Expand Up @@ -53,7 +53,7 @@ def _get_blocks_at_height(

def _get_second_to_last_transaction_block_in_previous_epoch(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
last_b: BlockRecord,
) -> BlockRecord:
"""
Expand Down Expand Up @@ -135,7 +135,7 @@ def height_can_be_first_in_epoch(constants: ConsensusConstants, height: uint32)

def can_finish_sub_and_full_epoch(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
height: uint32,
prev_header_hash: Optional[bytes32],
deficit: uint8,
Expand Down Expand Up @@ -188,7 +188,7 @@ def can_finish_sub_and_full_epoch(

def _get_next_sub_slot_iters(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
prev_header_hash: bytes32,
height: uint32,
curr_sub_slot_iters: uint64,
Expand Down Expand Up @@ -267,7 +267,7 @@ def _get_next_sub_slot_iters(

def _get_next_difficulty(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
prev_header_hash: bytes32,
height: uint32,
current_difficulty: uint64,
Expand Down Expand Up @@ -353,7 +353,7 @@ def get_next_sub_slot_iters_and_difficulty(
constants: ConsensusConstants,
is_first_in_sub_slot: bool,
prev_b: Optional[BlockRecord],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
) -> Tuple[uint64, uint64]:
"""
Retrieves the current sub_slot iters and difficulty of the next block after prev_b.
Expand Down
4 changes: 2 additions & 2 deletions chia/consensus/full_block_to_block_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional, Union

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.consensus.deficit import calculate_deficit
from chia.consensus.difficulty_adjustment import get_next_sub_slot_iters_and_difficulty
Expand All @@ -21,7 +21,7 @@

def block_to_block_record(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
required_iters: uint64,
full_block: Optional[Union[FullBlock, HeaderBlock]],
header_block: Optional[HeaderBlock],
Expand Down
6 changes: 3 additions & 3 deletions chia/consensus/get_block_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List, Union

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.full_block import FullBlock
Expand All @@ -18,7 +18,7 @@

def final_eos_is_already_included(
header_block: Union[UnfinishedHeaderBlock, UnfinishedBlock, HeaderBlock, FullBlock],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
sub_slot_iters: uint64,
) -> bool:
"""
Expand Down Expand Up @@ -56,7 +56,7 @@ def final_eos_is_already_included(
def get_block_challenge(
constants: ConsensusConstants,
header_block: Union[UnfinishedHeaderBlock, UnfinishedBlock, HeaderBlock, FullBlock],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
genesis_block: bool,
overflow: bool,
skip_overflow_last_ss_validation: bool,
Expand Down
6 changes: 3 additions & 3 deletions chia/consensus/make_sub_epoch_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional, Union

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.consensus.deficit import calculate_deficit
from chia.consensus.difficulty_adjustment import (
Expand All @@ -25,7 +25,7 @@

def make_sub_epoch_summary(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
blocks_included_height: uint32,
prev_prev_block: BlockRecord,
new_difficulty: Optional[uint64],
Expand Down Expand Up @@ -73,7 +73,7 @@ def make_sub_epoch_summary(

def next_sub_epoch_summary(
constants: ConsensusConstants,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
required_iters: uint64,
block: Union[UnfinishedBlock, FullBlock],
can_finish_soon: bool = False,
Expand Down
4 changes: 2 additions & 2 deletions chia/consensus/vdf_info_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional, Tuple

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.types.blockchain_format.classgroup import ClassgroupElement
from chia.types.blockchain_format.sized_bytes import bytes32
Expand All @@ -16,7 +16,7 @@ def get_signage_point_vdf_info(
finished_sub_slots: List[EndOfSubSlotBundle],
overflow: bool,
prev_b: Optional[BlockRecord],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
sp_total_iters: uint128,
sp_iters: uint64,
) -> Tuple[bytes32, bytes32, ClassgroupElement, ClassgroupElement, uint64, uint64]:
Expand Down
10 changes: 5 additions & 5 deletions chia/full_node/full_node_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Dict, List, Optional, Set, Tuple

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.constants import ConsensusConstants
from chia.consensus.difficulty_adjustment import can_finish_sub_and_full_epoch
from chia.consensus.make_sub_epoch_summary import make_sub_epoch_summary
Expand Down Expand Up @@ -409,7 +409,7 @@ def initialize_genesis_sub_slot(self) -> None:
def new_finished_sub_slot(
self,
eos: EndOfSubSlotBundle,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
peak: Optional[BlockRecord],
next_sub_slot_iters: uint64,
next_difficulty: uint64,
Expand Down Expand Up @@ -680,7 +680,7 @@ def new_finished_sub_slot(
def new_signage_point(
self,
index: uint8,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
peak: Optional[BlockRecord],
next_sub_slot_iters: uint64,
signage_point: SignagePoint,
Expand Down Expand Up @@ -887,7 +887,7 @@ def new_peak(
sp_sub_slot: Optional[EndOfSubSlotBundle], # None if not overflow, or in first/second slot
ip_sub_slot: Optional[EndOfSubSlotBundle], # None if in first slot
fork_block: Optional[BlockRecord],
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
next_sub_slot_iters: uint64,
next_difficulty: uint64,
) -> FullNodeStorePeakResult:
Expand Down Expand Up @@ -987,7 +987,7 @@ def new_peak(

def get_finished_sub_slots(
self,
block_records: BlockchainInterface,
block_records: BlockRecordsInterface,
prev_b: Optional[BlockRecord],
last_challenge_to_add: bytes32,
) -> Optional[List[EndOfSubSlotBundle]]:
Expand Down
5 changes: 3 additions & 2 deletions chia/simulator/block_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from chia.consensus.block_creation import create_unfinished_block, unfinished_block_to_full_block
from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.consensus.coinbase import create_puzzlehash_for_pk
from chia.consensus.condition_costs import ConditionCost
from chia.consensus.constants import ConsensusConstants, replace_str_to_bytes
Expand Down Expand Up @@ -1477,7 +1477,8 @@ def get_pospaces_for_challenge(

def get_signage_point(
constants: ConsensusConstants,
blocks: BlockchainInterface,
# blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
latest_block: Optional[BlockRecord],
sub_slot_start_total_iters: uint128,
signage_point_index: uint8,
Expand Down
25 changes: 2 additions & 23 deletions chia/util/block_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
from typing import Dict, List, Optional

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.sub_epoch_summary import SubEpochSummary
from chia.types.blockchain_format.vdf import VDFInfo
from chia.types.header_block import HeaderBlock
from chia.types.weight_proof import SubEpochChallengeSegment, SubEpochSegments
from chia.util.ints import uint32


class BlockCache(BlockchainInterface):
class BlockCache(BlockRecordsInterface):
def __init__(
self,
blocks: Dict[bytes32, BlockRecord],
Expand All @@ -34,12 +33,6 @@ def __init__(
self._sub_epoch_segments: Dict[bytes32, SubEpochSegments] = {}
self.log = logging.getLogger(__name__)

def get_peak(self) -> Optional[BlockRecord]:
return None

def get_peak_height(self) -> Optional[uint32]:
return None

def block_record(self, header_hash: bytes32) -> BlockRecord:
return self._block_records[header_hash]

Expand Down Expand Up @@ -72,20 +65,9 @@ async def contains_block_from_db(self, header_hash: bytes32) -> bool:
def contains_height(self, height: uint32) -> bool:
return height in self._height_to_hash

async def warmup(self, fork_point: uint32) -> None:
return

async def get_block_records_in_range(self, start: int, stop: int) -> Dict[bytes32, BlockRecord]:
return self._block_records

async def get_header_block_by_height(
self, height: int, header_hash: bytes32, tx_filter: bool = True
) -> Optional[HeaderBlock]:
hb = self._headers.get(header_hash)
if hb is not None and hb.height != height:
raise ValueError(f"Block at {header_hash} is no longer in the blockchain (it's in a fork)")
return hb

async def get_block_records_at(self, heights: List[uint32]) -> List[BlockRecord]:
block_records: List[BlockRecord] = []
for height in heights:
Expand Down Expand Up @@ -128,6 +110,3 @@ async def get_sub_epoch_challenge_segments(
if segments is None:
return None
return segments.challenge_segments

def seen_compact_proofs(self, vdf_info: VDFInfo, height: uint32) -> bool:
return False
4 changes: 2 additions & 2 deletions chia/util/prev_transaction_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from typing import Tuple

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.blockchain_interface import BlockRecordsInterface
from chia.util.ints import uint128


def get_prev_transaction_block(
curr: BlockRecord,
blocks: BlockchainInterface,
blocks: BlockRecordsInterface,
total_iters_sp: uint128,
) -> Tuple[bool, BlockRecord]:
prev_transaction_block = curr
Expand Down

0 comments on commit 5196436

Please sign in to comment.