Ejemplo n.º 1
0
    def test_forking_basic(self):
        """
        TODO: Cyyber review
        - Create a blockchain of 10 blocks
        - Add a new block 10b (linking to 9)
        - Check that it did not switch
        """
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            # Fork at node 9
            block_9 = mock_blockchain.xrdnode.get_block_from_index(9)
            block_10 = mock_blockchain.xrdnode.get_block_last()
            block_10b = mock_blockchain.create_block(block_9.headerhash)

            self.assertEqual(block_9.headerhash, block_10.prev_headerhash)
            self.assertEqual(block_9.headerhash, block_10b.prev_headerhash)

            # Add a new block pointing to 9
            mock_blockchain.add_block(block_10b)

            last_block = mock_blockchain.xrdnode.get_block_last()
            block_index_10 = mock_blockchain.xrdnode.get_block_from_index(10)

            self.assertEqual(last_block._data, block_index_10._data)
            self.assertEqual(last_block._data, block_10._data)
            self.assertNotEquals(last_block._data, block_10b._data)
Ejemplo n.º 2
0
    def test_forking_basic2(self):
        """
        TODO: Cyyber review
        - Create a blockchain of 10 blocks
        - Add a new blocks 10b, 11b (linking from 9)
        - Check that it did switch
        """
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            # Fork at node 9
            block_9 = mock_blockchain.xrdnode.get_block_from_index(9)

            # Fork with 2 blocks
            block_10b = mock_blockchain.create_block(block_9.headerhash)
            mock_blockchain.add_block(block_10b)
            block_11b = mock_blockchain.create_block(block_10b.headerhash)
            mock_blockchain.add_block(block_11b)

            last_block = mock_blockchain.xrdnode.get_block_last()
            print(last_block.block_number)
            block_index_10 = mock_blockchain.xrdnode.get_block_from_index(10)
            block_index_11 = mock_blockchain.xrdnode.get_block_from_index(11)

            self.assertEqual(block_10b._data, block_index_10._data)
            self.assertEqual(last_block._data, block_index_11._data)
            self.assertEqual(last_block._data, block_11b._data)
Ejemplo n.º 3
0
    def test_11blocks(self):
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            last_block = mock_blockchain.xrdnode.get_block_last()
            self.assertEqual(10, last_block.block_number)

            mock_blockchain.add_new_block()
            last_block = mock_blockchain.xrdnode.get_block_last()
            self.assertEqual(11, last_block.block_number)
Ejemplo n.º 4
0
    def test_sequential_index(self):
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            block = mock_blockchain.qrlnode.get_block_last()

            while block is not None:
                prev_block = mock_blockchain.qrlnode.get_block_from_hash(block.prev_headerhash)
                if prev_block:
                    self.assertEqual(block.block_number - 1, prev_block.block_number)
                block = prev_block
Ejemplo n.º 5
0
    def test_getStats_timeseries(self):
        with MockedBlockchain.create(10) as mock_blockchain:
            service = PublicAPIService(mock_blockchain.qrlnode)
            request = qrl_pb2.GetStatsReq(include_timeseries=1)
            stats = service.GetStats(request=request, context=None)

            print(stats.block_timeseries)

            self.assertEqual(11, len(stats.block_timeseries))
            self.assertEqual(61, stats.block_time_mean)
            self.assertEqual(1, stats.block_time_sd)
Ejemplo n.º 6
0
    def test_get_by_index(self):
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            block = mock_blockchain.qrlnode.get_block_last()

            while block is not None:
                prev_block = mock_blockchain.qrlnode.get_block_from_hash(block.prev_headerhash)
                if prev_block:
                    self.assertEqual(prev_block.block_number + 1, block.block_number)

                    block_from_index = mock_blockchain.qrlnode.get_block_from_index(block.block_number)
                    self.assertEqual(block._data, block_from_index._data)

                block = prev_block
Ejemplo n.º 7
0
    def test_getStats(self):
        number_blocks = 1
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            with set_timeseries_size(10):
                service = PublicAPIService(mock_blockchain.qrlnode)

                for i in range(number_blocks + 1, 50):
                    mock_blockchain.add_new_block()

                    request = qrl_pb2.GetStatsReq()
                    request.include_timeseries = True

                    stats = service.GetStats(request=request, context=None)

                    self.assertEqual(min(10, i + 1),
                                     len(stats.block_timeseries))
