def test_getHeight(self): with set_xrd_dir('no_data'): db_state = State() alice_xmss = get_alice_xmss() optimized_address_state = OptimizedAddressState.create(address=alice_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) addresses_state = {optimized_address_state.address: optimized_address_state} AddressState.put_addresses_state(db_state, addresses_state) p2p_factory = Mock(spec=P2PFactory) chain_manager = ChainManager(db_state) chain_manager._last_block = Mock() chain_manager._last_block.block_number = 100 xrdnode = xrdNode(mining_address=b'') xrdnode.set_chain_manager(chain_manager) xrdnode._p2pfactory = p2p_factory xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(xrdnode) context = Mock(spec=ServicerContext) request = xrd_pb2.GetHeightReq() response = service.GetHeight(request=request, context=context) self.assertEqual(response.height, 100)
def test_getAddressState(self): with set_xrd_dir('no_data'): db_state = State() alice_xmss = get_alice_xmss() optimized_address_state = OptimizedAddressState.create(address=alice_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, optimized_address_state) p2p_factory = Mock(spec=P2PFactory) chain_manager = ChainManager(db_state) xrdnode = xrdNode(mining_address=b'') xrdnode.set_chain_manager(chain_manager) xrdnode._p2pfactory = p2p_factory xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(xrdnode) context = Mock(spec=ServicerContext) request = xrd_pb2.GetAddressStateReq() service.GetAddressState(request=request, context=context) context.set_code.assert_called() context.set_details.assert_called() context = Mock(spec=ServicerContext) request = xrd_pb2.GetAddressStateReq() request.address = get_alice_xmss().address response = service.GetAddressState(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(alice_xmss.address, response.state.address) self.assertEqual(25, response.state.nonce) self.assertEqual(10, response.state.balance) self.assertEqual([b'\x00'] * config.dev.ots_bitfield_size, response.state.ots_bitfield) self.assertEqual([], response.state.transaction_hashes)
def test_getOTS(self): with set_xrd_dir('no_data'): db_state = State() alice_xmss = get_alice_xmss() optimized_address_state = OptimizedAddressState.create(address=alice_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) addresses_state = {optimized_address_state.address: optimized_address_state} AddressState.put_addresses_state(db_state, addresses_state) p2p_factory = Mock(spec=P2PFactory) chain_manager = ChainManager(db_state) xrdnode = xrdNode(mining_address=b'') xrdnode.set_chain_manager(chain_manager) xrdnode._p2pfactory = p2p_factory xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(xrdnode) context = Mock(spec=ServicerContext) request = xrd_pb2.GetOTSReq(address=alice_xmss.address) response = service.GetOTS(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(0, response.next_unused_ots_index) paginated_bitfield = PaginatedBitfield(True, db_state._db) paginated_bitfield.set_ots_key(addresses_state, optimized_address_state.address, 0) paginated_bitfield.put_addresses_bitfield(None) response = service.GetOTS(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(1, response.next_unused_ots_index)
def test_getTotalBalance(self): with set_xrd_dir('no_data'): db_state = State() xmss1 = get_alice_xmss() xmss2 = get_alice_xmss(4) xmss3 = get_bob_xmss(4) address_state1 = OptimizedAddressState.create(address=xmss1.address, nonce=25, balance=1000, ots_bitfield_used_page=0, transaction_hash_count=0, tokens_count=0, lattice_pk_count=0, slaves_count=0, multi_sig_address_count=0) address_state2 = OptimizedAddressState.create(address=xmss2.address, nonce=25, balance=2000, ots_bitfield_used_page=0, transaction_hash_count=0, tokens_count=0, lattice_pk_count=0, slaves_count=0, multi_sig_address_count=0) address_state3 = OptimizedAddressState.create(address=xmss3.address, nonce=25, balance=3000, 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, address_state1) AddressState.put_address_state(db_state, address_state2) AddressState.put_address_state(db_state, address_state3) p2p_factory = Mock(spec=P2PFactory) chain_manager = ChainManager(db_state) xrdnode = xrdNode(mining_address=b'') xrdnode.set_chain_manager(chain_manager) xrdnode._p2pfactory = p2p_factory xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(xrdnode) context = Mock(spec=ServicerContext) request = xrd_pb2.GetAddressStateReq() service.GetAddressState(request=request, context=context) context.set_code.assert_called() context.set_details.assert_called() context = Mock(spec=ServicerContext) request = xrd_pb2.GetTotalBalanceReq() request.addresses.extend([xmss1.address, xmss2.address, xmss3.address]) response = service.GetTotalBalance(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(6000, response.balance)
def test_getObject(self): SOME_ODD_HASH = sha256(b'this should not be found') with set_xrd_dir('no_data'): db_state = State() p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager = ChainManager(db_state) xrdnode = xrdNode(mining_address=b'') xrdnode.set_chain_manager(chain_manager) xrdnode._p2pfactory = p2p_factory xrdnode._pow = p2p_factory.pow xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(xrdnode) # First try an empty request context = Mock(spec=ServicerContext) request = xrd_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 = xrd_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 = xrd_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 = xrd_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, xrd_pb2.BlockNumberMapping(headerhash=block.headerhash), None) context = Mock(spec=ServicerContext) request = xrd_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)