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
|
2021-02-12 23:24:33 +03:00
|
|
|
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
|
2021-02-09 10:14:37 +03:00
|
|
|
async def new_peak_timelord(self, new_peak: timelord_protocol.NewPeakTimelord):
|
2020-12-02 06:01:47 +03:00
|
|
|
async with self.timelord.lock:
|
2021-02-12 23:24:33 +03:00
|
|
|
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(
|
2021-02-12 23:24:33 +03:00
|
|
|
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
|
2021-02-12 23:24:33 +03:00
|
|
|
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:
|
2021-02-12 23:24:33 +03:00
|
|
|
log.warning("block that we don't have, changing to it.")
|
2020-12-11 09:35:08 +03:00
|
|
|
self.timelord.new_peak = new_peak
|
|
|
|
self.timelord.new_subslot_end = None
|
2020-12-02 06:01:47 +03:00
|
|
|
|
|
|
|
@api_request
|
2021-02-12 23:24:33 +03:00
|
|
|
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:
|
2020-12-15 20:20:59 +03:00
|
|
|
try:
|
2021-02-12 23:24:33 +03:00
|
|
|
sp_iters, ip_iters = iters_from_block(
|
2020-12-15 20:20:59 +03:00
|
|
|
self.timelord.constants,
|
2021-02-12 23:24:33 +03:00
|
|
|
new_unfinished_block.reward_chain_block,
|
2020-12-15 20:20:59 +03:00
|
|
|
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:
|
2021-02-12 23:24:33 +03:00
|
|
|
self.timelord.overflow_blocks.append(new_unfinished_block)
|
2020-12-02 06:01:47 +03:00
|
|
|
elif ip_iters > last_ip_iters:
|
2021-02-12 23:24:33 +03:00
|
|
|
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:
|
2021-02-12 23:24:33 +03:00
|
|
|
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)
|
2021-02-12 23:24:33 +03:00
|
|
|
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
|