Esempio n. 1
0
    def test_measurement_0(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(
                    state)  # to avoid warning (unused variable)
                state.get_block = Mock(
                    side_effect=TestStateMeasurement.get_block_example1)
                parent_metadata = Mock()
                parent_metadata.last_N_headerhashes = []

                measurement = state.get_measurement(
                    100,
                    parent_headerhash=b'0',
                    parent_metadata=parent_metadata)
                self.assertEqual(60, measurement)

                measurement = state.get_measurement(
                    110,
                    parent_headerhash=b'0',
                    parent_metadata=parent_metadata)
                self.assertEqual(60, measurement)

                measurement = state.get_measurement(
                    1000,
                    parent_headerhash=b'0',
                    parent_metadata=parent_metadata)
                self.assertEqual(60, measurement)
Esempio n. 2
0
    def test_destroy_current_state(self):
        with set_data_dir('no_data'):
            with State() as state:
                genesis_block = GenesisBlock()
                state.put_block(genesis_block, None)
                dummy_state = StateLoader(
                    bin2hstr(genesis_block.headerhash).encode(), state._db)
                state.state_objects.append_state_loader(dummy_state)

                alice_xmss = get_alice_xmss()
                alice_address_state = AddressState.get_default(
                    alice_xmss.address)

                for _ in range(10):
                    alice_address_state.increase_nonce()

                addresses_state = {alice_xmss.address: alice_address_state}
                state.state_objects.update_current_state(addresses_state)
                alice_address_state2 = state.get_address(alice_xmss.address)
                self.assertEqual(alice_address_state.nonce,
                                 alice_address_state2.nonce)

                state.state_objects.destroy_current_state(None)
                alice_address_state2 = state.get_address(alice_xmss.address)
                self.assertEqual(0, alice_address_state2.nonce)
Esempio n. 3
0
    def test_update_token_metadata(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                bob_xmss = get_bob_xmss()

                token_transaction = get_token_transaction(alice_xmss, bob_xmss)
                state.create_token_metadata(token_transaction)

                transfer_token_transaction = TransferTokenTransaction.create(
                    addr_from=bob_xmss.address,
                    token_txhash=token_transaction.txhash,
                    addrs_to=[alice_xmss.address],
                    amounts=[100000000],
                    fee=1,
                    xmss_pk=bob_xmss.pk)

                state.update_token_metadata(transfer_token_transaction)

                token_metadata = state.get_token_metadata(
                    token_transaction.txhash)
                self.assertEqual(len(token_metadata.transfer_token_tx_hashes),
                                 2)
                self.assertEqual(token_metadata.transfer_token_tx_hashes[0],
                                 token_transaction.txhash)
                self.assertEqual(token_metadata.transfer_token_tx_hashes[1],
                                 transfer_token_transaction.txhash)
Esempio n. 4
0
 def test_block_size_limit(self):
     with set_data_dir('no_data'):
         with State() as state:
             alice_xmss = get_alice_xmss()
             blocks = gen_blocks(20, state, alice_xmss.address)
             self.assertEqual(state.get_block_size_limit(blocks[-1]),
                              1048576)
Esempio n. 5
0
    def test_release_state(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(state)  # to avoid warning (unused variable)

            with State() as state:
                self.assertIsNotNone(state)  # to avoid warning (unused variable)
Esempio n. 6
0
    def test_state_object(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                blocks = gen_blocks(20, state, alice_xmss.address)

                self.assertEqual(state.state_objects.total_coin_supply(), 0)
                self.assertEqual(state.state_objects.state_loaders, [])
                state.state_objects.append_state_loader(StateLoader(b'current_', state._db))
                self.assertIsNotNone(state.state_objects.get_state_loader_by_index(0))
                state.state_objects.destroy_state_loader(0)
                self.assertEqual(state.state_objects.state_loaders, [])

                self.assertIsNone(state.state_objects.get(b'current'))
                state.state_objects.push(blocks[-1].headerhash, None)
                self.assertEqual(len(state.state_objects.state_loaders), 1)

                loader = state.state_objects.state_loaders[0]
                loader.increase_txn_count(state._db, b'', 0, alice_xmss.address)
                self.assertEqual(loader.get_txn_count(state._db, b'', alice_xmss.address), 1)
                self.assertEqual(loader.get_last_txs(state._db, b''), [])
                loader.commit(StateLoader(b'current_', state._db))
                loader.update_last_tx(blocks[-1], None)
                loader.commit(StateLoader(b'current_', state._db))

                state.state_objects.update_current_state({alice_xmss.address: state._get_address_state(alice_xmss.address)})
                self.assertEqual(state.state_objects.get_txn_count(alice_xmss.address), 1)
                state.state_objects.increase_txn_count(alice_xmss.address)
                self.assertEqual(state.state_objects.get_last_txs(), [])
                state.state_objects.update_last_tx(blocks[-1], None)
                state.state_objects.update_tx_metadata(blocks[-1], None)
                self.assertEqual(state.state_objects.get_last_txs(), [])
                state.state_objects.destroy_fork_states(blocks[-1].block_number, blocks[-1].headerhash)
                state.state_objects.destroy_current_state(None)
                self.assertIsNone(state.state_objects.get(b'current'))
Esempio n. 7
0
    def create(num_blocks):
        start_time = time.time()
        with mock.patch('qrl.core.misc.ntp.getTime') as ntp_mock, \
                set_data_dir('no_data'), \
                State() as state, \
                mock.patch('time.time') as time_mock:  # noqa
            time_mock.return_value = start_time
            ntp_mock.return_value = start_time

            state.get_measurement = MagicMock(return_value=10000000)

            genesis_difficulty = config.dev.genesis_difficulty
            try:
                config.dev.genesis_difficulty = 10
                genesis_block = GenesisBlock()
                chain_manager = ChainManager(state)
                chain_manager.load(genesis_block)

                chain_manager._difficulty_tracker = Mock()
                dt = DifficultyTracker()
                tmp_difficulty = StringToUInt256('2')
                tmp_target = dt.get_target(tmp_difficulty)

                chain_manager._difficulty_tracker.get = MagicMock(return_value=(tmp_difficulty, tmp_target))

                qrlnode = QRLNode(state, mining_credit_wallet=b'')
                qrlnode.set_chain_manager(chain_manager)

                mock_blockchain = MockedBlockchain(qrlnode, time_mock, ntp_mock, )
                for block_idx in range(1, num_blocks + 1):
                    mock_blockchain.add_new_block()

                yield mock_blockchain
            finally:
                config.dev.genesis_difficulty = genesis_difficulty
Esempio n. 8
0
 def test_load(self):
     with set_data_dir('no_data'):
         with State() as state:
             genesis_block = GenesisBlock()
             chain_manager = ChainManager(state)
             chain_manager.load(genesis_block)
             block = state.get_block(GenesisBlock().headerhash)
             self.assertIsNotNone(block)
Esempio n. 9
0
 def test_load(self):
     with set_data_dir('no_data'):
         with State() as state:
             genesis_block = GenesisBlock()
             chain_manager = ChainManager(state)
             chain_manager.load(genesis_block)
             block = state.get_block(GenesisBlock().headerhash)
             self.assertIsNotNone(block)
Esempio n. 10
0
 def test_state_block_map(self):
     with set_data_dir('no_data'):
         with State() as state:
             bm = qrl_pb2.BlockNumberMapping()
             state.put_block_number_mapping(b"0", bm, None)
             read_bm = state.get_block_number_mapping(b"0")
             self.assertEqual(type(bm), type(read_bm))
             self.assertIsNone(state.get_block_by_number(b"4"))
Esempio n. 11
0
    def test_address_list(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                blocks = gen_blocks(20, state, alice_xmss.address)

                for block in blocks:
                    self.assertIn(alice_xmss.address, State.prepare_address_list(block))
Esempio n. 12
0
    def test_state_tx(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                blocks = gen_blocks(20, state, alice_xmss.address)

                self.assertEqual(state.get_last_txs(), [])
                state.update_last_tx(blocks[-1], None)
                self.assertEqual(state.get_txn_count(alice_xmss.address), 0)
Esempio n. 13
0
    def test_transferCoins_get_unsigned(self):
        with set_data_dir('no_data'):
            with State() as db_state:
                with set_wallet_dir("test_wallet"):
                    p2p_factory = Mock(spec=P2PFactory)
                    p2p_factory.pow = Mock(spec=POW)
                    chain_manager = ChainManager(db_state)

                    qrlnode = QRLNode(db_state, slaves=[])
                    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)

                    context = Mock(spec=ServicerContext)

                    alice = get_alice_xmss()
                    bob = get_bob_xmss()

                    request = qrl_pb2.TransferCoinsReq(
                        address_from=alice.address,
                        addresses_to=[bob.address],
                        amounts=[101],
                        fee=12,
                        xmss_pk=alice.pk)

                    response = service.TransferCoins(request=request,
                                                     context=context)
                    context.set_code.assert_not_called()
                    context.set_details.assert_not_called()

                    self.assertIsNotNone(response)
                    self.assertIsNotNone(response.transaction_unsigned)
                    self.assertEqual(
                        'transfer',
                        response.transaction_unsigned.WhichOneof(
                            'transactionType'))

                    self.assertEqual(alice.address,
                                     response.transaction_unsigned.addr_from)
                    self.assertEqual(12, response.transaction_unsigned.fee)
                    self.assertEqual(alice.pk,
                                     response.transaction_unsigned.public_key)
                    self.assertEqual(0, response.transaction_unsigned.nonce)

                    self.assertEqual(b'',
                                     response.transaction_unsigned.signature)
                    self.assertEqual(
                        b'', response.transaction_unsigned.transaction_hash)

                    self.assertEqual(
                        bob.address,
                        response.transaction_unsigned.transfer.addrs_to[0])
                    self.assertEqual(
                        101, response.transaction_unsigned.transfer.amounts[0])
Esempio n. 14
0
 def test_state_loader(self):
     with set_data_dir('no_data'):
         alice_xmss = get_alice_xmss()
         state_loader = StateLoader(b'current_', db.DB())
         self.assertEqual(state_loader.total_coin_supply, 0)
         state_loader.update_total_coin_supply(100)
         self.assertEqual(state_loader.total_coin_supply, 100)
         self.assertIsNone(state_loader.get_address(alice_xmss.address))
         state_loader.add_address(alice_xmss.address)
         self.assertEqual(state_loader._data.addresses, [alice_xmss.address])
Esempio n. 15
0
    def test_measurement_1(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(state)  # to avoid warning (unused variable)
                state.get_block = Mock(side_effect=TestStateMeasurement.get_block_example1)

                measurement = state.get_measurement(210, b'1')
                self.assertEqual(55, measurement)

                measurement = state.get_measurement(250, b'1')
                self.assertEqual(75, measurement)
Esempio n. 16
0
    def test_get_address2(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()

                alice_address = alice_xmss.get_address()
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
                state._save_address_state(address_state)
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
Esempio n. 17
0
 def test_get_block_datapoint(self):
     with set_data_dir('no_data'):
         with State() as state:
             alice_xmss = get_alice_xmss()
             blocks = gen_blocks(20, state, alice_xmss.address)
             for i in range(1, 20):
                 datapoint = state.get_block_datapoint(blocks[i].headerhash)
                 self.assertEqual(datapoint.difficulty, "0")
                 self.assertEqual(datapoint.timestamp, 1615270948)
                 self.assertEqual(datapoint.header_hash, blocks[i].headerhash)
                 self.assertEqual(datapoint.header_hash_prev, blocks[i - 1].headerhash)
Esempio n. 18
0
    def test_get_address2(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()

                alice_address = alice_xmss.address
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
                state._save_address_state(address_state)
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
Esempio n. 19
0
    def test_block_metadata(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                blocks = gen_blocks(20, state, alice_xmss.address)

                for block in blocks:
                    state.put_block_metadata(block.headerhash, BlockMetadata(), None)

                for block in blocks:
                    self.assertEqual(state.get_block_metadata(block.headerhash).to_json(), b'{}')
Esempio n. 20
0
 def test_basic_state_funcs(self):
     with set_data_dir('no_data'):
         with State() as state:
             alice_xmss = get_alice_xmss()
             self.assertEqual(state.nonce(alice_xmss.address), 0)
             self.assertNotEqual(state.balance(alice_xmss.address), 0)
             self.assertTrue(state.address_used(alice_xmss.address))
             self.assertEqual(state.return_all_addresses(), [])
             batch = state.get_batch()
             self.assertIsNotNone(batch)
             state.write_batch(batch)
             self.assertEqual(state.total_coin_supply(), 0)
Esempio n. 21
0
    def test_create_token_metadata(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                bob_xmss = get_bob_xmss()

                token_transaction = get_token_transaction(alice_xmss, bob_xmss)
                state.create_token_metadata(token_transaction)

                token_metadata = state.get_token_metadata(token_transaction.txhash)
                self.assertEqual(token_metadata.token_txhash, token_transaction.txhash)
                self.assertEqual(token_metadata.transfer_token_tx_hashes[0], token_transaction.txhash)
Esempio n. 22
0
    def test_create_token_metadata(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                bob_xmss = get_bob_xmss()

                token_transaction = get_token_transaction(alice_xmss, bob_xmss)
                state.create_token_metadata(token_transaction)

                token_metadata = state.get_token_metadata(token_transaction.txhash)
                self.assertEqual(token_metadata.token_txhash, token_transaction.txhash)
                self.assertEqual(token_metadata.transfer_token_tx_hashes[0], token_transaction.txhash)
Esempio n. 23
0
    def test_address_state(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                blocks = gen_blocks(20, state, alice_xmss.address)

                for i, block in enumerate(blocks):
                    self.assertIn(alice_xmss.address, state.prepare_address_list(block))
                    address_state = state.get_state(block.headerhash, state.prepare_address_list(block))
                    self.assertIn(alice_xmss.address, address_state.keys())
                    self.assertEqual(address_state[config.dev.coinbase_address].nonce, i + 1)
                    with self.assertRaises(Exception):
                        state.set_addresses_state({"state": 'test'}, 0)
Esempio n. 24
0
 def test_state_mainchain(self):
     with set_data_dir('no_data'):
         with State() as state:
             alice_xmss = get_alice_xmss()
             blocks = gen_blocks(20, state, alice_xmss.address)
             address_set = state.prepare_address_list(blocks[-1])
             m = state.get_state_mainchain(address_set)
             state.update_mainchain_state(m, 20, blocks[-1].headerhash)
             self.assertIsNotNone(m)
             self.assertTrue(len(m) > 0)
             state.update_mainchain_height(5, None)
             self.assertEqual(state.get_mainchain_height(), 5)
             self.assertIsNotNone(state.get_ephemeral_metadata(b"0"))
Esempio n. 25
0
    def test_check_mock(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(state)  # to avoid warning (unused variable)

                state.get_block = Mock()

                state.get_block.side_effect = self.get_block_list_example1()

                block = state.get_block(b'0')

                self.assertEqual(10, block.timestamp)
                self.assertEqual(0, block.block_number)
                self.assertEqual(b'0', block.headerhash)
Esempio n. 26
0
    def test_measurement_0(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(state)  # to avoid warning (unused variable)
                state.get_block = Mock(side_effect=TestStateMeasurement.get_block_example1)

                measurement = state.get_measurement(100, prev_headerhash=b'0')
                self.assertEqual(60, measurement)

                measurement = state.get_measurement(110, prev_headerhash=b'0')
                self.assertEqual(60, measurement)

                measurement = state.get_measurement(1000, prev_headerhash=b'0')
                self.assertEqual(60, measurement)
Esempio n. 27
0
    def test_addr_tx_hashes(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                alice_address = bytes(alice_xmss.get_address().encode())  # FIXME: This needs to be refactored
                some_hash1 = bin2hstr(sha256(b'some_hash1')).encode()
                some_hash2 = bin2hstr(sha256(b'some_hash2')).encode()

                state.update_address_tx_hashes(alice_address, some_hash1)
                state.update_address_tx_hashes(alice_address, some_hash2)
                result = state.get_address_tx_hashes(alice_address)

                self.assertEqual(some_hash1, result[0])
                self.assertEqual(some_hash2, result[1])
Esempio n. 28
0
    def test_measurement_4(self):
        db_name = config.dev.db_name
        with mock.patch('qrl.core.config.dev') as devconfig:
            devconfig.N_measurement = 3
            devconfig.db_name = db_name
            with set_data_dir('no_data'):
                with State() as state:
                    self.assertIsNotNone(state)  # to avoid warning (unused variable)
                    state.get_block = Mock(side_effect=TestStateMeasurement.get_block_example1)

                    measurement = state.get_measurement(350, b'3')
                    self.assertEqual(63, measurement)

                    measurement = state.get_measurement(370, b'3')
                    self.assertEqual(70, measurement)
Esempio n. 29
0
    def test_set_block_pos(self):
        with set_data_dir('no_data'):
            with State() as state:
                block_number = 123

                block_position = 234
                block_size = 345

                state._db.put('block_{}'.format(block_number), [block_position, block_size])

                pos_size = state._db.get('block_{}'.format(block_number))
                read_position, read_size = pos_size

                self.assertEqual(block_position, read_position)
                self.assertEqual(block_size, read_size)
Esempio n. 30
0
    def test_check_mock(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(
                    state)  # to avoid warning (unused variable)

                state.get_block = Mock()

                state.get_block.side_effect = self.get_block_list_example1()

                block = state.get_block(b'0')

                self.assertEqual(10, block.timestamp)
                self.assertEqual(0, block.block_number)
                self.assertEqual(b'0', block.headerhash)
Esempio n. 31
0
    def test_set_block_pos(self):
        with set_data_dir('no_data'):
            with State() as state:
                block_number = 123

                block_position = 234
                block_size = 345

                state._db.put('block_{}'.format(block_number), [block_position, block_size])

                pos_size = state._db.get('block_{}'.format(block_number))
                read_position, read_size = pos_size

                self.assertEqual(block_position, read_position)
                self.assertEqual(block_size, read_size)
Esempio n. 32
0
    def test_check_mock_get(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(state)  # to avoid warning (unused variable)

                state.get_block = Mock(side_effect=TestStateMeasurement.get_block_example1)

                block = state.get_block(b'1')
                self.assertEqual(1, block.block_number)
                self.assertEqual(160, block.timestamp)

                block = state.get_block(b'3')
                self.assertEqual(3, block.block_number)
                self.assertEqual(310, block.timestamp)

                block = state.get_block(b'0')
                self.assertEqual(0, block.block_number)
                self.assertEqual(10, block.timestamp)
Esempio n. 33
0
    def test_check_mock_get(self):
        with set_data_dir('no_data'):
            with State() as state:
                self.assertIsNotNone(
                    state)  # to avoid warning (unused variable)

                state.get_block = Mock(
                    side_effect=TestStateMeasurement.get_block_example1)

                block = state.get_block(b'1')
                self.assertEqual(1, block.block_number)
                self.assertEqual(160, block.timestamp)

                block = state.get_block(b'3')
                self.assertEqual(3, block.block_number)
                self.assertEqual(310, block.timestamp)

                block = state.get_block(b'0')
                self.assertEqual(0, block.block_number)
                self.assertEqual(10, block.timestamp)
Esempio n. 34
0
    def test_get_address(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()

                alice_address = alice_xmss.get_address()
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))

                alice_address = bytearray(alice_xmss.get_address())
                with self.assertRaises(TypeError):
                    address_state = state.get_address(alice_address)

                alice_address = alice_xmss.get_address()
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
                state._save_address_state(address_state)

                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
Esempio n. 35
0
    def test_get_address(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()

                alice_address = bytes(alice_xmss.get_address().encode())  # FIXME: This needs to be refactored
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))

                alice_address = bytearray(alice_xmss.get_address().encode())  # FIXME: This needs to be refactored
                with self.assertRaises(TypeError):
                    address_state = state.get_address(alice_address)

                alice_address = bytes(alice_xmss.get_address().encode())  # FIXME: This needs to be refactored
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
                state._save_address_state(address_state)

                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
Esempio n. 36
0
    def test_get_address(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()

                alice_address = alice_xmss.address
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))

                alice_address = bytearray(alice_xmss.address)
                with self.assertRaises(TypeError):
                    address_state = state.get_address(alice_address)

                alice_address = alice_xmss.address
                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
                state._save_address_state(address_state)

                address_state = state.get_address(alice_address)
                self.assertTrue(isinstance(address_state.address, bytes))
Esempio n. 37
0
    def test_simple_add_block(self):
        with set_data_dir('no_data'):
            with State() as state:
                state.get_measurement = MagicMock(return_value=10000000)
                alice_xmss = get_alice_xmss()

                genesis_block = GenesisBlock()
                chain_manager = ChainManager(state)
                chain_manager.load(genesis_block)

                chain_manager._difficulty_tracker = Mock()
                dt = DifficultyTracker()
                tmp_difficulty = StringToUInt256('2')
                tmp_target = dt.get_target(tmp_difficulty)
                chain_manager._difficulty_tracker.get = MagicMock(
                    return_value=(tmp_difficulty, tmp_target))

                block = state.get_block(genesis_block.headerhash)
                self.assertIsNotNone(block)

                with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                    time_mock.return_value = 1615270948  # Very high to get an easy difficulty

                    block_1 = Block.create(
                        block_number=1,
                        prevblock_headerhash=genesis_block.headerhash,
                        transactions=[],
                        miner_address=alice_xmss.address)
                    block_1.set_nonces(491, 0)
                    from pyqrllib.pyqrllib import bin2hstr
                    print(bin2hstr(block.headerhash))
                    # Uncomment only to determine the correct mining_nonce of above blocks
                    # from qrl.core.PoWValidator import PoWValidator
                    # while not PoWValidator().validate_mining_nonce(state, block_1.blockheader, False):
                    #     block_1.set_nonces(block_1.mining_nonce + 1)
                    #     print(block_1.mining_nonce)

                    result = chain_manager.add_block(block_1)

                self.assertTrue(result)
                self.assertEqual(chain_manager.last_block, block_1)
Esempio n. 38
0
    def test_measurement_4(self):
        db_name = config.dev.db_name
        with mock.patch('qrl.core.config.dev') as devconfig:
            devconfig.N_measurement = 3
            devconfig.db_name = db_name
            with set_data_dir('no_data'):
                with State() as state:
                    self.assertIsNotNone(
                        state)  # to avoid warning (unused variable)
                    state.get_block = Mock(
                        side_effect=TestStateMeasurement.get_block_example1)
                    parent_metadata = Mock()
                    parent_metadata.last_N_headerhashes = [b'0', b'1', b'2']

                    measurement = state.get_measurement(
                        350, b'3', parent_metadata)
                    self.assertEqual(63, measurement)

                    measurement = state.get_measurement(
                        370, b'3', parent_metadata)
                    self.assertEqual(70, measurement)
Esempio n. 39
0
    def test_simple_add_block(self):
        with set_data_dir('no_data'):
            with State() as state:
                state.get_measurement = MagicMock(return_value=10000000)
                alice_xmss = get_alice_xmss()

                genesis_block = GenesisBlock()
                chain_manager = ChainManager(state)
                chain_manager.load(genesis_block)

                chain_manager._difficulty_tracker = Mock()
                dt = DifficultyTracker()
                tmp_difficulty = StringToUInt256('2')
                tmp_boundary = dt.get_target(tmp_difficulty)
                chain_manager._difficulty_tracker.get = MagicMock(
                    return_value=(tmp_difficulty, tmp_boundary))

                block = state.get_block(genesis_block.headerhash)
                self.assertIsNotNone(block)

                with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                    time_mock.return_value = 1615270948  # Very high to get an easy difficulty

                    block_1 = Block.create(
                        block_number=1,
                        prevblock_headerhash=genesis_block.headerhash,
                        transactions=[],
                        signing_xmss=alice_xmss,
                        master_address=alice_xmss.address,
                        nonce=1)

                    while not PoWValidator().validate_mining_nonce(
                            state, block_1.blockheader, False):
                        block_1.set_mining_nonce(block_1.mining_nonce + 1)

                    result = chain_manager.add_block(block_1)

                self.assertTrue(result)
                self.assertEqual(chain_manager.last_block, block_1)
Esempio n. 40
0
    def test_update_token_metadata(self):
        with set_data_dir('no_data'):
            with State() as state:
                alice_xmss = get_alice_xmss()
                bob_xmss = get_bob_xmss()

                token_transaction = get_token_transaction(alice_xmss, bob_xmss)
                state.create_token_metadata(token_transaction)

                transfer_token_transaction = TransferTokenTransaction.create(addr_from=bob_xmss.get_address(),
                                                                             token_txhash=token_transaction.txhash,
                                                                             addr_to=alice_xmss.get_address(),
                                                                             amount=100000000,
                                                                             fee=1,
                                                                             xmss_pk=bob_xmss.pk(),
                                                                             xmss_ots_index=bob_xmss.get_index())

                state.update_token_metadata(transfer_token_transaction)

                token_metadata = state.get_token_metadata(token_transaction.txhash)
                self.assertEqual(len(token_metadata.transfer_token_tx_hashes), 2)
                self.assertEqual(token_metadata.transfer_token_tx_hashes[0], token_transaction.txhash)
                self.assertEqual(token_metadata.transfer_token_tx_hashes[1], transfer_token_transaction.txhash)
Esempio n. 41
0
    def test_orphan_block(self):
        """
        Testing add_block logic in case of orphan_blocks
        :return:
        """
        with mock.patch('qrl.core.config.DevConfig') as devconfig:
            devconfig.genesis_difficulty = 2
            devconfig.minimum_minting_delay = 10
            with set_data_dir('no_data'):
                with State() as state:  # FIXME: Move state to temporary directory
                    state.get_measurement = MagicMock(return_value=10000000)
                    genesis_block = GenesisBlock()

                    chain_manager = ChainManager(state)
                    chain_manager.load(genesis_block)

                    chain_manager._difficulty_tracker = Mock()
                    dt = DifficultyTracker()
                    tmp_difficulty = StringToUInt256('2')
                    tmp_boundary = dt.get_boundary(tmp_difficulty)
                    chain_manager._difficulty_tracker.get = MagicMock(return_value=(tmp_difficulty, tmp_boundary))

                    block = state.get_block(genesis_block.headerhash)
                    self.assertIsNotNone(block)
                    alice_xmss = get_alice_xmss()

                    with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                        time_mock.return_value = 1615270948  # Very high to get an easy difficulty
                        block_1 = Block.create(mining_nonce=10,
                                               block_number=1,
                                               prevblock_headerhash=genesis_block.headerhash,
                                               transactions=[],
                                               signing_xmss=alice_xmss,
                                               master_address=alice_xmss.get_address(),
                                               nonce=1)
                        block_1.set_mining_nonce(10)

                        while not chain_manager.validate_mining_nonce(block_1, False):
                            block_1.set_mining_nonce(block_1.mining_nonce + 1)

                        result = chain_manager.add_block(block_1)

                    self.assertTrue(result)
                    self.assertEqual(chain_manager.last_block, block_1)

                    bob_xmss = get_bob_xmss()

                    with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                        time_mock.return_value = 1615270948 + devconfig.minimum_minting_delay * 2
                        block = Block.create(mining_nonce=18,
                                             block_number=1,
                                             prevblock_headerhash=genesis_block.headerhash,
                                             transactions=[],
                                             signing_xmss=bob_xmss,
                                             master_address=bob_xmss.get_address(),
                                             nonce=1)
                        block.set_mining_nonce(18)

                        while not chain_manager.validate_mining_nonce(block, False):
                            block.set_mining_nonce(block.mining_nonce + 1)

                    with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                        time_mock.return_value = 1615270948 + devconfig.minimum_minting_delay * 3
                        block_2 = Block.create(mining_nonce=17,
                                               block_number=2,
                                               prevblock_headerhash=block.headerhash,
                                               transactions=[],
                                               signing_xmss=bob_xmss,
                                               master_address=bob_xmss.get_address(),
                                               nonce=2)
                        block_2.set_mining_nonce(17)

                    result = chain_manager.add_block(block_2)
                    self.assertTrue(result)

                    result = chain_manager.add_block(block)
                    self.assertTrue(result)

                    block = state.get_block(block.headerhash)
                    self.assertIsNotNone(block)

                    self.assertEqual(chain_manager.last_block.block_number, block_1.block_number)
                    self.assertEqual(chain_manager.last_block.headerhash, block_1.headerhash)
Esempio n. 42
0
 def test_create_state(self):
     with set_data_dir('no_data'):
         with State() as state:
             self.assertIsNotNone(state)  # to avoid warning (unused variable)
Esempio n. 43
0
    def test_add_block(self):
        """
        Testing add_block, with fork logic
        :return:
        """
        with set_data_dir('no_data'):
            with State() as state:
                state.get_measurement = MagicMock(return_value=10000000)

                alice_xmss = get_alice_xmss()
                bob_xmss = get_bob_xmss()

                genesis_block = GenesisBlock()
                chain_manager = ChainManager(state)
                chain_manager.load(genesis_block)

                chain_manager._difficulty_tracker = Mock()
                dt = DifficultyTracker()
                tmp_difficulty = StringToUInt256('2')
                tmp_boundary = dt.get_boundary(tmp_difficulty)
                chain_manager._difficulty_tracker.get = MagicMock(return_value=(tmp_difficulty, tmp_boundary))

                block = state.get_block(genesis_block.headerhash)
                self.assertIsNotNone(block)

                slave_tx = SlaveTransaction.create(addr_from=alice_xmss.get_address(),
                                                   slave_pks=[bob_xmss.pk()],
                                                   access_types=[0],
                                                   fee=0,
                                                   xmss_pk=alice_xmss.pk(),
                                                   xmss_ots_index=alice_xmss.get_index())
                slave_tx.sign(alice_xmss)
                slave_tx._data.nonce = 2
                self.assertTrue(slave_tx.validate())
                with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                    time_mock.return_value = 1615270948  # Very high to get an easy difficulty

                    block_1 = Block.create(mining_nonce=10,
                                           block_number=1,
                                           prevblock_headerhash=genesis_block.headerhash,
                                           transactions=[slave_tx],
                                           signing_xmss=alice_xmss,
                                           master_address=alice_xmss.get_address(),
                                           nonce=1)

                    while not chain_manager.validate_mining_nonce(block_1, False):
                        block_1.set_mining_nonce(block_1.mining_nonce + 1)

                    result = chain_manager.add_block(block_1)

                self.assertTrue(result)
                self.assertEqual(chain_manager.last_block, block_1)

                alice_state = chain_manager.get_address(alice_xmss.get_address())

                self.assertEqual(len(alice_state.slave_pks_access_type), 1)
                self.assertTrue(str(bob_xmss.pk()) in alice_state.slave_pks_access_type)

                with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                    time_mock.return_value = 1715270948  # Very high to get an easy difficulty
                    block = Block.create(mining_nonce=15,
                                         block_number=1,
                                         prevblock_headerhash=genesis_block.headerhash,
                                         transactions=[],
                                         signing_xmss=bob_xmss,
                                         master_address=bob_xmss.get_address(),
                                         nonce=1)

                    while not chain_manager.validate_mining_nonce(block, False):
                        block.set_mining_nonce(block.mining_nonce + 1)

                    result = chain_manager.add_block(block)

                self.assertTrue(result)
                self.assertEqual(chain_manager.last_block, block_1)

                block = state.get_block(block.headerhash)
                self.assertIsNotNone(block)

                with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                    time_mock.return_value = 1815270948  # Very high to get an easy difficulty
                    block_2 = Block.create(mining_nonce=15,
                                           block_number=2,
                                           prevblock_headerhash=block.headerhash,
                                           transactions=[],
                                           signing_xmss=bob_xmss,
                                           master_address=bob_xmss.get_address(),
                                           nonce=2)

                    while not chain_manager.validate_mining_nonce(block_2, False):
                        block_2.set_mining_nonce(block_2.mining_nonce + 1)

                    result = chain_manager.add_block(block_2)

                self.assertTrue(result)
                self.assertEqual(chain_manager.last_block.block_number, block_2.block_number)
                self.assertEqual(chain_manager.last_block.to_json(), block_2.to_json())
Esempio n. 44
0
    def test_multi_output_transaction_add_block(self):
        with set_data_dir('no_data'):
            with State() as state:
                state.get_measurement = MagicMock(return_value=10000000)
                alice_xmss = get_alice_xmss()
                bob_xmss = get_bob_xmss()
                extended_seed = "010300cebc4e25553afa0aab899f7838e59e18a48852fa9dfd5" \
                                "ae78278c371902aa9e6e9c1fa8a196d2dba0cbfd2f2d212d16c"
                random_xmss = XMSS.from_extended_seed(hstr2bin(extended_seed))

                transfer_transaction = TransferTransaction.create(
                    addrs_to=[alice_xmss.address, random_xmss.address],
                    amounts=[
                        40 * int(config.dev.shor_per_quanta),
                        59 * int(config.dev.shor_per_quanta)
                    ],
                    fee=1 * config.dev.shor_per_quanta,
                    xmss_pk=bob_xmss.pk)
                transfer_transaction._data.nonce = 1
                transfer_transaction.sign(bob_xmss)

                genesis_block = GenesisBlock()
                chain_manager = ChainManager(state)
                chain_manager.load(genesis_block)

                chain_manager._difficulty_tracker = Mock()
                dt = DifficultyTracker()
                tmp_difficulty = StringToUInt256('2')
                tmp_boundary = dt.get_target(tmp_difficulty)
                chain_manager._difficulty_tracker.get = MagicMock(
                    return_value=(tmp_difficulty, tmp_boundary))

                block = state.get_block(genesis_block.headerhash)
                self.assertIsNotNone(block)

                with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                    time_mock.return_value = 1615270948  # Very high to get an easy difficulty

                    block_1 = Block.create(
                        block_number=1,
                        prevblock_headerhash=genesis_block.headerhash,
                        transactions=[transfer_transaction],
                        miner_address=alice_xmss.address)
                    block_1.set_nonces(274, 0)

                    # Uncomment only to determine the correct mining_nonce of above blocks
                    # from qrl.core.PoWValidator import PoWValidator
                    # while not PoWValidator().validate_mining_nonce(state, block_1.blockheader, False):
                    #     block_1.set_nonces(block_1.mining_nonce + 1)
                    #     print(block_1.mining_nonce)

                    result = chain_manager.add_block(block_1)

                self.assertTrue(result)
                self.assertEqual(chain_manager.last_block, block_1)

                bob_addr_state = state.get_address(bob_xmss.address)
                alice_addr_state = state.get_address(alice_xmss.address)
                random_addr_state = state.get_address(random_xmss.address)

                self.assertEqual(bob_addr_state.balance, 0)
                self.assertEqual(
                    alice_addr_state.balance,
                    140 * int(config.dev.shor_per_quanta) +
                    block_1.block_reward + block_1.fee_reward)
                self.assertEqual(random_addr_state.balance,
                                 159 * int(config.dev.shor_per_quanta))