def test_log(self): print() logger.info("INFO LOG") logger.warning("WARNING LOG") logger.error("ERROR LOG") logger.debug("DEBUG LOG") logger.info(self) logger.error(1, 2)
def handle_message(self, msg: bytes): try: message = msgpack.loads(msg) msg_type = Message(message.pop("type")) logger.info(f"Handling: {msg_type}") func_mapping: Dict[Message, Callable] = { Message.REQUEST_LATEST_BLOCK: self.handle_request_latest_block, Message.RECEIVE_LATEST_BLOCK: self.handle_receive_latest_block, Message.REQUEST_BLOCKCHAIN: self.handle_request_blockchain, Message.RECEIVE_BLOCKCHAIN: self.handle_receive_blockchain, } func_mapping[msg_type](**message) except (UnpackException, KeyError, ValueError) as e: logger.error("Unknown message received") logger.error(f"{e}")
def listen(self, port: int, interface: str = "0.0.0.0") -> None: logger.info( f"Node {self.node.long_id} listening on {interface}:{port}") loop = asyncio.get_event_loop() listen_udp = loop.create_datagram_endpoint(self._create_protocol, local_addr=(interface, port)) self.transport, self.protocol = loop.run_until_complete(listen_udp) listen_tcp = loop.create_server(lambda: TCPProtocol(self), interface, port) self.tcp_server = loop.run_until_complete(listen_tcp) self.refresh_table() self.sync_blockchain()
def retarget(self) -> str: lb = self.latest_block block_count = 10 target_timespan = block_count * self.interval if len(self.blocks) % block_count != 0: return lb.target else: ratio_limit = 4 actual_timespan = lb.timestamp - self.blocks[-block_count].timestamp adjusted_timespan = min( max(actual_timespan, target_timespan / ratio_limit), target_timespan * ratio_limit, ) assert 1 / ratio_limit <= adjusted_timespan / target_timespan <= ratio_limit logger.info( f"Retargeting at {self.length}, difficulty change: {target_timespan/adjusted_timespan:.2%}" ) new_target = int(lb.target, 16) * adjusted_timespan / target_timespan return f"{int(new_target):x}".rjust(64, "0")