async def relay_all_txs(self): rs_client = ObjectManager().channel_service.rs_client if not rs_client: return items = list(self.__tx_queue.d.values()) self.__tx_queue.d.clear() for item in items: tx = item.value if not util.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND, util.get_now_time_stamp()): continue ts = TransactionSerializer.new(tx.version, tx.type(), self.blockchain.tx_versioner) if tx.version == v2.version: rest_method = RestMethod.SendTransaction2 elif tx.version == v3.version: rest_method = RestMethod.SendTransaction3 else: continue raw_data = ts.to_raw_data(tx) raw_data["from_"] = raw_data.pop("from") for i in range(conf.RELAY_RETRY_TIMES): try: await rs_client.call_async( rest_method, rest_method.value.params(**raw_data)) except Exception as e: util.logger.warning(f"Relay failed. Tx({tx}), {e}") else: break
def __pre_validate(self, tx: Transaction): if tx.hash.hex() in self.__tx_queue: raise TransactionDuplicatedHashError(tx) if not util.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND): raise TransactionOutOfTimeBound(tx, util.get_now_time_stamp())
def __pre_validate(self, tx: Transaction): if tx.hash.hex() in self.__txQueue: raise TransactionInvalidDuplicatedHash(tx.hash.hex()) if not util.is_in_time_boundary(tx.timestamp, conf.ALLOW_TIMESTAMP_BOUNDARY_SECOND): raise TransactionInvalidOutOfTimeBound(tx.hash.hex(), tx.timestamp, util.get_now_time_stamp())
def verify_transactions(self, block: 'Block', blockchain=None): for tx in block.body.transactions.values(): if not utils.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND, block.header.timestamp): exception = TransactionOutOfTimeBound(tx, block.header.timestamp) self._handle_exception(exception) tv = TransactionVerifier.new(tx.version, tx.type(), self._tx_versioner, self._raise_exceptions) tv.verify(tx, blockchain) if not self._raise_exceptions: self.exceptions.extend(tv.exceptions)
def __add_tx_to_block(self, block_builder): tx_queue = self.__block_manager.get_tx_queue() block_tx_size = 0 tx_versioner = self.__blockchain.tx_versioner while tx_queue: if block_tx_size >= conf.MAX_TX_SIZE_IN_BLOCK: utils.logger.warning( f"consensus_base total size({block_builder.size()}) " f"count({len(block_builder.transactions)}) " f"_txQueue size ({len(tx_queue)})") break tx: 'Transaction' = tx_queue.get_item_in_status( get_status=TransactionStatusInQueue.normal, set_status=TransactionStatusInQueue.added_to_block ) if tx is None: break block_timestamp = block_builder.fixed_timestamp if not utils.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND, block_timestamp): utils.logger.info(f"fail add tx to block by TIMESTAMP_BOUNDARY_SECOND" f"({conf.TIMESTAMP_BOUNDARY_SECOND}) " f"tx({tx.hash}), timestamp({tx.timestamp})") continue tv = TransactionVerifier.new(tx.version, tx.type(), tx_versioner) try: tv.verify(tx, self.__blockchain) except Exception as e: utils.logger.warning( f"tx hash invalid.\n" f"tx: {tx}\n" f"exception: {e}" ) traceback.print_exc() else: block_builder.transactions[tx.hash] = tx block_tx_size += tx.size(tx_versioner)
def __pre_validate(self, tx: Transaction): if not util.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND): raise TransactionOutOfTimeBound(tx, util.get_now_time_stamp())