Ejemplo n.º 8
0
    def test_forking_basic3(self):
        """
        TODO: Cyyber review
        - Create a blockchain of 10 blocks
        - Add a new blocks 10b, 11b (linking from 9)
        - Check that it did switch
        - Add a new blocks 11, 12 (linking from 10)
        - Check that it did switch again
        """
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            # Fork at node 9
            block_9 = mock_blockchain.xrdnode.get_block_from_index(9)
            block_10 = mock_blockchain.xrdnode.get_block_last()

            # Fork with 2 blocks (b)
            block_10b = mock_blockchain.create_block(block_9.headerhash)
            mock_blockchain.add_block(block_10b)
            block_11b = mock_blockchain.create_block(block_10b.headerhash)
            mock_blockchain.add_block(block_11b)

            # Check
            last_block = mock_blockchain.xrdnode.get_block_last()
            block_index_10 = mock_blockchain.xrdnode.get_block_from_index(10)
            block_index_11 = mock_blockchain.xrdnode.get_block_from_index(11)
            self.assertEqual(block_10b._data, block_index_10._data)
            self.assertEqual(last_block._data, block_index_11._data)
            self.assertEqual(last_block._data, block_11b._data)

            # Add back to the original chain (a)
            block_11 = mock_blockchain.create_block(block_10.headerhash)
            mock_blockchain.add_block(block_11)
            block_12 = mock_blockchain.create_block(block_11.headerhash)
            mock_blockchain.add_block(block_12)

            # Check
            last_block = mock_blockchain.xrdnode.get_block_last()
            block_index_10 = mock_blockchain.xrdnode.get_block_from_index(10)
            block_index_11 = mock_blockchain.xrdnode.get_block_from_index(11)
            block_index_12 = mock_blockchain.xrdnode.get_block_from_index(12)
            self.assertEqual(block_10._data, block_index_10._data)
            self.assertEqual(block_11._data, block_index_11._data)
            self.assertEqual(last_block._data, block_index_12._data)
            self.assertEqual(last_block._data, block_12._data)
Ejemplo n.º 9
0
    def test_get_state(self):
        alice_xmss = get_alice_xmss()
        number_of_blocks = 10
        alice_balances = [
            0, 6656349462, 13312697815, 19969045061, 26625391199, 33281736229,
            39938080152, 46594422967, 53250764674, 59907105274
        ]

        with MockedBlockchain.create(number_of_blocks,
                                     alice_xmss.address) as mock_blockchain:
            state = mock_blockchain.qrlnode._chain_manager.state
            for i in range(number_of_blocks):
                block = mock_blockchain.qrlnode.get_block_from_index(i)
                addresses_state, _, _ = state.get_state(
                    block.headerhash, {
                        alice_xmss.address,
                    })
                self.assertEqual(addresses_state[alice_xmss.address].balance,
                                 alice_balances[i])
Ejemplo n.º 10
0
    def getStats_forking(self, timeseries_size):
        number_blocks = 10
        with MockedBlockchain.create(number_blocks) as mock_blockchain:
            with set_timeseries_size(timeseries_size):
                service = PublicAPIService(mock_blockchain.qrlnode)

                request = qrl_pb2.GetStatsReq(include_timeseries=True)
                stats = service.GetStats(request=request, context=None)
                self.assertEqual(timeseries_size, len(stats.block_timeseries))

                # Fork at node 9 with 2 blocks (b)
                block_9 = mock_blockchain.qrlnode.get_block_from_index(9)
                block_10 = mock_blockchain.qrlnode.get_block_last()
                block_10b = mock_blockchain.create_block(block_9.headerhash)
                mock_blockchain.add_block(block_10b)
                block_11b = mock_blockchain.create_block(block_10b.headerhash)
                mock_blockchain.add_block(block_11b)

                # Get time series
                request = qrl_pb2.GetStatsReq(include_timeseries=True)
                stats = service.GetStats(request=request, context=None)
                self.assertEqual(timeseries_size, len(stats.block_timeseries))

                # check stats have moved to the correct branch
                self.assertEqual(block_11b.headerhash,
                                 stats.block_timeseries[-1].header_hash)

                # Add back to the original chain (a)
                block_11 = mock_blockchain.create_block(block_10.headerhash)
                mock_blockchain.add_block(block_11)
                block_12 = mock_blockchain.create_block(block_11.headerhash)
                mock_blockchain.add_block(block_12)

                # Get time series
                request = qrl_pb2.GetStatsReq(include_timeseries=True)
                stats = service.GetStats(request=request, context=None)
                self.assertEqual(timeseries_size, len(stats.block_timeseries))
                # check stats have moved back to the correct branch
                self.assertEqual(block_12.headerhash,
                                 stats.block_timeseries[-1].header_hash)
Ejemplo n.º 11
0
 def test_MockChain_forking(self):
     number_blocks = 10
     with MockedBlockchain.create(number_blocks) as mock_blockchain:
         block_5 = mock_blockchain.xrdnode.get_block_from_index(9)
         block_6b = mock_blockchain.create_block(block_5.headerhash)
         mock_blockchain.add_block(block_6b)
Ejemplo n.º 12
0
 def test_10blocks(self):
     number_blocks = 10
     with MockedBlockchain.create(number_blocks) as mock_blockchain:
         last_block = mock_blockchain.qrlnode.get_block_last()
         self.assertEqual(10, last_block.block_number)