コード例 #1
0
    def setUp(self, autoload_discovery=True):
        yield super(TestTrustchainNetworkEndpoint,
                    self).setUp(autoload_discovery=autoload_discovery)

        self.tc_community = TriblerChainCommunity(self.dispersy,
                                                  self.master_member,
                                                  self.member)
        self.tc_community.initialize()
        self.dispersy._communities["a" * 20] = self.tc_community
        self.session.get_dispersy_instance = lambda: self.dispersy
コード例 #2
0
    def create_node(self):
        mock_ipv8 = MockIPv8(u"curve25519",
                             TriblerTunnelCommunity,
                             socks_listen_ports=[])
        mock_ipv8.overlay.settings.max_circuits = 1

        # Load the TriblerChain community
        overlay = TriblerChainCommunity(mock_ipv8.my_peer,
                                        mock_ipv8.endpoint,
                                        mock_ipv8.network,
                                        working_directory=u":memory:")
        overlay._use_main_thread = False
        mock_ipv8.overlay.triblerchain_community = overlay
        mock_ipv8.overlay.dht_provider = MockDHTProvider(
            mock_ipv8.endpoint.wan_address)

        return mock_ipv8
コード例 #3
0
    def setUp(self, autoload_discovery=True):
        yield super(TestTrustchainStatsEndpoint, self).setUp(autoload_discovery=autoload_discovery)

        self.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
        self.dispersy._database.open()
        master_member = DummyMember(self.dispersy, 1, "a" * 20)
        self.member = self.dispersy.get_new_member(u"curve25519")

        self.tc_community = TriblerChainCommunity(self.dispersy, master_member, self.member)
        self.dispersy.get_communities = lambda: [self.tc_community]
        self.session.get_dispersy_instance = lambda: self.dispersy
コード例 #4
0
    def load_ipv8_overlays(self):
        # Discovery Community
        with open(self.session.config.get_permid_keypair_filename(),
                  'r') as key_file:
            content = key_file.read()
        content = content[31:-30].replace('\n', '').decode("BASE64")
        peer = Peer(M2CryptoSK(keystring=content))
        discovery_community = DiscoveryCommunity(peer, self.ipv8.endpoint,
                                                 self.ipv8.network)
        discovery_community.resolve_dns_bootstrap_addresses()
        self.ipv8.overlays.append(discovery_community)
        self.ipv8.strategies.append((RandomChurn(discovery_community), -1))

        if not self.session.config.get_dispersy_enabled():
            self.ipv8.strategies.append((RandomWalk(discovery_community), 20))

        # TriblerChain Community
        if self.session.config.get_trustchain_enabled():
            triblerchain_peer = Peer(self.session.trustchain_keypair)

            from Tribler.community.triblerchain.community import TriblerChainCommunity
            self.triblerchain_community = TriblerChainCommunity(
                triblerchain_peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                tribler_session=self.session,
                working_directory=self.session.config.get_state_dir())
            self.ipv8.overlays.append(self.triblerchain_community)
            self.ipv8.strategies.append(
                (EdgeWalk(self.triblerchain_community), 20))

        # Tunnel Community
        if self.session.config.get_tunnel_community_enabled():
            tunnel_peer = Peer(self.session.trustchain_keypair)

            from Tribler.community.triblertunnel.community import TriblerTunnelCommunity
            self.tunnel_community = TriblerTunnelCommunity(
                tunnel_peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                tribler_session=self.session,
                dht_provider=MainlineDHTProvider(
                    self.mainline_dht,
                    self.session.config.get_dispersy_port()),
                triblerchain_community=self.triblerchain_community)
            self.ipv8.overlays.append(self.tunnel_community)
            self.ipv8.strategies.append(
                (RandomWalk(self.tunnel_community), 20))

        # Market Community
        if self.session.config.get_market_community_enabled():
            wallets = {}

            try:
                from Tribler.community.market.wallet.btc_wallet import BitcoinWallet, BitcoinTestnetWallet
                wallet_type = BitcoinTestnetWallet if self.session.config.get_btc_testnet(
                ) else BitcoinWallet
                btc_wallet = wallet_type(
                    os.path.join(self.session.config.get_state_dir(),
                                 'wallet'))
                wallets[btc_wallet.get_identifier()] = btc_wallet
            except ImportError:
                self._logger.error(
                    "Electrum wallet cannot be found, Bitcoin trading not available!"
                )

            mc_wallet = TrustchainWallet(self.triblerchain_community)
            wallets[mc_wallet.get_identifier()] = mc_wallet

            if self.session.config.get_dummy_wallets_enabled():
                # For debugging purposes, we create dummy wallets
                dummy_wallet1 = DummyWallet1()
                wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

                dummy_wallet2 = DummyWallet2()
                wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

            from Tribler.community.market.community import MarketCommunity
            market_peer = Peer(self.session.tradechain_keypair)

            self.market_community = MarketCommunity(
                market_peer,
                self.ipv8.endpoint,
                self.ipv8.network,
                tribler_session=self.session,
                wallets=wallets,
                working_directory=self.session.config.get_state_dir())

            self.ipv8.overlays.append(self.market_community)

            self.ipv8.strategies.append(
                (RandomWalk(self.market_community), 20))
