chia-blockchain/src/timelord/timelord_api.py

67 lines
3.0 KiB
Python
Raw Normal View History

2020-12-05 14:07:49 +03:00
from typing import Callable, Optional
2020-12-04 12:07:01 +03:00
import logging
2020-10-16 04:03:46 +03:00
from src.protocols import timelord_protocol
from src.timelord.timelord import Timelord, iters_from_block, Chain, IterationType
2020-12-02 06:01:47 +03:00
from src.util.api_decorators import api_request
from src.util.ints import uint64
2020-10-16 04:03:46 +03:00
2020-12-04 12:07:01 +03:00
log = logging.getLogger(__name__)
2020-10-16 04:03:46 +03:00
class TimelordAPI:
timelord: Timelord
def __init__(self, timelord):
self.timelord = timelord
2020-10-21 11:19:40 +03:00
def _set_state_changed_callback(self, callback: Callable):
2020-11-11 11:35:31 +03:00
pass
2020-10-21 11:19:40 +03:00
2020-12-02 06:01:47 +03:00
@api_request
async def new_peak_timelord(self, new_peak: timelord_protocol.NewPeakTimelord):
2020-12-02 06:01:47 +03:00
async with self.timelord.lock:
if new_peak.reward_chain_block.weight > self.timelord.last_state.get_weight():
2020-12-05 19:24:36 +03:00
log.info("Not skipping peak, don't have. Maybe we are not the fastest timelord")
log.info(
f"New peak: height: {new_peak.reward_chain_block.height} weight: "
f"{new_peak.reward_chain_block.weight} "
2020-12-05 19:24:36 +03:00
)
2020-12-05 18:31:45 +03:00
self.timelord.new_peak = new_peak
elif (
self.timelord.last_state.peak is not None
and self.timelord.last_state.peak.reward_chain_block == new_peak.reward_chain_block
2020-12-05 18:31:45 +03:00
):
2020-12-04 12:07:01 +03:00
log.info("Skipping peak, already have.")
return
else:
log.warning("block that we don't have, changing to it.")
self.timelord.new_peak = new_peak
self.timelord.new_subslot_end = None
2020-12-02 06:01:47 +03:00
@api_request
async def new_unfinished_block(self, new_unfinished_block: timelord_protocol.NewUnfinishedBlock):
2020-12-02 06:01:47 +03:00
async with self.timelord.lock:
try:
sp_iters, ip_iters = iters_from_block(
self.timelord.constants,
new_unfinished_block.reward_chain_block,
self.timelord.last_state.get_sub_slot_iters(),
self.timelord.last_state.get_difficulty(),
)
except Exception:
return
2020-12-02 06:01:47 +03:00
last_ip_iters = self.timelord.last_state.get_last_ip()
2020-12-03 04:10:58 +03:00
if sp_iters > ip_iters:
self.timelord.overflow_blocks.append(new_unfinished_block)
2020-12-02 06:01:47 +03:00
elif ip_iters > last_ip_iters:
new_block_iters: Optional[uint64] = self.timelord._can_infuse_unfinished_block(new_unfinished_block)
2020-12-05 14:07:49 +03:00
if new_block_iters:
self.timelord.unfinished_blocks.append(new_unfinished_block)
2020-12-05 19:24:36 +03:00
for chain in [Chain.REWARD_CHAIN, Chain.CHALLENGE_CHAIN]:
2020-12-05 14:07:49 +03:00
self.timelord.iters_to_submit[chain].append(new_block_iters)
if self.timelord.last_state.get_deficit() < self.timelord.constants.MIN_BLOCKS_PER_CHALLENGE_BLOCK:
2020-12-05 19:24:36 +03:00
self.timelord.iters_to_submit[Chain.INFUSED_CHALLENGE_CHAIN].append(new_block_iters)
2020-12-05 14:07:49 +03:00
self.timelord.iteration_to_proof_type[new_block_iters] = IterationType.INFUSION_POINT
2020-12-15 19:31:41 +03:00
self.timelord.total_unfinished += 1