def test_get_optimized_address_state2(self): alice_xmss = get_alice_xmss() alice_address = alice_xmss.address address_state = OptimizedAddressState.get_optimized_address_state(self.state, alice_address) addresses_state = { alice_address: address_state } self.assertTrue(isinstance(address_state.address, bytes)) OptimizedAddressState.put_optimized_addresses_state(self.state, addresses_state) address_state = OptimizedAddressState.get_optimized_address_state(self.state, alice_address) self.assertTrue(isinstance(address_state.address, bytes))
def test_getBalance(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} OptimizedAddressState.put_optimized_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.GetAddressStateReq() service.GetAddressState(request=request, context=context) context.set_code.assert_called() context.set_details.assert_called() context = Mock(spec=ServicerContext) request = xrd_pb2.GetBalanceReq() request.address = get_alice_xmss().address response = service.GetBalance(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(10, response.balance)
def gen_blocks(block_count, state, miner_address): blocks = [] block = None with mock.patch('xrd.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1615270948 addresses_state = dict() for i in range(0, block_count): if i == 0: block = GenesisBlock() for genesis_balance in GenesisBlock().genesis_balance: bytes_addr = genesis_balance.address addresses_state[ bytes_addr] = OptimizedAddressState.get_default( bytes_addr) addresses_state[ bytes_addr]._data.balance = genesis_balance.balance else: block = Block.create(dev_config=config.dev, block_number=i, prev_headerhash=block.headerhash, prev_timestamp=block.timestamp, transactions=[], miner_address=miner_address, seed_height=None, seed_hash=None) addresses_set = ChainManager.set_affected_address(block) coin_base_tx = Transaction.from_pbdata(block.transactions[0]) coin_base_tx.set_affected_address(addresses_set) chain_manager = ChainManager(state) state_container = chain_manager.new_state_container( addresses_set, block.block_number, False, None) coin_base_tx.apply(state, state_container) for tx_idx in range(1, len(block.transactions)): tx = Transaction.from_pbdata(block.transactions[tx_idx]) if not chain_manager.update_state_container( tx, state_container): return False tx.apply(state, state_container) block.set_nonces(dev_config=config.dev, mining_nonce=10, extra_nonce=0) blocks.append(block) metadata = BlockMetadata() metadata.set_block_difficulty(StringToUInt256('256')) BlockMetadata.put_block_metadata(state, block.headerhash, metadata, None) Block.put_block(state, block, None) bm = xrd_pb2.BlockNumberMapping( headerhash=block.headerhash, prev_headerhash=block.prev_headerhash) Block.put_block_number_mapping(state, block.block_number, bm, None) state.update_mainchain_height(block.block_number, None) OptimizedAddressState.put_optimized_addresses_state( state, addresses_state) return blocks
def test_getMiniTransactionsByAddress(self, mock_dev_config): mock_dev_config.data_per_page = 5 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) # Find a transaction alice_xmss = get_alice_xmss(8) context = Mock(spec=ServicerContext) bob_xmss = get_bob_xmss(4) txs = [] for i in range(0, 100): tx = TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[10], message_data=None, fee=0, xmss_pk=alice_xmss.pk) tx.sign(alice_xmss) txs.append(tx) addresses_set = {bob_xmss.address, alice_xmss.address} block = Block.create(config.dev, 10, b'', 100000000, txs, alice_xmss.address, seed_height=0, seed_hash=None) state_container = chain_manager.new_state_container(addresses_set, 5, True, None) for tx in txs: chain_manager.update_state_container(tx, state_container) address_state = state_container.addresses_state[tx.addr_from] state_container.paginated_tx_hash.insert(address_state, tx.txhash) address_state = state_container.addresses_state[tx.addrs_to[0]] state_container.paginated_tx_hash.insert(address_state, tx.txhash) state_container.paginated_tx_hash.put_paginated_data(None) OptimizedAddressState.put_optimized_addresses_state(db_state, state_container.addresses_state) TransactionMetadata.update_tx_metadata(db_state, block, None) request = xrd_pb2.GetMiniTransactionsByAddressReq(address=alice_xmss.address, item_per_page=10, page_number=1) response = service.GetMiniTransactionsByAddress(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(len(response.mini_transactions), 10) for i in range(0, 10): self.assertEqual(bin2hstr(txs[len(txs) - i - 1].txhash), response.mini_transactions[i].transaction_hash) self.assertEqual(response.balance, 0) TransactionMetadata.update_tx_metadata(db_state, block, None) request = xrd_pb2.GetMiniTransactionsByAddressReq(address=alice_xmss.address, item_per_page=8, page_number=3) response = service.GetMiniTransactionsByAddress(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(len(response.mini_transactions), 8) for i in range(0, 8): self.assertEqual(bin2hstr(txs[len(txs) - i - 17].txhash), response.mini_transactions[i].transaction_hash) self.assertEqual(response.balance, 0)