コード例 #5
0
class TestTrustchainNetworkEndpoint(AbstractApiTest, AbstractTestCommunity):
    """
    Evaluate the TrustNetworkEndpoint, the endpoint from which you can retrieve
    aggregated data from the trustchain.
    """
    @blocking_call_on_reactor_thread
    @inlineCallbacks
    def setUp(self, autoload_discovery=True):
        yield super(TestTrustchainNetworkEndpoint,
                    self).setUp(autoload_discovery=autoload_discovery)

        self.tc_community = TriblerChainCommunity(self.dispersy,
                                                  self.master_member,
                                                  self.member)
        self.tc_community.initialize()
        self.dispersy._communities["a" * 20] = self.tc_community
        self.session.get_dispersy_instance = lambda: self.dispersy

    def set_up_endpoint_request(self,
                                focus_node,
                                neighbor_level,
                                max_neighbors=1):
        """
        Create a mocked session, create a TrustchainNetworkEndpoint instance
        and create a request from the provided parameters.

        :param focus_node: node for which to request the data
        :param neighbor_level: amount of levels from this node to request
        :return: a 2-tuple of the TrustchainNetworkEndpoint and the request
        """
        mocked_session = MockObject()
        network_endpoint = TrustChainNetworkEndpoint(mocked_session)
        network_endpoint.get_tribler_chain_community = lambda: self.tc_community
        request = MockObject()
        request.setHeader = lambda header, flags: None
        request.setResponseCode = lambda status_code: None
        request.args = {
            "focus_node": [str(focus_node)],
            "neighbor_level": [str(neighbor_level)],
            "max_neighbors": [str(max_neighbors)]
        }
        return network_endpoint, request

    def test_get_empty_focus_node(self):
        """
        Evaluate whether the API returns a Bad Request error if the focus node is empty.
        """
        exp_message = {"error": "focus_node parameter empty"}
        network_endpoint, request = self.set_up_endpoint_request("X", 1)
        request.args["focus_node"] = [""]
        self.assertEqual(dumps(exp_message),
                         network_endpoint.render_GET(request))

    def test_max_neighbors(self):
        """
        Evaluate whether the max_neighbors argument is correctly parsed.
        """
        network_endpoint, request = self.set_up_endpoint_request("X", 1, 4)
        self.assertEqual(4, network_endpoint.get_max_neighbors(request.args))

    def test_no_max_neighbors(self):
        """
        Evaluate whether max_neighbors return the correct default value when the argument is not present.
        """
        network_endpoint, request = self.set_up_endpoint_request("X", 1, 4)
        del request.args["max_neighbors"]
        self.assertEqual(maxint,
                         network_endpoint.get_max_neighbors(request.args))

    def test_negative_max_neighbors(self):
        """
        Evaluate whether max_neighbors return the correct default value when the argument is negative.
        """
        network_endpoint, request = self.set_up_endpoint_request("X", 1, 4)
        request.args["max_neighbors"] = ["-1"]
        self.assertEqual(maxint,
                         network_endpoint.get_max_neighbors(request.args))

    def test_mandatory_nodes(self):
        """
        Evaluate whether the mandatory_nodes function works correctly.
        """
        arguments = {"mandatory_nodes": ["x,y,z"]}
        expected_result = ["x", "y", "z"]
        self.assertEqual(
            TrustChainNetworkEndpoint.get_mandatory_nodes(arguments),
            expected_result)

    @staticmethod
    def setup_mock_community(public_key):
        """
        Set up a fake community to use for testing.
        :param public_key: public key of the my_member in the community
        :return: deferred object to trigger callback chain on
        """
        mock_community = MockObject()
        mock_community.my_member = MockObject()
        mock_community.my_member.public_key = unhexlify(public_key)
        mock_community.persistence = MockObject()
        mock_community.persistence.dummy_setup = False
        d = Deferred()
        mock_community.get_graph = lambda _1, _2, _3, _4: d
        TrustChainNetworkEndpoint.get_tribler_chain_community = lambda _: mock_community
        return d

    @deferred(timeout=10)
    def test_get_no_edges(self):
        """
        Evaluate whether the API passes the correct data if there are no edges returned.
        """
        d = self.setup_mock_community("0000")
        exp_message = {
            u"user_node":
            u"0000",
            u"focus_node":
            u"30",
            u"neighbor_level":
            1,
            u"nodes": [{
                u"public_key": u"xyz",
                u"total_up": 0,
                u"total_down": 0,
                u"score": 0.5
            }],
            u"edges": []
        }
        d.callback(([{
            "public_key": "xyz",
            "total_up": 0,
            "total_down": 0,
            "score": 0.5
        }], []))
        return self.do_request('trustchain/network?focus_node=30&neighbor_level=1',
                               expected_code=200, expected_json=exp_message)\
            .addCallback(lambda message: self.assertEqual(message, dumps(exp_message)))

    @deferred(timeout=10)
    def test_get_edges(self):
        """
        Evaluate whether the API passes the correct data if there are edges returned.
        """
        d = self.setup_mock_community("0000")
        exp_message = {
            u"user_node":
            u"0000",
            u"focus_node":
            u"30",
            u"neighbor_level":
            1,
            u"nodes": [{
                u"public_key": u"xyz",
                u"total_up": 0,
                u"total_down": 0,
                u"score": 0.5
            }],
            u"edges": [{
                u"from": u"xyz",
                u"to": u"abc",
                u"amount": 30
            }]
        }
        d.callback(([{
            "public_key": "xyz",
            "total_up": 0,
            "total_down": 0,
            u"score": 0.5
        }], [{
            "from": "xyz",
            "to": "abc",
            "amount": 30
        }]))
        return self.do_request('trustchain/network?focus_node=30&neighbor_level=1',
                               expected_code=200, expected_json=exp_message) \
            .addCallback(lambda message: self.assertEqual(message, dumps(exp_message)))

    @deferred(timeout=10)
    def test_get_self(self):
        """
        Evaluate whether the API uses the own public key when public_key is set to 'self'.
        """
        d = self.setup_mock_community("0000")
        exp_message = {
            u"user_node":
            u"0000",
            u"focus_node":
            u"0000",
            u"neighbor_level":
            1,
            u"nodes": [{
                u"public_key": u"0000",
                u"total_up": 0,
                u"total_down": 0,
                u"score": 0.5,
                u"total_neighbors": 0
            }],
            u"edges": []
        }
        d.callback(([{
            "public_key": "0000",
            "total_up": 0,
            "total_down": 0,
            "score": 0.5,
            "total_neighbors": 0
        }], []))
        return self.do_request('trustchain/network?focus_node=self&neighbor_level=1',
                               expected_code=200, expected_json=exp_message) \
            .addCallback(lambda message: self.assertEqual(message, dumps(exp_message)))

    @deferred(timeout=10)
    def test_negative_neighbor_level(self):
        """
        Evaluate whether the API uses neighbor level 1 when a negative number is provided.
        """
        d = self.setup_mock_community("0000")
        exp_message = {
            u"user_node":
            u"0000",
            u"focus_node":
            u"0000",
            u"neighbor_level":
            1,
            u"nodes": [{
                u"public_key": u"0000",
                u"total_up": 0,
                u"total_down": 0,
                u"score": 0.5,
                u"total_neighbors": 0
            }],
            u"edges": []
        }

        d.callback(([{
            "public_key": "0000",
            "total_up": 0,
            "total_down": 0,
            "score": 0.5,
            "total_neighbors": 0
        }], []))
        return self.do_request('trustchain/network?focus_node=self&neighbor_level=-1',
                               expected_code=200, expected_json=exp_message) \
            .addCallback(lambda message: self.assertEqual(message, dumps(exp_message)))

    @deferred(timeout=10)
    def test_empty_dataset(self):
        """
        Evaluate whether the API sends a response when the dataset is not well-defined.
        """
        d = self.setup_mock_community("0000")
        exp_message = {
            u"user_node":
            u"0000",
            u"focus_node":
            u"0000",
            u"neighbor_level":
            1,
            u"nodes": [{
                u"public_key": u"0000",
                u"total_up": 0,
                u"total_down": 0,
                u"score": 0.5,
                u"total_neighbors": 0
            }],
            u"edges": []
        }
        d.callback(([{
            "public_key": "0000",
            "total_up": 0,
            "total_down": 0,
            "score": 0.5,
            "total_neighbors": 0
        }], []))
        return self.do_request('trustchain/network?dataset=&focus_node=self&neighbor_level=-1',
                               expected_code=200, expected_json=exp_message) \
            .addCallback(lambda message: self.assertEqual(message, dumps(exp_message)))

    @deferred(timeout=10)
    def test_no_dataset(self):
        """
        Evaluate whether the API sends a response when the dataset is not defined.
        """
        d = self.setup_mock_community("0000")
        exp_message = {
            u"nodes": [{
                u"public_key": u"0000",
                u"total_down": 0,
                u"total_up": 0,
                u"score": 0.5,
                u"total_neighbors": 0
            }],
            u"neighbor_level":
            1,
            u"user_node":
            u"0000",
            u"focus_node":
            u"0000",
            u"edges": []
        }
        d.callback(([{
            "public_key": "0000",
            "total_up": 0,
            "total_down": 0,
            "score": 0.5,
            "total_neighbors": 0
        }], []))
        return self.do_request('trustchain/network?focus_node=self&neighbor_level=-1',
                               expected_code=200, expected_json=exp_message) \
            .addCallback(lambda message: self.assertEqual(message, dumps(exp_message)))

    @deferred(timeout=10)
    def test_mc_community_exception(self):
        """
        Evaluate whether the API returns the correct error when the trustchain community can't be found.
        """
        TrustChainNetworkEndpoint.get_tribler_chain_community = lambda _:\
            (_ for _ in ()).throw(OperationNotEnabledByConfigurationException("trustchain is not enabled"))
        exp_message = {u"error": u"trustchain is not enabled"}
        return self.do_request('trustchain/network?focus_node=self',
                               expected_code=http.NOT_FOUND,
                               expected_json=exp_message)
