def add_tx_to_pool(self, tx_class_obj) -> bool: if self.is_full_transaction_pool(): return False heapq.heappush( self.transaction_pool, [tx_class_obj.fee, TransactionInfo(tx_class_obj)]) return True
def add_tx_to_pool(self, tx_class_obj, block_number, timestamp: int = None) -> bool: if self.is_full_transaction_pool(): return False heapq.heappush(self.transaction_pool, [ tx_class_obj.fee, TransactionInfo(tx_class_obj, block_number, timestamp) ]) return True
def test_getTransaction(self): db_state = Mock(spec=State) db_state.get_tx_metadata = MagicMock(return_value=None) db_state.get_block = MagicMock(return_value=None) p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager = ChainManager(db_state) qrlnode = QRLNode(mining_address=b'') qrlnode.set_chain_manager(chain_manager) qrlnode._p2pfactory = p2p_factory qrlnode._pow = p2p_factory.pow qrlnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(qrlnode) # Find an address bob_xmss = get_bob_xmss() # Find a transaction alice_xmss = get_alice_xmss() db_state.address_used = MagicMock(return_value=False) tx1 = TransferTransaction.create( addrs_to=[bob_xmss.address], amounts=[125], message_data=None, fee=19, xmss_pk=bob_xmss.pk, master_addr=alice_xmss.address) chain_manager.tx_pool.transaction_pool = [(0, TransactionInfo(tx1, 0))] context = Mock(spec=ServicerContext) request = qrl_pb2.GetTransactionReq() request.tx_hash = tx1.txhash response = service.GetTransaction(request=request, context=context) context.set_code.assert_not_called() self.assertIsNotNone(response.tx) self.assertEqual('transfer', response.tx.WhichOneof('transactionType')) self.assertEqual(alice_xmss.address, response.tx.master_addr) self.assertEqual(bob_xmss.pk, response.tx.public_key) self.assertEqual(tx1.txhash, response.tx.transaction_hash) self.assertEqual(b'', response.tx.signature) self.assertEqual(bob_xmss.address, response.tx.transfer.addrs_to[0]) self.assertEqual(125, response.tx.transfer.amounts[0]) self.assertEqual(19, response.tx.fee)
def update_pending_tx_pool(self, tx, ip, ignore_reserve=True) -> bool: if self.is_full_pending_transaction_pool(ignore_reserve): return False idx = self.get_tx_index_from_pool(tx.txhash) if idx > -1: return False if tx.txhash in self.pending_tx_pool_hash: return False # Since its a min heap giving priority to lower number # So -1 multiplied to give higher priority to higher txn heapq.heappush(self.pending_tx_pool, [tx.fee * -1, TransactionInfo(tx), ip]) self.pending_tx_pool_hash.add(tx.txhash) return True
def update_pending_tx_pool(self, tx, ip, ignore_reserve=True) -> bool: if self.is_full_pending_transaction_pool(ignore_reserve): return False idx = self.get_tx_index_from_pool(tx.txhash) if idx > -1: return False if isinstance(tx, CoinBase): logger.warning( 'Rejected CoinBase Transaction as received without block') return False if tx.txhash in self.pending_tx_pool_hash: return False # Since its a min heap giving priority to lower number # So -1 multiplied to give higher priority to higher txn heapq.heappush(self.pending_tx_pool, [tx.fee * -1, TransactionInfo(tx, -1), ip]) self.pending_tx_pool_hash.add(tx.txhash) return True
def test_getLatestData(self): blocks = [] txs = [] alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() for i in range(1, 4): for j in range(1, 3): txs.append( TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[i * 100 + j], fee=j, xmss_pk=alice_xmss.pk)) blocks.append( Block.create(block_number=i, prev_headerhash=sha256(b'reveal'), prev_timestamp=10, transactions=txs, miner_address=alice_xmss.address)) txpool = [] for j in range(10, 15): tx = TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[1000 + j], fee=j, xmss_pk=get_alice_xmss().pk) txpool.append((tx.fee, TransactionInfo(tx, 0))) db_state = Mock(spec=State) db_state.get_tx_metadata = MagicMock(return_value=None) db_state.get_last_txs = MagicMock(return_value=txs) db_state.get_block_by_number = Mock() db_state.get_block_by_number.side_effect = blocks p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager = ChainManager(db_state) chain_manager.tx_pool = Mock() chain_manager.tx_pool.transactions = heapq.nlargest( len(txpool), txpool) chain_manager.tx_pool.transaction_pool = txpool chain_manager._last_block = Mock() chain_manager._last_block.block_number = len(blocks) qrlnode = QRLNode(mining_address=b'') qrlnode.set_chain_manager(chain_manager) qrlnode.get_block_from_index = MagicMock(return_value=None) qrlnode._p2pfactory = p2p_factory qrlnode._pow = p2p_factory.pow service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) request = qrl_pb2.GetLatestDataReq(filter=qrl_pb2.GetLatestDataReq.ALL, offset=1, quantity=3) response = service.GetLatestData(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() # Verify blockheaders self.assertEqual(3, len(response.blockheaders)) self.assertEqual(1, response.blockheaders[0].header.block_number) self.assertEqual(2, response.blockheaders[1].header.block_number) self.assertEqual(3, response.blockheaders[2].header.block_number) # Verify transactions_unconfirmed self.assertEqual(3, len(response.transactions_unconfirmed)) # TODO: Verify expected order self.assertEqual( 1013, response.transactions_unconfirmed[0].tx.transfer.amounts[0]) self.assertEqual( 1012, response.transactions_unconfirmed[1].tx.transfer.amounts[0]) self.assertEqual( 1011, response.transactions_unconfirmed[2].tx.transfer.amounts[0]) # Verify transactions self.assertEqual(3, len(response.transactions)) self.assertEqual(2, response.transactions[0].tx.fee) self.assertEqual(1, response.transactions[1].tx.fee) self.assertEqual(2, response.transactions[2].tx.fee) self.assertEqual(102, response.transactions[0].tx.transfer.amounts[0]) self.assertEqual(201, response.transactions[1].tx.transfer.amounts[0]) self.assertEqual(202, response.transactions[2].tx.transfer.amounts[0])
def test_getObject(self): SOME_ODD_HASH = sha256(b'this should not be found') db_state = Mock(spec=State) db_state.get_tx_metadata = MagicMock(return_value=None) db_state.get_block = MagicMock(return_value=None) p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager = ChainManager(db_state) qrlnode = QRLNode(mining_address=b'') qrlnode.set_chain_manager(chain_manager) qrlnode._p2pfactory = p2p_factory qrlnode._pow = p2p_factory.pow qrlnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(qrlnode) # First try an empty request context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() self.assertFalse(response.found) # Some odd address context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = SOME_ODD_HASH response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertFalse(response.found) # Find an address bob_xmss = get_bob_xmss() addr1_state = AddressState.create(address=bob_xmss.address, nonce=25, balance=10, ots_bitfield=[b'\x00'] * config.dev.ots_bitfield_size, tokens=dict(), slave_pks_access_type=dict(), ots_counter=0) addr1_state.transaction_hashes.append(sha256(b'0')) addr1_state.transaction_hashes.append(sha256(b'1')) db_state.get_address_state = MagicMock(return_value=addr1_state) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = bob_xmss.address response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.address_state) self.assertEqual(bob_xmss.address, response.address_state.address) self.assertEqual(25, response.address_state.nonce) self.assertEqual(10, response.address_state.balance) self.assertEqual([sha256(b'0'), sha256(b'1')], response.address_state.transaction_hashes) # Find a transaction alice_xmss = get_alice_xmss() db_state.address_used = MagicMock(return_value=False) tx1 = TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[125], fee=19, xmss_pk=bob_xmss.pk, master_addr=alice_xmss.address) chain_manager.tx_pool.transaction_pool = [(0, TransactionInfo(tx1, 0))] context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = tx1.txhash response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.transaction) self.assertEqual('transfer', response.transaction.tx.WhichOneof('transactionType')) self.assertEqual(alice_xmss.address, response.transaction.tx.master_addr) self.assertEqual(bob_xmss.pk, response.transaction.tx.public_key) self.assertEqual(tx1.txhash, response.transaction.tx.transaction_hash) self.assertEqual(b'', response.transaction.tx.signature) self.assertEqual(bob_xmss.address, response.transaction.tx.transfer.addrs_to[0]) self.assertEqual(125, response.transaction.tx.transfer.amounts[0]) self.assertEqual(19, response.transaction.tx.fee) alice_xmss = get_alice_xmss() # Find a block db_state.get_block_by_number = MagicMock( return_value=Block.create(block_number=1, prev_headerhash=sha256(b'reveal'), prev_timestamp=10, transactions=[], miner_address=alice_xmss.address)) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = bytes(str2bin('1')) response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.block_extended) self.assertEqual(1, response.block_extended.header.block_number)
def test_getLatestData(self): with set_qrl_dir('no_data'): db_state = State() chain_manager = ChainManager(db_state) blocks = [] txs = [] alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() for i in range(0, 4): for j in range(1, 3): txs.append(TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[i * 100 + j], message_data=None, fee=j, xmss_pk=alice_xmss.pk)) blocks.append(Block.create(dev_config=config.dev, block_number=i, prev_headerhash=sha256(b'reveal'), prev_timestamp=10, transactions=txs, miner_address=alice_xmss.address, seed_height=10, seed_hash=None)) block = blocks[i] Block.put_block(db_state, block, None) Block.put_block_number_mapping(db_state, block.block_number, qrl_pb2.BlockNumberMapping(headerhash=block.headerhash), None) TransactionMetadata.update_tx_metadata(db_state, block, None) chain_manager._last_block = blocks[-1] txpool = [] txs = [] for j in range(10, 15): tx = TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[1000 + j], message_data=None, fee=j, xmss_pk=get_alice_xmss().pk) txpool.append((tx.fee, TransactionInfo(tx, 0))) txs.append(tx) p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager.tx_pool = Mock() chain_manager.tx_pool.transactions = heapq.nlargest(len(txpool), txpool) chain_manager.tx_pool.transaction_pool = txpool qrlnode = QRLNode(mining_address=b'') qrlnode.set_chain_manager(chain_manager) qrlnode.get_block_from_index = MagicMock(return_value=None) qrlnode._p2pfactory = p2p_factory qrlnode._pow = p2p_factory.pow service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) request = qrl_pb2.GetLatestDataReq(filter=qrl_pb2.GetLatestDataReq.ALL, offset=0, quantity=3) response = service.GetLatestData(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() # Verify blockheaders self.assertEqual(3, len(response.blockheaders)) self.assertEqual(1, response.blockheaders[0].header.block_number) self.assertEqual(2, response.blockheaders[1].header.block_number) self.assertEqual(3, response.blockheaders[2].header.block_number) # Verify transactions_unconfirmed self.assertEqual(3, len(response.transactions_unconfirmed)) # TODO: Verify expected order self.assertEqual(1014, response.transactions_unconfirmed[0].tx.transfer.amounts[0]) self.assertEqual(1013, response.transactions_unconfirmed[1].tx.transfer.amounts[0]) self.assertEqual(1012, response.transactions_unconfirmed[2].tx.transfer.amounts[0]) # Verify transactions self.assertEqual(3, len(response.transactions)) self.assertEqual(2, response.transactions[0].tx.fee) self.assertEqual(1, response.transactions[1].tx.fee) self.assertEqual(2, response.transactions[2].tx.fee) self.assertEqual(302, response.transactions[0].tx.transfer.amounts[0]) self.assertEqual(301, response.transactions[1].tx.transfer.amounts[0]) self.assertEqual(202, response.transactions[2].tx.transfer.amounts[0])
def test_getObject(self): SOME_ODD_HASH = sha256(b'this should not be found') with set_qrl_dir('no_data'): db_state = State() p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager = ChainManager(db_state) qrlnode = QRLNode(mining_address=b'') qrlnode.set_chain_manager(chain_manager) qrlnode._p2pfactory = p2p_factory qrlnode._pow = p2p_factory.pow qrlnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(qrlnode) # First try an empty request context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() self.assertFalse(response.found) # Some odd address context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = SOME_ODD_HASH response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertFalse(response.found) # Find an address bob_xmss = get_bob_xmss() addr1_state = OptimizedAddressState.create(address=bob_xmss.address, nonce=25, balance=10, ots_bitfield_used_page=0, transaction_hash_count=0, tokens_count=0, lattice_pk_count=0, slaves_count=0, multi_sig_address_count=0) AddressState.put_address_state(db_state, addr1_state, None) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = bob_xmss.address response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.address_state) self.assertEqual(bob_xmss.address, response.address_state.address) self.assertEqual(25, response.address_state.nonce) self.assertEqual(10, response.address_state.balance) # Find a transaction alice_xmss = get_alice_xmss() db_state.address_used = MagicMock(return_value=False) tx1 = TransferTransaction.create( addrs_to=[bob_xmss.address], amounts=[125], message_data=None, fee=19, xmss_pk=bob_xmss.pk, master_addr=alice_xmss.address) chain_manager.tx_pool.transaction_pool = [(0, TransactionInfo(tx1, 0))] context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = tx1.txhash response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.transaction) self.assertEqual('transfer', response.transaction.tx.WhichOneof('transactionType')) self.assertEqual(alice_xmss.address, response.transaction.tx.master_addr) self.assertEqual(bob_xmss.pk, response.transaction.tx.public_key) self.assertEqual(tx1.txhash, response.transaction.tx.transaction_hash) self.assertEqual(b'', response.transaction.tx.signature) self.assertEqual(bob_xmss.address, response.transaction.tx.transfer.addrs_to[0]) self.assertEqual(125, response.transaction.tx.transfer.amounts[0]) self.assertEqual(19, response.transaction.tx.fee) alice_xmss = get_alice_xmss() # Find a block block = Block.create(dev_config=config.dev, block_number=1, prev_headerhash=sha256(b'reveal'), prev_timestamp=10, transactions=[], miner_address=alice_xmss.address, seed_height=0, seed_hash=None) Block.put_block(db_state, block, None) Block.put_block_number_mapping(db_state, block.block_number, qrl_pb2.BlockNumberMapping(headerhash=block.headerhash), None) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = bytes(str2bin('1')) response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.block_extended) self.assertEqual(1, response.block_extended.header.block_number)