Esempio n. 1
0
    def run_node_with_associations(self, title, node_index, args, stream_policies, cb_class=AssociationCB, ip='127.0.0.1', strSubVer=None):
        logger.debug("setup %s", title)

        self.start_node(node_index, args)

        # Create associations and their connections to the node
        associations = []
        for stream_policy in stream_policies:
            association = Association(stream_policy, cb_class)
            association.create_streams(self.nodes[node_index], ip, strSubVer)
            associations.append(association)

        # Start network handling thread
        thr = NetworkThread()
        thr.start()

        # Allow associations to exchange their setup messages and fully initialise
        for association in associations:
            association.setup()
        wait_until(lambda: len(self.nodes[node_index].getpeerinfo()) == len(associations))

        # Test can now proceed
        logger.debug("before %s", title)
        yield tuple(associations)
        logger.debug("after %s", title)

        # Shutdown associations and their connections
        for association in associations:
            association.close_streams()
        del associations

        # Once all connections are closed, NetworkThread run loop completes and thr.join() returns success
        thr.join()
        self.stop_node(node_index)
        logger.debug("finished %s", title)
Esempio n. 2
0
    def run_node_with_connections(self, title, node_index, args, number_of_connections, ip='127.0.0.1', strSubVer=None, wait_for_verack=True):
        logger.debug("setup %s", title)

        self.start_node(node_index, args)

        connectionCbs = []
        for i in range(number_of_connections):
            connectionCbs.append(NodeConnCB())

        connections = []
        for connCb in connectionCbs:
            connection = NodeConn(ip, p2p_port(0), self.nodes[node_index], connCb, strSubVer=strSubVer)
            connections.append(connection)
            connCb.add_connection(connection)

        thr = NetworkThread()
        thr.start()
        if wait_for_verack:
            for connCb in connectionCbs:
                connCb.wait_for_verack()

        logger.debug("before %s", title)
        yield tuple(connections)
        logger.debug("after %s", title)

        for connection in connections:
            connection.close()
        del connections
        # once all connection.close() are complete, NetworkThread run loop completes and thr.join() returns success
        thr.join()
        self.stop_node(node_index)
        logger.debug("finished %s", title)
Esempio n. 3
0
    def run_test(self):
        logging.info("Testing xversion handling")

        # test regular set up including xversion
        conn = self.restart_node()
        conn.allow0Checksum = True
        nt = NetworkThread()
        nt.start()

        conn.wait_for_verack()
        conn.send_message(msg_verack())

        # now it is time for xversion
        conn.send_message(msg_xversion({0x00020002: 1}))

        # send extra buversion and buverack, shouldn't harm
        conn.send_message(msg_buversion(addrFromPort=12345))
        conn.send_message(msg_buverack())
        conn.wait_for(lambda: conn.remote_xversion)

        conn.send_message(msg_ping())
        conn.wait_for(lambda: conn.pong_counter)
        # check that we are getting 0-value checksums from the BU node
        assert (self.hndlr.num0Checksums > 0)

        conn.connection.disconnect_node()
        nt.join()
Esempio n. 4
0
    def run_test(self):
        logging.info("Testing early ping replies")

        conn = self.restart_node(send_initial_version=False)
        conn.send_message(msg_ping(), pushbuf=True)
        nt = NetworkThread()
        nt.start()
        conn.wait_for(lambda: conn.pong_counter)
        conn.connection.disconnect_node()
        nt.join()
Esempio n. 5
0
    def run_test(self):
        logging.info("Testing xversion handling")

        ex1_xver = { 1 : b"2", 3 : b"4"}

        def test_too_early(msg):
            """ Test that the given message if it comes right after start up will
            lead to rejection / banning as it comes too early. """
            print("Testing that an an early %s fails." % msg)
            conn = self.restart_node(send_initial_version = False)
            conn.send_message(msg, pushbuf = True)
            self.network_and_finish()
            assert conn.disconnected

        # test failure due to early receipt
        if 0:
            for msg in [msg_buversion(1000), msg_buverack(), msg_xversion(), msg_xverack(),
                        msg_xversion({1:b"2",3:b"45"})]:
                test_too_early(msg)

        # test regular set up including xversion
        conn = self.restart_node()
        nt = NetworkThread()
        nt.start()

        conn.wait_for_verack()
        conn.send_message(msg_verack())

        # now it is time for xversion
        conn.send_message(msg_xversion({1000 : b"test string"}))

        # send extra buversion and buverack, shouldn't harm
        conn.send_message(msg_buversion(addrFromPort = 12345))
        conn.send_message(msg_buverack())
        conn.wait_for(lambda : conn.remote_xversion)

        # make sure xversion has actually been received properly

        # test that it contains the BU_LISTEN_PORT (replacement for buversion message)
        # FIXME: use proper constant
        assert 1<<17 in conn.remote_xversion.xver.keys()

        # Likewise, check that the remote end got our message
        node = self.nodes[0]

        peer_info = node.getpeerinfo()
        assert len(peer_info) == 1
        assert "xversion_map" in peer_info[0]
        xv_map = peer_info[0]["xversion_map"]

        assert len(xv_map) == 1
        assert unhexlify(list(xv_map.values())[0]) == b"test string"

        conn.connection.disconnect_node()
        nt.join()