コード例 #6
0
class TestTrustchainStatsEndpoint(AbstractApiTest, AbstractTestCommunity):
    @blocking_call_on_reactor_thread
    @inlineCallbacks
    def setUp(self, autoload_discovery=True):
        yield super(TestTrustchainStatsEndpoint,
                    self).setUp(autoload_discovery=autoload_discovery)

        self.tc_community = TriblerChainCommunity(self.dispersy,
                                                  self.master_member,
                                                  self.member)
        self.dispersy._communities["a" * 20] = self.tc_community
        self.tc_community.initialize()
        self.session.get_dispersy_instance = lambda: self.dispersy

    @deferred(timeout=10)
    def test_get_statistics_no_community(self):
        """
        Testing whether the API returns error 404 if no trustchain community is loaded
        """
        # Manually unload triblerchain community, as it won't be reachable by tearDown()
        self.tc_community.unload_community()
        self.dispersy._communities = {}
        return self.do_request('trustchain/statistics', expected_code=404)

    @deferred(timeout=10)
    def test_get_statistics(self):
        """
        Testing whether the API returns the correct statistics
        """
        block = TrustChainBlock()
        block.public_key = self.member.public_key
        block.link_public_key = "deadbeef".decode("HEX")
        block.link_sequence_number = 21
        block.transaction = {
            "up": 42,
            "down": 8,
            "total_up": 1024,
            "total_down": 2048
        }
        block.sequence_number = 3
        block.previous_hash = "babecafe".decode("HEX")
        block.signature = "babebeef".decode("HEX")
        self.tc_community.persistence.add_block(block)

        def verify_response(response):
            response_json = json.loads(response)
            self.assertTrue("statistics" in response_json)
            stats = response_json["statistics"]
            self.assertEqual(stats["id"], self.member.public_key.encode("HEX"))
            self.assertEqual(stats["total_blocks"], 3)
            self.assertEqual(stats["total_up"], 1024)
            self.assertEqual(stats["total_down"], 2048)
            self.assertEqual(stats["peers_that_pk_helped"], 1)
            self.assertEqual(stats["peers_that_helped_pk"], 1)
            self.assertIn("latest_block", stats)
            self.assertNotEqual(stats["latest_block"]["insert_time"], "")
            self.assertEqual(stats["latest_block"]["hash"],
                             block.hash.encode("HEX"))
            self.assertEqual(stats["latest_block"]["link_public_key"],
                             "deadbeef")
            self.assertEqual(stats["latest_block"]["link_sequence_number"], 21)
            self.assertEqual(stats["latest_block"]["up"], 42)
            self.assertEqual(stats["latest_block"]["down"], 8)

        self.should_check_equality = False
        return self.do_request('trustchain/statistics',
                               expected_code=200).addCallback(verify_response)

    @deferred(timeout=10)
    def test_get_statistics_no_data(self):
        """
        Testing whether the API returns the correct statistics
        """
        def verify_response(response):
            response_json = json.loads(response)
            self.assertTrue("statistics" in response_json)
            stats = response_json["statistics"]
            self.assertEqual(stats["id"], self.member.public_key.encode("HEX"))
            self.assertEqual(stats["total_blocks"], 0)
            self.assertEqual(stats["total_up"], 0)
            self.assertEqual(stats["total_down"], 0)
            self.assertEqual(stats["peers_that_pk_helped"], 0)
            self.assertEqual(stats["peers_that_helped_pk"], 0)
            self.assertNotIn("latest_block", stats)

        self.should_check_equality = False
        return self.do_request('trustchain/statistics',
                               expected_code=200).addCallback(verify_response)

    @deferred(timeout=10)
    def test_get_blocks_no_community(self):
        """
        Testing whether the API returns error 404 if no trustchain community is loaded when requesting blocks
        """
        # Manually unload triblerchain community, as it won't be reachable by tearDown()
        self.tc_community.unload_community()
        self.dispersy._communities = {}
        return self.do_request('trustchain/blocks/aaaaa', expected_code=404)

    @deferred(timeout=10)
    def test_get_blocks(self):
        """
        Testing whether the API returns the correct blocks
        """
        def verify_response(response):
            response_json = json.loads(response)
            self.assertEqual(len(response_json["blocks"]), 1)

        test_block = TriblerTestBlock()
        self.tc_community.persistence.add_block(test_block)
        self.should_check_equality = False
        return self.do_request('trustchain/blocks/%s?limit=10' %
                               test_block.public_key.encode("HEX"),
                               expected_code=200).addCallback(verify_response)

    @deferred(timeout=10)
    def test_get_blocks_bad_limit_too_many(self):
        """
        Testing whether the API takes large values for the limit
        """
        self.should_check_equality = False
        return self.do_request('trustchain/blocks/%s?limit=10000000' %
                               TestBlock().public_key.encode("HEX"),
                               expected_code=400)

    @deferred(timeout=10)
    def test_get_blocks_bad_limit_negative(self):
        """
        Testing whether the API takes negative values for the limit
        """
        self.should_check_equality = False
        return self.do_request('trustchain/blocks/%s?limit=-10000000' %
                               TestBlock().public_key.encode("HEX"),
                               expected_code=400)

    @deferred(timeout=10)
    def test_get_blocks_bad_limit_nan(self):
        """
        Testing whether the API takes odd values for the limit
        """
        self.should_check_equality = False
        return self.do_request('trustchain/blocks/%s?limit=bla' %
                               TestBlock().public_key.encode("HEX"),
                               expected_code=400)

    @deferred(timeout=10)
    def test_get_blocks_bad_limit_nothing(self):
        """
        Testing whether the API takes no values for the limit
        """
        self.should_check_equality = False
        return self.do_request('trustchain/blocks/%s?limit=' %
                               TestBlock().public_key.encode("HEX"),
                               expected_code=400)

    @deferred(timeout=10)
    def test_get_blocks_unlimited(self):
        """
        Testing whether the API takes no limit argument
        """
        self.should_check_equality = False
        return self.do_request('trustchain/blocks/%s' %
                               TestBlock().public_key.encode("HEX"),
                               expected_code=200)

    def test_get_tribler_chain_community_positive(self):
        """
        Should return the TriblerChainCommunity when enabled.
        """
        session = MockObject()
        session.config = MockObject()
        session.config.get_trustchain_enabled = lambda: True

        dispersy = MockObject()
        dispersy.get_communities = lambda: [self.tc_community]
        session.get_dispersy_instance = lambda: dispersy

        endpoint = TrustChainNetworkEndpoint(session)

        result = endpoint.get_tribler_chain_community()

        self.assertEqual(result, self.tc_community)

    @raises(OperationNotEnabledByConfigurationException)
    def test_get_tribler_chain_community_negative(self):
        """
        Should throw an exception as the TriblerChainCommunity is not enabled
        """
        session = MockObject()
        session.config = MockObject()
        session.config.get_trustchain_enabled = lambda: False

        TrustChainNetworkEndpoint(session).get_tribler_chain_community()