def TT(self, data): """ Vote Transaction This function processes whenever a Transaction having subtype VOTE is received. :return: """ try: message_tx = Transaction.from_json(data) except Exception as e: logger.info( 'Transfer Token Txn rejected - unable to decode serialised data - closing connection' ) logger.exception(e) self.transport.loseConnection() return if not self.factory.master_mr.isRequested( message_tx.get_message_hash(), self): return if message_tx.txhash in self.prev_txpool_hashes or message_tx.txhash in self.factory.buffered_chain.tx_pool.pending_tx_pool_hash: return del self.prev_txpool_hashes[0] self.prev_txpool_hashes.append(message_tx.txhash) # duplicate tx already received, would mess up nonce.. for t in self.factory.buffered_chain.tx_pool.transaction_pool: if message_tx.txhash == t.txhash: return self.factory.buffered_chain.tx_pool.update_pending_tx_pool( message_tx, self) self.factory.master_mr.register('TT', message_tx.get_message_hash(), data) self.factory.broadcast(message_tx.get_message_hash(), 'TT') # FIXME: This is tx processor related.. if not self.factory.txn_processor_running: # FIXME: TxnProcessor breaks tx_pool encapsulation txn_processor = TxnProcessor( buffered_chain=self.factory.buffered_chain, pending_tx_pool=self.factory.buffered_chain.tx_pool. pending_tx_pool, transaction_pool=self.factory.buffered_chain.tx_pool. transaction_pool) task_defer = TxnProcessor.create_cooperate( txn_processor).whenDone() task_defer.addCallback(self.factory.reset_processor_flag) \ .addErrback(self.factory.reset_processor_flag_with_err) self.factory.txn_processor_running = True
def add_unprocessed_txn(self, tx, ip): self._chain_manager.tx_pool.update_pending_tx_pool(tx, ip) if not self._txn_processor_running: txn_processor = TxnProcessor( state=self._chain_manager.state, transaction_pool_obj=self._chain_manager.tx_pool, broadcast_tx=self.broadcast_tx) task_defer = TxnProcessor.create_cooperate( txn_processor).whenDone() task_defer.addCallback(self.reset_processor_flag) \ .addErrback(self.reset_processor_flag_with_err) self._txn_processor_running = True
def setUp(self): m_state = Mock(name='A Mock State', autospec=State) m_state.get_address_state.return_value = Mock( name='A Mock AddressState', autospec=OptimizedAddressState) self.chain_manager = Mock(autospec=ChainManager) self.chain_manager._state = m_state tx_attrs = { 'validate.return_value': True, # Custom validation for different Transaction Types 'validate_extended.return_value': True, # Master/slave XMSS tree validation; balance & fee, OTS key reuse 'validate_transaction_pool.return_value': True # checks for OTS key reuse within TransactionPool only } self.tx1 = make_tx(name='Mock TX 1', **tx_attrs) self.tx2 = make_tx(name='Mock TX 2', **tx_attrs) self.tx3 = make_tx(name='Mock TX 3', **tx_attrs) self.tx4 = make_tx(name='Mock TX 4', **tx_attrs) self.m_txpool = Mock(autospec=TransactionPool) self.m_txpool.get_pending_transaction.side_effect = [ (self.tx1, replacement_getTime()), (self.tx2, replacement_getTime()), (self.tx3, replacement_getTime()), (self.tx4, replacement_getTime()) ] self.m_broadcast_tx = Mock(autospec=P2PFactory.broadcast_tx) self.txnprocessor = TxnProcessor(chain_manager=self.chain_manager, transaction_pool_obj=self.m_txpool, broadcast_tx=self.m_broadcast_tx)
def _receive_tx(self, json_tx_obj): try: tx = Transaction.from_json(json_tx_obj) except Exception as e: logger.info( 'tx rejected - unable to decode serialised data - closing connection' ) logger.exception(e) self.transport.loseConnection() return if not self.factory.master_mr.isRequested(tx.get_message_hash(), self): return if tx.txhash in self.prev_txpool_hashes or tx.txhash in self.factory.buffered_chain.tx_pool.pending_tx_pool_hash: return del self.prev_txpool_hashes[0] self.prev_txpool_hashes.append(tx.txhash) # duplicate tx already received, would mess up nonce.. for t in self.factory.buffered_chain.tx_pool.transaction_pool: if tx.txhash == t.txhash: return self.factory.buffered_chain.tx_pool.update_pending_tx_pool(tx, self) self.factory.master_mr.register('TX', tx.get_message_hash(), json_tx_obj) self.factory.broadcast(tx.get_message_hash(), 'TX') # FIXME: This is tx processor related.. if not self.factory.txn_processor_running: # FIXME: TxnProcessor breaks tx_pool encapsulation txn_processor = TxnProcessor( buffered_chain=self.factory.buffered_chain, pending_tx_pool=self.factory.buffered_chain.tx_pool. pending_tx_pool, transaction_pool=self.factory.buffered_chain.tx_pool. transaction_pool) task_defer = TxnProcessor.create_cooperate( txn_processor).whenDone() task_defer.addCallback(self.factory.reset_processor_flag) \ .addErrback(self.factory.reset_processor_flag_with_err) self.factory.txn_processor_running = True
def recv_tx(self, json_tx_obj): try: tx = SimpleTransaction().json_to_transaction(json_tx_obj) except Exception as e: logger.info( 'tx rejected - unable to decode serialised data - closing connection' ) logger.exception(e) self.transport.loseConnection() return if not self.factory.master_mr.isRequested(tx.get_message_hash(), self): return if tx.txhash in self.factory.chain.prev_txpool or tx.txhash in self.factory.chain.pending_tx_pool_hash: return del self.factory.chain.prev_txpool[0] self.factory.chain.prev_txpool.append(tx.txhash) for t in self.factory.chain.transaction_pool: # duplicate tx already received, would mess up nonce.. if tx.txhash == t.txhash: return self.factory.chain.update_pending_tx_pool(tx, self) self.factory.master_mr.register(tx.get_message_hash(), json_tx_obj, 'TX') self.factory.broadcast(tx.get_message_hash(), 'TX') if not self.factory.txn_processor_running: txn_processor = TxnProcessor( block_chain_buffer=self.factory.chain.block_chain_buffer, pending_tx_pool=self.factory.chain.pending_tx_pool, transaction_pool=self.factory.chain.transaction_pool, txhash_timestamp=self.factory.chain.txhash_timestamp) task_defer = TxnProcessor.create_cooperate( txn_processor).whenDone() task_defer.addCallback(self.factory.reset_processor_flag) \ .addErrback(self.factory.reset_processor_flag_with_err) self.factory.txn_processor_running = True return
def add_unprocessed_txn(self, tx, ip): self._chain_manager.tx_pool.update_pending_tx_pool(tx, ip) if not self._txn_processor_running: txn_processor = TxnProcessor(state=self._chain_manager.state, transaction_pool_obj=self._chain_manager.tx_pool, broadcast_tx=self.broadcast_tx) task_defer = TxnProcessor.create_cooperate(txn_processor).whenDone() task_defer.addCallback(self.reset_processor_flag) \ .addErrback(self.reset_processor_flag_with_err) self._txn_processor_running = True
def add_unprocessed_txn(self, tx, ip) -> bool: if tx.fee < config.user.transaction_minimum_fee: logger.info("Dropping Txn %s", bin2hstr(tx.txhash)) logger.info("Reason: Fee %s is below threshold fee %s", tx.fee, config.user.transaction_minimum_fee) return False if not self._chain_manager.tx_pool.update_pending_tx_pool(tx, ip): return False if not self._txn_processor_running: txn_processor = TxnProcessor( chain_manager=self._chain_manager, transaction_pool_obj=self._chain_manager.tx_pool, broadcast_tx=self.broadcast_tx) task_defer = TxnProcessor.create_cooperate( txn_processor).whenDone() task_defer.addCallback(self.reset_processor_flag) \ .addErrback(self.reset_processor_flag_with_err) self._txn_processor_running = True return True