Esempio n. 6
0
def check_seeds(network, dnsseeds, print_out=False):
    seeds_count = 0
    response_count = 0
    for dnsseed in dnsseeds:
        if print_out:
            print(f"Name: {dnsseed[0]} ### Host: {dnsseed[1]}")
        ip_addresses = set()
        for addinfo in socket.getaddrinfo(dnsseed[1], 80):
            ip_addresses.add(addinfo[4][0])

        ip_addresses = sorted(ip_addresses)
        for ip_address in ip_addresses:
            if print_out:
                print('%s %-15s' % (dnsseed[1], ip_address),
                      end=" version_string: ")
            node0 = SeedNode()
            if print_out:
                node0.enable_verion_log()
            connection = SeedNodeConn(ip_address,
                                      NETWORK_PORTS[network],
                                      rpc=None,
                                      callback=node0,
                                      net=network)
            node0.add_connection(connection)

            network_thread = NetworkThread()
            network_thread.start()
            try:
                seeds_count += 1
                node0.wait_for_verack(timeout=3)
                node0.connection.close()
                network_thread.join()
                response_count += 1
            except:
                if print_out:
                    print("no response")
                if node0.connection is not None:
                    node0.connection.close()
                network_thread.join()
        if print_out:
            print()
    if print_out:
        print("Results:\n" +
              f"Seeds: {seeds_count} Responded: {response_count}")

    # we return true if all seed nodes responded
    if seeds_count == response_count:
        return True
    else:
        return False
Esempio n. 7
0
    def run_test(self):
        logging.info("Testing extversion handling")

        # test regular set up including extversion
        testStringKey = 18446744073709551600
        # a large number that requires uint64_t
        testStringvalue = 1844674407370955161
        # a large number that requires uint64_t

        conn = self.restart_node()
        nt = NetworkThread()
        nt.start()
        logging.info("sent version")
        conn.wait_for(lambda: conn.remote_extversion)
        logging.info("sent extversion")
        conn.send_message(
            msg_extversion({
                1234: testStringvalue,
                testStringKey: b"test string"
            }))
        conn.wait_for_verack()
        logging.info("sent verack")
        conn.send_message(msg_verack())

        # make sure extversion has actually been received properly

        # test that it contains the BU_LISTEN_PORT (replacement for buversion message)
        # FIXME: use proper constant
        assert 1 << 33 in conn.remote_extversion.xver.keys()

        # Likewise, check that the remote end got our message
        node = self.nodes[0]

        peer_info = node.getpeerinfo()
        assert len(peer_info) == 1
        assert "extversion_map" in peer_info[0]
        xv_map = peer_info[0]["extversion_map"]

        assert len(xv_map) == 2
        assert unhexlify(list(xv_map.values())[1]) == b"test string"

        # send xupdate to test what would happen if someone tries to update non-chaneable key
        conn.send_message(msg_xupdate({testStringKey: b"test string changed"}))
        # some arbitrary sleep time
        time.sleep(3)

        # nothing should have changed, 1000 is not listed as a changeable key
        node = self.nodes[0]
        peer_info = node.getpeerinfo()
        assert len(peer_info) == 1
        assert "extversion_map" in peer_info[0]
        xv_map = peer_info[0]["extversion_map"]
        assert len(xv_map) == 2
        assert unhexlify(list(xv_map.values())[1]) == b"test string"

        # send xupdate to test what would happen if someone tries to update a non-existant key
        conn.send_message(msg_xupdate({1111: b"bad string"}))
        # some arbitrary sleep time
        time.sleep(3)
        # nothing should have changed, 1111 is not listed as a known key
        node = self.nodes[0]
        peer_info = node.getpeerinfo()
        assert len(peer_info) == 1
        assert "extversion_map" in peer_info[0]
        xv_map = peer_info[0]["extversion_map"]
        assert len(xv_map) == 2

        # TODO appent to this test to test a changeable key once one has been implemented in the node

        conn.connection.disconnect_node()
        nt.join()

        # Test versionbit mismatch

        logging.info("Testing extversion service bit mismatch")

        # test regular set up including extversion
        conn = self.restart_node()
        nt = NetworkThread()
        nt.start()
        logging.info("sent version")
        conn.wait_for(lambda: conn.remote_extversion)
        # if we send verack instead of extversion we should get a verack response
        logging.info("sent verack")
        conn.send_message(msg_verack())
        conn.wait_for_verack()

        conn.connection.disconnect_node()
        nt.join()
Esempio n. 8
0
 def network_and_finish(self):
     nt = NetworkThread()
     nt.start()
     nt.join()