chia-blockchain/src/timelord_api.py

43 lines
1.8 KiB
Python
Raw Normal View History

2020-10-21 11:19:40 +03:00
from typing import Callable
2020-10-16 04:03:46 +03:00
from src.protocols import timelord_protocol
2020-12-02 06:01:47 +03:00
from src.timelord import Timelord, iters_from_sub_block, Chain, IterationType
from src.util.api_decorators import api_request
from src.util.ints import uint64
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(self, new_peak: timelord_protocol.NewPeak):
async with self.timelord.lock:
if self.timelord.last_state is None or self.timelord.last_state.get_weight() < new_peak.weight:
self.timelord.new_peak = new_peak
@api_request
async def new_unfinished_subblock(self, new_unfinished_subblock: timelord_protocol.NewUnfinishedSubBlock):
async with self.timelord.lock:
if not self.timelord.accept_unfinished_block(new_unfinished_subblock):
return
sp_iters, ip_iters = iters_from_sub_block(
self.timelord.constants,
new_unfinished_subblock.reward_chain_sub_block,
self.timelord.last_state.get_sub_slot_iters(),
self.timelord.last_state.get_difficulty(),
)
last_ip_iters = self.timelord.last_state.get_last_ip()
if sp_iters < ip_iters:
self.timelord.overflow_blocks.append(new_unfinished_subblock)
elif ip_iters > last_ip_iters:
self.timelord.unfinished_blocks.append(new_unfinished_subblock)
for chain in Chain:
self.timelord.iters_to_submit[chain].append(uint64(ip_iters - last_ip_iters))
self.timelord.iteration_to_proof_type[ip_iters - last_ip_iters] = IterationType.INFUSION_POINT